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,181 +1,181 @@
1
- require 'abstract_unit'
2
-
3
- class TestAssociations < ActiveSupport::TestCase
4
- fixtures :articles, :products, :tariffs, :product_tariffs, :suburbs, :streets, :restaurants, :restaurants_suburbs,
5
- :dorms, :rooms, :room_attributes, :room_attribute_assignments, :students, :room_assignments, :users, :readings,
6
- :memberships
7
-
8
- def test_has_many_through_with_conditions_when_through_association_is_not_composite
9
- user = User.find(:first)
10
- assert_equal 1, user.articles.find(:all, :conditions => ["articles.name = ?", "Article One"]).size
11
- end
12
-
13
- def test_has_many_through_with_conditions_when_through_association_is_composite
14
- room = Room.find(:first)
15
- assert_equal 0, room.room_attributes.find(:all, :conditions => ["room_attributes.name != ?", "keg"]).size
16
- end
17
-
18
- def test_has_many_through_on_custom_finder_when_through_association_is_composite_finder_when_through_association_is_not_composite
19
- user = User.find(:first)
20
- assert_equal 1, user.find_custom_articles.size
21
- end
22
-
23
- def test_has_many_through_on_custom_finder_when_through_association_is_composite
24
- room = Room.find(:first)
25
- assert_equal 0, room.find_custom_room_attributes.size
26
- end
27
-
28
- def test_count
29
- assert_equal 2, Product.count(:include => :product_tariffs)
30
- assert_equal 3, Tariff.count(:include => :product_tariffs)
31
- assert_equal 2, Tariff.count(:group => :start_date).size
32
- end
33
-
34
- def test_products
35
- assert_not_nil products(:first_product).product_tariffs
36
- assert_equal 2, products(:first_product).product_tariffs.length
37
- assert_not_nil products(:first_product).tariffs
38
- assert_equal 2, products(:first_product).tariffs.length
39
- assert_not_nil products(:first_product).product_tariff
40
- end
41
-
42
- def test_product_tariffs
43
- assert_not_nil product_tariffs(:first_flat).product
44
- assert_not_nil product_tariffs(:first_flat).tariff
45
- assert_equal Product, product_tariffs(:first_flat).product.class
46
- assert_equal Tariff, product_tariffs(:first_flat).tariff.class
47
- end
48
-
49
- def test_tariffs
50
- assert_not_nil tariffs(:flat).product_tariffs
51
- assert_equal 1, tariffs(:flat).product_tariffs.length
52
- assert_not_nil tariffs(:flat).products
53
- assert_equal 1, tariffs(:flat).products.length
54
- assert_not_nil tariffs(:flat).product_tariff
55
- end
56
-
57
- # Its not generating the instances of associated classes from the rows
58
- def test_find_includes_products
59
- assert @products = Product.find(:all, :include => :product_tariffs)
60
- assert_equal 2, @products.length
61
- assert_not_nil @products.first.instance_variable_get('@product_tariffs'), '@product_tariffs not set; should be array'
62
- assert_equal 3, @products.inject(0) {|sum, tariff| sum + tariff.instance_variable_get('@product_tariffs').length},
63
- "Incorrect number of product_tariffs returned"
64
- end
65
-
66
- def test_find_includes_tariffs
67
- assert @tariffs = Tariff.find(:all, :include => :product_tariffs)
68
- assert_equal 3, @tariffs.length
69
- assert_not_nil @tariffs.first.instance_variable_get('@product_tariffs'), '@product_tariffs not set; should be array'
70
- assert_equal 3, @tariffs.inject(0) {|sum, tariff| sum + tariff.instance_variable_get('@product_tariffs').length},
71
- "Incorrect number of product_tariffs returnedturned"
72
- end
73
-
74
- def test_find_includes_product
75
- assert @product_tariffs = ProductTariff.find(:all, :include => :product)
76
- assert_equal 3, @product_tariffs.length
77
- assert_not_nil @product_tariffs.first.instance_variable_get('@product'), '@product not set'
78
- end
79
-
80
- def test_find_includes_comp_belongs_to_tariff
81
- assert @product_tariffs = ProductTariff.find(:all, :include => :tariff)
82
- assert_equal 3, @product_tariffs.length
83
- assert_not_nil @product_tariffs.first.instance_variable_get('@tariff'), '@tariff not set'
84
- end
85
-
86
- def test_find_includes_extended
87
- assert @products = Product.find(:all, :include => {:product_tariffs => :tariff})
88
- assert_equal 3, @products.inject(0) {|sum, product| sum + product.instance_variable_get('@product_tariffs').length},
89
- "Incorrect number of product_tariffs returned"
90
-
91
- assert @tariffs = Tariff.find(:all, :include => {:product_tariffs => :product})
92
- assert_equal 3, @tariffs.inject(0) {|sum, tariff| sum + tariff.instance_variable_get('@product_tariffs').length},
93
- "Incorrect number of product_tariffs returned"
94
- end
95
-
96
- def test_has_many_through
97
- @products = Product.find(:all, :include => :tariffs)
98
- assert_equal 3, @products.inject(0) {|sum, product| sum + product.instance_variable_get('@tariffs').length},
99
- "Incorrect number of tariffs returned"
100
- end
101
-
102
- def test_has_many_through_when_not_pre_loaded
103
- student = Student.find(:first)
104
- rooms = student.rooms
105
- assert_equal 1, rooms.size
106
- assert_equal 1, rooms.first.dorm_id
107
- assert_equal 1, rooms.first.room_id
108
- end
109
-
110
- def test_has_many_through_when_through_association_is_composite
111
- dorm = Dorm.find(:first)
112
- assert_equal 1, dorm.rooms.length
113
- assert_equal 1, dorm.rooms.first.room_attributes.length
114
- assert_equal 'keg', dorm.rooms.first.room_attributes.first.name
115
- end
116
-
117
- def test_associations_with_conditions
118
- @suburb = Suburb.find([2, 1])
119
- assert_equal 2, @suburb.streets.size
120
-
121
- @suburb = Suburb.find([2, 1])
122
- assert_equal 1, @suburb.first_streets.size
123
-
124
- @suburb = Suburb.find([2, 1], :include => :streets)
125
- assert_equal 2, @suburb.streets.size
126
-
127
- @suburb = Suburb.find([2, 1], :include => :first_streets)
128
- assert_equal 1, @suburb.first_streets.size
129
- end
130
-
131
- def test_has_and_belongs_to_many
132
- @restaurant = Restaurant.find([1,1])
133
- assert_equal 2, @restaurant.suburbs.size
134
-
135
- @restaurant = Restaurant.find([1,1], :include => :suburbs)
136
- assert_equal 2, @restaurant.suburbs.size
137
- end
138
-
139
- def test_has_many_with_primary_key
140
- @membership = Membership.find([1, 1])
141
-
142
- assert_equal 2, @membership.readings.size
143
- end
144
-
145
- def test_has_one_with_primary_key
146
- @membership = Membership.find([1, 1])
147
-
148
- assert_equal 2, @membership.reading.id
149
- end
150
-
151
- def test_joins_has_many_with_primary_key
152
- @membership = Membership.find(:first, :joins => :readings, :conditions => { :readings => { :id => 1 } })
153
-
154
- assert_equal [1, 1], @membership.id
155
- end
156
-
157
- def test_joins_has_one_with_primary_key
158
- @membership = Membership.find(:first, :joins => :reading, :conditions => { :readings => { :id => 2 } })
159
-
160
- assert_equal [1, 1], @membership.id
161
- end
162
-
163
- def test_has_many_with_primary_key_with_associations
164
- # Trigger Active Records find_with_associations method
165
- memberships = Membership.find(:all, :include => :statuses,
166
- :conditions => ["membership_statuses.status = ?",
167
- 'Active'])
168
-
169
- assert_equal(1, memberships.length)
170
- assert_equal([1,1], memberships[0].id)
171
- end
172
-
173
- def test_limitable_reflections
174
- memberships = Membership.find(:all, :include => :statuses,
175
- :conditions => ["membership_statuses.status = ?",
176
- 'Active'],
177
- :limit => 1)
178
- assert_equal(1, memberships.length)
179
- assert_equal([1,1], memberships[0].id)
180
- end
1
+ require 'abstract_unit'
2
+
3
+ class TestAssociations < ActiveSupport::TestCase
4
+ fixtures :articles, :products, :tariffs, :product_tariffs, :suburbs, :streets, :restaurants, :restaurants_suburbs,
5
+ :dorms, :rooms, :room_attributes, :room_attribute_assignments, :students, :room_assignments, :users, :readings,
6
+ :memberships
7
+
8
+ def test_has_many_through_with_conditions_when_through_association_is_not_composite
9
+ user = User.find(:first)
10
+ assert_equal 1, user.articles.find(:all, :conditions => ["articles.name = ?", "Article One"]).size
11
+ end
12
+
13
+ def test_has_many_through_with_conditions_when_through_association_is_composite
14
+ room = Room.find(:first)
15
+ assert_equal 0, room.room_attributes.find(:all, :conditions => ["room_attributes.name != ?", "keg"]).size
16
+ end
17
+
18
+ def test_has_many_through_on_custom_finder_when_through_association_is_composite_finder_when_through_association_is_not_composite
19
+ user = User.find(:first)
20
+ assert_equal 1, user.find_custom_articles.size
21
+ end
22
+
23
+ def test_has_many_through_on_custom_finder_when_through_association_is_composite
24
+ room = Room.find(:first)
25
+ assert_equal 0, room.find_custom_room_attributes.size
26
+ end
27
+
28
+ def test_count
29
+ assert_equal 2, Product.count(:include => :product_tariffs)
30
+ assert_equal 3, Tariff.count(:include => :product_tariffs)
31
+ assert_equal 2, Tariff.count(:group => :start_date).size
32
+ end
33
+
34
+ def test_products
35
+ assert_not_nil products(:first_product).product_tariffs
36
+ assert_equal 2, products(:first_product).product_tariffs.length
37
+ assert_not_nil products(:first_product).tariffs
38
+ assert_equal 2, products(:first_product).tariffs.length
39
+ assert_not_nil products(:first_product).product_tariff
40
+ end
41
+
42
+ def test_product_tariffs
43
+ assert_not_nil product_tariffs(:first_flat).product
44
+ assert_not_nil product_tariffs(:first_flat).tariff
45
+ assert_equal Product, product_tariffs(:first_flat).product.class
46
+ assert_equal Tariff, product_tariffs(:first_flat).tariff.class
47
+ end
48
+
49
+ def test_tariffs
50
+ assert_not_nil tariffs(:flat).product_tariffs
51
+ assert_equal 1, tariffs(:flat).product_tariffs.length
52
+ assert_not_nil tariffs(:flat).products
53
+ assert_equal 1, tariffs(:flat).products.length
54
+ assert_not_nil tariffs(:flat).product_tariff
55
+ end
56
+
57
+ # Its not generating the instances of associated classes from the rows
58
+ def test_find_includes_products
59
+ assert @products = Product.find(:all, :include => :product_tariffs)
60
+ assert_equal 2, @products.length
61
+ assert_not_nil @products.first.instance_variable_get('@product_tariffs'), '@product_tariffs not set; should be array'
62
+ assert_equal 3, @products.inject(0) {|sum, tariff| sum + tariff.instance_variable_get('@product_tariffs').length},
63
+ "Incorrect number of product_tariffs returned"
64
+ end
65
+
66
+ def test_find_includes_tariffs
67
+ assert @tariffs = Tariff.find(:all, :include => :product_tariffs)
68
+ assert_equal 3, @tariffs.length
69
+ assert_not_nil @tariffs.first.instance_variable_get('@product_tariffs'), '@product_tariffs not set; should be array'
70
+ assert_equal 3, @tariffs.inject(0) {|sum, tariff| sum + tariff.instance_variable_get('@product_tariffs').length},
71
+ "Incorrect number of product_tariffs returnedturned"
72
+ end
73
+
74
+ def test_find_includes_product
75
+ assert @product_tariffs = ProductTariff.find(:all, :include => :product)
76
+ assert_equal 3, @product_tariffs.length
77
+ assert_not_nil @product_tariffs.first.instance_variable_get('@product'), '@product not set'
78
+ end
79
+
80
+ def test_find_includes_comp_belongs_to_tariff
81
+ assert @product_tariffs = ProductTariff.find(:all, :include => :tariff)
82
+ assert_equal 3, @product_tariffs.length
83
+ assert_not_nil @product_tariffs.first.instance_variable_get('@tariff'), '@tariff not set'
84
+ end
85
+
86
+ def test_find_includes_extended
87
+ assert @products = Product.find(:all, :include => {:product_tariffs => :tariff})
88
+ assert_equal 3, @products.inject(0) {|sum, product| sum + product.instance_variable_get('@product_tariffs').length},
89
+ "Incorrect number of product_tariffs returned"
90
+
91
+ assert @tariffs = Tariff.find(:all, :include => {:product_tariffs => :product})
92
+ assert_equal 3, @tariffs.inject(0) {|sum, tariff| sum + tariff.instance_variable_get('@product_tariffs').length},
93
+ "Incorrect number of product_tariffs returned"
94
+ end
95
+
96
+ def test_has_many_through
97
+ @products = Product.find(:all, :include => :tariffs)
98
+ assert_equal 3, @products.inject(0) {|sum, product| sum + product.instance_variable_get('@tariffs').length},
99
+ "Incorrect number of tariffs returned"
100
+ end
101
+
102
+ def test_has_many_through_when_not_pre_loaded
103
+ student = Student.find(:first)
104
+ rooms = student.rooms
105
+ assert_equal 1, rooms.size
106
+ assert_equal 1, rooms.first.dorm_id
107
+ assert_equal 1, rooms.first.room_id
108
+ end
109
+
110
+ def test_has_many_through_when_through_association_is_composite
111
+ dorm = Dorm.find(:first)
112
+ assert_equal 1, dorm.rooms.length
113
+ assert_equal 1, dorm.rooms.first.room_attributes.length
114
+ assert_equal 'keg', dorm.rooms.first.room_attributes.first.name
115
+ end
116
+
117
+ def test_associations_with_conditions
118
+ @suburb = Suburb.find([2, 1])
119
+ assert_equal 2, @suburb.streets.size
120
+
121
+ @suburb = Suburb.find([2, 1])
122
+ assert_equal 1, @suburb.first_streets.size
123
+
124
+ @suburb = Suburb.find([2, 1], :include => :streets)
125
+ assert_equal 2, @suburb.streets.size
126
+
127
+ @suburb = Suburb.find([2, 1], :include => :first_streets)
128
+ assert_equal 1, @suburb.first_streets.size
129
+ end
130
+
131
+ def test_has_and_belongs_to_many
132
+ @restaurant = Restaurant.find([1,1])
133
+ assert_equal 2, @restaurant.suburbs.size
134
+
135
+ @restaurant = Restaurant.find([1,1], :include => :suburbs)
136
+ assert_equal 2, @restaurant.suburbs.size
137
+ end
138
+
139
+ def test_has_many_with_primary_key
140
+ @membership = Membership.find([1, 1])
141
+
142
+ assert_equal 2, @membership.readings.size
143
+ end
144
+
145
+ def test_has_one_with_primary_key
146
+ @membership = Membership.find([1, 1])
147
+
148
+ assert_equal 2, @membership.reading.id
149
+ end
150
+
151
+ def test_joins_has_many_with_primary_key
152
+ @membership = Membership.find(:first, :joins => :readings, :conditions => { :readings => { :id => 1 } })
153
+
154
+ assert_equal [1, 1], @membership.id
155
+ end
156
+
157
+ def test_joins_has_one_with_primary_key
158
+ @membership = Membership.find(:first, :joins => :reading, :conditions => { :readings => { :id => 2 } })
159
+
160
+ assert_equal [1, 1], @membership.id
161
+ end
162
+
163
+ def test_has_many_with_primary_key_with_associations
164
+ # Trigger Active Records find_with_associations method
165
+ memberships = Membership.find(:all, :include => :statuses,
166
+ :conditions => ["membership_statuses.status = ?",
167
+ 'Active'])
168
+
169
+ assert_equal(1, memberships.length)
170
+ assert_equal([1,1], memberships[0].id)
171
+ end
172
+
173
+ def test_limitable_reflections
174
+ memberships = Membership.find(:all, :include => :statuses,
175
+ :conditions => ["membership_statuses.status = ?",
176
+ 'Active'],
177
+ :limit => 1)
178
+ assert_equal(1, memberships.length)
179
+ assert_equal([1,1], memberships[0].id)
180
+ end
181
181
  end
@@ -1,6 +1,4 @@
1
1
  require 'abstract_unit'
2
- require 'fixtures/kitchen_sink'
3
- require 'fixtures/reference_type'
4
2
 
5
3
  class TestAttributeMethods < ActiveSupport::TestCase
6
4
  fixtures :kitchen_sinks, :reference_types
@@ -1,9 +1,4 @@
1
1
  require 'abstract_unit'
2
- require 'fixtures/reference_type'
3
- require 'fixtures/reference_code'
4
- require 'fixtures/product'
5
- require 'fixtures/tariff'
6
- require 'fixtures/product_tariff'
7
2
 
8
3
  class TestAttributes < ActiveSupport::TestCase
9
4
  fixtures :reference_types, :reference_codes, :products, :tariffs, :product_tariffs
@@ -1,34 +1,32 @@
1
- require 'abstract_unit'
2
- require 'fixtures/reference_type'
3
- require 'fixtures/reference_code'
4
-
5
- class TestClone < 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
- }
18
-
19
- def setup
20
- self.class.classes = CLASSES
21
- end
22
-
23
- def test_truth
24
- testing_with do
25
- clone = @first.clone
26
- assert_equal @first.attributes.block(@klass.primary_key), clone.attributes
27
- if composite?
28
- @klass.primary_key.each {|key| assert_nil clone[key], "Primary key '#{key}' should be nil"}
29
- else
30
- assert_nil clone[@klass.primary_key], "Sole primary key should be nil"
31
- end
32
- end
33
- end
1
+ require 'abstract_unit'
2
+
3
+ class TestClone < 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
+ }
16
+
17
+ def setup
18
+ self.class.classes = CLASSES
19
+ end
20
+
21
+ def test_truth
22
+ testing_with do
23
+ clone = @first.clone
24
+ assert_equal @first.attributes.block(@klass.primary_key), clone.attributes
25
+ if composite?
26
+ @klass.primary_key.each {|key| assert_nil clone[key], "Primary key '#{key}' should be nil"}
27
+ else
28
+ assert_nil clone[@klass.primary_key], "Sole primary key should be nil"
29
+ end
30
+ end
31
+ end
34
32
  end
@@ -1,6 +1,4 @@
1
1
  require 'abstract_unit'
2
- require 'fixtures/reference_type'
3
- require 'fixtures/reference_code'
4
2
 
5
3
  class CompositeArraysTest < ActiveSupport::TestCase
6
4