composite_primary_keys 8.1.0 → 8.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/History.rdoc +642 -625
  3. data/README.rdoc +5 -2
  4. data/lib/composite_primary_keys.rb +115 -115
  5. data/lib/composite_primary_keys/associations/association.rb +23 -23
  6. data/lib/composite_primary_keys/associations/association_scope.rb +73 -73
  7. data/lib/composite_primary_keys/associations/collection_association.rb +14 -14
  8. data/lib/composite_primary_keys/associations/has_many_association.rb +69 -69
  9. data/lib/composite_primary_keys/associations/join_dependency.rb +87 -87
  10. data/lib/composite_primary_keys/associations/preloader/association.rb +90 -90
  11. data/lib/composite_primary_keys/associations/singular_association.rb +18 -18
  12. data/lib/composite_primary_keys/attribute_methods.rb +9 -9
  13. data/lib/composite_primary_keys/attribute_methods/dirty.rb +29 -29
  14. data/lib/composite_primary_keys/attribute_methods/read.rb +24 -24
  15. data/lib/composite_primary_keys/attribute_methods/write.rb +30 -30
  16. data/lib/composite_primary_keys/attribute_set/builder.rb +19 -19
  17. data/lib/composite_primary_keys/base.rb +129 -135
  18. data/lib/composite_primary_keys/composite_arrays.rb +43 -43
  19. data/lib/composite_primary_keys/connection_adapters/abstract/connection_specification_changes.rb +2 -3
  20. data/lib/composite_primary_keys/core.rb +60 -60
  21. data/lib/composite_primary_keys/persistence.rb +56 -56
  22. data/lib/composite_primary_keys/relation.rb +68 -68
  23. data/lib/composite_primary_keys/relation/calculations.rb +78 -78
  24. data/lib/composite_primary_keys/relation/finder_methods.rb +179 -179
  25. data/lib/composite_primary_keys/sanitization.rb +52 -52
  26. data/lib/composite_primary_keys/validations/uniqueness.rb +36 -36
  27. data/lib/composite_primary_keys/version.rb +8 -8
  28. data/tasks/databases/sqlserver.rake +27 -27
  29. data/test/abstract_unit.rb +114 -113
  30. data/test/connections/databases.example.yml +25 -25
  31. data/test/connections/native_sqlserver/connection.rb +11 -11
  32. data/test/fixtures/db_definitions/mysql.sql +218 -218
  33. data/test/fixtures/db_definitions/postgresql.sql +220 -220
  34. data/test/fixtures/db_definitions/sqlite.sql +206 -206
  35. data/test/fixtures/db_definitions/sqlserver.drop.sql +91 -91
  36. data/test/fixtures/db_definitions/sqlserver.sql +226 -226
  37. data/test/fixtures/employee.rb +11 -11
  38. data/test/fixtures/salary.rb +5 -5
  39. data/test/test_associations.rb +341 -340
  40. data/test/test_attributes.rb +60 -60
  41. data/test/test_create.rb +157 -157
  42. data/test/test_delete.rb +158 -158
  43. data/test/test_delete_all.rb +33 -28
  44. data/test/test_enum.rb +21 -21
  45. data/test/test_equal.rb +26 -26
  46. data/test/test_find.rb +119 -118
  47. data/test/test_habtm.rb +117 -113
  48. data/test/test_polymorphic.rb +27 -26
  49. data/test/test_tutorial_example.rb +25 -25
  50. metadata +44 -2
@@ -1,61 +1,61 @@
1
- require File.expand_path('../abstract_unit', __FILE__)
2
-
3
- class TestAttributes < ActiveSupport::TestCase
4
- fixtures :reference_types, :reference_codes, :products, :tariffs, :product_tariffs
5
-
6
- CLASSES = {
7
- :single => {
8
- :class => ReferenceType,
9
- :primary_keys => :reference_type_id,
10
- },
11
- :dual => {
12
- :class => ReferenceCode,
13
- :primary_keys => [:reference_type_id, :reference_code],
14
- },
15
- }
16
-
17
- def setup
18
- self.class.classes = CLASSES
19
- end
20
-
21
- def test_brackets
22
- testing_with do
23
- @first.attributes.each_pair do |attr_name, value|
24
- assert_equal value, @first[attr_name]
25
- end
26
- end
27
- end
28
-
29
- def test_brackets_primary_key
30
- testing_with do
31
- assert_equal(@first.id, @first[@primary_keys])
32
- assert_equal(@first.id, @first[@first.class.primary_key])
33
- end
34
- end
35
-
36
- def test_brackets_assignment
37
- testing_with do
38
- @first.attributes.each_pair do |attr_name, value|
39
- next if attr_name == @first.class.primary_key
40
- @first[attr_name]= !value.nil? ? value * 2 : '1'
41
- assert_equal !value.nil? ? value * 2 : '1', @first[attr_name]
42
- end
43
- end
44
- end
45
-
46
- def test_brackets_foreign_key_assignment
47
- tarrif = tariffs(:flat)
48
- product_tariff = product_tariffs(:first_flat)
49
- compare_indexes(tarrif, tarrif.class.primary_key, product_tariff, [:tariff_id, :tariff_start_date])
50
- end
51
-
52
- private
53
-
54
- def compare_indexes(obj1, indexes1, obj2, indexes2)
55
- indexes1.length.times do |key_index|
56
- key1 = indexes1[key_index]
57
- key2 = indexes2[key_index]
58
- assert_equal(obj1[key1], obj2[key2])
59
- end
60
- end
1
+ require File.expand_path('../abstract_unit', __FILE__)
2
+
3
+ class TestAttributes < ActiveSupport::TestCase
4
+ fixtures :reference_types, :reference_codes, :products, :tariffs, :product_tariffs
5
+
6
+ CLASSES = {
7
+ :single => {
8
+ :class => ReferenceType,
9
+ :primary_keys => :reference_type_id,
10
+ },
11
+ :dual => {
12
+ :class => ReferenceCode,
13
+ :primary_keys => [:reference_type_id, :reference_code],
14
+ },
15
+ }
16
+
17
+ def setup
18
+ self.class.classes = CLASSES
19
+ end
20
+
21
+ def test_brackets
22
+ testing_with do
23
+ @first.attributes.each_pair do |attr_name, value|
24
+ assert_equal value, @first[attr_name]
25
+ end
26
+ end
27
+ end
28
+
29
+ def test_brackets_primary_key
30
+ testing_with do
31
+ assert_equal(@first.id, @first[@primary_keys])
32
+ assert_equal(@first.id, @first[@first.class.primary_key])
33
+ end
34
+ end
35
+
36
+ def test_brackets_assignment
37
+ testing_with do
38
+ @first.attributes.each_pair do |attr_name, value|
39
+ next if attr_name == @first.class.primary_key
40
+ @first[attr_name]= !value.nil? ? value * 2 : '1'
41
+ assert_equal !value.nil? ? value * 2 : '1', @first[attr_name]
42
+ end
43
+ end
44
+ end
45
+
46
+ def test_brackets_foreign_key_assignment
47
+ tarrif = tariffs(:flat)
48
+ product_tariff = product_tariffs(:first_flat)
49
+ compare_indexes(tarrif, tarrif.class.primary_key, product_tariff, [:tariff_id, :tariff_start_date])
50
+ end
51
+
52
+ private
53
+
54
+ def compare_indexes(obj1, indexes1, obj2, indexes2)
55
+ indexes1.length.times do |key_index|
56
+ key1 = indexes1[key_index]
57
+ key2 = indexes2[key_index]
58
+ assert_equal(obj1[key1], obj2[key2])
59
+ end
60
+ end
61
61
  end
@@ -1,157 +1,157 @@
1
- require File.expand_path('../abstract_unit', __FILE__)
2
-
3
- class TestCreate < ActiveSupport::TestCase
4
- fixtures :students, :dorms, :rooms, :room_assignments, :reference_types, :reference_codes, :streets, :suburbs
5
-
6
- CLASSES = {
7
- :single => {
8
- :class => ReferenceType,
9
- :primary_keys => :reference_type_id,
10
- :create => {:reference_type_id => 10, :type_label => 'NEW_TYPE', :abbreviation => 'New Type'}
11
- },
12
- :dual => {
13
- :class => ReferenceCode,
14
- :primary_keys => [:reference_type_id, :reference_code],
15
- :create => {:reference_type_id => 1, :reference_code => 20, :code_label => 'NEW_CODE', :abbreviation => 'New Code'}
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[:create]
26
- end
27
- end
28
-
29
- def test_create
30
- testing_with do
31
- assert new_obj = @klass.create(@klass_info[:create])
32
- assert !new_obj.new_record?
33
- assert new_obj.id
34
- end
35
- end
36
-
37
- def test_create_no_id
38
- testing_with do
39
- begin
40
- @obj = @klass.create(@klass_info[:create].except(@klass.primary_key))
41
- @successful = !composite?
42
- rescue ActiveRecord::CompositeKeyError
43
- @successful = false
44
- rescue
45
- flunk "Incorrect exception raised: #{$!}, #{$!.class}"
46
- end
47
- assert_equal composite?, !@successful, "Create should have failed for composites; #{@obj.inspect}"
48
- end
49
- end
50
-
51
- def test_create_on_association
52
- suburb = Suburb.first
53
- suburb.streets.create(:name => "my street")
54
- street = Street.find_by_name('my street')
55
- assert_equal(suburb.city_id, street.city_id)
56
- assert_equal(suburb.suburb_id, street.suburb_id)
57
- end
58
-
59
- def test_create_on_association_when_belongs_to_is_single_key
60
- rt = ReferenceType.first
61
- rt.reference_codes.create(:reference_code => 4321, :code_label => 'foo', :abbreviation => 'bar')
62
- rc = ReferenceCode.find_by_reference_code(4321)
63
- assert_equal(rc.reference_type_id, rt.reference_type_id)
64
- end
65
-
66
- def test_new_habtm
67
- restaurant = Restaurant.new(:franchise_id => 101,
68
- :store_id => 201,
69
- :name => "My Store")
70
-
71
- restaurant.suburbs << Suburb.new(:city_id => 24,
72
- :suburb_id => 25,
73
- :name => "My Suburb")
74
-
75
- restaurant.save!
76
-
77
- # Test restaurant
78
- assert_equal(101, restaurant.franchise_id)
79
- assert_equal(201, restaurant.store_id)
80
- assert_equal("My Store", restaurant.name)
81
- assert_equal(1, restaurant.suburbs.length)
82
-
83
- # Test suburbs
84
- suburb = restaurant.suburbs[0]
85
- assert_equal(24, suburb.city_id)
86
- assert_equal(25, suburb.suburb_id)
87
- assert_equal("My Suburb", suburb.name)
88
- end
89
-
90
- def test_create_habtm
91
- restaurant = Restaurant.create(:franchise_id => 100,
92
- :store_id => 200,
93
- :name => "My Store")
94
-
95
- restaurant.suburbs.create(:city_id => 24,
96
- :suburb_id => 25,
97
- :name => "My Suburb")
98
-
99
- # Test restaurant
100
- assert_equal(100, restaurant.franchise_id)
101
- assert_equal(200, restaurant.store_id)
102
- assert_equal("My Store", restaurant.name)
103
-
104
- assert_equal(1, restaurant.suburbs(true).length)
105
-
106
- # Test suburbs
107
- suburb = restaurant.suburbs[0]
108
- assert_equal(24, suburb.city_id)
109
- assert_equal(25, suburb.suburb_id)
110
- assert_equal("My Suburb", suburb.name)
111
- end
112
-
113
- def test_has_many_ids_1
114
- dorm = dorms(:toyon)
115
- room = Room.new(:dorm_id => dorm.id, :room_id => 5)
116
- room.save!
117
-
118
- student1 = students(:kelly)
119
- student2 = students(:jordan)
120
-
121
- RoomAssignment.delete_all
122
-
123
- assignment1 = RoomAssignment.new(:student_id => student1.id, :dorm_id => room.dorm_id, :room_id => room.room_id)
124
- assignment1.save!
125
-
126
- room.room_assignment_ids = [[assignment1.student_id, assignment1.dorm_id, assignment1.room_id]]
127
- room.save!
128
-
129
- assert_equal(1, room.room_assignments.length)
130
- assert_equal(assignment1, room.room_assignments.first)
131
- end
132
-
133
- def test_has_many_ids_2
134
- dorm = dorms(:toyon)
135
- room = Room.new(:dorm_id => dorm.id, :room_id => 5)
136
- room.save!
137
-
138
- student1 = students(:kelly)
139
- student2 = students(:jordan)
140
-
141
- RoomAssignment.delete_all
142
-
143
- assignment1 = RoomAssignment.new(:student_id => student1.id, :dorm_id => room.dorm_id, :room_id => room.room_id)
144
- assignment1.save!
145
-
146
- assignment2 = RoomAssignment.new(:student_id => student2.id, :dorm_id => room.dorm_id, :room_id => room.room_id)
147
- assignment2.save!
148
-
149
- room.room_assignment_ids = [[assignment1.student_id, assignment1.dorm_id, assignment1.room_id],
150
- [assignment2.student_id, assignment2.dorm_id, assignment2.room_id]]
151
- room.save!
152
-
153
- assert_equal(2, room.room_assignments.length)
154
- assert_equal(assignment1, room.room_assignments[0])
155
- assert_equal(assignment2, room.room_assignments[1])
156
- end
157
- end
1
+ require File.expand_path('../abstract_unit', __FILE__)
2
+
3
+ class TestCreate < ActiveSupport::TestCase
4
+ fixtures :students, :dorms, :rooms, :room_assignments, :reference_types, :reference_codes, :streets, :suburbs
5
+
6
+ CLASSES = {
7
+ :single => {
8
+ :class => ReferenceType,
9
+ :primary_keys => :reference_type_id,
10
+ :create => {:reference_type_id => 10, :type_label => 'NEW_TYPE', :abbreviation => 'New Type'}
11
+ },
12
+ :dual => {
13
+ :class => ReferenceCode,
14
+ :primary_keys => [:reference_type_id, :reference_code],
15
+ :create => {:reference_type_id => 1, :reference_code => 20, :code_label => 'NEW_CODE', :abbreviation => 'New Code'}
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[:create]
26
+ end
27
+ end
28
+
29
+ def test_create
30
+ testing_with do
31
+ assert new_obj = @klass.create(@klass_info[:create])
32
+ assert !new_obj.new_record?
33
+ assert new_obj.id
34
+ end
35
+ end
36
+
37
+ def test_create_no_id
38
+ testing_with do
39
+ begin
40
+ @obj = @klass.create(@klass_info[:create].except(@klass.primary_key))
41
+ @successful = !composite?
42
+ rescue ActiveRecord::CompositeKeyError
43
+ @successful = false
44
+ rescue
45
+ flunk "Incorrect exception raised: #{$!}, #{$!.class}"
46
+ end
47
+ assert_equal composite?, !@successful, "Create should have failed for composites; #{@obj.inspect}"
48
+ end
49
+ end
50
+
51
+ def test_create_on_association
52
+ suburb = Suburb.first
53
+ suburb.streets.create(:name => "my street")
54
+ street = Street.find_by_name('my street')
55
+ assert_equal(suburb.city_id, street.city_id)
56
+ assert_equal(suburb.suburb_id, street.suburb_id)
57
+ end
58
+
59
+ def test_create_on_association_when_belongs_to_is_single_key
60
+ rt = ReferenceType.first
61
+ rt.reference_codes.create(:reference_code => 4321, :code_label => 'foo', :abbreviation => 'bar')
62
+ rc = ReferenceCode.find_by_reference_code(4321)
63
+ assert_equal(rc.reference_type_id, rt.reference_type_id)
64
+ end
65
+
66
+ def test_new_habtm
67
+ restaurant = Restaurant.new(:franchise_id => 101,
68
+ :store_id => 201,
69
+ :name => "My Store")
70
+
71
+ restaurant.suburbs << Suburb.new(:city_id => 24,
72
+ :suburb_id => 25,
73
+ :name => "My Suburb")
74
+
75
+ restaurant.save!
76
+
77
+ # Test restaurant
78
+ assert_equal(101, restaurant.franchise_id)
79
+ assert_equal(201, restaurant.store_id)
80
+ assert_equal("My Store", restaurant.name)
81
+ assert_equal(1, restaurant.suburbs.length)
82
+
83
+ # Test suburbs
84
+ suburb = restaurant.suburbs[0]
85
+ assert_equal(24, suburb.city_id)
86
+ assert_equal(25, suburb.suburb_id)
87
+ assert_equal("My Suburb", suburb.name)
88
+ end
89
+
90
+ def test_create_habtm
91
+ restaurant = Restaurant.create(:franchise_id => 100,
92
+ :store_id => 200,
93
+ :name => "My Store")
94
+
95
+ restaurant.suburbs.create(:city_id => 24,
96
+ :suburb_id => 25,
97
+ :name => "My Suburb")
98
+
99
+ # Test restaurant
100
+ assert_equal(100, restaurant.franchise_id)
101
+ assert_equal(200, restaurant.store_id)
102
+ assert_equal("My Store", restaurant.name)
103
+
104
+ assert_equal(1, restaurant.suburbs(true).length)
105
+
106
+ # Test suburbs
107
+ suburb = restaurant.suburbs[0]
108
+ assert_equal(24, suburb.city_id)
109
+ assert_equal(25, suburb.suburb_id)
110
+ assert_equal("My Suburb", suburb.name)
111
+ end
112
+
113
+ def test_has_many_ids_1
114
+ dorm = dorms(:toyon)
115
+ room = Room.new(:dorm_id => dorm.id, :room_id => 5)
116
+ room.save!
117
+
118
+ student1 = students(:kelly)
119
+ student2 = students(:jordan)
120
+
121
+ RoomAssignment.delete_all
122
+
123
+ assignment1 = RoomAssignment.new(:student_id => student1.id, :dorm_id => room.dorm_id, :room_id => room.room_id)
124
+ assignment1.save!
125
+
126
+ room.room_assignment_ids = [[assignment1.student_id, assignment1.dorm_id, assignment1.room_id]]
127
+ room.save!
128
+
129
+ assert_equal(1, room.room_assignments.length)
130
+ assert_equal(assignment1, room.room_assignments.first)
131
+ end
132
+
133
+ def test_has_many_ids_2
134
+ dorm = dorms(:toyon)
135
+ room = Room.new(:dorm_id => dorm.id, :room_id => 5)
136
+ room.save!
137
+
138
+ student1 = students(:kelly)
139
+ student2 = students(:jordan)
140
+
141
+ RoomAssignment.delete_all
142
+
143
+ assignment1 = RoomAssignment.new(:student_id => student1.id, :dorm_id => room.dorm_id, :room_id => room.room_id)
144
+ assignment1.save!
145
+
146
+ assignment2 = RoomAssignment.new(:student_id => student2.id, :dorm_id => room.dorm_id, :room_id => room.room_id)
147
+ assignment2.save!
148
+
149
+ room.room_assignment_ids = [[assignment1.student_id, assignment1.dorm_id, assignment1.room_id],
150
+ [assignment2.student_id, assignment2.dorm_id, assignment2.room_id]]
151
+ room.save!
152
+
153
+ assert_equal(2, room.room_assignments.length)
154
+ assert_equal(assignment1, room.room_assignments[0])
155
+ assert_equal(assignment2, room.room_assignments[1])
156
+ end
157
+ end
@@ -1,158 +1,158 @@
1
- require File.expand_path('../abstract_unit', __FILE__)
2
-
3
- class TestDelete < ActiveSupport::TestCase
4
- fixtures :articles, :departments, :employees, :products, :tariffs, :product_tariffs,
5
- :reference_types, :reference_codes
6
-
7
- CLASSES = {
8
- :single => {
9
- :class => ReferenceType,
10
- :primary_keys => :reference_type_id,
11
- },
12
- :dual => {
13
- :class => ReferenceCode,
14
- :primary_keys => [:reference_type_id, :reference_code],
15
- },
16
- }
17
-
18
- def setup
19
- self.class.classes = CLASSES
20
- end
21
-
22
- def test_destroy_one
23
- testing_with do
24
- assert @first.destroy
25
- end
26
- end
27
-
28
- def test_destroy_one_alone_via_class
29
- testing_with do
30
- assert @klass.destroy(@first.id)
31
- end
32
- end
33
-
34
- def test_delete_one_alone
35
- testing_with do
36
- assert @klass.delete(@first.id)
37
- end
38
- end
39
-
40
- def test_delete_many
41
- testing_with do
42
- to_delete = @klass.limit(2)
43
- assert_equal 2, to_delete.length
44
- end
45
- end
46
-
47
- def test_delete_all
48
- testing_with do
49
- @klass.delete_all
50
- end
51
- end
52
-
53
- def test_clear_association
54
- department = Department.find([1,1])
55
- assert_equal(2, department.employees.size, "Before clear employee count should be 2.")
56
-
57
- department.employees.clear
58
- assert_equal(0, department.employees.size, "After clear employee count should be 0.")
59
-
60
- department.reload
61
- assert_equal(0, department.employees.size, "After clear and a reload from DB employee count should be 0.")
62
- end
63
-
64
- def test_delete_association
65
- department = Department.find([1,1])
66
- assert_equal 2, department.employees.size , "Before delete employee count should be 2."
67
- first_employee = department.employees[0]
68
- department.employees.delete(first_employee)
69
- assert_equal 1, department.employees.size, "After delete employee count should be 1."
70
- department.reload
71
- assert_equal 1, department.employees.size, "After delete and a reload from DB employee count should be 1."
72
- end
73
-
74
- def test_destroy_has_one
75
- # In this case the association is a has_one with
76
- # dependent set to :destroy
77
- department = departments(:engineering)
78
- assert_not_nil(department.head)
79
-
80
- # Get head employee id
81
- head_id = department.head.id
82
-
83
- # Delete department - should delete the head
84
- department.destroy
85
-
86
- # Verify the head is also
87
- assert_raise(ActiveRecord::RecordNotFound) do
88
- Employee.find(head_id)
89
- end
90
- end
91
-
92
- def test_destroy_has_and_belongs_to_many_on_non_cpk
93
- steve = employees(:steve)
94
- records_before = ActiveRecord::Base.connection.execute("select * from employees_groups").count
95
- steve.destroy
96
- records_after = ActiveRecord::Base.connection.execute("select * from employees_groups").count
97
- assert_equal records_after, records_before - steve.groups.count
98
- end
99
-
100
- def test_destroy_has_and_belongs_to_many_on_non_cpk
101
- records_before = ActiveRecord::Base.connection.execute("select * from employees_groups").count
102
- employee = Employee.create
103
- employee.groups << Group.create(name: 'test')
104
- employees_groups_count = employee.groups.count
105
- employee.destroy!
106
- records_after = ActiveRecord::Base.connection.execute("select * from employees_groups").count
107
- assert_equal records_before, records_after
108
- end
109
-
110
- def test_delete_not_destroy_on_cpk
111
- tariff = Tariff.where(tariff_id: 2).first
112
- tariff.delete
113
- assert !tariff.persisted?
114
- end
115
-
116
- def test_delete_not_destroy_on_non_cpk
117
- article = Article.first
118
- article.delete
119
- assert !article.persisted?
120
- end
121
-
122
- def test_destroy_has_many_delete_all
123
- # In this case the association is a has_many composite key with
124
- # dependent set to :delete_all
125
- product = Product.find(1)
126
- assert_equal(2, product.product_tariffs.length)
127
-
128
- # Get product_tariff length
129
- product_tariff_size = ProductTariff.count
130
-
131
- # Delete product - should delete 2 product tariffs
132
- product.destroy
133
-
134
- # Verify product_tariff are deleted
135
- assert_equal(product_tariff_size - 2, ProductTariff.count)
136
- end
137
-
138
- def test_delete_cpk_association
139
- product = Product.find(1)
140
- assert_equal(2, product.product_tariffs.length)
141
-
142
- product_tariff = product.product_tariffs.first
143
- product.product_tariffs.delete(product_tariff)
144
-
145
- product.reload
146
- assert_equal(1, product.product_tariffs.length)
147
- end
148
-
149
- def test_delete_records_for_has_many_association_with_composite_primary_key
150
- reference_type = ReferenceType.find(1)
151
- codes_to_delete = reference_type.reference_codes[0..1]
152
- assert_equal(3, reference_type.reference_codes.size, "Before deleting records reference_code count should be 3.")
153
-
154
- reference_type.reference_codes.delete(codes_to_delete)
155
- reference_type.reload
156
- assert_equal(1, reference_type.reference_codes.size, "After deleting 2 records and a reload from DB reference_code count should be 1.")
157
- end
158
- end
1
+ require File.expand_path('../abstract_unit', __FILE__)
2
+
3
+ class TestDelete < ActiveSupport::TestCase
4
+ fixtures :articles, :departments, :employees, :products, :tariffs, :product_tariffs,
5
+ :reference_types, :reference_codes
6
+
7
+ CLASSES = {
8
+ :single => {
9
+ :class => ReferenceType,
10
+ :primary_keys => :reference_type_id,
11
+ },
12
+ :dual => {
13
+ :class => ReferenceCode,
14
+ :primary_keys => [:reference_type_id, :reference_code],
15
+ },
16
+ }
17
+
18
+ def setup
19
+ self.class.classes = CLASSES
20
+ end
21
+
22
+ def test_destroy_one
23
+ testing_with do
24
+ assert @first.destroy
25
+ end
26
+ end
27
+
28
+ def test_destroy_one_alone_via_class
29
+ testing_with do
30
+ assert @klass.destroy(@first.id)
31
+ end
32
+ end
33
+
34
+ def test_delete_one_alone
35
+ testing_with do
36
+ assert @klass.delete(@first.id)
37
+ end
38
+ end
39
+
40
+ def test_delete_many
41
+ testing_with do
42
+ to_delete = @klass.limit(2)
43
+ assert_equal 2, to_delete.length
44
+ end
45
+ end
46
+
47
+ def test_delete_all
48
+ testing_with do
49
+ @klass.delete_all
50
+ end
51
+ end
52
+
53
+ def test_clear_association
54
+ department = Department.find([1,1])
55
+ assert_equal(2, department.employees.size, "Before clear employee count should be 2.")
56
+
57
+ department.employees.clear
58
+ assert_equal(0, department.employees.size, "After clear employee count should be 0.")
59
+
60
+ department.reload
61
+ assert_equal(0, department.employees.size, "After clear and a reload from DB employee count should be 0.")
62
+ end
63
+
64
+ def test_delete_association
65
+ department = Department.find([1,1])
66
+ assert_equal 2, department.employees.size , "Before delete employee count should be 2."
67
+ first_employee = department.employees[0]
68
+ department.employees.delete(first_employee)
69
+ assert_equal 1, department.employees.size, "After delete employee count should be 1."
70
+ department.reload
71
+ assert_equal 1, department.employees.size, "After delete and a reload from DB employee count should be 1."
72
+ end
73
+
74
+ def test_destroy_has_one
75
+ # In this case the association is a has_one with
76
+ # dependent set to :destroy
77
+ department = departments(:engineering)
78
+ assert_not_nil(department.head)
79
+
80
+ # Get head employee id
81
+ head_id = department.head.id
82
+
83
+ # Delete department - should delete the head
84
+ department.destroy
85
+
86
+ # Verify the head is also
87
+ assert_raise(ActiveRecord::RecordNotFound) do
88
+ Employee.find(head_id)
89
+ end
90
+ end
91
+
92
+ def test_destroy_has_and_belongs_to_many_on_non_cpk
93
+ steve = employees(:steve)
94
+ records_before = ActiveRecord::Base.connection.execute("select * from employees_groups").count
95
+ steve.destroy
96
+ records_after = ActiveRecord::Base.connection.execute("select * from employees_groups").count
97
+ assert_equal records_after, records_before - steve.groups.count
98
+ end
99
+
100
+ def test_destroy_has_and_belongs_to_many_on_non_cpk
101
+ records_before = ActiveRecord::Base.connection.execute("select * from employees_groups").count
102
+ employee = Employee.create
103
+ employee.groups << Group.create(name: 'test')
104
+ employees_groups_count = employee.groups.count
105
+ employee.destroy!
106
+ records_after = ActiveRecord::Base.connection.execute("select * from employees_groups").count
107
+ assert_equal records_before, records_after
108
+ end
109
+
110
+ def test_delete_not_destroy_on_cpk
111
+ tariff = Tariff.where(tariff_id: 2).first
112
+ tariff.delete
113
+ assert !tariff.persisted?
114
+ end
115
+
116
+ def test_delete_not_destroy_on_non_cpk
117
+ article = Article.first
118
+ article.delete
119
+ assert !article.persisted?
120
+ end
121
+
122
+ def test_destroy_has_many_delete_all
123
+ # In this case the association is a has_many composite key with
124
+ # dependent set to :delete_all
125
+ product = Product.find(1)
126
+ assert_equal(2, product.product_tariffs.length)
127
+
128
+ # Get product_tariff length
129
+ product_tariff_size = ProductTariff.count
130
+
131
+ # Delete product - should delete 2 product tariffs
132
+ product.destroy
133
+
134
+ # Verify product_tariff are deleted
135
+ assert_equal(product_tariff_size - 2, ProductTariff.count)
136
+ end
137
+
138
+ def test_delete_cpk_association
139
+ product = Product.find(1)
140
+ assert_equal(2, product.product_tariffs.length)
141
+
142
+ product_tariff = product.product_tariffs.first
143
+ product.product_tariffs.delete(product_tariff)
144
+
145
+ product.reload
146
+ assert_equal(1, product.product_tariffs.length)
147
+ end
148
+
149
+ def test_delete_records_for_has_many_association_with_composite_primary_key
150
+ reference_type = ReferenceType.find(1)
151
+ codes_to_delete = reference_type.reference_codes[0..1]
152
+ assert_equal(3, reference_type.reference_codes.size, "Before deleting records reference_code count should be 3.")
153
+
154
+ reference_type.reference_codes.delete(codes_to_delete)
155
+ reference_type.reload
156
+ assert_equal(1, reference_type.reference_codes.size, "After deleting 2 records and a reload from DB reference_code count should be 1.")
157
+ end
158
+ end