composite_primary_keys 9.0.5 → 9.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/History.rdoc +5 -0
- data/README.rdoc +61 -1
- data/lib/composite_primary_keys/version.rb +1 -1
- data/test/test_associations.rb +334 -334
- metadata +2 -3
- data/README_DB2.rdoc +0 -33
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f68e1e5f247cfca5284cf7c4beee35386fefd396
|
4
|
+
data.tar.gz: 0532ee35ef717e91580af50068588e4d4b623809
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5577302f2ee234fcc8511bf83606bfed415973b3c84fcb8d9d5fc1d26b5545c634c1d671f7dbc5aaf82e636a531d17831bf46f4126198c575f8ef436cbc54d99
|
7
|
+
data.tar.gz: 452fa7ead38c8a096ac1c4ed106dd4280e77be768d379860c8b790b9ee26d97c79e87de384d86229c689c88e9209037b6d3d59ec173c555d9620ff4c4def5ba9
|
data/History.rdoc
CHANGED
data/README.rdoc
CHANGED
@@ -98,9 +98,69 @@ CPK supports the following databases:
|
|
98
98
|
|
99
99
|
== Tests
|
100
100
|
|
101
|
+
To run tests you first need to install the appropriate gems for the database you want to test. Database gems are
|
102
|
+
divided into the following bundler groups:
|
103
|
+
|
104
|
+
* mysql
|
105
|
+
* oracle
|
106
|
+
* postgresql
|
107
|
+
* sqlite
|
108
|
+
* sqlserver
|
109
|
+
|
110
|
+
Since it is likely you do not have all the above databases installed on your computer, you want to install just the
|
111
|
+
gems for your database. For example, to test postgresql you would install the appropriate gems like this:
|
112
|
+
|
113
|
+
bundler install --without "mysql oracle sqlite sqlserver"
|
114
|
+
|
115
|
+
Once you have installed the appropriate gems, the next step is to create the test database. There is a rake
|
116
|
+
command for each database. Using our example:
|
117
|
+
|
118
|
+
rake postgresql:build_database
|
119
|
+
|
120
|
+
You can also rebuild the database if it already exists using this command:
|
121
|
+
|
122
|
+
rake postgresql:rebuild_database
|
123
|
+
|
124
|
+
To get a list of commands for your database use:
|
125
|
+
|
126
|
+
Rake -T
|
127
|
+
|
128
|
+
Finally, to run tests:
|
129
|
+
|
130
|
+
rake postgresql:test
|
131
|
+
|
101
132
|
Travis build status: {<img src="https://travis-ci.org/composite-primary-keys/composite_primary_keys.svg" alt="Build Status" />}[https://travis-ci.org/composite-primary-keys/composite_primary_keys]
|
102
133
|
|
103
|
-
|
134
|
+
=== DB2
|
135
|
+
|
136
|
+
DB2 is no longer supported due to difficulties in getting the ibm_db2 gem to build. Thus tests
|
137
|
+
have not been run against db2.
|
138
|
+
|
139
|
+
=== MariaDb (mysql)
|
140
|
+
|
141
|
+
MariaDb is fully supported with all tests passing.
|
142
|
+
|
143
|
+
=== Oracle
|
144
|
+
|
145
|
+
Oracle is fully supported with all tests passing.
|
146
|
+
|
147
|
+
=== Postgresql
|
148
|
+
|
149
|
+
Postgresql is fully supported with all tests passing.
|
150
|
+
|
151
|
+
=== Sqlite 3
|
152
|
+
|
153
|
+
The sqlite database is created at the path composite_primary_keys/db. Note you must *first* create the database using the
|
154
|
+
built-in rake task before running tests:
|
155
|
+
|
156
|
+
rake sqlite:build_database
|
157
|
+
|
158
|
+
Note not all tests currently pass on sqlite3. These failures happen when trying to DELETE records where the query
|
159
|
+
includes a JOIN.
|
160
|
+
|
161
|
+
=== SqlServer
|
162
|
+
|
163
|
+
SqlServer is partially supported. There are a number of failing tests - patches welcomed.
|
104
164
|
|
105
165
|
== Questions, Discussion and Contributions
|
106
166
|
|
data/test/test_associations.rb
CHANGED
@@ -12,338 +12,338 @@ class TestAssociations < ActiveSupport::TestCase
|
|
12
12
|
assert_equal 2, products(:first_product).tariffs.length
|
13
13
|
end
|
14
14
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
#
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
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
|
349
349
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: composite_primary_keys
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 9.0.
|
4
|
+
version: 9.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Charlie Savage
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-01-
|
11
|
+
date: 2017-01-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -88,7 +88,6 @@ extra_rdoc_files: []
|
|
88
88
|
files:
|
89
89
|
- History.rdoc
|
90
90
|
- README.rdoc
|
91
|
-
- README_DB2.rdoc
|
92
91
|
- Rakefile
|
93
92
|
- lib/composite_primary_keys.rb
|
94
93
|
- lib/composite_primary_keys/arel/in.rb
|
data/README_DB2.rdoc
DELETED
@@ -1,33 +0,0 @@
|
|
1
|
-
Composite Primary key support for db2
|
2
|
-
|
3
|
-
== Driver Support
|
4
|
-
|
5
|
-
DB2 support requires the IBM_DB driver provided by http://rubyforge.org/projects/rubyibm/
|
6
|
-
project. Install using gem install ibm_db. Tested against version 0.60 of the driver.
|
7
|
-
This rubyforge project appears to be permenant location for the IBM adapter.
|
8
|
-
Older versions of the driver available from IBM Alphaworks will not work.
|
9
|
-
|
10
|
-
== Driver Bug and workaround provided as part of this plugin
|
11
|
-
|
12
|
-
Unlike the basic quote routine available for Rails AR, the DB2 adapter's quote
|
13
|
-
method doesn't return " column_name = 1 " when string values (integers in string type variable)
|
14
|
-
are passed for quoting numeric column. Rather it returns "column_name = '1'.
|
15
|
-
DB2 doesn't accept single quoting numeric columns in SQL. Currently, as part of
|
16
|
-
this plugin a fix is provided for the DB2 adapter since this plugin does
|
17
|
-
pass string values like this. Perhaps a patch should be sent to the DB2 adapter
|
18
|
-
project for a permanant fix.
|
19
|
-
|
20
|
-
== Database Setup
|
21
|
-
|
22
|
-
Database must be manually created using a separate command. Read the rake task
|
23
|
-
for creating tables and change the db name, user and passwords accordingly.
|
24
|
-
|
25
|
-
== Tested Database Server version
|
26
|
-
|
27
|
-
This is tested against DB2 v9.1 in Ubuntu Feisty Fawn (7.04)
|
28
|
-
|
29
|
-
== Tested Database Client version
|
30
|
-
|
31
|
-
This is tested against DB2 v9.1 in Ubuntu Feisty Fawn (7.04)
|
32
|
-
|
33
|
-
|