composite_primary_keys 7.0.13 → 7.0.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/History.rdoc +615 -608
- data/lib/composite_primary_keys.rb +110 -110
- data/lib/composite_primary_keys/associations/association.rb +23 -23
- data/lib/composite_primary_keys/associations/association_scope.rb +77 -77
- data/lib/composite_primary_keys/associations/has_and_belongs_to_many_association.rb +59 -59
- data/lib/composite_primary_keys/associations/has_many_association.rb +56 -56
- data/lib/composite_primary_keys/associations/join_dependency.rb +89 -89
- data/lib/composite_primary_keys/associations/join_dependency/join_part.rb +38 -38
- data/lib/composite_primary_keys/associations/preloader/association.rb +78 -78
- data/lib/composite_primary_keys/associations/preloader/has_and_belongs_to_many.rb +46 -46
- data/lib/composite_primary_keys/attribute_methods/dirty.rb +26 -26
- data/lib/composite_primary_keys/attribute_methods/read.rb +34 -34
- data/lib/composite_primary_keys/attribute_methods/write.rb +36 -36
- data/lib/composite_primary_keys/base.rb +0 -6
- data/lib/composite_primary_keys/composite_arrays.rb +30 -30
- data/lib/composite_primary_keys/connection_adapters/abstract/connection_specification_changes.rb +4 -2
- data/lib/composite_primary_keys/connection_adapters/sqlserver_adapter.rb +17 -0
- data/lib/composite_primary_keys/core.rb +47 -47
- data/lib/composite_primary_keys/persistence.rb +60 -60
- data/lib/composite_primary_keys/relation.rb +56 -56
- data/lib/composite_primary_keys/relation/calculations.rb +75 -65
- data/lib/composite_primary_keys/relation/finder_methods.rb +196 -196
- data/lib/composite_primary_keys/sanitization.rb +52 -52
- data/lib/composite_primary_keys/validations/uniqueness.rb +37 -39
- data/lib/composite_primary_keys/version.rb +8 -8
- data/tasks/databases/sqlserver.rake +40 -27
- data/test/connections/databases.example.yml +18 -18
- data/test/connections/native_sqlserver/connection.rb +14 -11
- data/test/fixtures/db_definitions/mysql.sql +208 -208
- data/test/fixtures/db_definitions/postgresql.sql +210 -210
- data/test/fixtures/db_definitions/sqlite.sql +197 -197
- data/test/fixtures/db_definitions/sqlserver.drop.sql +94 -91
- data/test/fixtures/db_definitions/sqlserver.sql +232 -226
- data/test/fixtures/employee.rb +5 -5
- data/test/test_associations.rb +275 -275
- data/test/test_attributes.rb +60 -60
- data/test/test_create.rb +112 -112
- data/test/test_delete.rb +152 -148
- data/test/test_delete_all.rb +21 -21
- data/test/test_enum.rb +20 -20
- data/test/test_equal.rb +1 -1
- data/test/test_tutorial_example.rb +21 -21
- metadata +3 -2
data/test/fixtures/employee.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
class Employee < ActiveRecord::Base
|
2
|
-
belongs_to :department, :foreign_key => [:department_id, :location_id]
|
3
|
-
has_many :comments, :as => :person
|
4
|
-
has_and_belongs_to_many :groups
|
5
|
-
end
|
1
|
+
class Employee < ActiveRecord::Base
|
2
|
+
belongs_to :department, :foreign_key => [:department_id, :location_id]
|
3
|
+
has_many :comments, :as => :person
|
4
|
+
has_and_belongs_to_many :groups
|
5
|
+
end
|
data/test/test_associations.rb
CHANGED
@@ -1,275 +1,275 @@
|
|
1
|
-
require File.expand_path('../abstract_unit', __FILE__)
|
2
|
-
|
3
|
-
class TestAssociations < ActiveSupport::TestCase
|
4
|
-
fixtures :articles, :products, :tariffs, :product_tariffs, :suburbs, :streets, :restaurants,
|
5
|
-
:dorms, :rooms, :room_attributes, :room_attribute_assignments, :students, :room_assignments, :users, :readings,
|
6
|
-
:departments, :employees, :memberships, :membership_statuses
|
7
|
-
|
8
|
-
def test_products
|
9
|
-
assert_not_nil products(:first_product).product_tariffs
|
10
|
-
assert_equal 2, products(:first_product).product_tariffs.length
|
11
|
-
assert_not_nil products(:first_product).tariffs
|
12
|
-
assert_equal 2, products(:first_product).tariffs.length
|
13
|
-
end
|
14
|
-
|
15
|
-
def test_product_tariffs
|
16
|
-
assert_not_nil product_tariffs(:first_flat).product
|
17
|
-
assert_not_nil product_tariffs(:first_flat).tariff
|
18
|
-
assert_equal Product, product_tariffs(:first_flat).product.class
|
19
|
-
assert_equal Tariff, product_tariffs(:first_flat).tariff.class
|
20
|
-
end
|
21
|
-
|
22
|
-
def test_tariffs
|
23
|
-
assert_not_nil tariffs(:flat).product_tariffs
|
24
|
-
assert_equal 1, tariffs(:flat).product_tariffs.length
|
25
|
-
assert_not_nil tariffs(:flat).products
|
26
|
-
assert_equal 1, tariffs(:flat).products.length
|
27
|
-
end
|
28
|
-
|
29
|
-
# Its not generating the instances of associated classes from the rows
|
30
|
-
def test_find_includes
|
31
|
-
# Old style
|
32
|
-
products = Product.includes(:product_tariffs).all
|
33
|
-
assert_equal(3, products.length)
|
34
|
-
assert_equal(3, products.inject(0) {|sum, product| sum + product.product_tariffs.length})
|
35
|
-
|
36
|
-
# New style
|
37
|
-
products = Product.includes(:product_tariffs)
|
38
|
-
assert_equal(3, products.length)
|
39
|
-
assert_equal(3, products.inject(0) {|sum, product| sum + product.product_tariffs.length})
|
40
|
-
end
|
41
|
-
|
42
|
-
def test_find_includes_eager_loading
|
43
|
-
product = products(:second_product)
|
44
|
-
product_tarrif = product_tariffs(:second_free)
|
45
|
-
|
46
|
-
# First get a legitimate product tarrif
|
47
|
-
products = Product.includes(:product_tariffs).where('product_tariffs.product_id = ?', product.id).references(:product_tariffs)
|
48
|
-
assert_equal(1, products.length)
|
49
|
-
assert_equal(product, products.first)
|
50
|
-
assert_equal([product_tarrif], products.first.product_tariffs)
|
51
|
-
end
|
52
|
-
|
53
|
-
def test_find_eager_loading_none
|
54
|
-
product = products(:third_product)
|
55
|
-
|
56
|
-
products = Product.includes(:product_tariffs).where(:id => product.id).references(:product_tariffs)
|
57
|
-
assert_equal(1, products.length)
|
58
|
-
assert_equal(product, products.first)
|
59
|
-
assert_empty(products.first.product_tariffs)
|
60
|
-
end
|
61
|
-
|
62
|
-
def test_find_includes_tariffs
|
63
|
-
# Old style
|
64
|
-
tariffs = Tariff.includes(:product_tariffs)
|
65
|
-
assert_equal(3, tariffs.length)
|
66
|
-
assert_equal(3, tariffs.inject(0) {|sum, tariff| sum + tariff.product_tariffs.length})
|
67
|
-
|
68
|
-
# New style
|
69
|
-
tariffs = Tariff.includes(:product_tariffs)
|
70
|
-
assert_equal(3, tariffs.length)
|
71
|
-
assert_equal(3, tariffs.inject(0) {|sum, tariff| sum + tariff.product_tariffs.length})
|
72
|
-
end
|
73
|
-
|
74
|
-
def test_find_includes_product_tariffs_product
|
75
|
-
# Old style
|
76
|
-
product_tariffs = ProductTariff.includes(:product)
|
77
|
-
assert_not_nil(product_tariffs)
|
78
|
-
assert_equal(3, product_tariffs.length)
|
79
|
-
|
80
|
-
# New style
|
81
|
-
product_tariffs = ProductTariff.includes(:product)
|
82
|
-
assert_not_nil(product_tariffs)
|
83
|
-
assert_equal(3, product_tariffs.length)
|
84
|
-
end
|
85
|
-
|
86
|
-
def test_find_includes_product_tariffs_tariff
|
87
|
-
# Old style
|
88
|
-
product_tariffs = ProductTariff.includes(:tariff)
|
89
|
-
assert_equal(3, product_tariffs.length)
|
90
|
-
|
91
|
-
# New style
|
92
|
-
product_tariffs = ProductTariff.includes(:tariff)
|
93
|
-
assert_equal(3, product_tariffs.length)
|
94
|
-
end
|
95
|
-
|
96
|
-
def test_has_many_through
|
97
|
-
products = Product.includes(:tariffs)
|
98
|
-
assert_equal(3, products.length)
|
99
|
-
|
100
|
-
tarrifs_length = products.inject(0) {|sum, product| sum + product.tariffs.length}
|
101
|
-
assert_equal(3, tarrifs_length)
|
102
|
-
end
|
103
|
-
|
104
|
-
def test_new_style_includes_with_conditions
|
105
|
-
product_tariff = ProductTariff.includes(:tariff).where('tariffs.amount < 5').references(:tariffs).first
|
106
|
-
assert_equal(0, product_tariff.tariff.amount)
|
107
|
-
end
|
108
|
-
|
109
|
-
def test_find_product_includes
|
110
|
-
products = Product.includes(:product_tariffs => :tariff)
|
111
|
-
assert_equal(3, products.length)
|
112
|
-
|
113
|
-
product_tariffs_length = products.inject(0) {|sum, product| sum + product.product_tariffs.length}
|
114
|
-
assert_equal(3, product_tariffs_length)
|
115
|
-
end
|
116
|
-
|
117
|
-
def test_find_tariffs_includes
|
118
|
-
tariffs = Tariff.includes(:product_tariffs => :product)
|
119
|
-
assert_equal(3, tariffs.length)
|
120
|
-
|
121
|
-
product_tariffs_length = tariffs.inject(0) {|sum, tariff| sum + tariff.product_tariffs.length}
|
122
|
-
assert_equal(3, product_tariffs_length)
|
123
|
-
end
|
124
|
-
|
125
|
-
def test_has_many_through_when_not_pre_loaded
|
126
|
-
student = Student.first
|
127
|
-
rooms = student.rooms
|
128
|
-
assert_equal(1, rooms.size)
|
129
|
-
assert_equal(1, rooms.first.dorm_id)
|
130
|
-
assert_equal(1, rooms.first.room_id)
|
131
|
-
end
|
132
|
-
|
133
|
-
def test_has_many_through_when_through_association_is_composite
|
134
|
-
dorm = Dorm.first
|
135
|
-
assert_equal(3, dorm.rooms.length)
|
136
|
-
assert_equal(1, dorm.rooms.first.room_attributes.length)
|
137
|
-
assert_equal('type', dorm.rooms.first.room_attributes.first.name)
|
138
|
-
end
|
139
|
-
|
140
|
-
def test_associations_with_conditions
|
141
|
-
suburb = Suburb.find([2, 1])
|
142
|
-
assert_equal 2, suburb.streets.size
|
143
|
-
|
144
|
-
suburb = Suburb.find([2, 1])
|
145
|
-
assert_equal 1, suburb.first_streets.size
|
146
|
-
|
147
|
-
suburb = Suburb.includes(:streets).find([2, 1])
|
148
|
-
assert_equal 2, suburb.streets.size
|
149
|
-
|
150
|
-
suburb = Suburb.includes(:first_streets).find([2, 1])
|
151
|
-
assert_equal 1, suburb.first_streets.size
|
152
|
-
end
|
153
|
-
|
154
|
-
def test_composite_has_many_composites
|
155
|
-
room = rooms(:branner_room_1)
|
156
|
-
assert_equal(2, room.room_assignments.length)
|
157
|
-
assert_equal(room_assignments(:jacksons_room), room.room_assignments[0])
|
158
|
-
assert_equal(room_assignments(:bobs_room), room.room_assignments[1])
|
159
|
-
end
|
160
|
-
|
161
|
-
def test_composite_belongs_to_composite
|
162
|
-
room_assignment = room_assignments(:jacksons_room)
|
163
|
-
assert_equal(rooms(:branner_room_1), room_assignment.room)
|
164
|
-
end
|
165
|
-
|
166
|
-
def test_composite_belongs_to_changes
|
167
|
-
room_assignment = room_assignments(:jacksons_room)
|
168
|
-
room_assignment.room = rooms(:branner_room_2)
|
169
|
-
# This was raising an error before:
|
170
|
-
# TypeError: [:dorm_id, :room_id] is not a symbol
|
171
|
-
# changes returns HashWithIndifferentAccess
|
172
|
-
assert_equal({"room_id"=>[1, 2]}, room_assignment.changes)
|
173
|
-
|
174
|
-
steve = employees(:steve)
|
175
|
-
steve.department = departments(:engineering)
|
176
|
-
# It was returning this before:
|
177
|
-
# {"[:department_id, :location_id]"=>[nil, [2, 1]]}
|
178
|
-
assert_equal({"department_id"=>[1, 2]}, steve.changes)
|
179
|
-
end
|
180
|
-
|
181
|
-
def test_composite_belongs_to__setting_to_nil
|
182
|
-
room_assignment = room_assignments(:jacksons_room)
|
183
|
-
# This was raising an error before:
|
184
|
-
# NoMethodError: undefined method `length' for nil:NilClass
|
185
|
-
assert_nothing_raised { room_assignment.room = nil }
|
186
|
-
end
|
187
|
-
|
188
|
-
def test_has_one_with_composite
|
189
|
-
# In this case a regular model has_one composite model
|
190
|
-
department = departments(:engineering)
|
191
|
-
assert_not_nil(department.head)
|
192
|
-
end
|
193
|
-
|
194
|
-
def test_has_many_build__simple_key
|
195
|
-
user = users(:santiago)
|
196
|
-
reading = user.readings.build
|
197
|
-
assert_equal user.id, reading.user_id
|
198
|
-
assert_equal user, reading.user
|
199
|
-
end
|
200
|
-
|
201
|
-
def test_has_many_build__composite_key
|
202
|
-
department = departments(:engineering)
|
203
|
-
employee = department.employees.build
|
204
|
-
assert_equal department.department_id, employee.department_id
|
205
|
-
assert_equal department.location_id, employee.location_id
|
206
|
-
assert_equal department, employee.department
|
207
|
-
end
|
208
|
-
|
209
|
-
def test_has_many_with_primary_key
|
210
|
-
@membership = Membership.find([1, 1])
|
211
|
-
assert_equal 2, @membership.readings.size
|
212
|
-
end
|
213
|
-
|
214
|
-
def test_has_many_with_composite_key
|
215
|
-
# In this case a regular model (Dorm) has_many composite models (Rooms)
|
216
|
-
dorm = dorms(:branner)
|
217
|
-
assert_equal(3, dorm.rooms.length)
|
218
|
-
assert_equal(1, dorm.rooms[0].room_id)
|
219
|
-
assert_equal(2, dorm.rooms[1].room_id)
|
220
|
-
assert_equal(3, dorm.rooms[2].room_id)
|
221
|
-
end
|
222
|
-
|
223
|
-
def test_joins_has_many_with_primary_key
|
224
|
-
#@membership = Membership.find(:first, :joins => :readings, :conditions => { :readings => { :id => 1 } })
|
225
|
-
@membership = Membership.joins(:readings).where(readings: { id: 1 }).first
|
226
|
-
|
227
|
-
assert_equal [1, 1], @membership.id
|
228
|
-
end
|
229
|
-
|
230
|
-
def test_joins_has_one_with_primary_key
|
231
|
-
@membership = Membership.joins(:readings).where(readings: { id: 2 }).first
|
232
|
-
|
233
|
-
assert_equal [1, 1], @membership.id
|
234
|
-
end
|
235
|
-
|
236
|
-
def test_has_many_through_with_conditions_when_through_association_is_not_composite
|
237
|
-
user = User.first
|
238
|
-
assert_equal 1, user.articles.where("articles.name = ?", "Article One").size
|
239
|
-
end
|
240
|
-
|
241
|
-
def test_has_many_through_with_conditions_when_through_association_is_composite
|
242
|
-
room = Room.first
|
243
|
-
assert_equal 0, room.room_attributes.where("room_attributes.name != ?", "type").size
|
244
|
-
end
|
245
|
-
|
246
|
-
def test_has_many_through_on_custom_finder_when_through_association_is_composite_finder_when_through_association_is_not_composite
|
247
|
-
user = User.first
|
248
|
-
assert_equal(1, user.find_custom_articles.size)
|
249
|
-
end
|
250
|
-
|
251
|
-
def test_has_many_through_on_custom_finder_when_through_association_is_composite
|
252
|
-
room = Room.first
|
253
|
-
assert_equal(0, room.find_custom_room_attributes.size)
|
254
|
-
end
|
255
|
-
|
256
|
-
def test_has_many_with_primary_key_with_associations
|
257
|
-
memberships = Membership.includes(:statuses).where("membership_statuses.status = ?", 'Active').references(:membership_statuses)
|
258
|
-
assert_equal(2, memberships.length)
|
259
|
-
assert_equal([1,1], memberships[0].id)
|
260
|
-
assert_equal([3,2], memberships[1].id)
|
261
|
-
end
|
262
|
-
|
263
|
-
def test_limitable_reflections
|
264
|
-
memberships = Membership.includes(:statuses).where("membership_statuses.status = ?", 'Active').references(:membership_statuses).limit(1)
|
265
|
-
assert_equal(1, memberships.length)
|
266
|
-
assert_equal([1,1], memberships[0].id)
|
267
|
-
end
|
268
|
-
|
269
|
-
def test_foreign_key_present_with_null_association_ids
|
270
|
-
group = Group.new
|
271
|
-
group.memberships.build
|
272
|
-
associations = group.association_cache[:memberships]
|
273
|
-
assert_equal(false, associations.send('foreign_key_present?'))
|
274
|
-
end
|
275
|
-
end
|
1
|
+
require File.expand_path('../abstract_unit', __FILE__)
|
2
|
+
|
3
|
+
class TestAssociations < ActiveSupport::TestCase
|
4
|
+
fixtures :articles, :products, :tariffs, :product_tariffs, :suburbs, :streets, :restaurants,
|
5
|
+
:dorms, :rooms, :room_attributes, :room_attribute_assignments, :students, :room_assignments, :users, :readings,
|
6
|
+
:departments, :employees, :memberships, :membership_statuses
|
7
|
+
|
8
|
+
def test_products
|
9
|
+
assert_not_nil products(:first_product).product_tariffs
|
10
|
+
assert_equal 2, products(:first_product).product_tariffs.length
|
11
|
+
assert_not_nil products(:first_product).tariffs
|
12
|
+
assert_equal 2, products(:first_product).tariffs.length
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_product_tariffs
|
16
|
+
assert_not_nil product_tariffs(:first_flat).product
|
17
|
+
assert_not_nil product_tariffs(:first_flat).tariff
|
18
|
+
assert_equal Product, product_tariffs(:first_flat).product.class
|
19
|
+
assert_equal Tariff, product_tariffs(:first_flat).tariff.class
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_tariffs
|
23
|
+
assert_not_nil tariffs(:flat).product_tariffs
|
24
|
+
assert_equal 1, tariffs(:flat).product_tariffs.length
|
25
|
+
assert_not_nil tariffs(:flat).products
|
26
|
+
assert_equal 1, tariffs(:flat).products.length
|
27
|
+
end
|
28
|
+
|
29
|
+
# Its not generating the instances of associated classes from the rows
|
30
|
+
def test_find_includes
|
31
|
+
# Old style
|
32
|
+
products = Product.includes(:product_tariffs).all
|
33
|
+
assert_equal(3, products.length)
|
34
|
+
assert_equal(3, products.inject(0) {|sum, product| sum + product.product_tariffs.length})
|
35
|
+
|
36
|
+
# New style
|
37
|
+
products = Product.includes(:product_tariffs)
|
38
|
+
assert_equal(3, products.length)
|
39
|
+
assert_equal(3, products.inject(0) {|sum, product| sum + product.product_tariffs.length})
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_find_includes_eager_loading
|
43
|
+
product = products(:second_product)
|
44
|
+
product_tarrif = product_tariffs(:second_free)
|
45
|
+
|
46
|
+
# First get a legitimate product tarrif
|
47
|
+
products = Product.includes(:product_tariffs).where('product_tariffs.product_id = ?', product.id).references(:product_tariffs)
|
48
|
+
assert_equal(1, products.length)
|
49
|
+
assert_equal(product, products.first)
|
50
|
+
assert_equal([product_tarrif], products.first.product_tariffs)
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_find_eager_loading_none
|
54
|
+
product = products(:third_product)
|
55
|
+
|
56
|
+
products = Product.includes(:product_tariffs).where(:id => product.id).references(:product_tariffs)
|
57
|
+
assert_equal(1, products.length)
|
58
|
+
assert_equal(product, products.first)
|
59
|
+
assert_empty(products.first.product_tariffs)
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_find_includes_tariffs
|
63
|
+
# Old style
|
64
|
+
tariffs = Tariff.includes(:product_tariffs)
|
65
|
+
assert_equal(3, tariffs.length)
|
66
|
+
assert_equal(3, tariffs.inject(0) {|sum, tariff| sum + tariff.product_tariffs.length})
|
67
|
+
|
68
|
+
# New style
|
69
|
+
tariffs = Tariff.includes(:product_tariffs)
|
70
|
+
assert_equal(3, tariffs.length)
|
71
|
+
assert_equal(3, tariffs.inject(0) {|sum, tariff| sum + tariff.product_tariffs.length})
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_find_includes_product_tariffs_product
|
75
|
+
# Old style
|
76
|
+
product_tariffs = ProductTariff.includes(:product)
|
77
|
+
assert_not_nil(product_tariffs)
|
78
|
+
assert_equal(3, product_tariffs.length)
|
79
|
+
|
80
|
+
# New style
|
81
|
+
product_tariffs = ProductTariff.includes(:product)
|
82
|
+
assert_not_nil(product_tariffs)
|
83
|
+
assert_equal(3, product_tariffs.length)
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_find_includes_product_tariffs_tariff
|
87
|
+
# Old style
|
88
|
+
product_tariffs = ProductTariff.includes(:tariff)
|
89
|
+
assert_equal(3, product_tariffs.length)
|
90
|
+
|
91
|
+
# New style
|
92
|
+
product_tariffs = ProductTariff.includes(:tariff)
|
93
|
+
assert_equal(3, product_tariffs.length)
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_has_many_through
|
97
|
+
products = Product.includes(:tariffs)
|
98
|
+
assert_equal(3, products.length)
|
99
|
+
|
100
|
+
tarrifs_length = products.inject(0) {|sum, product| sum + product.tariffs.length}
|
101
|
+
assert_equal(3, tarrifs_length)
|
102
|
+
end
|
103
|
+
|
104
|
+
def test_new_style_includes_with_conditions
|
105
|
+
product_tariff = ProductTariff.includes(:tariff).where('tariffs.amount < 5').references(:tariffs).first
|
106
|
+
assert_equal(0, product_tariff.tariff.amount)
|
107
|
+
end
|
108
|
+
|
109
|
+
def test_find_product_includes
|
110
|
+
products = Product.includes(:product_tariffs => :tariff)
|
111
|
+
assert_equal(3, products.length)
|
112
|
+
|
113
|
+
product_tariffs_length = products.inject(0) {|sum, product| sum + product.product_tariffs.length}
|
114
|
+
assert_equal(3, product_tariffs_length)
|
115
|
+
end
|
116
|
+
|
117
|
+
def test_find_tariffs_includes
|
118
|
+
tariffs = Tariff.includes(:product_tariffs => :product)
|
119
|
+
assert_equal(3, tariffs.length)
|
120
|
+
|
121
|
+
product_tariffs_length = tariffs.inject(0) {|sum, tariff| sum + tariff.product_tariffs.length}
|
122
|
+
assert_equal(3, product_tariffs_length)
|
123
|
+
end
|
124
|
+
|
125
|
+
def test_has_many_through_when_not_pre_loaded
|
126
|
+
student = Student.first
|
127
|
+
rooms = student.rooms
|
128
|
+
assert_equal(1, rooms.size)
|
129
|
+
assert_equal(1, rooms.first.dorm_id)
|
130
|
+
assert_equal(1, rooms.first.room_id)
|
131
|
+
end
|
132
|
+
|
133
|
+
def test_has_many_through_when_through_association_is_composite
|
134
|
+
dorm = Dorm.first
|
135
|
+
assert_equal(3, dorm.rooms.length)
|
136
|
+
assert_equal(1, dorm.rooms.first.room_attributes.length)
|
137
|
+
assert_equal('type', dorm.rooms.first.room_attributes.first.name)
|
138
|
+
end
|
139
|
+
|
140
|
+
def test_associations_with_conditions
|
141
|
+
suburb = Suburb.find([2, 1])
|
142
|
+
assert_equal 2, suburb.streets.size
|
143
|
+
|
144
|
+
suburb = Suburb.find([2, 1])
|
145
|
+
assert_equal 1, suburb.first_streets.size
|
146
|
+
|
147
|
+
suburb = Suburb.includes(:streets).find([2, 1])
|
148
|
+
assert_equal 2, suburb.streets.size
|
149
|
+
|
150
|
+
suburb = Suburb.includes(:first_streets).find([2, 1])
|
151
|
+
assert_equal 1, suburb.first_streets.size
|
152
|
+
end
|
153
|
+
|
154
|
+
def test_composite_has_many_composites
|
155
|
+
room = rooms(:branner_room_1)
|
156
|
+
assert_equal(2, room.room_assignments.length)
|
157
|
+
assert_equal(room_assignments(:jacksons_room), room.room_assignments[0])
|
158
|
+
assert_equal(room_assignments(:bobs_room), room.room_assignments[1])
|
159
|
+
end
|
160
|
+
|
161
|
+
def test_composite_belongs_to_composite
|
162
|
+
room_assignment = room_assignments(:jacksons_room)
|
163
|
+
assert_equal(rooms(:branner_room_1), room_assignment.room)
|
164
|
+
end
|
165
|
+
|
166
|
+
def test_composite_belongs_to_changes
|
167
|
+
room_assignment = room_assignments(:jacksons_room)
|
168
|
+
room_assignment.room = rooms(:branner_room_2)
|
169
|
+
# This was raising an error before:
|
170
|
+
# TypeError: [:dorm_id, :room_id] is not a symbol
|
171
|
+
# changes returns HashWithIndifferentAccess
|
172
|
+
assert_equal({"room_id"=>[1, 2]}, room_assignment.changes)
|
173
|
+
|
174
|
+
steve = employees(:steve)
|
175
|
+
steve.department = departments(:engineering)
|
176
|
+
# It was returning this before:
|
177
|
+
# {"[:department_id, :location_id]"=>[nil, [2, 1]]}
|
178
|
+
assert_equal({"department_id"=>[1, 2]}, steve.changes)
|
179
|
+
end
|
180
|
+
|
181
|
+
def test_composite_belongs_to__setting_to_nil
|
182
|
+
room_assignment = room_assignments(:jacksons_room)
|
183
|
+
# This was raising an error before:
|
184
|
+
# NoMethodError: undefined method `length' for nil:NilClass
|
185
|
+
assert_nothing_raised { room_assignment.room = nil }
|
186
|
+
end
|
187
|
+
|
188
|
+
def test_has_one_with_composite
|
189
|
+
# In this case a regular model has_one composite model
|
190
|
+
department = departments(:engineering)
|
191
|
+
assert_not_nil(department.head)
|
192
|
+
end
|
193
|
+
|
194
|
+
def test_has_many_build__simple_key
|
195
|
+
user = users(:santiago)
|
196
|
+
reading = user.readings.build
|
197
|
+
assert_equal user.id, reading.user_id
|
198
|
+
assert_equal user, reading.user
|
199
|
+
end
|
200
|
+
|
201
|
+
def test_has_many_build__composite_key
|
202
|
+
department = departments(:engineering)
|
203
|
+
employee = department.employees.build
|
204
|
+
assert_equal department.department_id, employee.department_id
|
205
|
+
assert_equal department.location_id, employee.location_id
|
206
|
+
assert_equal department, employee.department
|
207
|
+
end
|
208
|
+
|
209
|
+
def test_has_many_with_primary_key
|
210
|
+
@membership = Membership.find([1, 1])
|
211
|
+
assert_equal 2, @membership.readings.size
|
212
|
+
end
|
213
|
+
|
214
|
+
def test_has_many_with_composite_key
|
215
|
+
# In this case a regular model (Dorm) has_many composite models (Rooms)
|
216
|
+
dorm = dorms(:branner)
|
217
|
+
assert_equal(3, dorm.rooms.length)
|
218
|
+
assert_equal(1, dorm.rooms[0].room_id)
|
219
|
+
assert_equal(2, dorm.rooms[1].room_id)
|
220
|
+
assert_equal(3, dorm.rooms[2].room_id)
|
221
|
+
end
|
222
|
+
|
223
|
+
def test_joins_has_many_with_primary_key
|
224
|
+
#@membership = Membership.find(:first, :joins => :readings, :conditions => { :readings => { :id => 1 } })
|
225
|
+
@membership = Membership.joins(:readings).where(readings: { id: 1 }).first
|
226
|
+
|
227
|
+
assert_equal [1, 1], @membership.id
|
228
|
+
end
|
229
|
+
|
230
|
+
def test_joins_has_one_with_primary_key
|
231
|
+
@membership = Membership.joins(:readings).where(readings: { id: 2 }).first
|
232
|
+
|
233
|
+
assert_equal [1, 1], @membership.id
|
234
|
+
end
|
235
|
+
|
236
|
+
def test_has_many_through_with_conditions_when_through_association_is_not_composite
|
237
|
+
user = User.first
|
238
|
+
assert_equal 1, user.articles.where("articles.name = ?", "Article One").size
|
239
|
+
end
|
240
|
+
|
241
|
+
def test_has_many_through_with_conditions_when_through_association_is_composite
|
242
|
+
room = Room.first
|
243
|
+
assert_equal 0, room.room_attributes.where("room_attributes.name != ?", "type").size
|
244
|
+
end
|
245
|
+
|
246
|
+
def test_has_many_through_on_custom_finder_when_through_association_is_composite_finder_when_through_association_is_not_composite
|
247
|
+
user = User.first
|
248
|
+
assert_equal(1, user.find_custom_articles.size)
|
249
|
+
end
|
250
|
+
|
251
|
+
def test_has_many_through_on_custom_finder_when_through_association_is_composite
|
252
|
+
room = Room.first
|
253
|
+
assert_equal(0, room.find_custom_room_attributes.size)
|
254
|
+
end
|
255
|
+
|
256
|
+
def test_has_many_with_primary_key_with_associations
|
257
|
+
memberships = Membership.includes(:statuses).where("membership_statuses.status = ?", 'Active').references(:membership_statuses)
|
258
|
+
assert_equal(2, memberships.length)
|
259
|
+
assert_equal([1,1], memberships[0].id)
|
260
|
+
assert_equal([3,2], memberships[1].id)
|
261
|
+
end
|
262
|
+
|
263
|
+
def test_limitable_reflections
|
264
|
+
memberships = Membership.includes(:statuses).where("membership_statuses.status = ?", 'Active').references(:membership_statuses).limit(1)
|
265
|
+
assert_equal(1, memberships.length)
|
266
|
+
assert_equal([1,1], memberships[0].id)
|
267
|
+
end
|
268
|
+
|
269
|
+
def test_foreign_key_present_with_null_association_ids
|
270
|
+
group = Group.new
|
271
|
+
group.memberships.build
|
272
|
+
associations = group.association_cache[:memberships]
|
273
|
+
assert_equal(false, associations.send('foreign_key_present?'))
|
274
|
+
end
|
275
|
+
end
|