composite_primary_keys 3.0.9 → 3.1.0

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