composite_primary_keys 3.1.11 → 4.0.0.beta1
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.
- data/History.txt +6 -8
- data/lib/composite_primary_keys.rb +53 -36
- data/lib/composite_primary_keys/associations/association.rb +23 -0
- data/lib/composite_primary_keys/associations/association_scope.rb +67 -0
- data/lib/composite_primary_keys/associations/has_and_belongs_to_many_association.rb +31 -121
- data/lib/composite_primary_keys/associations/has_many_association.rb +27 -66
- data/lib/composite_primary_keys/associations/join_dependency/join_association.rb +22 -0
- data/lib/composite_primary_keys/associations/join_dependency/join_part.rb +39 -0
- data/lib/composite_primary_keys/associations/preloader/association.rb +61 -0
- data/lib/composite_primary_keys/associations/preloader/belongs_to.rb +13 -0
- data/lib/composite_primary_keys/associations/preloader/has_and_belongs_to_many.rb +46 -0
- data/lib/composite_primary_keys/attribute_methods/dirty.rb +30 -0
- data/lib/composite_primary_keys/attribute_methods/read.rb +88 -0
- data/lib/composite_primary_keys/attribute_methods/write.rb +33 -0
- data/lib/composite_primary_keys/base.rb +18 -70
- data/lib/composite_primary_keys/composite_predicates.rb +53 -0
- data/lib/composite_primary_keys/connection_adapters/abstract_adapter.rb +6 -4
- data/lib/composite_primary_keys/connection_adapters/postgresql_adapter.rb +19 -41
- data/lib/composite_primary_keys/fixtures.rb +19 -6
- data/lib/composite_primary_keys/persistence.rb +32 -13
- data/lib/composite_primary_keys/relation.rb +23 -16
- data/lib/composite_primary_keys/relation/calculations.rb +48 -0
- data/lib/composite_primary_keys/relation/finder_methods.rb +117 -0
- data/lib/composite_primary_keys/relation/query_methods.rb +24 -0
- data/lib/composite_primary_keys/validations/uniqueness.rb +19 -23
- data/lib/composite_primary_keys/version.rb +5 -5
- data/test/connections/native_mysql/connection.rb +1 -1
- data/test/fixtures/articles.yml +1 -0
- data/test/fixtures/products.yml +2 -4
- data/test/fixtures/readings.yml +1 -0
- data/test/fixtures/suburbs.yml +1 -4
- data/test/fixtures/users.yml +1 -0
- data/test/test_associations.rb +61 -63
- data/test/test_attributes.rb +16 -21
- data/test/test_create.rb +3 -3
- data/test/test_delete.rb +87 -84
- data/test/{test_clone.rb → test_dup.rb} +8 -5
- data/test/test_exists.rb +22 -10
- data/test/test_habtm.rb +0 -74
- data/test/test_ids.rb +2 -1
- data/test/test_miscellaneous.rb +2 -2
- data/test/test_polymorphic.rb +1 -1
- data/test/test_suite.rb +1 -1
- data/test/test_update.rb +3 -3
- metadata +76 -75
- data/lib/composite_primary_keys/association_preload.rb +0 -158
- data/lib/composite_primary_keys/associations.rb +0 -155
- data/lib/composite_primary_keys/associations/association_proxy.rb +0 -33
- data/lib/composite_primary_keys/associations/has_one_association.rb +0 -27
- data/lib/composite_primary_keys/associations/through_association_scope.rb +0 -103
- data/lib/composite_primary_keys/attribute_methods.rb +0 -84
- data/lib/composite_primary_keys/calculations.rb +0 -31
- data/lib/composite_primary_keys/connection_adapters/ibm_db_adapter.rb +0 -21
- data/lib/composite_primary_keys/connection_adapters/oracle_adapter.rb +0 -15
- data/lib/composite_primary_keys/connection_adapters/oracle_enhanced_adapter.rb +0 -17
- data/lib/composite_primary_keys/connection_adapters/sqlite3_adapter.rb +0 -15
- data/lib/composite_primary_keys/finder_methods.rb +0 -123
- data/lib/composite_primary_keys/primary_key.rb +0 -19
- data/lib/composite_primary_keys/query_methods.rb +0 -24
- data/lib/composite_primary_keys/read.rb +0 -25
- data/lib/composite_primary_keys/reflection.rb +0 -37
- data/lib/composite_primary_keys/write.rb +0 -18
@@ -0,0 +1,24 @@
|
|
1
|
+
module CompositePrimaryKeys
|
2
|
+
module ActiveRecord
|
3
|
+
module QueryMethods
|
4
|
+
def reverse_order
|
5
|
+
order_clause = arel.order_clauses
|
6
|
+
|
7
|
+
# CPK
|
8
|
+
# order = order_clause.empty? ?
|
9
|
+
# "#{table_name}.#{primary_key} DESC" :
|
10
|
+
# reverse_sql_order(order_clause).join(', ')
|
11
|
+
|
12
|
+
order = unless order_clause.empty?
|
13
|
+
reverse_sql_order(order_clause).join(', ')
|
14
|
+
else
|
15
|
+
klass.primary_key.map do |key|
|
16
|
+
"#{table_name}.#{key} DESC"
|
17
|
+
end.join(", ")
|
18
|
+
end
|
19
|
+
|
20
|
+
except(:order).order(Arel.sql(order))
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -3,39 +3,35 @@ module ActiveRecord
|
|
3
3
|
class UniquenessValidator
|
4
4
|
def validate_each(record, attribute, value)
|
5
5
|
finder_class = find_finder_class_for(record)
|
6
|
-
table = finder_class.
|
6
|
+
table = finder_class.arel_table
|
7
7
|
|
8
|
-
|
8
|
+
coder = record.class.serialized_attributes[attribute.to_s]
|
9
9
|
|
10
|
-
if value &&
|
11
|
-
value =
|
10
|
+
if value && coder
|
11
|
+
value = coder.dump value
|
12
12
|
end
|
13
13
|
|
14
|
-
|
14
|
+
relation = build_relation(finder_class, table, attribute, value)
|
15
|
+
# CPK
|
16
|
+
# relation = relation.and(table[finder_class.primary_key.to_sym].not_eq(record.send(:id))) if record.persisted?
|
17
|
+
if record.persisted?
|
18
|
+
not_eq_conditions = Array(finder_class.primary_key).zip(Array(record.send(:id))).map do |name, value|
|
19
|
+
table[name.to_sym].not_eq(value)
|
20
|
+
end
|
15
21
|
|
16
|
-
|
22
|
+
condition = not_eq_conditions.shift
|
23
|
+
not_eq_conditions.each do |not_eq_condition|
|
24
|
+
condition = condition.or(not_eq_conditions)
|
25
|
+
end
|
26
|
+
relation = relation.and(condition)
|
27
|
+
end
|
17
28
|
|
18
29
|
Array.wrap(options[:scope]).each do |scope_item|
|
19
30
|
scope_value = record.send(scope_item)
|
20
|
-
relation = relation.
|
21
|
-
end
|
22
|
-
|
23
|
-
if record.persisted?
|
24
|
-
# CPK
|
25
|
-
if record.composite?
|
26
|
-
predicate = nil
|
27
|
-
record.ids_hash.each do |key, value|
|
28
|
-
neq = relation.table[key].not_eq(value)
|
29
|
-
predicate = predicate ? predicate.or(neq) : neq
|
30
|
-
end
|
31
|
-
relation = relation.where(predicate)
|
32
|
-
else
|
33
|
-
# TODO : This should be in Arel
|
34
|
-
relation = relation.where("#{record.class.quoted_table_name}.#{record.class.primary_key} <> ?", record.send(:id))
|
35
|
-
end
|
31
|
+
relation = relation.and(table[scope_item].eq(scope_value))
|
36
32
|
end
|
37
33
|
|
38
|
-
if relation.exists?
|
34
|
+
if finder_class.unscoped.where(relation).exists?
|
39
35
|
record.errors.add(attribute, :taken, options.except(:case_sensitive, :scope).merge(:value => value))
|
40
36
|
end
|
41
37
|
end
|
@@ -7,7 +7,7 @@ def connection_string
|
|
7
7
|
options['u'] = SPEC['username'] if SPEC['username']
|
8
8
|
options['p'] = SPEC['password'] if SPEC['password']
|
9
9
|
options['S'] = SPEC['sock'] if SPEC['sock']
|
10
|
-
options.map { |key, value| "-#{key}
|
10
|
+
options.map { |key, value| "-#{key}#{value}" }.join(" ")
|
11
11
|
end
|
12
12
|
|
13
13
|
# Adapter config setup in locals/database_connections.rb
|
data/test/fixtures/articles.yml
CHANGED
data/test/fixtures/products.yml
CHANGED
data/test/fixtures/readings.yml
CHANGED
data/test/fixtures/suburbs.yml
CHANGED
data/test/fixtures/users.yml
CHANGED
data/test/test_associations.rb
CHANGED
@@ -6,15 +6,20 @@ class TestAssociations < ActiveSupport::TestCase
|
|
6
6
|
:departments, :memberships
|
7
7
|
|
8
8
|
def test_count
|
9
|
-
assert_equal(
|
9
|
+
assert_equal(2, Product.count(:include => :product_tariffs))
|
10
10
|
assert_equal(3, Tariff.count(:include => :product_tariffs))
|
11
11
|
|
12
12
|
expected = {Date.today => 2,
|
13
13
|
Date.today.next => 1}
|
14
|
-
|
14
|
+
|
15
15
|
assert_equal(expected, Tariff.count(:group => :start_date))
|
16
16
|
end
|
17
17
|
|
18
|
+
def test_count_distinct
|
19
|
+
product = products(:first_product)
|
20
|
+
assert_equal(2, product.product_tariffs.count(:distinct => true))
|
21
|
+
end
|
22
|
+
|
18
23
|
def test_products
|
19
24
|
assert_not_nil products(:first_product).product_tariffs
|
20
25
|
assert_equal 2, products(:first_product).product_tariffs.length
|
@@ -39,87 +44,80 @@ class TestAssociations < ActiveSupport::TestCase
|
|
39
44
|
# Its not generating the instances of associated classes from the rows
|
40
45
|
def test_find_includes_products
|
41
46
|
# Old style
|
42
|
-
|
43
|
-
assert_equal
|
44
|
-
|
45
|
-
assert_equal 3, @products.inject(0) {|sum, tariff| sum + tariff.instance_variable_get('@product_tariffs').length},
|
46
|
-
"Incorrect number of product_tariffs returned"
|
47
|
+
products = Product.find(:all, :include => :product_tariffs)
|
48
|
+
assert_equal(2, products.length)
|
49
|
+
assert_equal(3, products.inject(0) {|sum, product| sum + product.product_tariffs.length})
|
47
50
|
|
48
51
|
# New style
|
49
|
-
|
50
|
-
assert_equal
|
51
|
-
|
52
|
-
assert_equal 3, @products.inject(0) {|sum, tariff| sum + tariff.instance_variable_get('@product_tariffs').length},
|
53
|
-
"Incorrect number of product_tariffs returned"
|
52
|
+
products = Product.includes(:product_tariffs)
|
53
|
+
assert_equal(2, products.length)
|
54
|
+
assert_equal(3, products.inject(0) {|sum, product| sum + product.product_tariffs.length})
|
54
55
|
end
|
55
56
|
|
56
57
|
def test_find_includes_tariffs
|
57
58
|
# Old style
|
58
|
-
|
59
|
-
assert_equal
|
60
|
-
|
61
|
-
assert_equal 3, @tariffs.inject(0) {|sum, tariff| sum + tariff.instance_variable_get('@product_tariffs').length},
|
62
|
-
"Incorrect number of product_tariffs returnedturned"
|
59
|
+
tariffs = Tariff.find(:all, :include => :product_tariffs)
|
60
|
+
assert_equal(3, tariffs.length)
|
61
|
+
assert_equal(3, tariffs.inject(0) {|sum, tariff| sum + tariff.product_tariffs.length})
|
63
62
|
|
64
63
|
# New style
|
65
|
-
|
66
|
-
assert_equal
|
67
|
-
|
68
|
-
assert_equal 3, @tariffs.inject(0) {|sum, tariff| sum + tariff.instance_variable_get('@product_tariffs').length},
|
69
|
-
"Incorrect number of product_tariffs returnedturned"
|
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})
|
70
67
|
end
|
71
68
|
|
72
|
-
def
|
69
|
+
def test_find_includes_product_tariffs_product
|
73
70
|
# Old style
|
74
|
-
|
75
|
-
|
76
|
-
|
71
|
+
product_tariffs = ProductTariff.find(:all, :include => :product)
|
72
|
+
assert_not_nil(product_tariffs)
|
73
|
+
assert_equal(3, product_tariffs.length)
|
77
74
|
|
78
75
|
# New style
|
79
|
-
|
80
|
-
|
81
|
-
|
76
|
+
product_tariffs = ProductTariff.includes(:product)
|
77
|
+
assert_not_nil(product_tariffs)
|
78
|
+
assert_equal(3, product_tariffs.length)
|
82
79
|
end
|
83
80
|
|
84
|
-
def
|
81
|
+
def test_find_includes_product_tariffs_tariff
|
85
82
|
# Old style
|
86
|
-
|
87
|
-
assert_equal
|
88
|
-
assert_not_nil @product_tariffs.first.instance_variable_get('@tariff'), '@tariff not set'
|
83
|
+
product_tariffs = ProductTariff.find(:all, :include => :tariff)
|
84
|
+
assert_equal(3, product_tariffs.length)
|
89
85
|
|
90
86
|
# New style
|
91
|
-
|
92
|
-
assert_equal
|
93
|
-
assert_not_nil @product_tariffs.first.instance_variable_get('@tariff'), '@tariff not set'
|
87
|
+
product_tariffs = ProductTariff.includes(:tariff)
|
88
|
+
assert_equal(3, product_tariffs.length)
|
94
89
|
end
|
95
90
|
|
96
|
-
def
|
97
|
-
|
98
|
-
assert_equal(
|
91
|
+
def test_has_many_through
|
92
|
+
products = Product.find(:all, :include => :tariffs)
|
93
|
+
assert_equal(2, products.length)
|
94
|
+
|
95
|
+
tarrifs_length = products.inject(0) {|sum, product| sum + product.tariffs.length}
|
96
|
+
assert_equal(3, tarrifs_length)
|
99
97
|
end
|
100
98
|
|
101
|
-
def
|
102
|
-
|
103
|
-
assert_equal
|
104
|
-
"Incorrect number of product_tariffs returned"
|
99
|
+
def test_find_product_includes
|
100
|
+
products = Product.find(:all, :include => {:product_tariffs => :tariff})
|
101
|
+
assert_equal(2, products.length)
|
105
102
|
|
106
|
-
|
107
|
-
assert_equal
|
108
|
-
"Incorrect number of product_tariffs returned"
|
103
|
+
product_tariffs_length = products.inject(0) {|sum, product| sum + product.product_tariffs.length}
|
104
|
+
assert_equal(3, product_tariffs_length)
|
109
105
|
end
|
110
106
|
|
111
|
-
def
|
112
|
-
|
113
|
-
assert_equal
|
114
|
-
|
107
|
+
def test_find_tariffs_includes
|
108
|
+
tariffs = Tariff.find(:all, :include => {:product_tariffs => :product})
|
109
|
+
assert_equal(3, tariffs.length)
|
110
|
+
|
111
|
+
product_tariffs_length = tariffs.inject(0) {|sum, tariff| sum + tariff.product_tariffs.length}
|
112
|
+
assert_equal(3, product_tariffs_length)
|
115
113
|
end
|
116
114
|
|
117
115
|
def test_has_many_through_when_not_pre_loaded
|
118
116
|
student = Student.find(:first)
|
119
117
|
rooms = student.rooms
|
120
|
-
assert_equal
|
121
|
-
assert_equal
|
122
|
-
assert_equal
|
118
|
+
assert_equal(1, rooms.size)
|
119
|
+
assert_equal(1, rooms.first.dorm_id)
|
120
|
+
assert_equal(1, rooms.first.room_id)
|
123
121
|
end
|
124
122
|
|
125
123
|
def test_has_many_through_when_through_association_is_composite
|
@@ -130,17 +128,17 @@ class TestAssociations < ActiveSupport::TestCase
|
|
130
128
|
end
|
131
129
|
|
132
130
|
def test_associations_with_conditions
|
133
|
-
|
134
|
-
assert_equal 2,
|
131
|
+
suburb = Suburb.find([2, 1])
|
132
|
+
assert_equal 2, suburb.streets.size
|
135
133
|
|
136
|
-
|
137
|
-
assert_equal 1,
|
134
|
+
suburb = Suburb.find([2, 1])
|
135
|
+
assert_equal 1, suburb.first_streets.size
|
138
136
|
|
139
|
-
|
140
|
-
assert_equal 2,
|
137
|
+
suburb = Suburb.find([2, 1], :include => :streets)
|
138
|
+
assert_equal 2, suburb.streets.size
|
141
139
|
|
142
|
-
|
143
|
-
assert_equal 1,
|
140
|
+
suburb = Suburb.find([2, 1], :include => :first_streets)
|
141
|
+
assert_equal 1, suburb.first_streets.size
|
144
142
|
end
|
145
143
|
|
146
144
|
def test_composite_has_many_composites
|
@@ -205,12 +203,12 @@ class TestAssociations < ActiveSupport::TestCase
|
|
205
203
|
|
206
204
|
def test_has_many_through_on_custom_finder_when_through_association_is_composite_finder_when_through_association_is_not_composite
|
207
205
|
user = User.find(:first)
|
208
|
-
assert_equal
|
206
|
+
assert_equal(1, user.find_custom_articles.size)
|
209
207
|
end
|
210
208
|
|
211
209
|
def test_has_many_through_on_custom_finder_when_through_association_is_composite
|
212
210
|
room = Room.find(:first)
|
213
|
-
assert_equal
|
211
|
+
assert_equal(0, room.find_custom_room_attributes.size)
|
214
212
|
end
|
215
213
|
|
216
214
|
def test_has_many_with_primary_key_with_associations
|
data/test/test_attributes.rb
CHANGED
@@ -28,8 +28,8 @@ class TestAttributes < ActiveSupport::TestCase
|
|
28
28
|
|
29
29
|
def test_brackets_primary_key
|
30
30
|
testing_with do
|
31
|
-
assert_equal
|
32
|
-
assert_equal
|
31
|
+
assert_equal(@first.id, @first[@primary_keys])
|
32
|
+
assert_equal(@first.id, @first[@first.class.primary_key])
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
@@ -43,33 +43,28 @@ class TestAttributes < ActiveSupport::TestCase
|
|
43
43
|
end
|
44
44
|
|
45
45
|
def test_brackets_foreign_key_assignment
|
46
|
-
|
47
|
-
|
48
|
-
|
46
|
+
flat = tariffs(:flat)
|
47
|
+
second_free = tariffs(:free)
|
48
|
+
second_free_fk = [:tariff_id, :tariff_start_date]
|
49
49
|
|
50
|
-
|
51
|
-
compare_indexes(
|
52
|
-
assert_equal
|
50
|
+
second_free[key = second_free_fk] = flat.id
|
51
|
+
compare_indexes(flat, flat.class.primary_key, second_free, second_free_fk)
|
52
|
+
assert_equal flat.id, second_free[key]
|
53
53
|
|
54
|
-
|
55
|
-
assert_equal
|
56
|
-
compare_indexes(
|
54
|
+
second_free[key = second_free_fk.to_composite_keys] = flat.id
|
55
|
+
assert_equal flat.id, second_free[key]
|
56
|
+
compare_indexes(flat, flat.class.primary_key, second_free, second_free_fk)
|
57
57
|
|
58
|
-
|
59
|
-
assert_equal
|
60
|
-
compare_indexes(
|
58
|
+
second_free[key = second_free_fk.to_composite_keys.to_s] = flat.id
|
59
|
+
assert_equal flat.id, second_free[key]
|
60
|
+
compare_indexes(flat, flat.class.primary_key, second_free, second_free_fk)
|
61
61
|
end
|
62
62
|
|
63
63
|
private
|
64
64
|
|
65
|
-
def compare_indexes(
|
66
|
-
obj1, obj2 = eval "[#{obj_name1}, #{obj_name2}]"
|
65
|
+
def compare_indexes(obj1, indexes1, obj2, indexes2)
|
67
66
|
indexes1.length.times do |key_index|
|
68
|
-
assert_equal
|
69
|
-
obj2[indexes2[key_index].to_s],
|
70
|
-
"#{obj_name1}[#{indexes1[key_index]}]=#{obj1[indexes1[key_index].to_s].inspect} != " +
|
71
|
-
"#{obj_name2}[#{indexes2[key_index]}]=#{obj2[indexes2[key_index].to_s].inspect}; " +
|
72
|
-
"#{obj_name2} = #{obj2.inspect}"
|
67
|
+
assert_equal(obj1[indexes1[key_index].to_s], obj2[indexes2[key_index].to_s])
|
73
68
|
end
|
74
69
|
end
|
75
70
|
end
|
data/test/test_create.rb
CHANGED
@@ -13,7 +13,7 @@ class TestCreate < ActiveSupport::TestCase
|
|
13
13
|
:class => ReferenceCode,
|
14
14
|
:primary_keys => [:reference_type_id, :reference_code],
|
15
15
|
:create => {:reference_type_id => 1, :reference_code => 20, :code_label => 'NEW_CODE', :abbreviation => 'New Code'}
|
16
|
-
}
|
16
|
+
}
|
17
17
|
}
|
18
18
|
|
19
19
|
def setup
|
@@ -38,7 +38,7 @@ class TestCreate < ActiveSupport::TestCase
|
|
38
38
|
begin
|
39
39
|
@obj = @klass.create(@klass_info[:create].block(@klass.primary_key))
|
40
40
|
@successful = !composite?
|
41
|
-
rescue
|
41
|
+
rescue ActiveRecord::CompositeKeyError
|
42
42
|
@successful = false
|
43
43
|
rescue
|
44
44
|
flunk "Incorrect exception raised: #{$!}, #{$!.class}"
|
@@ -87,4 +87,4 @@ class TestCreate < ActiveSupport::TestCase
|
|
87
87
|
assert_equal(25, suburb.suburb_id)
|
88
88
|
assert_equal("My Suburb", suburb.name)
|
89
89
|
end
|
90
|
-
end
|
90
|
+
end
|
data/test/test_delete.rb
CHANGED
@@ -19,55 +19,57 @@ class TestDelete < ActiveSupport::TestCase
|
|
19
19
|
self.class.classes = CLASSES
|
20
20
|
end
|
21
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.find(:all)[0..1]
|
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
|
56
|
-
|
57
|
-
|
58
|
-
department.
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
department.employees.delete
|
67
|
-
|
68
|
-
department.
|
69
|
-
assert_equal 1, department.employees.size, "After delete
|
70
|
-
|
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.find(:all)[0..1]
|
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
|
71
73
|
|
72
74
|
def test_destroy_has_one
|
73
75
|
# In this case the association is a has_one with
|
@@ -87,39 +89,40 @@ class TestDelete < ActiveSupport::TestCase
|
|
87
89
|
end
|
88
90
|
end
|
89
91
|
|
90
|
-
def test_destroy_has_many_delete_all
|
91
|
-
# In this case the association is a has_many composite key with
|
92
|
-
# dependent set to :delete_all
|
93
|
-
product = Product.find(1)
|
94
|
-
assert_equal(2, product.product_tariffs.length)
|
95
|
-
|
96
|
-
# Get product_tariff length
|
97
|
-
product_tariff_size = ProductTariff.count
|
98
|
-
|
99
|
-
# Delete product - should delete 2 product tariffs
|
100
|
-
product.destroy
|
101
|
-
|
102
|
-
# Verify product_tariff are deleted
|
103
|
-
assert_equal(product_tariff_size - 2, ProductTariff.count)
|
104
|
-
end
|
105
|
-
|
106
|
-
def test_delete_cpk_association
|
107
|
-
product = Product.find(1)
|
108
|
-
assert_equal(2, product.product_tariffs.length)
|
109
|
-
|
110
|
-
product_tariff = product.product_tariffs.first
|
111
|
-
product.product_tariffs.delete(product_tariff)
|
112
|
-
|
113
|
-
product.reload
|
114
|
-
assert_equal(1, product.product_tariffs.length)
|
115
|
-
end
|
116
|
-
|
117
|
-
def test_delete_records_for_has_many_association_with_composite_primary_key
|
118
|
-
reference_type = ReferenceType.find(1)
|
119
|
-
codes_to_delete = reference_type.reference_codes[0..1]
|
120
|
-
assert_equal
|
121
|
-
|
122
|
-
reference_type.
|
123
|
-
|
124
|
-
|
92
|
+
# def test_destroy_has_many_delete_all
|
93
|
+
# # In this case the association is a has_many composite key with
|
94
|
+
# # dependent set to :delete_all
|
95
|
+
# product = Product.find(1)
|
96
|
+
# assert_equal(2, product.product_tariffs.length)
|
97
|
+
#
|
98
|
+
# # Get product_tariff length
|
99
|
+
# product_tariff_size = ProductTariff.count
|
100
|
+
#
|
101
|
+
# # Delete product - should delete 2 product tariffs
|
102
|
+
# product.destroy
|
103
|
+
#
|
104
|
+
# # Verify product_tariff are deleted
|
105
|
+
# assert_equal(product_tariff_size - 2, ProductTariff.count)
|
106
|
+
# end
|
107
|
+
#
|
108
|
+
# def test_delete_cpk_association
|
109
|
+
# product = Product.find(1)
|
110
|
+
# assert_equal(2, product.product_tariffs.length)
|
111
|
+
#
|
112
|
+
# product_tariff = product.product_tariffs.first
|
113
|
+
# product.product_tariffs.delete(product_tariff)
|
114
|
+
#
|
115
|
+
# product.reload
|
116
|
+
# assert_equal(1, product.product_tariffs.length)
|
117
|
+
# end
|
118
|
+
#
|
119
|
+
# def test_delete_records_for_has_many_association_with_composite_primary_key
|
120
|
+
# reference_type = ReferenceType.find(1)
|
121
|
+
# codes_to_delete = reference_type.reference_codes[0..1]
|
122
|
+
# assert_equal(3, reference_type.reference_codes.size, "Before deleting records reference_code count should be 3.")
|
123
|
+
#
|
124
|
+
# reference_type.reference_codes.delete(codes_to_delete)
|
125
|
+
# reference_type.reload
|
126
|
+
# assert_equal(1, reference_type.reference_codes.size, "After deleting 2 records and a reload from DB reference_code count should be 1.")
|
127
|
+
# end
|
125
128
|
end
|