composite_primary_keys 9.0.4 → 9.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/History.rdoc +20 -0
  3. data/Rakefile +37 -34
  4. data/lib/composite_primary_keys.rb +5 -10
  5. data/lib/composite_primary_keys/arel/in.rb +6 -6
  6. data/lib/composite_primary_keys/arel/sqlserver.rb +36 -0
  7. data/lib/composite_primary_keys/associations/association_scope.rb +51 -29
  8. data/lib/composite_primary_keys/attribute_methods/read.rb +3 -1
  9. data/lib/composite_primary_keys/connection_adapters/abstract_mysql_adapter.rb +22 -0
  10. data/lib/composite_primary_keys/relation.rb +30 -0
  11. data/lib/composite_primary_keys/relation/query_methods.rb +25 -36
  12. data/lib/composite_primary_keys/sanitization.rb +31 -47
  13. data/lib/composite_primary_keys/version.rb +1 -1
  14. data/tasks/databases/mysql.rake +40 -42
  15. data/tasks/databases/oracle.rake +29 -15
  16. data/tasks/databases/postgresql.rake +38 -47
  17. data/tasks/databases/sqlite.rake +25 -0
  18. data/tasks/databases/sqlserver.rake +32 -16
  19. data/test/abstract_unit.rb +12 -11
  20. data/test/connections/connection_spec.rb +27 -18
  21. data/test/connections/databases.ci.yml +5 -4
  22. data/test/connections/databases.example.yml +19 -4
  23. data/test/connections/databases.yml +25 -4
  24. data/test/fixtures/article.rb +6 -5
  25. data/test/fixtures/db_definitions/mysql.sql +16 -7
  26. data/test/fixtures/db_definitions/oracle.drop.sql +2 -0
  27. data/test/fixtures/db_definitions/oracle.sql +25 -15
  28. data/test/fixtures/db_definitions/postgresql.sql +11 -2
  29. data/test/fixtures/db_definitions/sqlite.sql +8 -0
  30. data/test/fixtures/db_definitions/sqlserver.sql +19 -33
  31. data/test/fixtures/pk_called_id.rb +5 -0
  32. data/test/fixtures/pk_called_ids.yml +11 -0
  33. data/test/test_associations.rb +334 -332
  34. data/test/test_create.rb +9 -1
  35. data/test/test_delete.rb +17 -39
  36. data/test/test_ids.rb +113 -109
  37. data/test/test_preload.rb +94 -0
  38. data/test/test_suite.rb +1 -1
  39. data/test/test_update.rb +12 -7
  40. metadata +14 -24
  41. data/lib/composite_primary_keys/associations/singular_association.rb +0 -14
  42. data/lib/composite_primary_keys/connection_adapters/abstract/connection_specification_changes.rb +0 -71
  43. data/lib/composite_primary_keys/connection_adapters/postgresql_adapter.rb +0 -50
  44. data/lib/composite_primary_keys/dirty.rb +0 -19
  45. data/lib/composite_primary_keys/validations/uniqueness.rb +0 -41
  46. data/tasks/databases/oracle_enhanced.rake +0 -27
  47. data/tasks/databases/sqlite3.rake +0 -27
  48. data/test/connections/native_ibm_db/connection.rb +0 -19
  49. data/test/connections/native_mysql/connection.rb +0 -17
  50. data/test/connections/native_oracle/connection.rb +0 -11
  51. data/test/connections/native_oracle_enhanced/connection.rb +0 -16
  52. data/test/connections/native_postgresql/connection.rb +0 -13
  53. data/test/connections/native_sqlite3/connection.rb +0 -9
  54. data/test/connections/native_sqlserver/connection.rb +0 -11
  55. data/test/fixtures/db_definitions/sqlserver.drop.sql +0 -92
  56. data/test/test_delete_all.rb +0 -29
@@ -0,0 +1,5 @@
1
+ class PkCalledId < ActiveRecord::Base
2
+ self.primary_keys = :id, :reference_code
3
+
4
+ validates_presence_of :reference_code, :code_label, :abbreviation
5
+ end
@@ -0,0 +1,11 @@
1
+ name_prefix_mr:
2
+ id: 1
3
+ reference_code: 1
4
+ code_label: MR
5
+ abbreviation: Mr
6
+
7
+ name_prefix_mrs:
8
+ id: 2
9
+ reference_code: 1
10
+ code_label: MRS
11
+ abbreviation: Mrs
@@ -12,336 +12,338 @@ class TestAssociations < ActiveSupport::TestCase
12
12
  assert_equal 2, products(:first_product).tariffs.length
13
13
  end
14
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_has_one_association_is_not_cached_to_where_it_returns_the_wrong_one
75
- engineering = departments(:engineering)
76
- engineering_head = engineering.head
77
-
78
- accounting = departments(:accounting)
79
- accounting_head = accounting.head
80
-
81
- refute_equal accounting_head, engineering_head
82
- end
83
-
84
- def test_has_one_association_primary_key_and_foreign_key_are_present
85
- steve = employees(:steve)
86
- steve_salary = steve.create_one_salary(year: "2015", month: "1")
87
-
88
- jill = employees(:jill)
89
- jill_salary = jill.create_one_salary(year: "2015", month: "1")
90
-
91
- steve_salary.reload
92
- jill_salary.reload
93
- assert_equal(steve.id, steve_salary.employee_id)
94
- assert_equal(1, steve_salary.location_id)
95
- assert_equal(jill.id, jill_salary.employee_id)
96
- assert_equal(1, jill_salary.location_id)
97
- end
98
-
99
- def test_has_many_association_is_not_cached_to_where_it_returns_the_wrong_ones
100
- engineering = departments(:engineering)
101
- engineering_employees = engineering.employees
102
-
103
- accounting = departments(:accounting)
104
- accounting_employees = accounting.employees
105
-
106
- refute_equal accounting_employees, engineering_employees
107
- end
108
-
109
- def test_has_many_association_primary_key_and_foreign_key_are_present
110
- steve = employees(:steve)
111
- steve_salary = steve.salaries.create(year: 2015, month: 1)
112
-
113
- jill = employees(:jill)
114
- jill_salary = jill.salaries.create(year: 2015, month: 1)
115
-
116
- steve_salary.reload
117
- jill_salary.reload
118
- assert_equal(steve.id, steve_salary.employee_id)
119
- assert_equal(1, steve_salary.location_id)
120
- assert_equal(jill.id, jill_salary.employee_id)
121
- assert_equal(1, jill_salary.location_id)
122
- end
123
-
124
- def test_belongs_to_association_primary_key_and_foreign_key_are_present
125
- bogus_foreign_key = 2_500_000
126
- salary_01 = Salary.new(
127
- year: 2015,
128
- month: 1,
129
- employee_id: bogus_foreign_key,
130
- location_id: 1
131
- )
132
- employee_01 = salary_01.create_employee
133
- employee_01.reload
134
-
135
- assert_equal(salary_01.employee_id, employee_01.id, "Generated id used")
136
- assert_not_equal(bogus_foreign_key, employee_01.id, "Bogus value ignored")
137
- assert_equal(1, employee_01.location_id)
138
- end
139
-
140
- def test_find_includes_product_tariffs_product
141
- # Old style
142
- product_tariffs = ProductTariff.includes(:product)
143
- assert_not_nil(product_tariffs)
144
- assert_equal(3, product_tariffs.length)
145
-
146
- # New style
147
- product_tariffs = ProductTariff.includes(:product)
148
- assert_not_nil(product_tariffs)
149
- assert_equal(3, product_tariffs.length)
150
- end
151
-
152
- def test_find_includes_product_tariffs_tariff
153
- # Old style
154
- product_tariffs = ProductTariff.includes(:tariff)
155
- assert_equal(3, product_tariffs.length)
156
-
157
- # New style
158
- product_tariffs = ProductTariff.includes(:tariff)
159
- assert_equal(3, product_tariffs.length)
160
- end
161
-
162
- def test_has_many_through
163
- products = Product.includes(:tariffs)
164
- assert_equal(3, products.length)
165
-
166
- tarrifs_length = products.inject(0) {|sum, product| sum + product.tariffs.length}
167
- assert_equal(3, tarrifs_length)
168
- end
169
-
170
- def test_new_style_includes_with_conditions
171
- product_tariff = ProductTariff.includes(:tariff).where('tariffs.amount < 5').references(:tariffs).first
172
- assert_equal(0, product_tariff.tariff.amount)
173
- end
174
-
175
- def test_find_product_includes
176
- products = Product.includes(:product_tariffs => :tariff)
177
- assert_equal(3, products.length)
178
-
179
- product_tariffs_length = products.inject(0) {|sum, product| sum + product.product_tariffs.length}
180
- assert_equal(3, product_tariffs_length)
181
- end
182
-
183
- def test_find_tariffs_includes
184
- tariffs = Tariff.includes(:product_tariffs => :product)
185
- assert_equal(3, tariffs.length)
186
-
187
- product_tariffs_length = tariffs.inject(0) {|sum, tariff| sum + tariff.product_tariffs.length}
188
- assert_equal(3, product_tariffs_length)
189
- end
190
-
191
- def test_has_many_through_when_not_pre_loaded
192
- student = Student.first
193
- rooms = student.rooms
194
- assert_equal(1, rooms.size)
195
- assert_equal(1, rooms.first.dorm_id)
196
- assert_equal(1, rooms.first.room_id)
197
- end
198
-
199
- def test_has_many_through_when_through_association_is_composite
200
- dorm = Dorm.first
201
- assert_equal(3, dorm.rooms.length)
202
- assert_equal(1, dorm.rooms.first.room_attributes.length)
203
- assert_equal('type', dorm.rooms.first.room_attributes.first.name)
204
- end
205
-
206
- def test_associations_with_conditions
207
- suburb = Suburb.find([2, 1])
208
- assert_equal 2, suburb.streets.size
209
-
210
- suburb = Suburb.find([2, 1])
211
- assert_equal 1, suburb.first_streets.size
212
-
213
- suburb = Suburb.includes(:streets).find([2, 1])
214
- assert_equal 2, suburb.streets.size
215
-
216
- suburb = Suburb.includes(:first_streets).find([2, 1])
217
- assert_equal 1, suburb.first_streets.size
218
- end
219
-
220
- def test_composite_has_many_composites
221
- room = rooms(:branner_room_1)
222
- assert_equal(2, room.room_assignments.length)
223
- assert_equal(room_assignments(:jacksons_room), room.room_assignments[0])
224
- assert_equal(room_assignments(:bobs_room), room.room_assignments[1])
225
- end
226
-
227
- def test_composite_belongs_to_composite
228
- room_assignment = room_assignments(:jacksons_room)
229
- assert_equal(rooms(:branner_room_1), room_assignment.room)
230
- end
231
-
232
- def test_composite_belongs_to_changes
233
- room_assignment = room_assignments(:jacksons_room)
234
- room_assignment.room = rooms(:branner_room_2)
235
- # This was raising an error before:
236
- # TypeError: [:dorm_id, :room_id] is not a symbol
237
- # changes returns HashWithIndifferentAccess
238
- assert_equal({"room_id"=>[1, 2]}, room_assignment.changes)
239
-
240
- steve = employees(:steve)
241
- steve.department = departments(:engineering)
242
- # It was returning this before:
243
- # {"[:department_id, :location_id]"=>[nil, [2, 1]]}
244
- assert_equal({"department_id"=>[1, 2]}, steve.changes)
245
- end
246
-
247
- def test_composite_belongs_to__setting_to_nil
248
- room_assignment = room_assignments(:jacksons_room)
249
- # This was raising an error before:
250
- # NoMethodError: undefined method `length' for nil:NilClass
251
- assert_nothing_raised { room_assignment.room = nil }
252
- end
253
-
254
- def test_has_one_with_composite
255
- # In this case a regular model has_one composite model
256
- department = departments(:engineering)
257
- assert_not_nil(department.head)
258
- end
259
-
260
- def test_has_many_build_simple_key
261
- user = users(:santiago)
262
- reading = user.readings.build
263
- assert_equal user.id, reading.user_id
264
- assert_equal user, reading.user
265
- end
266
-
267
- def test_has_many_build__composite_key
268
- department = departments(:engineering)
269
- employee = department.employees.build
270
- assert_equal department.department_id, employee.department_id
271
- assert_equal department.location_id, employee.location_id
272
- assert_equal department, employee.department
273
- end
274
-
275
- def test_has_many_with_primary_key
276
- @membership = Membership.find([1, 1])
277
- assert_equal 2, @membership.readings.size
278
- end
279
-
280
- def test_has_many_with_composite_key
281
- # In this case a regular model (Dorm) has_many composite models (Rooms)
282
- dorm = dorms(:branner)
283
- assert_equal(3, dorm.rooms.length)
284
- assert_equal(1, dorm.rooms[0].room_id)
285
- assert_equal(2, dorm.rooms[1].room_id)
286
- assert_equal(3, dorm.rooms[2].room_id)
287
- end
288
-
289
- def test_joins_has_many_with_primary_key
290
- #@membership = Membership.find(:first, :joins => :readings, :conditions => { :readings => { :id => 1 } })
291
- @membership = Membership.joins(:readings).where(readings: { id: 1 }).first
292
-
293
- assert_equal [1, 1], @membership.id
294
- end
295
-
296
- def test_joins_has_one_with_primary_key
297
- @membership = Membership.joins(:readings).where(readings: { id: 2 }).first
298
-
299
- assert_equal [1, 1], @membership.id
300
- end
301
-
302
- def test_has_many_through_with_conditions_when_through_association_is_not_composite
303
- user = User.first
304
- assert_equal 1, user.articles.where("articles.name = ?", "Article One").size
305
- end
306
-
307
- def test_has_many_through_with_conditions_when_through_association_is_composite
308
- room = Room.first
309
- assert_equal 0, room.room_attributes.where("room_attributes.name != ?", "type").size
310
- end
311
-
312
- def test_has_many_through_on_custom_finder_when_through_association_is_composite_finder_when_through_association_is_not_composite
313
- user = User.first
314
- assert_equal(1, user.find_custom_articles.size)
315
- end
316
-
317
- def test_has_many_through_on_custom_finder_when_through_association_is_composite
318
- room = Room.first
319
- assert_equal(0, room.find_custom_room_attributes.size)
320
- end
321
-
322
- def test_has_many_with_primary_key_with_associations
323
- memberships = Membership.includes(:statuses).where("membership_statuses.status = ?", 'Active').references(:membership_statuses)
324
- assert_equal(2, memberships.length)
325
- assert_equal([1,1], memberships[0].id)
326
- assert_equal([3,2], memberships[1].id)
327
- end
328
-
329
- def test_limitable_reflections
330
- memberships = Membership.includes(:statuses).where("membership_statuses.status = ?", 'Active').references(:membership_statuses).limit(1)
331
- assert_equal(1, memberships.length)
332
- assert_equal([1,1], memberships[0].id)
333
- end
334
-
335
- def test_foreign_key_present_with_null_association_ids
336
- group = Group.new
337
- group.memberships.build
338
- associations = group.association(:memberships)
339
- assert_equal(false, associations.send('foreign_key_present?'))
340
- end
341
-
342
- def test_ids_equals_for_non_CPK_case
343
- article = Article.new
344
- article.reading_ids = Reading.pluck(:id)
345
- assert_equal article.reading_ids, Reading.pluck(:id)
346
- end
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_has_one_association_is_not_cached_to_where_it_returns_the_wrong_one
75
+ # engineering = departments(:engineering)
76
+ # engineering_head = engineering.head
77
+ # assert_equal(employees(:sarah), engineering_head)
78
+ #
79
+ # accounting = departments(:accounting)
80
+ # accounting_head = accounting.head
81
+ # assert_equal(employees(:steve), accounting_head)
82
+ #
83
+ # refute_equal accounting_head, engineering_head
84
+ # end
85
+ #
86
+ # def test_has_one_association_primary_key_and_foreign_key_are_present
87
+ # steve = employees(:steve)
88
+ # steve_salary = steve.create_one_salary(year: "2015", month: "1")
89
+ #
90
+ # jill = employees(:jill)
91
+ # jill_salary = jill.create_one_salary(year: "2015", month: "1")
92
+ #
93
+ # steve_salary.reload
94
+ # jill_salary.reload
95
+ # assert_equal(steve.id, steve_salary.employee_id)
96
+ # assert_equal(1, steve_salary.location_id)
97
+ # assert_equal(jill.id, jill_salary.employee_id)
98
+ # assert_equal(1, jill_salary.location_id)
99
+ # end
100
+ #
101
+ # def test_has_many_association_is_not_cached_to_where_it_returns_the_wrong_ones
102
+ # engineering = departments(:engineering)
103
+ # engineering_employees = engineering.employees
104
+ #
105
+ # accounting = departments(:accounting)
106
+ # accounting_employees = accounting.employees
107
+ #
108
+ # refute_equal accounting_employees, engineering_employees
109
+ # end
110
+ #
111
+ # def test_has_many_association_primary_key_and_foreign_key_are_present
112
+ # steve = employees(:steve)
113
+ # steve_salary = steve.salaries.create(year: 2015, month: 1)
114
+ #
115
+ # jill = employees(:jill)
116
+ # jill_salary = jill.salaries.create(year: 2015, month: 1)
117
+ #
118
+ # steve_salary.reload
119
+ # jill_salary.reload
120
+ # assert_equal(steve.id, steve_salary.employee_id)
121
+ # assert_equal(1, steve_salary.location_id)
122
+ # assert_equal(jill.id, jill_salary.employee_id)
123
+ # assert_equal(1, jill_salary.location_id)
124
+ # end
125
+ #
126
+ # def test_belongs_to_association_primary_key_and_foreign_key_are_present
127
+ # bogus_foreign_key = 2_500_000
128
+ # salary_01 = Salary.new(
129
+ # year: 2015,
130
+ # month: 1,
131
+ # employee_id: bogus_foreign_key,
132
+ # location_id: 1
133
+ # )
134
+ # employee_01 = salary_01.create_employee
135
+ # employee_01.reload
136
+ #
137
+ # assert_equal(salary_01.employee_id, employee_01.id, "Generated id used")
138
+ # assert_not_equal(bogus_foreign_key, employee_01.id, "Bogus value ignored")
139
+ # assert_equal(1, employee_01.location_id)
140
+ # end
141
+ #
142
+ # def test_find_includes_product_tariffs_product
143
+ # # Old style
144
+ # product_tariffs = ProductTariff.includes(:product)
145
+ # assert_not_nil(product_tariffs)
146
+ # assert_equal(3, product_tariffs.length)
147
+ #
148
+ # # New style
149
+ # product_tariffs = ProductTariff.includes(:product)
150
+ # assert_not_nil(product_tariffs)
151
+ # assert_equal(3, product_tariffs.length)
152
+ # end
153
+ #
154
+ # def test_find_includes_product_tariffs_tariff
155
+ # # Old style
156
+ # product_tariffs = ProductTariff.includes(:tariff)
157
+ # assert_equal(3, product_tariffs.length)
158
+ #
159
+ # # New style
160
+ # product_tariffs = ProductTariff.includes(:tariff)
161
+ # assert_equal(3, product_tariffs.length)
162
+ # end
163
+ #
164
+ # def test_has_many_through
165
+ # products = Product.includes(:tariffs)
166
+ # assert_equal(3, products.length)
167
+ #
168
+ # tarrifs_length = products.inject(0) {|sum, product| sum + product.tariffs.length}
169
+ # assert_equal(3, tarrifs_length)
170
+ # end
171
+ #
172
+ # def test_new_style_includes_with_conditions
173
+ # product_tariff = ProductTariff.includes(:tariff).where('tariffs.amount < 5').references(:tariffs).first
174
+ # assert_equal(0, product_tariff.tariff.amount)
175
+ # end
176
+ #
177
+ # def test_find_product_includes
178
+ # products = Product.includes(:product_tariffs => :tariff)
179
+ # assert_equal(3, products.length)
180
+ #
181
+ # product_tariffs_length = products.inject(0) {|sum, product| sum + product.product_tariffs.length}
182
+ # assert_equal(3, product_tariffs_length)
183
+ # end
184
+ #
185
+ # def test_find_tariffs_includes
186
+ # tariffs = Tariff.includes(:product_tariffs => :product)
187
+ # assert_equal(3, tariffs.length)
188
+ #
189
+ # product_tariffs_length = tariffs.inject(0) {|sum, tariff| sum + tariff.product_tariffs.length}
190
+ # assert_equal(3, product_tariffs_length)
191
+ # end
192
+ #
193
+ # def test_has_many_through_when_not_pre_loaded
194
+ # student = Student.first
195
+ # rooms = student.rooms
196
+ # assert_equal(1, rooms.size)
197
+ # assert_equal(1, rooms.first.dorm_id)
198
+ # assert_equal(1, rooms.first.room_id)
199
+ # end
200
+ #
201
+ # def test_has_many_through_when_through_association_is_composite
202
+ # dorm = Dorm.first
203
+ # assert_equal(3, dorm.rooms.length)
204
+ # assert_equal(1, dorm.rooms.first.room_attributes.length)
205
+ # assert_equal('type', dorm.rooms.first.room_attributes.first.name)
206
+ # end
207
+ #
208
+ # def test_associations_with_conditions
209
+ # suburb = Suburb.find([2, 1])
210
+ # assert_equal 2, suburb.streets.size
211
+ #
212
+ # suburb = Suburb.find([2, 1])
213
+ # assert_equal 1, suburb.first_streets.size
214
+ #
215
+ # suburb = Suburb.includes(:streets).find([2, 1])
216
+ # assert_equal 2, suburb.streets.size
217
+ #
218
+ # suburb = Suburb.includes(:first_streets).find([2, 1])
219
+ # assert_equal 1, suburb.first_streets.size
220
+ # end
221
+ #
222
+ # def test_composite_has_many_composites
223
+ # room = rooms(:branner_room_1)
224
+ # assert_equal(2, room.room_assignments.length)
225
+ # assert_equal(room_assignments(:jacksons_room), room.room_assignments[0])
226
+ # assert_equal(room_assignments(:bobs_room), room.room_assignments[1])
227
+ # end
228
+ #
229
+ # def test_composite_belongs_to_composite
230
+ # room_assignment = room_assignments(:jacksons_room)
231
+ # assert_equal(rooms(:branner_room_1), room_assignment.room)
232
+ # end
233
+ #
234
+ # def test_composite_belongs_to_changes
235
+ # room_assignment = room_assignments(:jacksons_room)
236
+ # room_assignment.room = rooms(:branner_room_2)
237
+ # # This was raising an error before:
238
+ # # TypeError: [:dorm_id, :room_id] is not a symbol
239
+ # # changes returns HashWithIndifferentAccess
240
+ # assert_equal({"room_id"=>[1, 2]}, room_assignment.changes)
241
+ #
242
+ # steve = employees(:steve)
243
+ # steve.department = departments(:engineering)
244
+ # # It was returning this before:
245
+ # # {"[:department_id, :location_id]"=>[nil, [2, 1]]}
246
+ # assert_equal({"department_id"=>[1, 2]}, steve.changes)
247
+ # end
248
+ #
249
+ # def test_composite_belongs_to__setting_to_nil
250
+ # room_assignment = room_assignments(:jacksons_room)
251
+ # # This was raising an error before:
252
+ # # NoMethodError: undefined method `length' for nil:NilClass
253
+ # assert_nothing_raised { room_assignment.room = nil }
254
+ # end
255
+ #
256
+ # def test_has_one_with_composite
257
+ # # In this case a regular model has_one composite model
258
+ # department = departments(:engineering)
259
+ # assert_not_nil(department.head)
260
+ # end
261
+ #
262
+ # def test_has_many_build_simple_key
263
+ # user = users(:santiago)
264
+ # reading = user.readings.build
265
+ # assert_equal user.id, reading.user_id
266
+ # assert_equal user, reading.user
267
+ # end
268
+ #
269
+ # def test_has_many_build__composite_key
270
+ # department = departments(:engineering)
271
+ # employee = department.employees.build
272
+ # assert_equal department.department_id, employee.department_id
273
+ # assert_equal department.location_id, employee.location_id
274
+ # assert_equal department, employee.department
275
+ # end
276
+ #
277
+ # def test_has_many_with_primary_key
278
+ # @membership = Membership.find([1, 1])
279
+ # assert_equal 2, @membership.readings.size
280
+ # end
281
+ #
282
+ # def test_has_many_with_composite_key
283
+ # # In this case a regular model (Dorm) has_many composite models (Rooms)
284
+ # dorm = dorms(:branner)
285
+ # assert_equal(3, dorm.rooms.length)
286
+ # assert_equal(1, dorm.rooms[0].room_id)
287
+ # assert_equal(2, dorm.rooms[1].room_id)
288
+ # assert_equal(3, dorm.rooms[2].room_id)
289
+ # end
290
+ #
291
+ # def test_joins_has_many_with_primary_key
292
+ # #@membership = Membership.find(:first, :joins => :readings, :conditions => { :readings => { :id => 1 } })
293
+ # @membership = Membership.joins(:readings).where(readings: { id: 1 }).first
294
+ #
295
+ # assert_equal [1, 1], @membership.id
296
+ # end
297
+ #
298
+ # def test_joins_has_one_with_primary_key
299
+ # @membership = Membership.joins(:readings).where(readings: { id: 2 }).first
300
+ #
301
+ # assert_equal [1, 1], @membership.id
302
+ # end
303
+ #
304
+ # def test_has_many_through_with_conditions_when_through_association_is_not_composite
305
+ # user = User.first
306
+ # assert_equal 1, user.articles.where("articles.name = ?", "Article One").size
307
+ # end
308
+ #
309
+ # def test_has_many_through_with_conditions_when_through_association_is_composite
310
+ # room = Room.first
311
+ # assert_equal 0, room.room_attributes.where("room_attributes.name != ?", "type").size
312
+ # end
313
+ #
314
+ # def test_has_many_through_on_custom_finder_when_through_association_is_composite_finder_when_through_association_is_not_composite
315
+ # user = User.first
316
+ # assert_equal(1, user.find_custom_articles.size)
317
+ # end
318
+ #
319
+ # def test_has_many_through_on_custom_finder_when_through_association_is_composite
320
+ # room = Room.first
321
+ # assert_equal(0, room.find_custom_room_attributes.size)
322
+ # end
323
+ #
324
+ # def test_has_many_with_primary_key_with_associations
325
+ # memberships = Membership.includes(:statuses).where("membership_statuses.status = ?", 'Active').references(:membership_statuses)
326
+ # assert_equal(2, memberships.length)
327
+ # assert_equal([1,1], memberships[0].id)
328
+ # assert_equal([3,2], memberships[1].id)
329
+ # end
330
+ #
331
+ # def test_limitable_reflections
332
+ # memberships = Membership.includes(:statuses).where("membership_statuses.status = ?", 'Active').references(:membership_statuses).limit(1)
333
+ # assert_equal(1, memberships.length)
334
+ # assert_equal([1,1], memberships[0].id)
335
+ # end
336
+ #
337
+ # def test_foreign_key_present_with_null_association_ids
338
+ # group = Group.new
339
+ # group.memberships.build
340
+ # associations = group.association(:memberships)
341
+ # assert_equal(false, associations.send('foreign_key_present?'))
342
+ # end
343
+ #
344
+ # def test_ids_equals_for_non_CPK_case
345
+ # article = Article.new
346
+ # article.reading_ids = Reading.pluck(:id)
347
+ # assert_equal article.reading_ids, Reading.pluck(:id)
348
+ # end
347
349
  end