composite_primary_keys 11.0.0.rc1 → 11.0.0.rc2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/History.rdoc +763 -754
- data/Rakefile +37 -37
- data/lib/composite_primary_keys.rb +122 -121
- data/lib/composite_primary_keys/arel/sqlserver.rb +37 -37
- data/lib/composite_primary_keys/arel/to_sql.rb +32 -32
- data/lib/composite_primary_keys/associations/association.rb +23 -23
- data/lib/composite_primary_keys/associations/association_scope.rb +68 -68
- data/lib/composite_primary_keys/associations/collection_association.rb +31 -31
- data/lib/composite_primary_keys/associations/has_many_association.rb +34 -34
- data/lib/composite_primary_keys/associations/join_dependency.rb +102 -102
- data/lib/composite_primary_keys/associations/preloader/association.rb +117 -117
- data/lib/composite_primary_keys/attribute_methods.rb +9 -9
- data/lib/composite_primary_keys/attribute_methods/read.rb +27 -27
- data/lib/composite_primary_keys/attribute_methods/write.rb +44 -44
- data/lib/composite_primary_keys/autosave_association.rb +32 -32
- data/lib/composite_primary_keys/base.rb +130 -130
- data/lib/composite_primary_keys/composite_arrays.rb +51 -51
- data/lib/composite_primary_keys/composite_predicates.rb +69 -69
- data/lib/composite_primary_keys/composite_relation.rb +29 -48
- data/lib/composite_primary_keys/connection_adapters/abstract_mysql_adapter.rb +29 -22
- data/lib/composite_primary_keys/connection_adapters/postgresql/database_statements.rb +26 -26
- data/lib/composite_primary_keys/connection_adapters/sqlite3_adapter.rb +22 -22
- data/lib/composite_primary_keys/core.rb +54 -54
- data/lib/composite_primary_keys/counter_cache.rb +28 -0
- data/lib/composite_primary_keys/fixtures.rb +26 -26
- data/lib/composite_primary_keys/locking/optimistic.rb +56 -56
- data/lib/composite_primary_keys/persistence.rb +112 -93
- data/lib/composite_primary_keys/reflection.rb +20 -20
- data/lib/composite_primary_keys/relation.rb +88 -88
- data/lib/composite_primary_keys/relation/calculations.rb +79 -79
- data/lib/composite_primary_keys/relation/finder_methods.rb +229 -229
- data/lib/composite_primary_keys/relation/predicate_builder/association_query_value.rb +20 -20
- data/lib/composite_primary_keys/relation/query_methods.rb +42 -42
- data/lib/composite_primary_keys/relation/where_clause.rb +24 -24
- data/lib/composite_primary_keys/sanitization.rb +42 -42
- data/lib/composite_primary_keys/validations/uniqueness.rb +32 -32
- data/lib/composite_primary_keys/version.rb +8 -8
- data/scripts/console.rb +0 -0
- data/tasks/databases/mysql.rake +40 -40
- data/tasks/databases/oracle.rake +41 -41
- data/tasks/databases/postgresql.rake +38 -38
- data/tasks/databases/sqlite.rake +25 -25
- data/tasks/databases/sqlserver.rake +43 -43
- data/test/abstract_unit.rb +120 -120
- data/test/connections/connection_spec.rb +27 -27
- data/test/connections/databases.ci.yml +16 -16
- data/test/connections/databases.example.yml +40 -40
- data/test/connections/databases.yml +10 -0
- data/test/fixtures/article.rb +6 -6
- data/test/fixtures/comment.rb +7 -7
- data/test/fixtures/db_definitions/db2-create-tables.sql +126 -126
- data/test/fixtures/db_definitions/db2-drop-tables.sql +18 -18
- data/test/fixtures/db_definitions/mysql.sql +226 -226
- data/test/fixtures/db_definitions/oracle.drop.sql +49 -49
- data/test/fixtures/db_definitions/oracle.sql +246 -246
- data/test/fixtures/db_definitions/postgresql.sql +229 -229
- data/test/fixtures/db_definitions/sqlite.sql +214 -214
- data/test/fixtures/db_definitions/sqlserver.sql +211 -211
- data/test/fixtures/dorm.rb +2 -2
- data/test/fixtures/employee.rb +10 -10
- data/test/fixtures/employees.yml +18 -18
- data/test/fixtures/membership.rb +6 -6
- data/test/fixtures/membership_statuses.yml +16 -16
- data/test/fixtures/memberships.yml +10 -10
- data/test/fixtures/pk_called_id.rb +5 -5
- data/test/fixtures/pk_called_ids.yml +11 -11
- data/test/fixtures/product_tariffs.yml +14 -14
- data/test/fixtures/reference_code.rb +7 -7
- data/test/fixtures/reference_type.rb +12 -12
- data/test/fixtures/restaurants_suburb.rb +2 -2
- data/test/fixtures/salary.rb +5 -5
- data/test/fixtures/suburb.rb +5 -5
- data/test/fixtures/tariff.rb +5 -5
- data/test/fixtures/tariffs.yml +14 -14
- data/test/fixtures/topic.rb +5 -5
- data/test/fixtures/topic_source.rb +6 -6
- data/test/fixtures/topic_sources.yml +3 -3
- data/test/fixtures/topics.yml +8 -8
- data/test/fixtures/user.rb +10 -10
- data/test/fixtures/users.yml +10 -10
- data/test/test_associations.rb +345 -345
- data/test/test_attribute_methods.rb +63 -63
- data/test/test_attributes.rb +60 -60
- data/test/test_calculations.rb +42 -42
- data/test/test_callbacks.rb +99 -99
- data/test/test_counter_cache.rb +1 -1
- data/test/test_create.rb +164 -164
- data/test/test_delete.rb +167 -167
- data/test/test_dumpable.rb +15 -15
- data/test/test_enum.rb +21 -21
- data/test/test_equal.rb +26 -26
- data/test/test_find.rb +137 -137
- data/test/test_habtm.rb +141 -141
- data/test/test_ids.rb +113 -113
- data/test/test_nested_attributes.rb +124 -124
- data/test/test_optimistic.rb +18 -18
- data/test/test_polymorphic.rb +27 -27
- data/test/test_predicates.rb +59 -59
- data/test/test_preload.rb +94 -94
- data/test/test_santiago.rb +23 -23
- data/test/test_suite.rb +34 -34
- data/test/test_touch.rb +23 -23
- data/test/test_tutorial_example.rb +25 -25
- data/test/test_update.rb +76 -76
- metadata +29 -28
@@ -1,63 +1,63 @@
|
|
1
|
-
require File.expand_path('../abstract_unit', __FILE__)
|
2
|
-
|
3
|
-
class TestAttributeMethods < ActiveSupport::TestCase
|
4
|
-
fixtures :reference_types, :reference_codes
|
5
|
-
|
6
|
-
def test_read_attribute_with_single_key
|
7
|
-
rt = ReferenceType.find(1)
|
8
|
-
assert_equal(1, rt.reference_type_id)
|
9
|
-
assert_equal('NAME_PREFIX', rt.type_label)
|
10
|
-
assert_equal('Name Prefix', rt.abbreviation)
|
11
|
-
end
|
12
|
-
|
13
|
-
def test_read_attribute_with_composite_keys
|
14
|
-
ref_code = ReferenceCode.find([1, 1])
|
15
|
-
assert_equal(1, ref_code.id.first)
|
16
|
-
assert_equal(1, ref_code.id.last)
|
17
|
-
assert_equal('Mr', ref_code.abbreviation)
|
18
|
-
end
|
19
|
-
|
20
|
-
# to_key returns array even for single key
|
21
|
-
def test_to_key_with_single_key
|
22
|
-
rt = ReferenceType.find(1)
|
23
|
-
assert_equal([1], rt.to_key)
|
24
|
-
end
|
25
|
-
|
26
|
-
def test_to_key_with_composite_keys
|
27
|
-
ref_code = ReferenceCode.find([1, 1])
|
28
|
-
assert_equal(1, ref_code.to_key.first)
|
29
|
-
assert_equal(1, ref_code.to_key.last)
|
30
|
-
end
|
31
|
-
|
32
|
-
def test_to_key_with_single_key_unsaved
|
33
|
-
rt = ReferenceType.new
|
34
|
-
assert_nil(rt.to_key)
|
35
|
-
end
|
36
|
-
|
37
|
-
def test_to_key_with_composite_keys_unsaved
|
38
|
-
ref_code = ReferenceCode.new
|
39
|
-
assert_nil(ref_code.to_key)
|
40
|
-
end
|
41
|
-
|
42
|
-
def test_to_key_with_single_key_destroyed
|
43
|
-
rt = ReferenceType.find(1)
|
44
|
-
rt.destroy
|
45
|
-
assert_equal([1], rt.to_key)
|
46
|
-
end
|
47
|
-
|
48
|
-
def test_to_key_with_composite_key_destroyed
|
49
|
-
ref_code = ReferenceCode.find([1, 1])
|
50
|
-
ref_code.destroy
|
51
|
-
assert_equal([1,1], ref_code.to_key)
|
52
|
-
end
|
53
|
-
|
54
|
-
def test_id_was
|
55
|
-
rt = ReferenceType.find(1)
|
56
|
-
rt.id = 2
|
57
|
-
assert_equal 1, rt.id_was
|
58
|
-
|
59
|
-
ref_code = ReferenceCode.find([1, 1])
|
60
|
-
ref_code.id = [1,2]
|
61
|
-
assert_equal [1,1], ref_code.id_was
|
62
|
-
end
|
63
|
-
end
|
1
|
+
require File.expand_path('../abstract_unit', __FILE__)
|
2
|
+
|
3
|
+
class TestAttributeMethods < ActiveSupport::TestCase
|
4
|
+
fixtures :reference_types, :reference_codes
|
5
|
+
|
6
|
+
def test_read_attribute_with_single_key
|
7
|
+
rt = ReferenceType.find(1)
|
8
|
+
assert_equal(1, rt.reference_type_id)
|
9
|
+
assert_equal('NAME_PREFIX', rt.type_label)
|
10
|
+
assert_equal('Name Prefix', rt.abbreviation)
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_read_attribute_with_composite_keys
|
14
|
+
ref_code = ReferenceCode.find([1, 1])
|
15
|
+
assert_equal(1, ref_code.id.first)
|
16
|
+
assert_equal(1, ref_code.id.last)
|
17
|
+
assert_equal('Mr', ref_code.abbreviation)
|
18
|
+
end
|
19
|
+
|
20
|
+
# to_key returns array even for single key
|
21
|
+
def test_to_key_with_single_key
|
22
|
+
rt = ReferenceType.find(1)
|
23
|
+
assert_equal([1], rt.to_key)
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_to_key_with_composite_keys
|
27
|
+
ref_code = ReferenceCode.find([1, 1])
|
28
|
+
assert_equal(1, ref_code.to_key.first)
|
29
|
+
assert_equal(1, ref_code.to_key.last)
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_to_key_with_single_key_unsaved
|
33
|
+
rt = ReferenceType.new
|
34
|
+
assert_nil(rt.to_key)
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_to_key_with_composite_keys_unsaved
|
38
|
+
ref_code = ReferenceCode.new
|
39
|
+
assert_nil(ref_code.to_key)
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_to_key_with_single_key_destroyed
|
43
|
+
rt = ReferenceType.find(1)
|
44
|
+
rt.destroy
|
45
|
+
assert_equal([1], rt.to_key)
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_to_key_with_composite_key_destroyed
|
49
|
+
ref_code = ReferenceCode.find([1, 1])
|
50
|
+
ref_code.destroy
|
51
|
+
assert_equal([1,1], ref_code.to_key)
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_id_was
|
55
|
+
rt = ReferenceType.find(1)
|
56
|
+
rt.id = 2
|
57
|
+
assert_equal 1, rt.id_was
|
58
|
+
|
59
|
+
ref_code = ReferenceCode.find([1, 1])
|
60
|
+
ref_code.id = [1,2]
|
61
|
+
assert_equal [1,1], ref_code.id_was
|
62
|
+
end
|
63
|
+
end
|
data/test/test_attributes.rb
CHANGED
@@ -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
|
data/test/test_calculations.rb
CHANGED
@@ -1,42 +1,42 @@
|
|
1
|
-
require File.expand_path('../abstract_unit', __FILE__)
|
2
|
-
|
3
|
-
class TestCalculations < ActiveSupport::TestCase
|
4
|
-
fixtures :articles, :products, :tariffs, :product_tariffs, :suburbs, :streets, :restaurants,
|
5
|
-
:dorms, :rooms, :room_attributes, :room_attribute_assignments, :students, :room_assignments, :users, :readings,
|
6
|
-
:departments, :employees, :memberships, :membership_statuses
|
7
|
-
|
8
|
-
def test_count
|
9
|
-
assert_equal(3, Product.includes(:product_tariffs).count)
|
10
|
-
assert_equal(3, Tariff.includes(:product_tariffs).count)
|
11
|
-
|
12
|
-
expected = {Date.today => 2,
|
13
|
-
Date.today.next => 1}
|
14
|
-
|
15
|
-
assert_equal(expected, Tariff.group(:start_date).count)
|
16
|
-
end
|
17
|
-
|
18
|
-
def test_count_distinct
|
19
|
-
product = products(:first_product)
|
20
|
-
assert_equal(1, product.product_tariffs.select('tariff_start_date').distinct.count)
|
21
|
-
end
|
22
|
-
|
23
|
-
def test_count_not_distinct
|
24
|
-
product = products(:first_product)
|
25
|
-
assert_equal(2, product.product_tariffs.select('tariff_start_date').count)
|
26
|
-
end
|
27
|
-
|
28
|
-
def test_count_includes
|
29
|
-
count = Dorm.where("rooms.room_id = ?", 2).includes(:rooms).references(:rooms).count
|
30
|
-
assert_equal(1, count)
|
31
|
-
end
|
32
|
-
|
33
|
-
def test_count_includes_dup_columns
|
34
|
-
count = Tariff.includes(:product_tariffs).references(:product_tariffs).where("product_tariffs.tariff_id = ?", 2).count
|
35
|
-
assert_equal(1, count)
|
36
|
-
end
|
37
|
-
|
38
|
-
def test_average
|
39
|
-
average = Tariff.average(:amount)
|
40
|
-
assert_equal(50, average)
|
41
|
-
end
|
42
|
-
end
|
1
|
+
require File.expand_path('../abstract_unit', __FILE__)
|
2
|
+
|
3
|
+
class TestCalculations < ActiveSupport::TestCase
|
4
|
+
fixtures :articles, :products, :tariffs, :product_tariffs, :suburbs, :streets, :restaurants,
|
5
|
+
:dorms, :rooms, :room_attributes, :room_attribute_assignments, :students, :room_assignments, :users, :readings,
|
6
|
+
:departments, :employees, :memberships, :membership_statuses
|
7
|
+
|
8
|
+
def test_count
|
9
|
+
assert_equal(3, Product.includes(:product_tariffs).count)
|
10
|
+
assert_equal(3, Tariff.includes(:product_tariffs).count)
|
11
|
+
|
12
|
+
expected = {Date.today => 2,
|
13
|
+
Date.today.next => 1}
|
14
|
+
|
15
|
+
assert_equal(expected, Tariff.group(:start_date).count)
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_count_distinct
|
19
|
+
product = products(:first_product)
|
20
|
+
assert_equal(1, product.product_tariffs.select('tariff_start_date').distinct.count)
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_count_not_distinct
|
24
|
+
product = products(:first_product)
|
25
|
+
assert_equal(2, product.product_tariffs.select('tariff_start_date').count)
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_count_includes
|
29
|
+
count = Dorm.where("rooms.room_id = ?", 2).includes(:rooms).references(:rooms).count
|
30
|
+
assert_equal(1, count)
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_count_includes_dup_columns
|
34
|
+
count = Tariff.includes(:product_tariffs).references(:product_tariffs).where("product_tariffs.tariff_id = ?", 2).count
|
35
|
+
assert_equal(1, count)
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_average
|
39
|
+
average = Tariff.average(:amount)
|
40
|
+
assert_equal(50, average)
|
41
|
+
end
|
42
|
+
end
|
data/test/test_callbacks.rb
CHANGED
@@ -1,99 +1,99 @@
|
|
1
|
-
require File.expand_path('../abstract_unit', __FILE__)
|
2
|
-
|
3
|
-
class TestCallbacks < ActiveSupport::TestCase
|
4
|
-
fixtures :suburbs
|
5
|
-
|
6
|
-
def setup
|
7
|
-
@@callbacks = OpenStruct.new
|
8
|
-
|
9
|
-
Suburb.class_eval do
|
10
|
-
before_create do
|
11
|
-
@@callbacks.before_create = true
|
12
|
-
end
|
13
|
-
|
14
|
-
after_create do
|
15
|
-
@@callbacks.after_create = true
|
16
|
-
end
|
17
|
-
|
18
|
-
around_create do |suburb, block|
|
19
|
-
@@callbacks.around_create = true
|
20
|
-
block.call
|
21
|
-
end
|
22
|
-
|
23
|
-
before_save do
|
24
|
-
@@callbacks.before_save = true
|
25
|
-
end
|
26
|
-
|
27
|
-
after_save do
|
28
|
-
@@callbacks.after_save = true
|
29
|
-
end
|
30
|
-
|
31
|
-
around_save do |suburb, block|
|
32
|
-
@@callbacks.around_save = true
|
33
|
-
block.call
|
34
|
-
end
|
35
|
-
|
36
|
-
before_update do
|
37
|
-
@@callbacks.before_update = true
|
38
|
-
end
|
39
|
-
|
40
|
-
after_update do
|
41
|
-
@@callbacks.after_update = true
|
42
|
-
end
|
43
|
-
|
44
|
-
around_update do |suburb, block|
|
45
|
-
@@callbacks.around_update = true
|
46
|
-
block.call
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
def teardown
|
52
|
-
Suburb.reset_callbacks(:create)
|
53
|
-
Suburb.reset_callbacks(:save)
|
54
|
-
Suburb.reset_callbacks(:update)
|
55
|
-
end
|
56
|
-
|
57
|
-
def test_create
|
58
|
-
refute(@@callbacks.before_save)
|
59
|
-
refute(@@callbacks.after_save)
|
60
|
-
refute(@@callbacks.around_save)
|
61
|
-
|
62
|
-
refute(@@callbacks.before_create)
|
63
|
-
refute(@@callbacks.after_create)
|
64
|
-
refute(@@callbacks.around_create)
|
65
|
-
|
66
|
-
suburb = Suburb.new(:city_id => 3, :suburb_id => 3, :name => 'created')
|
67
|
-
suburb.save!
|
68
|
-
|
69
|
-
assert(@@callbacks.before_save)
|
70
|
-
assert(@@callbacks.after_save)
|
71
|
-
assert(@@callbacks.around_save)
|
72
|
-
|
73
|
-
assert(@@callbacks.before_create)
|
74
|
-
assert(@@callbacks.after_create)
|
75
|
-
assert(@@callbacks.around_create)
|
76
|
-
end
|
77
|
-
|
78
|
-
def test_update
|
79
|
-
refute(@@callbacks.before_save)
|
80
|
-
refute(@@callbacks.after_save)
|
81
|
-
refute(@@callbacks.around_save)
|
82
|
-
|
83
|
-
refute(@@callbacks.before_create)
|
84
|
-
refute(@@callbacks.after_create)
|
85
|
-
refute(@@callbacks.around_create)
|
86
|
-
|
87
|
-
suburb = suburbs(:first)
|
88
|
-
suburb.name = 'Updated'
|
89
|
-
suburb.save
|
90
|
-
|
91
|
-
assert(@@callbacks.before_update)
|
92
|
-
assert(@@callbacks.after_update)
|
93
|
-
assert(@@callbacks.around_update)
|
94
|
-
|
95
|
-
assert(@@callbacks.before_save)
|
96
|
-
assert(@@callbacks.after_save)
|
97
|
-
assert(@@callbacks.around_save)
|
98
|
-
end
|
99
|
-
end
|
1
|
+
require File.expand_path('../abstract_unit', __FILE__)
|
2
|
+
|
3
|
+
class TestCallbacks < ActiveSupport::TestCase
|
4
|
+
fixtures :suburbs
|
5
|
+
|
6
|
+
def setup
|
7
|
+
@@callbacks = OpenStruct.new
|
8
|
+
|
9
|
+
Suburb.class_eval do
|
10
|
+
before_create do
|
11
|
+
@@callbacks.before_create = true
|
12
|
+
end
|
13
|
+
|
14
|
+
after_create do
|
15
|
+
@@callbacks.after_create = true
|
16
|
+
end
|
17
|
+
|
18
|
+
around_create do |suburb, block|
|
19
|
+
@@callbacks.around_create = true
|
20
|
+
block.call
|
21
|
+
end
|
22
|
+
|
23
|
+
before_save do
|
24
|
+
@@callbacks.before_save = true
|
25
|
+
end
|
26
|
+
|
27
|
+
after_save do
|
28
|
+
@@callbacks.after_save = true
|
29
|
+
end
|
30
|
+
|
31
|
+
around_save do |suburb, block|
|
32
|
+
@@callbacks.around_save = true
|
33
|
+
block.call
|
34
|
+
end
|
35
|
+
|
36
|
+
before_update do
|
37
|
+
@@callbacks.before_update = true
|
38
|
+
end
|
39
|
+
|
40
|
+
after_update do
|
41
|
+
@@callbacks.after_update = true
|
42
|
+
end
|
43
|
+
|
44
|
+
around_update do |suburb, block|
|
45
|
+
@@callbacks.around_update = true
|
46
|
+
block.call
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def teardown
|
52
|
+
Suburb.reset_callbacks(:create)
|
53
|
+
Suburb.reset_callbacks(:save)
|
54
|
+
Suburb.reset_callbacks(:update)
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_create
|
58
|
+
refute(@@callbacks.before_save)
|
59
|
+
refute(@@callbacks.after_save)
|
60
|
+
refute(@@callbacks.around_save)
|
61
|
+
|
62
|
+
refute(@@callbacks.before_create)
|
63
|
+
refute(@@callbacks.after_create)
|
64
|
+
refute(@@callbacks.around_create)
|
65
|
+
|
66
|
+
suburb = Suburb.new(:city_id => 3, :suburb_id => 3, :name => 'created')
|
67
|
+
suburb.save!
|
68
|
+
|
69
|
+
assert(@@callbacks.before_save)
|
70
|
+
assert(@@callbacks.after_save)
|
71
|
+
assert(@@callbacks.around_save)
|
72
|
+
|
73
|
+
assert(@@callbacks.before_create)
|
74
|
+
assert(@@callbacks.after_create)
|
75
|
+
assert(@@callbacks.around_create)
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_update
|
79
|
+
refute(@@callbacks.before_save)
|
80
|
+
refute(@@callbacks.after_save)
|
81
|
+
refute(@@callbacks.around_save)
|
82
|
+
|
83
|
+
refute(@@callbacks.before_create)
|
84
|
+
refute(@@callbacks.after_create)
|
85
|
+
refute(@@callbacks.around_create)
|
86
|
+
|
87
|
+
suburb = suburbs(:first)
|
88
|
+
suburb.name = 'Updated'
|
89
|
+
suburb.save
|
90
|
+
|
91
|
+
assert(@@callbacks.before_update)
|
92
|
+
assert(@@callbacks.after_update)
|
93
|
+
assert(@@callbacks.around_update)
|
94
|
+
|
95
|
+
assert(@@callbacks.before_save)
|
96
|
+
assert(@@callbacks.after_save)
|
97
|
+
assert(@@callbacks.around_save)
|
98
|
+
end
|
99
|
+
end
|