composite_primary_keys 13.0.9 → 14.0.1
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 +5 -28
- data/README.rdoc +1 -0
- data/lib/composite_primary_keys/associations/association.rb +2 -2
- data/lib/composite_primary_keys/associations/association_scope.rb +1 -1
- data/lib/composite_primary_keys/associations/{join_association.rb → join_dependency.rb} +137 -137
- data/lib/composite_primary_keys/associations/preloader/association.rb +26 -19
- data/lib/composite_primary_keys/associations/through_association.rb +2 -1
- data/lib/composite_primary_keys/base.rb +141 -137
- data/lib/composite_primary_keys/composite_predicates.rb +1 -50
- data/lib/composite_primary_keys/core.rb +0 -23
- data/lib/composite_primary_keys/persistence.rb +20 -7
- data/lib/composite_primary_keys/relation/calculations.rb +7 -1
- data/lib/composite_primary_keys/relation.rb +14 -11
- data/lib/composite_primary_keys/version.rb +2 -2
- data/lib/composite_primary_keys.rb +2 -2
- data/test/abstract_unit.rb +5 -1
- data/test/fixtures/comments.yml +0 -6
- data/test/fixtures/membership.rb +8 -8
- data/test/test_associations.rb +1 -1
- data/test/test_create.rb +218 -219
- data/test/test_equal.rb +11 -40
- data/test/test_polymorphic.rb +0 -6
- data/test/test_predicates.rb +60 -130
- metadata +11 -12
- data/test/fixtures/user_with_polymorphic_name.rb +0 -9
- data/test/test_hash.rb +0 -73
- data/test/test_relation.rb +0 -27
data/test/test_predicates.rb
CHANGED
@@ -1,130 +1,60 @@
|
|
1
|
-
require File.expand_path('../abstract_unit', __FILE__)
|
2
|
-
|
3
|
-
class TestPredicates < ActiveSupport::TestCase
|
4
|
-
fixtures :departments
|
5
|
-
|
6
|
-
include CompositePrimaryKeys::Predicates
|
7
|
-
|
8
|
-
def test_or
|
9
|
-
dep = Department.arel_table
|
10
|
-
|
11
|
-
predicates = Array.new
|
12
|
-
|
13
|
-
3.times do |i|
|
14
|
-
predicates << dep[:id].eq(i)
|
15
|
-
end
|
16
|
-
|
17
|
-
connection = ActiveRecord::Base.connection
|
18
|
-
quoted = "#{connection.quote_table_name('departments')}.#{connection.quote_column_name('id')}"
|
19
|
-
expected = "(#{quoted} = 0 OR #{quoted} = 1 OR #{quoted} = 2)"
|
20
|
-
|
21
|
-
pred = cpk_or_predicate(predicates)
|
22
|
-
assert_equal(with_quoted_identifiers(expected), pred.to_sql)
|
23
|
-
end
|
24
|
-
|
25
|
-
def test_or_with_many_values
|
26
|
-
dep = Arel::Table.new(:departments)
|
27
|
-
|
28
|
-
predicates = Array.new
|
29
|
-
|
30
|
-
number_of_predicates = 3000 # This should really be big
|
31
|
-
number_of_predicates.times do |i|
|
32
|
-
predicates << dep[:id].eq(i)
|
33
|
-
end
|
34
|
-
|
35
|
-
connection = ActiveRecord::Base.connection
|
36
|
-
quoted = "#{connection.quote_table_name('departments')}.#{connection.quote_column_name('id')}"
|
37
|
-
expected_ungrouped = ((0...number_of_predicates).map { |i| "#{quoted} = #{i}" }).join(' OR ')
|
38
|
-
expected = "(#{expected_ungrouped})"
|
39
|
-
|
40
|
-
pred = cpk_or_predicate(predicates)
|
41
|
-
assert_equal(with_quoted_identifiers(expected), pred.to_sql)
|
42
|
-
end
|
43
|
-
|
44
|
-
def test_and
|
45
|
-
dep = Department.arel_table
|
46
|
-
|
47
|
-
predicates = Array.new
|
48
|
-
|
49
|
-
3.times do |i|
|
50
|
-
predicates << dep[:id].eq(i)
|
51
|
-
end
|
52
|
-
|
53
|
-
connection = ActiveRecord::Base.connection
|
54
|
-
quoted = "#{connection.quote_table_name('departments')}.#{connection.quote_column_name('id')}"
|
55
|
-
expected = "#{quoted} = 0 AND #{quoted} = 1 AND #{quoted} = 2"
|
56
|
-
|
57
|
-
pred = cpk_and_predicate(predicates)
|
58
|
-
assert_equal(with_quoted_identifiers(expected), pred.to_sql)
|
59
|
-
end
|
60
|
-
|
61
|
-
def test_in
|
62
|
-
dep = Department.arel_table
|
63
|
-
|
64
|
-
primary_keys = [[1, 1], [1, 2]]
|
65
|
-
|
66
|
-
connection = ActiveRecord::Base.connection
|
67
|
-
quoted_id_column = "#{connection.quote_table_name('departments')}.#{connection.quote_column_name('id')}"
|
68
|
-
quoted_location_id_column = "#{connection.quote_table_name('departments')}.#{connection.quote_column_name('location_id')}"
|
69
|
-
expected = "#{quoted_id_column} = 1 AND #{quoted_location_id_column} IN (1, 2)"
|
70
|
-
|
71
|
-
pred = cpk_in_predicate(dep, [:id, :location_id], primary_keys)
|
72
|
-
assert_equal(with_quoted_identifiers(expected), pred.to_sql)
|
73
|
-
end
|
74
|
-
|
75
|
-
def test_in_with_low_cardinality_second_key_part
|
76
|
-
dep = Department.arel_table
|
77
|
-
|
78
|
-
primary_keys = [[1, 1], [2, 1]]
|
79
|
-
|
80
|
-
connection = ActiveRecord::Base.connection
|
81
|
-
quoted_id_column = "#{connection.quote_table_name('departments')}.#{connection.quote_column_name('id')}"
|
82
|
-
quoted_location_id_column = "#{connection.quote_table_name('departments')}.#{connection.quote_column_name('location_id')}"
|
83
|
-
expected = "#{quoted_location_id_column} = 1 AND #{quoted_id_column} IN (1, 2)"
|
84
|
-
|
85
|
-
pred = cpk_in_predicate(dep, [:id, :location_id], primary_keys)
|
86
|
-
assert_equal(with_quoted_identifiers(expected), pred.to_sql)
|
87
|
-
end
|
88
|
-
|
89
|
-
def test_in_with_nil_primary_key_part
|
90
|
-
dep = Department.arel_table
|
91
|
-
|
92
|
-
primary_keys = [[nil, 1], [nil, 2]]
|
93
|
-
|
94
|
-
connection = ActiveRecord::Base.connection
|
95
|
-
quoted_id_column = "#{connection.quote_table_name('departments')}.#{connection.quote_column_name('id')}"
|
96
|
-
quoted_location_id_column = "#{connection.quote_table_name('departments')}.#{connection.quote_column_name('location_id')}"
|
97
|
-
expected = "#{quoted_id_column} IS NULL AND #{quoted_location_id_column} IN (1, 2)"
|
98
|
-
|
99
|
-
pred = cpk_in_predicate(dep, [:id, :location_id], primary_keys)
|
100
|
-
assert_equal(with_quoted_identifiers(expected), pred.to_sql)
|
101
|
-
end
|
102
|
-
|
103
|
-
def test_in_with_nil_secondary_key_part
|
104
|
-
dep = Department.arel_table
|
105
|
-
|
106
|
-
primary_keys = [[1, 1], [1, nil]]
|
107
|
-
|
108
|
-
connection = ActiveRecord::Base.connection
|
109
|
-
quoted_id_column = "#{connection.quote_table_name('departments')}.#{connection.quote_column_name('id')}"
|
110
|
-
quoted_location_id_column = "#{connection.quote_table_name('departments')}.#{connection.quote_column_name('location_id')}"
|
111
|
-
expected = "#{quoted_id_column} = 1 AND (#{quoted_location_id_column} IN (1) OR #{quoted_location_id_column} IS NULL)"
|
112
|
-
|
113
|
-
pred = cpk_in_predicate(dep, [:id, :location_id], primary_keys)
|
114
|
-
assert_equal(with_quoted_identifiers(expected), pred.to_sql)
|
115
|
-
end
|
116
|
-
|
117
|
-
def test_in_with_multiple_primary_key_parts
|
118
|
-
dep = Department.arel_table
|
119
|
-
|
120
|
-
primary_keys = [[1, 1], [1, 2], [2, 3], [2, 4]]
|
121
|
-
|
122
|
-
connection = ActiveRecord::Base.connection
|
123
|
-
quoted_id_column = "#{connection.quote_table_name('departments')}.#{connection.quote_column_name('id')}"
|
124
|
-
quoted_location_id_column = "#{connection.quote_table_name('departments')}.#{connection.quote_column_name('location_id')}"
|
125
|
-
expected = "(#{quoted_id_column} = 1 AND #{quoted_location_id_column} IN (1, 2) OR #{quoted_id_column} = 2 AND #{quoted_location_id_column} IN (3, 4))"
|
126
|
-
|
127
|
-
pred = cpk_in_predicate(dep, [:id, :location_id], primary_keys)
|
128
|
-
assert_equal(with_quoted_identifiers(expected), pred.to_sql)
|
129
|
-
end
|
130
|
-
end
|
1
|
+
require File.expand_path('../abstract_unit', __FILE__)
|
2
|
+
|
3
|
+
class TestPredicates < ActiveSupport::TestCase
|
4
|
+
fixtures :departments
|
5
|
+
|
6
|
+
include CompositePrimaryKeys::Predicates
|
7
|
+
|
8
|
+
def test_or
|
9
|
+
dep = Department.arel_table
|
10
|
+
|
11
|
+
predicates = Array.new
|
12
|
+
|
13
|
+
3.times do |i|
|
14
|
+
predicates << dep[:id].eq(i)
|
15
|
+
end
|
16
|
+
|
17
|
+
connection = ActiveRecord::Base.connection
|
18
|
+
quoted = "#{connection.quote_table_name('departments')}.#{connection.quote_column_name('id')}"
|
19
|
+
expected = "(#{quoted} = 0 OR #{quoted} = 1 OR #{quoted} = 2)"
|
20
|
+
|
21
|
+
pred = cpk_or_predicate(predicates)
|
22
|
+
assert_equal(with_quoted_identifiers(expected), pred.to_sql)
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_or_with_many_values
|
26
|
+
dep = Arel::Table.new(:departments)
|
27
|
+
|
28
|
+
predicates = Array.new
|
29
|
+
|
30
|
+
number_of_predicates = 3000 # This should really be big
|
31
|
+
number_of_predicates.times do |i|
|
32
|
+
predicates << dep[:id].eq(i)
|
33
|
+
end
|
34
|
+
|
35
|
+
connection = ActiveRecord::Base.connection
|
36
|
+
quoted = "#{connection.quote_table_name('departments')}.#{connection.quote_column_name('id')}"
|
37
|
+
expected_ungrouped = ((0...number_of_predicates).map { |i| "#{quoted} = #{i}" }).join(' OR ')
|
38
|
+
expected = "(#{expected_ungrouped})"
|
39
|
+
|
40
|
+
pred = cpk_or_predicate(predicates)
|
41
|
+
assert_equal(with_quoted_identifiers(expected), pred.to_sql)
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_and
|
45
|
+
dep = Department.arel_table
|
46
|
+
|
47
|
+
predicates = Array.new
|
48
|
+
|
49
|
+
3.times do |i|
|
50
|
+
predicates << dep[:id].eq(i)
|
51
|
+
end
|
52
|
+
|
53
|
+
connection = ActiveRecord::Base.connection
|
54
|
+
quoted = "#{connection.quote_table_name('departments')}.#{connection.quote_column_name('id')}"
|
55
|
+
expected = "#{quoted} = 0 AND #{quoted} = 1 AND #{quoted} = 2"
|
56
|
+
|
57
|
+
pred = cpk_and_predicate(predicates)
|
58
|
+
assert_equal(with_quoted_identifiers(expected), pred.to_sql)
|
59
|
+
end
|
60
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: composite_primary_keys
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 14.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Charlie Savage
|
8
|
+
autorequire:
|
8
9
|
bindir: bin
|
9
10
|
cert_chain: []
|
10
|
-
date:
|
11
|
+
date: 2022-01-09 00:00:00.000000000 Z
|
11
12
|
dependencies:
|
12
13
|
- !ruby/object:Gem::Dependency
|
13
14
|
name: activerecord
|
@@ -15,14 +16,14 @@ dependencies:
|
|
15
16
|
requirements:
|
16
17
|
- - "~>"
|
17
18
|
- !ruby/object:Gem::Version
|
18
|
-
version:
|
19
|
+
version: 7.0.0
|
19
20
|
type: :runtime
|
20
21
|
prerelease: false
|
21
22
|
version_requirements: !ruby/object:Gem::Requirement
|
22
23
|
requirements:
|
23
24
|
- - "~>"
|
24
25
|
- !ruby/object:Gem::Version
|
25
|
-
version:
|
26
|
+
version: 7.0.0
|
26
27
|
- !ruby/object:Gem::Dependency
|
27
28
|
name: rake
|
28
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -38,6 +39,7 @@ dependencies:
|
|
38
39
|
- !ruby/object:Gem::Version
|
39
40
|
version: '0'
|
40
41
|
description: Composite key support for ActiveRecord
|
42
|
+
email:
|
41
43
|
executables: []
|
42
44
|
extensions: []
|
43
45
|
extra_rdoc_files: []
|
@@ -55,7 +57,7 @@ files:
|
|
55
57
|
- lib/composite_primary_keys/associations/foreign_association.rb
|
56
58
|
- lib/composite_primary_keys/associations/has_many_association.rb
|
57
59
|
- lib/composite_primary_keys/associations/has_many_through_association.rb
|
58
|
-
- lib/composite_primary_keys/associations/
|
60
|
+
- lib/composite_primary_keys/associations/join_dependency.rb
|
59
61
|
- lib/composite_primary_keys/associations/preloader/association.rb
|
60
62
|
- lib/composite_primary_keys/associations/through_association.rb
|
61
63
|
- lib/composite_primary_keys/attribute_methods.rb
|
@@ -163,7 +165,6 @@ files:
|
|
163
165
|
- test/fixtures/topic_sources.yml
|
164
166
|
- test/fixtures/topics.yml
|
165
167
|
- test/fixtures/user.rb
|
166
|
-
- test/fixtures/user_with_polymorphic_name.rb
|
167
168
|
- test/fixtures/users.yml
|
168
169
|
- test/plugins/pagination.rb
|
169
170
|
- test/plugins/pagination_helper.rb
|
@@ -182,7 +183,6 @@ files:
|
|
182
183
|
- test/test_exists.rb
|
183
184
|
- test/test_find.rb
|
184
185
|
- test/test_habtm.rb
|
185
|
-
- test/test_hash.rb
|
186
186
|
- test/test_ids.rb
|
187
187
|
- test/test_miscellaneous.rb
|
188
188
|
- test/test_nested_attributes.rb
|
@@ -191,7 +191,6 @@ files:
|
|
191
191
|
- test/test_polymorphic.rb
|
192
192
|
- test/test_predicates.rb
|
193
193
|
- test/test_preload.rb
|
194
|
-
- test/test_relation.rb
|
195
194
|
- test/test_santiago.rb
|
196
195
|
- test/test_serialize.rb
|
197
196
|
- test/test_touch.rb
|
@@ -202,6 +201,7 @@ homepage: https://github.com/composite-primary-keys/composite_primary_keys
|
|
202
201
|
licenses:
|
203
202
|
- MIT
|
204
203
|
metadata: {}
|
204
|
+
post_install_message:
|
205
205
|
rdoc_options: []
|
206
206
|
require_paths:
|
207
207
|
- lib
|
@@ -209,14 +209,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
209
209
|
requirements:
|
210
210
|
- - ">="
|
211
211
|
- !ruby/object:Gem::Version
|
212
|
-
version: 2.
|
212
|
+
version: 2.7.0
|
213
213
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
214
214
|
requirements:
|
215
215
|
- - ">="
|
216
216
|
- !ruby/object:Gem::Version
|
217
217
|
version: '0'
|
218
218
|
requirements: []
|
219
|
-
rubygems_version: 3.
|
219
|
+
rubygems_version: 3.3.4
|
220
|
+
signing_key:
|
220
221
|
specification_version: 4
|
221
222
|
summary: Composite key support for ActiveRecord
|
222
223
|
test_files:
|
@@ -237,7 +238,6 @@ test_files:
|
|
237
238
|
- test/test_exists.rb
|
238
239
|
- test/test_find.rb
|
239
240
|
- test/test_habtm.rb
|
240
|
-
- test/test_hash.rb
|
241
241
|
- test/test_ids.rb
|
242
242
|
- test/test_miscellaneous.rb
|
243
243
|
- test/test_nested_attributes.rb
|
@@ -246,7 +246,6 @@ test_files:
|
|
246
246
|
- test/test_polymorphic.rb
|
247
247
|
- test/test_predicates.rb
|
248
248
|
- test/test_preload.rb
|
249
|
-
- test/test_relation.rb
|
250
249
|
- test/test_santiago.rb
|
251
250
|
- test/test_serialize.rb
|
252
251
|
- test/test_touch.rb
|
data/test/test_hash.rb
DELETED
@@ -1,73 +0,0 @@
|
|
1
|
-
require File.expand_path('../abstract_unit', __FILE__)
|
2
|
-
|
3
|
-
class TestHash < ActiveSupport::TestCase
|
4
|
-
fixtures :restaurants, :products
|
5
|
-
|
6
|
-
############################################################
|
7
|
-
### Tests for Product model with single primary key (id) ###
|
8
|
-
############################################################
|
9
|
-
|
10
|
-
def test_single_same_object_has_the_same_hash
|
11
|
-
first = Product.find(1)
|
12
|
-
second = Product.find(1)
|
13
|
-
assert_equal(first.hash, second.hash)
|
14
|
-
end
|
15
|
-
|
16
|
-
def test_single_different_objects_have_different_hashes
|
17
|
-
first = Product.find(1)
|
18
|
-
second = Product.find(2)
|
19
|
-
assert_not_equal(first.hash, second.hash)
|
20
|
-
end
|
21
|
-
|
22
|
-
def test_single_persisted_object_hash_is_based_on_primary_key
|
23
|
-
first = Product.find(1)
|
24
|
-
second = Product.find(1)
|
25
|
-
|
26
|
-
assert_equal(first.hash, second.hash)
|
27
|
-
first.name = 'new name'
|
28
|
-
assert_equal(first.hash, second.hash)
|
29
|
-
end
|
30
|
-
|
31
|
-
def test_single_two_new_objects_have_different_hashes
|
32
|
-
assert_not_equal(Product.new.hash, Product.new.hash)
|
33
|
-
end
|
34
|
-
|
35
|
-
def test_single_same_new_object_has_the_same_hash
|
36
|
-
it = Product.new
|
37
|
-
assert_equal(it.hash, it.hash)
|
38
|
-
end
|
39
|
-
|
40
|
-
#####################################################################################
|
41
|
-
### Tests for Restaurant model with composite primary key (franchise_id, store_id) ##
|
42
|
-
#####################################################################################
|
43
|
-
|
44
|
-
def test_composite_same_object_has_the_same_hash
|
45
|
-
first = Restaurant.find([1, 1])
|
46
|
-
second = Restaurant.find([1, 1])
|
47
|
-
assert_equal(first.hash, second.hash)
|
48
|
-
end
|
49
|
-
|
50
|
-
def test_composite_different_objects_have_different_hashes
|
51
|
-
first = Restaurant.find([1, 1])
|
52
|
-
second = Restaurant.find([2, 2])
|
53
|
-
assert_not_equal(first.hash, second.hash)
|
54
|
-
end
|
55
|
-
|
56
|
-
def test_composite_persisted_object_hash_is_based_on_primary_key
|
57
|
-
first = Restaurant.find([1, 1])
|
58
|
-
second = Restaurant.find([1, 1])
|
59
|
-
|
60
|
-
assert_equal(first.hash, second.hash)
|
61
|
-
first.name = 'new name'
|
62
|
-
assert_equal(first.hash, second.hash)
|
63
|
-
end
|
64
|
-
|
65
|
-
def test_composite_two_new_objects_have_different_hashes
|
66
|
-
assert_not_equal(Restaurant.new.hash, Restaurant.new.hash)
|
67
|
-
end
|
68
|
-
|
69
|
-
def test_composite_same_new_object_has_the_same_hash
|
70
|
-
it = Restaurant.new
|
71
|
-
assert_equal(it.hash, it.hash)
|
72
|
-
end
|
73
|
-
end
|
data/test/test_relation.rb
DELETED
@@ -1,27 +0,0 @@
|
|
1
|
-
require File.expand_path('../abstract_unit', __FILE__)
|
2
|
-
|
3
|
-
class TestRelation < ActiveSupport::TestCase
|
4
|
-
fixtures :users, :readings
|
5
|
-
|
6
|
-
def test_update_all_updates_db_records
|
7
|
-
user = User.find(1)
|
8
|
-
assert_equal [4, 5], user.readings.map(&:rating)
|
9
|
-
|
10
|
-
user.readings.update_all(rating: 3)
|
11
|
-
|
12
|
-
# Reload to check that the records were updated in the DB
|
13
|
-
user.readings.reload
|
14
|
-
assert_equal [3, 3], user.readings.map(&:rating)
|
15
|
-
end
|
16
|
-
|
17
|
-
def test_update_all_updates_loaded_association
|
18
|
-
user = User.find(1)
|
19
|
-
assert_equal [4, 5], user.readings.map(&:rating)
|
20
|
-
|
21
|
-
user.readings.update_all(rating: 3)
|
22
|
-
|
23
|
-
# No reload to check that not only the records were updated in the DB
|
24
|
-
# but also in the loaded association
|
25
|
-
assert_equal [3, 3], user.readings.map(&:rating)
|
26
|
-
end
|
27
|
-
end
|