composite_primary_keys 12.0.5 → 13.0.0

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.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/History.rdoc +883 -862
  3. data/README.rdoc +181 -180
  4. data/lib/composite_primary_keys.rb +119 -118
  5. data/lib/composite_primary_keys/active_model/attribute_assignment.rb +19 -19
  6. data/lib/composite_primary_keys/associations/association_scope.rb +66 -68
  7. data/lib/composite_primary_keys/associations/join_dependency.rb +118 -103
  8. data/lib/composite_primary_keys/attribute_methods.rb +21 -9
  9. data/lib/composite_primary_keys/attribute_methods/primary_key.rb +0 -2
  10. data/lib/composite_primary_keys/attribute_methods/read.rb +30 -30
  11. data/lib/composite_primary_keys/attribute_methods/write.rb +35 -35
  12. data/lib/composite_primary_keys/base.rb +141 -141
  13. data/lib/composite_primary_keys/connection_adapters/abstract/database_statements.rb +37 -22
  14. data/lib/composite_primary_keys/connection_adapters/sqlserver/database_statements.rb +44 -44
  15. data/lib/composite_primary_keys/core.rb +48 -48
  16. data/lib/composite_primary_keys/nested_attributes.rb +1 -1
  17. data/lib/composite_primary_keys/persistence.rb +82 -81
  18. data/lib/composite_primary_keys/reflection.rb +91 -29
  19. data/lib/composite_primary_keys/relation.rb +197 -193
  20. data/lib/composite_primary_keys/relation/batches.rb +16 -8
  21. data/lib/composite_primary_keys/relation/calculations.rb +104 -81
  22. data/lib/composite_primary_keys/relation/finder_methods.rb +235 -235
  23. data/lib/composite_primary_keys/relation/predicate_builder/association_query_value.rb +39 -20
  24. data/lib/composite_primary_keys/relation/query_methods.rb +42 -42
  25. data/lib/composite_primary_keys/relation/where_clause.rb +18 -23
  26. data/lib/composite_primary_keys/table_metadata.rb +11 -0
  27. data/lib/composite_primary_keys/version.rb +8 -8
  28. data/test/abstract_unit.rb +114 -114
  29. data/test/connections/databases.ci.yml +22 -19
  30. data/test/fixtures/db_definitions/db2-create-tables.sql +112 -112
  31. data/test/fixtures/db_definitions/db2-drop-tables.sql +16 -16
  32. data/test/fixtures/db_definitions/mysql.sql +180 -180
  33. data/test/fixtures/db_definitions/oracle.drop.sql +41 -41
  34. data/test/fixtures/db_definitions/oracle.sql +199 -199
  35. data/test/fixtures/db_definitions/postgresql.sql +182 -182
  36. data/test/fixtures/db_definitions/sqlite.sql +169 -169
  37. data/test/fixtures/db_definitions/sqlserver.sql +176 -176
  38. data/test/fixtures/department.rb +16 -16
  39. data/test/fixtures/departments.yml +19 -15
  40. data/test/fixtures/employees.yml +33 -28
  41. data/test/fixtures/restaurants_suburbs.yml +10 -10
  42. data/test/fixtures/streets.yml +16 -16
  43. data/test/fixtures/suburbs.yml +14 -14
  44. data/test/fixtures/user.rb +11 -11
  45. data/test/test_associations.rb +364 -358
  46. data/test/test_attributes.rb +75 -60
  47. data/test/test_calculations.rb +49 -42
  48. data/test/test_create.rb +218 -180
  49. data/test/test_delete.rb +182 -179
  50. data/test/test_exists.rb +39 -39
  51. data/test/test_find.rb +170 -157
  52. data/test/test_ids.rb +112 -112
  53. data/test/test_nested_attributes.rb +67 -67
  54. data/test/test_update.rb +96 -96
  55. metadata +5 -5
  56. data/lib/composite_primary_keys/connection_adapters/mysql/database_statements.rb +0 -24
data/test/test_ids.rb CHANGED
@@ -1,112 +1,112 @@
1
- require File.expand_path('../abstract_unit', __FILE__)
2
-
3
- class ChildCpkTest < ReferenceCode
4
- end
5
-
6
- class TestIds < ActiveSupport::TestCase
7
- fixtures :reference_types, :reference_codes
8
-
9
- CLASSES = {
10
- :single => {
11
- :class => ReferenceType,
12
- :primary_keys => [:reference_type_id],
13
- },
14
- :dual => {
15
- :class => ReferenceCode,
16
- :primary_keys => [:reference_type_id, :reference_code],
17
- },
18
- :dual_strs => {
19
- :class => ReferenceCode,
20
- :primary_keys => ['reference_type_id', 'reference_code'],
21
- }
22
- }
23
-
24
- def setup
25
- self.class.classes = CLASSES
26
- end
27
-
28
- def test_id
29
- testing_with do
30
- assert_equal @first.id, @first.ids if composite?
31
- assert_kind_of(CompositePrimaryKeys::CompositeKeys, @first.id) if composite?
32
- end
33
- end
34
-
35
- def test_to_param
36
- testing_with do
37
- assert_equal '1,1', @first.to_param if composite?
38
- end
39
-
40
- capitol = Capitol.create!(country: 'The USA', city: 'Washington, D.C.')
41
- assert_equal 'The USA,Washington^2C D.C.', capitol.to_param
42
- end
43
-
44
- def test_ids_to_s
45
- testing_with do
46
- order = @klass.primary_key.is_a?(String) ? @klass.primary_key : @klass.primary_key.join(',')
47
- to_test = @klass.order(order)[0..1].map(&:id)
48
- assert_equal '(1,1),(1,2)', @klass.ids_to_s(to_test) if @key_test == :dual
49
- assert_equal '1,1;1,2', @klass.ids_to_s(to_test, ',', ';', '', '') if @key_test == :dual
50
- end
51
- end
52
-
53
- def test_set_ids_string
54
- testing_with do
55
- array = @primary_keys.collect {|key| 5}
56
- expected = composite? ? array.to_composite_keys : array.first
57
- @first.id = expected.to_s
58
- assert_equal expected, @first.id
59
- end
60
- end
61
-
62
- def test_set_ids_array
63
- testing_with do
64
- array = @primary_keys.collect {|key| 5}
65
- expected = composite? ? array.to_composite_keys : array.first
66
- @first.id = expected
67
- assert_equal expected, @first.id
68
- end
69
- end
70
-
71
- def test_set_ids_comp
72
- testing_with do
73
- array = @primary_keys.collect {|key| 5}
74
- expected = composite? ? array.to_composite_keys : array.first
75
- @first.id = expected
76
- assert_equal expected, @first.id
77
- end
78
- end
79
-
80
- def test_primary_keys
81
- testing_with do
82
- if composite?
83
- assert_not_nil @klass.primary_keys
84
- assert_equal @primary_keys.map {|key| key.to_s}, @klass.primary_keys
85
- assert_equal @klass.primary_keys, @klass.primary_key
86
- assert_kind_of(CompositePrimaryKeys::CompositeKeys, @klass.primary_keys)
87
- assert_equal @primary_keys.map {|key| key.to_sym}.join(','), @klass.primary_key.to_s
88
- else
89
- assert_not_nil @klass.primary_key
90
- assert_equal @primary_keys.first, @klass.primary_key.to_sym
91
- assert_equal @primary_keys.first.to_s, @klass.primary_key.to_s
92
- end
93
- end
94
- end
95
-
96
- def test_inherited_primary_keys
97
- assert_equal(["reference_type_id", "reference_code"], ChildCpkTest.primary_keys)
98
- end
99
-
100
- def test_inherited_ids
101
- cpk_test = ChildCpkTest.new
102
- assert_equal([nil, nil], cpk_test.id)
103
- end
104
-
105
- def test_assign_ids
106
- ref_code = ReferenceCode.new
107
- assert_equal([nil, nil], ref_code.id)
108
-
109
- ref_code.id = [2,1]
110
- assert_equal([2,1], ref_code.id)
111
- end
112
- end
1
+ require File.expand_path('../abstract_unit', __FILE__)
2
+
3
+ class ChildCpkTest < ReferenceCode
4
+ end
5
+
6
+ class TestIds < ActiveSupport::TestCase
7
+ fixtures :reference_types, :reference_codes
8
+
9
+ CLASSES = {
10
+ :single => {
11
+ :class => ReferenceType,
12
+ :primary_keys => [:reference_type_id],
13
+ },
14
+ :dual => {
15
+ :class => ReferenceCode,
16
+ :primary_keys => [:reference_type_id, :reference_code],
17
+ },
18
+ :dual_strs => {
19
+ :class => ReferenceCode,
20
+ :primary_keys => ['reference_type_id', 'reference_code'],
21
+ }
22
+ }
23
+
24
+ def setup
25
+ self.class.classes = CLASSES
26
+ end
27
+
28
+ def test_id
29
+ testing_with do
30
+ assert_equal @first.id, @first.ids if composite?
31
+ assert_kind_of(CompositePrimaryKeys::CompositeKeys, @first.id) if composite?
32
+ end
33
+ end
34
+
35
+ def test_to_param
36
+ testing_with do
37
+ assert_equal '1,1', @first.to_param if composite?
38
+ end
39
+
40
+ capitol = Capitol.create!(country: 'The USA', city: 'Washington, D.C.')
41
+ assert_equal 'The USA,Washington^2C D.C.', capitol.to_param
42
+ end
43
+
44
+ def test_ids_to_s
45
+ testing_with do
46
+ order = @klass.primary_key.is_a?(String) ? @klass.primary_key : @klass.primary_key.join(',')
47
+ to_test = @klass.order(order)[0..1].map(&:id)
48
+ assert_equal '(1,1),(1,2)', @klass.ids_to_s(to_test) if @key_test == :dual
49
+ assert_equal '1,1;1,2', @klass.ids_to_s(to_test, ',', ';', '', '') if @key_test == :dual
50
+ end
51
+ end
52
+
53
+ def test_set_ids_string
54
+ testing_with do
55
+ array = @primary_keys.collect {|key| 5}
56
+ expected = composite? ? array.to_composite_keys : array.first
57
+ @first.id = expected.to_s
58
+ assert_equal expected, @first.id
59
+ end
60
+ end
61
+
62
+ def test_set_ids_array
63
+ testing_with do
64
+ array = @primary_keys.collect {|key| 5}
65
+ expected = composite? ? array.to_composite_keys : array.first
66
+ @first.id = expected
67
+ assert_equal expected, @first.id
68
+ end
69
+ end
70
+
71
+ def test_set_ids_comp
72
+ testing_with do
73
+ array = @primary_keys.collect {|key| 5}
74
+ expected = composite? ? array.to_composite_keys : array.first
75
+ @first.id = expected
76
+ assert_equal expected, @first.id
77
+ end
78
+ end
79
+
80
+ def test_primary_keys
81
+ testing_with do
82
+ if composite?
83
+ assert_not_nil @klass.primary_keys
84
+ assert_equal @primary_keys.map {|key| key.to_s}, @klass.primary_keys
85
+ assert_equal @klass.primary_keys, @klass.primary_key
86
+ assert_kind_of(CompositePrimaryKeys::CompositeKeys, @klass.primary_keys)
87
+ assert_equal @primary_keys.map {|key| key.to_sym}.join(','), @klass.primary_key.to_s
88
+ else
89
+ assert_not_nil @klass.primary_key
90
+ assert_equal @primary_keys.first, @klass.primary_key.to_sym
91
+ assert_equal @primary_keys.first.to_s, @klass.primary_key.to_s
92
+ end
93
+ end
94
+ end
95
+
96
+ def test_inherited_primary_keys
97
+ assert_equal(["reference_type_id", "reference_code"], ChildCpkTest.primary_keys)
98
+ end
99
+
100
+ def test_inherited_ids
101
+ cpk_test = ChildCpkTest.new
102
+ assert_equal([nil, nil], cpk_test.id)
103
+ end
104
+
105
+ def test_assign_ids
106
+ ref_code = ReferenceCode.new
107
+ assert_equal([nil, nil], ref_code.id)
108
+
109
+ ref_code.id = [2,1]
110
+ assert_equal([2,1], ref_code.id)
111
+ end
112
+ end
@@ -1,67 +1,67 @@
1
- require File.expand_path('../abstract_unit', __FILE__)
2
-
3
- # Testing the find action on composite ActiveRecords with two primary keys
4
- class TestNestedAttributes < ActiveSupport::TestCase
5
- fixtures :reference_types, :reference_codes
6
-
7
- def test_nested_atttribute_create
8
- code_id = 1001
9
-
10
- reference_type = reference_types(:name_prefix)
11
- reference_type.update :reference_codes_attributes => [{
12
- :reference_code => code_id,
13
- :code_label => 'XX',
14
- :abbreviation => 'Xx'
15
- }]
16
- assert_not_nil ReferenceCode.find_by_reference_code(code_id)
17
- end
18
-
19
- def test_nested_atttribute_update
20
- code_id = 1002
21
-
22
- reference_type = reference_types(:name_prefix)
23
- reference_type.update :reference_codes_attributes => [{
24
- :reference_code => code_id,
25
- :code_label => 'XX',
26
- :abbreviation => 'Xx'
27
- }]
28
-
29
- reference_code = ReferenceCode.find_by_reference_code(code_id)
30
- cpk = CompositePrimaryKeys::CompositeKeys[reference_type.reference_type_id, code_id]
31
- reference_type.update :reference_codes_attributes => [{
32
- :id => cpk,
33
- :code_label => 'AAA',
34
- :abbreviation => 'Aaa'
35
- }]
36
-
37
- reference_code = ReferenceCode.find_by_reference_code(code_id)
38
- assert_kind_of(ReferenceCode, reference_code)
39
- assert_equal(reference_code.code_label, 'AAA')
40
- end
41
-
42
- def test_nested_atttribute_update_2
43
- reference_type = reference_types(:gender)
44
- reference_code = reference_codes(:gender_male)
45
-
46
- reference_type.update(:reference_codes_attributes => [{:id => reference_code.id,
47
- :code_label => 'XX',
48
- :abbreviation => 'Xx'}])
49
-
50
- reference_code.reload
51
- assert_equal(reference_code.code_label, 'XX')
52
- assert_equal(reference_code.abbreviation, 'Xx')
53
- end
54
-
55
- def test_nested_atttribute_update_3
56
- reference_type = reference_types(:gender)
57
- reference_code = reference_codes(:gender_male)
58
-
59
- reference_type.update(:reference_codes_attributes => [{:id => reference_code.id.to_s,
60
- :code_label => 'XX',
61
- :abbreviation => 'Xx'}])
62
-
63
- reference_code.reload
64
- assert_equal(reference_code.code_label, 'XX')
65
- assert_equal(reference_code.abbreviation, 'Xx')
66
- end
67
- end
1
+ require File.expand_path('../abstract_unit', __FILE__)
2
+
3
+ # Testing the find action on composite ActiveRecords with two primary keys
4
+ class TestNestedAttributes < ActiveSupport::TestCase
5
+ fixtures :reference_types, :reference_codes
6
+
7
+ def test_nested_atttribute_create
8
+ code_id = 1001
9
+
10
+ reference_type = reference_types(:name_prefix)
11
+ reference_type.update :reference_codes_attributes => [{
12
+ :reference_code => code_id,
13
+ :code_label => 'XX',
14
+ :abbreviation => 'Xx'
15
+ }]
16
+ assert_not_nil ReferenceCode.find_by_reference_code(code_id)
17
+ end
18
+
19
+ def test_nested_atttribute_update
20
+ code_id = 1002
21
+
22
+ reference_type = reference_types(:name_prefix)
23
+ reference_type.update :reference_codes_attributes => [{
24
+ :reference_code => code_id,
25
+ :code_label => 'XX',
26
+ :abbreviation => 'Xx'
27
+ }]
28
+
29
+ reference_code = ReferenceCode.find_by_reference_code(code_id)
30
+ cpk = CompositePrimaryKeys::CompositeKeys[reference_type.reference_type_id, code_id]
31
+ reference_type.update :reference_codes_attributes => [{
32
+ :id => cpk,
33
+ :code_label => 'AAA',
34
+ :abbreviation => 'Aaa'
35
+ }]
36
+
37
+ reference_code = ReferenceCode.find_by_reference_code(code_id)
38
+ assert_kind_of(ReferenceCode, reference_code)
39
+ assert_equal(reference_code.code_label, 'AAA')
40
+ end
41
+
42
+ def test_nested_atttribute_update_2
43
+ reference_type = reference_types(:gender)
44
+ reference_code = reference_codes(:gender_male)
45
+
46
+ reference_type.update(:reference_codes_attributes => [{:id => reference_code.id,
47
+ :code_label => 'XX',
48
+ :abbreviation => 'Xx'}])
49
+
50
+ reference_code.reload
51
+ assert_equal(reference_code.code_label, 'XX')
52
+ assert_equal(reference_code.abbreviation, 'Xx')
53
+ end
54
+
55
+ def test_nested_atttribute_update_3
56
+ reference_type = reference_types(:gender)
57
+ reference_code = reference_codes(:gender_male)
58
+
59
+ reference_type.update(:reference_codes_attributes => [{:id => reference_code.id.to_s,
60
+ :code_label => 'XX',
61
+ :abbreviation => 'Xx'}])
62
+
63
+ reference_code.reload
64
+ assert_equal(reference_code.code_label, 'XX')
65
+ assert_equal(reference_code.abbreviation, 'Xx')
66
+ end
67
+ end
data/test/test_update.rb CHANGED
@@ -1,97 +1,97 @@
1
- require File.expand_path('../abstract_unit', __FILE__)
2
-
3
- class TestUpdate < ActiveSupport::TestCase
4
- fixtures :departments, :reference_types, :reference_codes, :rooms, :room_assignments
5
-
6
- CLASSES = {
7
- :single => {
8
- :class => ReferenceType,
9
- :primary_keys => :reference_type_id,
10
- :update => { :description => 'RT Desc' },
11
- },
12
- :dual => {
13
- :class => ReferenceCode,
14
- :primary_keys => [:reference_type_id, :reference_code],
15
- :update => { :description => 'RT Desc' },
16
- },
17
- }
18
-
19
- def setup
20
- self.class.classes = CLASSES
21
- end
22
-
23
- def test_setup
24
- testing_with do
25
- assert_not_nil @klass_info[:update]
26
- end
27
- end
28
-
29
- def test_update_attributes
30
- testing_with do
31
- assert(@first.update(@klass_info[:update]))
32
- assert(@first.reload)
33
- @klass_info[:update].each_pair do |attr_name, new_value|
34
- assert_equal(new_value, @first[attr_name])
35
- end
36
- end
37
- end
38
-
39
- def test_update_attributes_with_id_field
40
- department = departments(:accounting)
41
- department.update_attribute(:location_id, 3)
42
- department.reload
43
- assert_equal(3, department.location_id)
44
- end
45
-
46
- def test_update_primary_key
47
- obj = ReferenceCode.find([1,1])
48
- obj.reference_type_id = 2
49
- obj.reference_code = 3
50
- assert_equal({"reference_type_id" => 2, "reference_code" => 3}, obj.ids_hash)
51
- assert(obj.save)
52
- assert(obj.reload)
53
- assert_equal(2, obj.reference_type_id)
54
- assert_equal(3, obj.reference_code)
55
- assert_equal({"reference_type_id" => 2, "reference_code" => 3}, obj.ids_hash)
56
- assert_equal([2, 3], obj.id)
57
- end
58
-
59
- def test_update_attribute
60
- obj = ReferenceType.find(1)
61
- obj[:abbreviation] = 'a'
62
- obj['abbreviation'] = 'b'
63
- assert(obj.save)
64
- assert(obj.reload)
65
- assert_equal('b', obj.abbreviation)
66
- end
67
-
68
- def test_update_all
69
- ReferenceCode.update_all(description: 'random value')
70
-
71
- ReferenceCode.all.each do |reference_code|
72
- assert_equal('random value', reference_code.description)
73
- end
74
- end
75
-
76
- def test_update_all_join
77
- ReferenceCode.joins(:reference_type).
78
- where('reference_types.reference_type_id = ?', 2).
79
- update_all(:description => 'random value')
80
-
81
- query = ReferenceCode.where('reference_type_id = ?', 2).
82
- where(:description => 'random value')
83
-
84
- assert_equal(2, query.count)
85
- end
86
-
87
- def test_update_with_uniqueness
88
- assignment = room_assignments(:jacksons_room)
89
- room_1 = rooms(:branner_room_1)
90
- room_2 = rooms(:branner_room_3)
91
-
92
- assert_equal(room_1, assignment.room)
93
- assignment.room = room_2
94
- assignment.save!
95
- assert_equal(room_2, assignment.room)
96
- end
1
+ require File.expand_path('../abstract_unit', __FILE__)
2
+
3
+ class TestUpdate < ActiveSupport::TestCase
4
+ fixtures :departments, :reference_types, :reference_codes, :rooms, :room_assignments
5
+
6
+ CLASSES = {
7
+ :single => {
8
+ :class => ReferenceType,
9
+ :primary_keys => :reference_type_id,
10
+ :update => { :description => 'RT Desc' },
11
+ },
12
+ :dual => {
13
+ :class => ReferenceCode,
14
+ :primary_keys => [:reference_type_id, :reference_code],
15
+ :update => { :description => 'RT Desc' },
16
+ },
17
+ }
18
+
19
+ def setup
20
+ self.class.classes = CLASSES
21
+ end
22
+
23
+ def test_setup
24
+ testing_with do
25
+ assert_not_nil @klass_info[:update]
26
+ end
27
+ end
28
+
29
+ def test_update_attributes
30
+ testing_with do
31
+ assert(@first.update(@klass_info[:update]))
32
+ assert(@first.reload)
33
+ @klass_info[:update].each_pair do |attr_name, new_value|
34
+ assert_equal(new_value, @first[attr_name])
35
+ end
36
+ end
37
+ end
38
+
39
+ def test_update_attributes_with_id_field
40
+ department = departments(:accounting)
41
+ department.update_attribute(:location_id, 3)
42
+ department.reload
43
+ assert_equal(3, department.location_id)
44
+ end
45
+
46
+ def test_update_primary_key
47
+ obj = ReferenceCode.find([1,1])
48
+ obj.reference_type_id = 2
49
+ obj.reference_code = 3
50
+ assert_equal({"reference_type_id" => 2, "reference_code" => 3}, obj.ids_hash)
51
+ assert(obj.save)
52
+ assert(obj.reload)
53
+ assert_equal(2, obj.reference_type_id)
54
+ assert_equal(3, obj.reference_code)
55
+ assert_equal({"reference_type_id" => 2, "reference_code" => 3}, obj.ids_hash)
56
+ assert_equal([2, 3], obj.id)
57
+ end
58
+
59
+ def test_update_attribute
60
+ obj = ReferenceType.find(1)
61
+ obj[:abbreviation] = 'a'
62
+ obj['abbreviation'] = 'b'
63
+ assert(obj.save)
64
+ assert(obj.reload)
65
+ assert_equal('b', obj.abbreviation)
66
+ end
67
+
68
+ def test_update_all
69
+ ReferenceCode.update_all(description: 'random value')
70
+
71
+ ReferenceCode.all.each do |reference_code|
72
+ assert_equal('random value', reference_code.description)
73
+ end
74
+ end
75
+
76
+ def test_update_all_join
77
+ ReferenceCode.joins(:reference_type).
78
+ where('reference_types.reference_type_id = ?', 2).
79
+ update_all(:description => 'random value')
80
+
81
+ query = ReferenceCode.where('reference_type_id = ?', 2).
82
+ where(:description => 'random value')
83
+
84
+ assert_equal(2, query.count)
85
+ end
86
+
87
+ def test_update_with_uniqueness
88
+ assignment = room_assignments(:jacksons_room)
89
+ room_1 = rooms(:branner_room_1)
90
+ room_2 = rooms(:branner_room_3)
91
+
92
+ assert_equal(room_1, assignment.room)
93
+ assignment.room = room_2
94
+ assignment.save!
95
+ assert_equal(room_2, assignment.room)
96
+ end
97
97
  end