composite_primary_keys 3.0.9 → 3.1.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.
- data/History.txt +6 -0
- data/Rakefile +10 -1
- data/lib/composite_primary_keys.rb +75 -75
- data/lib/composite_primary_keys/base.rb +190 -194
- data/lib/composite_primary_keys/composite_arrays.rb +23 -23
- data/lib/composite_primary_keys/finder_methods.rb +0 -11
- data/lib/composite_primary_keys/reflection.rb +38 -38
- data/lib/composite_primary_keys/version.rb +2 -2
- data/test/abstract_unit.rb +3 -2
- data/test/connections/connection_spec.rb +1 -2
- data/test/connections/databases.example.yml +14 -12
- data/test/connections/databases.yml +14 -14
- data/test/connections/native_ibm_db/connection.rb +0 -3
- data/test/connections/native_mysql/connection.rb +3 -9
- data/test/connections/native_oracle/connection.rb +4 -10
- data/test/connections/native_oracle_enhanced/connection.rb +4 -11
- data/test/connections/native_postgresql/connection.rb +1 -3
- data/test/connections/native_sqlite/connection.rb +2 -4
- data/test/debug.log +589 -589
- data/test/fixtures/article.rb +5 -5
- data/test/fixtures/articles.yml +5 -5
- data/test/fixtures/capitol.rb +3 -0
- data/test/fixtures/capitols.yml +16 -0
- data/test/fixtures/db_definitions/mysql.sql +5 -0
- data/test/fixtures/db_definitions/postgresql.sql +5 -0
- data/test/fixtures/product.rb +7 -7
- data/test/fixtures/product_tariff.rb +5 -5
- data/test/fixtures/product_tariffs.yml +12 -12
- data/test/fixtures/products.yml +5 -5
- data/test/fixtures/reading.rb +4 -4
- data/test/fixtures/readings.yml +9 -9
- data/test/fixtures/reference_code.rb +7 -7
- data/test/fixtures/reference_codes.yml +29 -29
- data/test/fixtures/reference_type.rb +7 -7
- data/test/fixtures/reference_types.yml +9 -9
- data/test/fixtures/suburb.rb +5 -5
- data/test/fixtures/suburbs.yml +8 -8
- data/test/fixtures/tariff.rb +6 -6
- data/test/fixtures/tariffs.yml +12 -12
- data/test/fixtures/user.rb +10 -10
- data/test/fixtures/users.yml +5 -5
- data/test/hash_tricks.rb +34 -34
- data/test/test_associations.rb +180 -180
- data/test/test_attribute_methods.rb +0 -2
- data/test/test_attributes.rb +0 -5
- data/test/test_clone.rb +31 -33
- data/test/test_composite_arrays.rb +0 -2
- data/test/test_create.rb +0 -4
- data/test/test_delete.rb +92 -96
- data/test/test_equal.rb +21 -0
- data/test/test_exists.rb +0 -2
- data/test/test_find.rb +79 -76
- data/test/test_ids.rb +81 -83
- data/test/test_miscellaneous.rb +36 -38
- data/test/test_pagination.rb +35 -37
- data/test/test_polymorphic.rb +0 -6
- data/test/test_santiago.rb +23 -27
- data/test/test_suite.rb +1 -0
- data/test/test_tutorial_example.rb +0 -4
- data/test/test_update.rb +37 -39
- data/test/test_validations.rb +0 -1
- metadata +8 -4
data/test/test_create.rb
CHANGED
data/test/test_delete.rb
CHANGED
@@ -1,96 +1,92 @@
|
|
1
|
-
require 'abstract_unit'
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
:
|
12
|
-
:class =>
|
13
|
-
:primary_keys => :reference_type_id,
|
14
|
-
},
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
def
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
department
|
58
|
-
assert_equal
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
product
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
reference_type
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
reference_type.reload
|
94
|
-
assert_equal 1, reference_type.reference_codes.size, "After deleting 2 records and a reload from DB reference_code count should be 1."
|
95
|
-
end
|
96
|
-
end
|
1
|
+
require 'abstract_unit'
|
2
|
+
|
3
|
+
class TestDelete < ActiveSupport::TestCase
|
4
|
+
fixtures :reference_types, :reference_codes, :departments, :employees
|
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_destroy_one
|
22
|
+
testing_with do
|
23
|
+
assert @first.destroy
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_destroy_one_alone_via_class
|
28
|
+
testing_with do
|
29
|
+
assert @klass.destroy(@first.id)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_delete_one_alone
|
34
|
+
testing_with do
|
35
|
+
assert @klass.delete(@first.id)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_delete_many
|
40
|
+
testing_with do
|
41
|
+
to_delete = @klass.find(:all)[0..1]
|
42
|
+
assert_equal 2, to_delete.length
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_delete_all
|
47
|
+
testing_with do
|
48
|
+
@klass.delete_all
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_clear_association
|
53
|
+
department = Department.find(1,1)
|
54
|
+
assert_equal 2, department.employees.size, "Before clear employee count should be 2."
|
55
|
+
department.employees.clear
|
56
|
+
assert_equal 0, department.employees.size, "After clear employee count should be 0."
|
57
|
+
department.reload
|
58
|
+
assert_equal 0, department.employees.size, "After clear and a reload from DB employee count should be 0."
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_delete_association
|
62
|
+
department = Department.find(1,1)
|
63
|
+
assert_equal 2, department.employees.size , "Before delete employee count should be 2."
|
64
|
+
first_employee = department.employees[0]
|
65
|
+
department.employees.delete(first_employee)
|
66
|
+
assert_equal 1, department.employees.size, "After delete employee count should be 1."
|
67
|
+
department.reload
|
68
|
+
assert_equal 1, department.employees.size, "After delete and a reload from DB employee count should be 1."
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_delete_cpk_all_association
|
72
|
+
# In this case the association is a has_many composite key with
|
73
|
+
# dependent set to :delete_all
|
74
|
+
product = Product.find(1)
|
75
|
+
assert_equal(2, product.product_tariffs.length)
|
76
|
+
|
77
|
+
product_tariff = product.product_tariffs.first
|
78
|
+
product.product_tariffs.delete(product_tariff)
|
79
|
+
|
80
|
+
product.reload
|
81
|
+
assert_equal(1, product.product_tariffs.length)
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_delete_records_for_has_many_association_with_composite_primary_key
|
85
|
+
reference_type = ReferenceType.find(1)
|
86
|
+
codes_to_delete = reference_type.reference_codes[0..1]
|
87
|
+
assert_equal 3, reference_type.reference_codes.size, "Before deleting records reference_code count should be 3."
|
88
|
+
reference_type.reference_codes.delete_records(codes_to_delete)
|
89
|
+
reference_type.reload
|
90
|
+
assert_equal 1, reference_type.reference_codes.size, "After deleting 2 records and a reload from DB reference_code count should be 1."
|
91
|
+
end
|
92
|
+
end
|
data/test/test_equal.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'abstract_unit'
|
2
|
+
|
3
|
+
class TestEqual < ActiveSupport::TestCase
|
4
|
+
fixtures :capitols
|
5
|
+
|
6
|
+
def test_new
|
7
|
+
assert_not_equal(Capitol.new, Capitol.new)
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_same
|
11
|
+
first = Capitol.find('Canada', 'Ottawa')
|
12
|
+
second = Capitol.find('Canada', 'Ottawa')
|
13
|
+
assert_equal(first, second)
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_different
|
17
|
+
first = Capitol.find('Mexico', 'Mexico City')
|
18
|
+
second = Capitol.find('Canada', 'Ottawa')
|
19
|
+
assert_not_equal(first, second)
|
20
|
+
end
|
21
|
+
end
|
data/test/test_exists.rb
CHANGED
data/test/test_find.rb
CHANGED
@@ -1,76 +1,79 @@
|
|
1
|
-
require 'abstract_unit'
|
2
|
-
|
3
|
-
# Testing the find action on composite ActiveRecords with two primary keys
|
4
|
-
class TestFind < ActiveSupport::TestCase
|
5
|
-
fixtures :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
|
-
:dual_strs => {
|
17
|
-
:class => ReferenceCode,
|
18
|
-
:primary_keys => ['reference_type_id', 'reference_code'],
|
19
|
-
},
|
20
|
-
}
|
21
|
-
|
22
|
-
def setup
|
23
|
-
self.class.classes = CLASSES
|
24
|
-
end
|
25
|
-
|
26
|
-
def test_find_first
|
27
|
-
testing_with do
|
28
|
-
obj = @klass.find(:first)
|
29
|
-
assert obj
|
30
|
-
assert_equal @klass, obj.class
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
def test_find
|
35
|
-
testing_with do
|
36
|
-
found = @klass.find(*first_id) # e.g. find(1,1) or find 1,1
|
37
|
-
assert found
|
38
|
-
assert_equal @klass, found.class
|
39
|
-
assert_equal found, @klass.find(found.id)
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
found
|
47
|
-
|
48
|
-
assert_equal @klass
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
1
|
+
require 'abstract_unit'
|
2
|
+
|
3
|
+
# Testing the find action on composite ActiveRecords with two primary keys
|
4
|
+
class TestFind < ActiveSupport::TestCase
|
5
|
+
fixtures :capitols, :reference_types, :reference_codes, :suburbs
|
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
|
+
:dual_strs => {
|
17
|
+
:class => ReferenceCode,
|
18
|
+
:primary_keys => ['reference_type_id', 'reference_code'],
|
19
|
+
},
|
20
|
+
}
|
21
|
+
|
22
|
+
def setup
|
23
|
+
self.class.classes = CLASSES
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_find_first
|
27
|
+
testing_with do
|
28
|
+
obj = @klass.find(:first)
|
29
|
+
assert obj
|
30
|
+
assert_equal @klass, obj.class
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_find
|
35
|
+
testing_with do
|
36
|
+
found = @klass.find(*first_id) # e.g. find(1,1) or find 1,1
|
37
|
+
assert found
|
38
|
+
assert_equal @klass, found.class
|
39
|
+
assert_equal found, @klass.find(found.id)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_find_composite_ids
|
44
|
+
testing_with do
|
45
|
+
found = @klass.find(first_id) # e.g. find([1,1].to_composite_ids)
|
46
|
+
assert found
|
47
|
+
assert_equal @klass, found.class
|
48
|
+
assert_equal found, @klass.find(found.id)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def things_to_look_at
|
53
|
+
testing_with do
|
54
|
+
assert_equal found, @klass.find(found.id.to_s) # fails for 2+ keys
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_find_with_strings_as_composite_keys
|
59
|
+
found = Capitol.find('The Netherlands', 'Amsterdam')
|
60
|
+
assert found
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_not_found
|
64
|
+
assert_raise(::ActiveRecord::RecordNotFound) do
|
65
|
+
ReferenceCode.find(['999', '999'])
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_find_last_suburb
|
70
|
+
suburb = Suburb.find(:last)
|
71
|
+
assert_equal([2,1], suburb.id)
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_find_last_suburb_with_order
|
75
|
+
# Rails actually changes city_id DESC to city_id ASC
|
76
|
+
suburb = Suburb.find(:last, :order => 'suburbs.city_id DESC')
|
77
|
+
assert_equal([1,1], suburb.id)
|
78
|
+
end
|
79
|
+
end
|
data/test/test_ids.rb
CHANGED
@@ -1,84 +1,82 @@
|
|
1
|
-
require 'abstract_unit'
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
expected
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
expected
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
expected
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
assert_equal @primary_keys.map {|key| key.to_sym}, @klass.
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
end
|
83
|
-
end
|
1
|
+
require 'abstract_unit'
|
2
|
+
|
3
|
+
class TestIds < ActiveSupport::TestCase
|
4
|
+
fixtures :reference_types, :reference_codes
|
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
|
+
:dual_strs => {
|
16
|
+
:class => ReferenceCode,
|
17
|
+
:primary_keys => ['reference_type_id', 'reference_code'],
|
18
|
+
},
|
19
|
+
}
|
20
|
+
|
21
|
+
def setup
|
22
|
+
self.class.classes = CLASSES
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_id
|
26
|
+
testing_with do
|
27
|
+
assert_equal @first.id, @first.ids if composite?
|
28
|
+
assert_kind_of(CompositePrimaryKeys::CompositeKeys, @first.id) if composite?
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_ids_to_s
|
33
|
+
testing_with do
|
34
|
+
order = @klass.primary_key.is_a?(String) ? @klass.primary_key : @klass.primary_key.join(',')
|
35
|
+
to_test = @klass.find(:all, :order => order)[0..1].map(&:id)
|
36
|
+
assert_equal '(1,1),(1,2)', @klass.ids_to_s(to_test) if @key_test == :dual
|
37
|
+
assert_equal '1,1;1,2', @klass.ids_to_s(to_test, ',', ';', '', '') if @key_test == :dual
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_set_ids_string
|
42
|
+
testing_with do
|
43
|
+
array = @primary_keys.collect {|key| 5}
|
44
|
+
expected = composite? ? array.to_composite_keys : array.first
|
45
|
+
@first.id = expected.to_s
|
46
|
+
assert_equal expected, @first.id
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_set_ids_array
|
51
|
+
testing_with do
|
52
|
+
array = @primary_keys.collect {|key| 5}
|
53
|
+
expected = composite? ? array.to_composite_keys : array.first
|
54
|
+
@first.id = expected
|
55
|
+
assert_equal expected, @first.id
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_set_ids_comp
|
60
|
+
testing_with do
|
61
|
+
array = @primary_keys.collect {|key| 5}
|
62
|
+
expected = composite? ? array.to_composite_keys : array.first
|
63
|
+
@first.id = expected
|
64
|
+
assert_equal expected, @first.id
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_primary_keys
|
69
|
+
testing_with do
|
70
|
+
if composite?
|
71
|
+
assert_not_nil @klass.primary_keys
|
72
|
+
assert_equal @primary_keys.map {|key| key.to_sym}, @klass.primary_keys
|
73
|
+
assert_equal @klass.primary_keys, @klass.primary_key
|
74
|
+
assert_equal @primary_keys.map {|key| key.to_sym}.to_s, @klass.primary_key.to_s
|
75
|
+
else
|
76
|
+
assert_not_nil @klass.primary_key
|
77
|
+
assert_equal @primary_keys.first, @klass.primary_key.to_sym
|
78
|
+
assert_equal @primary_keys.first.to_s, @klass.primary_key.to_s
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
84
82
|
end
|