tenacity 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. data/.gitignore +3 -0
  2. data/EXTEND.rdoc +11 -1
  3. data/README.rdoc +4 -1
  4. data/Rakefile +20 -9
  5. data/history.txt +21 -0
  6. data/lib/tenacity.rb +12 -4
  7. data/lib/tenacity/associates_proxy.rb +67 -0
  8. data/lib/tenacity/association.rb +19 -6
  9. data/lib/tenacity/associations/has_many.rb +6 -0
  10. data/lib/tenacity/class_methods.rb +52 -1
  11. data/lib/tenacity/instance_methods.rb +7 -3
  12. data/lib/tenacity/orm_ext/activerecord.rb +30 -13
  13. data/lib/tenacity/orm_ext/couchrest.rb +140 -0
  14. data/lib/tenacity/orm_ext/datamapper.rb +139 -0
  15. data/lib/tenacity/orm_ext/mongo_mapper.rb +88 -80
  16. data/lib/tenacity/orm_ext/mongoid.rb +108 -0
  17. data/lib/tenacity/orm_ext/sequel.rb +134 -0
  18. data/lib/tenacity/version.rb +1 -1
  19. data/tenacity.gemspec +14 -3
  20. data/test/association_features/belongs_to_test.rb +42 -0
  21. data/test/association_features/has_many_test.rb +110 -0
  22. data/test/association_features/has_one_test.rb +41 -0
  23. data/test/associations/belongs_to_test.rb +36 -127
  24. data/test/associations/has_many_test.rb +77 -196
  25. data/test/associations/has_one_test.rb +22 -84
  26. data/test/core/classmethods_test.rb +24 -22
  27. data/test/fixtures/active_record_has_many_target.rb +10 -0
  28. data/test/fixtures/active_record_has_one_target.rb +10 -0
  29. data/test/fixtures/{active_record_nuts.rb → active_record_nut.rb} +0 -0
  30. data/test/fixtures/active_record_object.rb +17 -0
  31. data/test/fixtures/couch_rest_door.rb +0 -2
  32. data/test/fixtures/couch_rest_has_many_target.rb +12 -0
  33. data/test/fixtures/couch_rest_has_one_target.rb +12 -0
  34. data/test/fixtures/couch_rest_object.rb +19 -0
  35. data/test/fixtures/couch_rest_windshield.rb +0 -2
  36. data/test/fixtures/data_mapper_has_many_target.rb +19 -0
  37. data/test/fixtures/data_mapper_has_one_target.rb +19 -0
  38. data/test/fixtures/data_mapper_object.rb +20 -0
  39. data/test/fixtures/mongo_mapper_ash_tray.rb +0 -2
  40. data/test/fixtures/mongo_mapper_dashboard.rb +0 -2
  41. data/test/fixtures/mongo_mapper_has_many_target.rb +11 -0
  42. data/test/fixtures/mongo_mapper_has_one_target.rb +11 -0
  43. data/test/fixtures/mongo_mapper_object.rb +18 -0
  44. data/test/fixtures/mongo_mapper_vent.rb +0 -2
  45. data/test/fixtures/mongo_mapper_wheel.rb +0 -2
  46. data/test/fixtures/mongoid_has_many_target.rb +13 -0
  47. data/test/fixtures/mongoid_has_one_target.rb +13 -0
  48. data/test/fixtures/mongoid_object.rb +20 -0
  49. data/test/fixtures/sequel_has_many_target.rb +10 -0
  50. data/test/fixtures/sequel_has_one_target.rb +10 -0
  51. data/test/fixtures/sequel_object.rb +17 -0
  52. data/test/helpers/active_record_test_helper.rb +51 -0
  53. data/test/helpers/data_mapper_test_helper.rb +44 -0
  54. data/test/helpers/mongoid_test_helper.rb +21 -0
  55. data/test/helpers/sequel_test_helper.rb +60 -0
  56. data/test/orm_ext/activerecord_test.rb +55 -35
  57. data/test/orm_ext/couchrest_test.rb +66 -46
  58. data/test/orm_ext/datamapper_test.rb +112 -0
  59. data/test/orm_ext/mongo_mapper_test.rb +64 -44
  60. data/test/orm_ext/mongoid_test.rb +121 -0
  61. data/test/orm_ext/sequel_test.rb +113 -0
  62. data/test/test_helper.rb +87 -11
  63. metadata +159 -59
  64. data/lib/tenacity/orm_ext/couchrest/couchrest_extended_document.rb +0 -42
  65. data/lib/tenacity/orm_ext/couchrest/couchrest_model.rb +0 -44
  66. data/lib/tenacity/orm_ext/couchrest/tenacity_class_methods.rb +0 -43
  67. data/lib/tenacity/orm_ext/couchrest/tenacity_instance_methods.rb +0 -21
  68. data/test/fixtures/couch_rest_radio.rb +0 -10
  69. data/test/fixtures/mongo_mapper_button.rb +0 -6
@@ -2,133 +2,42 @@ require 'test_helper'
2
2
 
3
3
  class BelongsToTest < Test::Unit::TestCase
4
4
 
5
- context "A class with a belongs_to association to another class" do
6
- setup do
7
- setup_fixtures
8
- @car = ActiveRecordCar.create
9
- @wheel = MongoMapperWheel.create(:active_record_car => @car)
10
- end
11
-
12
- should "memoize the association" do
13
- assert_equal @car, @wheel.active_record_car
14
-
15
- other_car = ActiveRecordCar.create
16
- assert_equal @car, MongoMapperWheel.find(@wheel.id).active_record_car
17
- MongoMapperWheel.update(@wheel.id, :active_record_car => other_car)
18
- assert_equal other_car, MongoMapperWheel.find(@wheel.id).active_record_car
19
-
20
- assert_equal @car, @wheel.active_record_car
21
- assert_equal other_car, @wheel.active_record_car(true)
22
- end
23
-
24
- should "be able to specify the class name of the associated class" do
25
- dashboard = MongoMapperDashboard.create
26
- ash_tray = MongoMapperAshTray.create(:dashboard => dashboard)
27
- assert_equal dashboard, ash_tray.dashboard
28
- end
29
-
30
- should "be able to specify the foreign key to use for the associated class" do
31
- car = ActiveRecordCar.create
32
- windshield = CouchRestWindshield.create(:active_record_car => car)
33
- assert_equal car.id.to_s, windshield.car_id
34
- assert !windshield.respond_to?(:active_record_car_id)
35
-
36
- engine = ActiveRecordEngine.create(:active_record_car => car)
37
- assert_equal car.id, engine.car_id
38
- assert !engine.respond_to?(:active_record_car_id)
39
- end
40
- end
41
-
42
- context "A MongoMapper class with belongs_to association to an ActiveRecord class" do
43
- setup do
44
- setup_fixtures
45
- @car = ActiveRecordCar.create
46
- @wheel = MongoMapperWheel.create
47
- end
48
-
49
- should "be able to fetch the id of the associated object" do
50
- @wheel.active_record_car_id = @car.id
51
- @wheel.save
52
- assert_equal @car.id, MongoMapperWheel.find(@wheel.id).active_record_car_id.to_i
53
- end
54
-
55
- should "be able to load the associated object" do
56
- @wheel.active_record_car = @car
57
- @wheel.save
58
- assert_equal @car.id, MongoMapperWheel.find(@wheel.id).active_record_car_id.to_i
59
- assert_equal @car, MongoMapperWheel.find(@wheel.id).active_record_car
60
- end
61
-
62
- should "be able to load the associated object if all we have is the id" do
63
- @wheel.active_record_car_id = @car.id
64
- @wheel.save
65
- assert_equal @car, MongoMapperWheel.find(@wheel.id).active_record_car
66
- end
67
-
68
- should "return nil if no association is set" do
69
- assert_nil MongoMapperWheel.find(@wheel.id).active_record_car
70
- end
71
- end
72
-
73
- context "An ActiveRecord class with belongs_to association to a MongoMapper class" do
74
- setup do
75
- setup_fixtures
76
- @wheel = MongoMapperWheel.create
77
- @nut = ActiveRecordNut.create
78
- end
79
-
80
- should "be able to fetch the id of the associated object" do
81
- @nut.mongo_mapper_wheel_id = @wheel.id
82
- @nut.save
83
- assert_equal @wheel.id.to_s, ActiveRecordNut.find(@nut.id).mongo_mapper_wheel_id
84
- end
85
-
86
- should "be able to load the associated object" do
87
- @nut.mongo_mapper_wheel = @wheel
88
- @nut.save
89
- assert_equal @wheel.id.to_s, ActiveRecordNut.find(@nut.id).mongo_mapper_wheel_id
90
- assert_equal @wheel, ActiveRecordNut.find(@nut.id).mongo_mapper_wheel
91
- end
92
-
93
- should "be be able to load the associated object if all we have is the id" do
94
- @nut.mongo_mapper_wheel_id = @wheel.id
95
- @nut.save
96
- assert_equal @wheel, ActiveRecordNut.find(@nut.id).mongo_mapper_wheel
97
- end
98
-
99
- should "return nil if no association is set" do
100
- assert_nil ActiveRecordNut.find(@nut.id).mongo_mapper_wheel
101
- end
102
- end
103
-
104
- context "A CouchRest class with belongs_to association to a MongoMapper class" do
105
- setup do
106
- setup_all_fixtures
107
- @dashboard = MongoMapperDashboard.create
108
- @radio = CouchRestRadio.create({})
109
- end
110
-
111
- should "be able to fetch the id of the associated object" do
112
- @radio.mongo_mapper_dashboard_id = @dashboard.id
113
- @radio.save
114
- assert_equal @dashboard.id.to_s, CouchRestRadio.find(@radio.id).mongo_mapper_dashboard_id
115
- end
116
-
117
- should "be able to load the associated object" do
118
- @radio.mongo_mapper_dashboard = @dashboard
119
- @radio.save
120
- assert_equal @dashboard.id.to_s, CouchRestRadio.find(@radio.id).mongo_mapper_dashboard_id
121
- assert_equal @dashboard, CouchRestRadio.find(@radio.id).mongo_mapper_dashboard
122
- end
123
-
124
- should "be be able to load the associated object if all we have is the id" do
125
- @radio.mongo_mapper_dashboard_id = @dashboard.id
126
- @radio.save
127
- assert_equal @dashboard, CouchRestRadio.find(@radio.id).mongo_mapper_dashboard
128
- end
129
-
130
- should "return nil if no association is set" do
131
- assert_nil CouchRestRadio.find(@radio.id).mongo_mapper_dashboard
5
+ for_each_orm_extension_combination do |source, target|
6
+ context "A #{source} class with a belongs_to association to a #{target} class" do
7
+ setup do
8
+ setup_fixtures_for(source, target)
9
+
10
+ @source_class = class_for_extension(source)
11
+ @source = @source_class.create({})
12
+ @target_class = class_for_extension(target, :belongs_to)
13
+ @target = @target_class.create({})
14
+
15
+ @foreign_key = foreign_key_for(source, :belongs_to)
16
+ @foreign_key_id = foreign_key_id_for(source, :belongs_to)
17
+ end
18
+
19
+ should "be able to fetch the id of the associated object" do
20
+ @target.send("#{@foreign_key_id}=", @source.id.to_s)
21
+ @target.save
22
+ assert_equal @source.id.to_s, @target_class._t_find(@target.id.to_s).send(@foreign_key_id)
23
+ end
24
+
25
+ should "be able to load the associated object" do
26
+ @target.send("#{@foreign_key}=", @source)
27
+ @target.save
28
+ assert_equal @source.id.to_s, @target_class._t_find(@target.id.to_s).send(@foreign_key_id)
29
+ assert_equal @source, @target_class._t_find(@target.id.to_s).send(@foreign_key)
30
+ end
31
+
32
+ should "be be able to load the associated object if all we have is the id" do
33
+ @target.send("#{@foreign_key_id}=", @source.id.to_s)
34
+ @target.save
35
+ assert_equal @source, @target_class._t_find(@target.id.to_s).send(@foreign_key)
36
+ end
37
+
38
+ should "return nil if no association is set" do
39
+ assert_nil @target_class._t_find(@target.id.to_s).send(@foreign_key)
40
+ end
132
41
  end
133
42
  end
134
43
 
@@ -2,203 +2,84 @@ require 'test_helper'
2
2
 
3
3
  class HasManyTest < Test::Unit::TestCase
4
4
 
5
- context "A class with a belongs_to association to another class" do
6
- setup do
7
- setup_fixtures
8
- @car = ActiveRecordCar.create
9
- @wheels = [MongoMapperWheel.create, MongoMapperWheel.create, MongoMapperWheel.create]
10
-
11
- @car.mongo_mapper_wheels = @wheels
12
- @car.save
13
- end
14
-
15
- should "memoize the association" do
16
- assert_equal @wheels, @car.mongo_mapper_wheels
17
-
18
- other_wheels = [MongoMapperWheel.create, MongoMapperWheel.create, MongoMapperWheel.create]
19
- assert_equal @wheels, ActiveRecordCar.find(@car.id).mongo_mapper_wheels
20
- ActiveRecordCar.find(@car.id).update_attribute(:mongo_mapper_wheels, other_wheels)
21
- assert_equal other_wheels, ActiveRecordCar.find(@car.id).mongo_mapper_wheels
22
-
23
- assert_equal @wheels, @car.mongo_mapper_wheels
24
- assert_equal other_wheels, @car.mongo_mapper_wheels(true)
25
- end
26
-
27
- should "be able to specify the class name of the associated class" do
28
- vent_1 = MongoMapperVent.create
29
- vent_2 = MongoMapperVent.create
30
- vent_3 = MongoMapperVent.create
31
- dashboard = MongoMapperDashboard.create
32
- dashboard.vents = [vent_1, vent_2, vent_3]
33
- dashboard.save
34
- assert_set_equal [vent_1, vent_2, vent_3], MongoMapperDashboard.find(dashboard.id).vents
35
- end
36
-
37
- should "be able to specify the foreign key to use for the class" do
38
- car = ActiveRecordCar.create
39
- door_1 = CouchRestDoor.create({})
40
- door_2 = CouchRestDoor.create({})
41
- door_3 = CouchRestDoor.create({})
42
- car.couch_rest_doors = [door_1, door_2, door_3]
43
- car.save
44
-
45
- assert_set_equal [door_1, door_2, door_3], ActiveRecordCar.find(car.id).couch_rest_doors
46
- assert_set_equal [door_1.id.to_s, door_2.id.to_s, door_3.id.to_s], ActiveRecordCar.find(car.id).couch_rest_door_ids
47
- end
48
- end
49
-
50
- context "An ActiveRecord class with a has_many association to a MongoMapper class" do
51
- setup do
52
- setup_fixtures
53
- @car = ActiveRecordCar.create
54
- @wheel_1 = MongoMapperWheel.create
55
- @wheel_2 = MongoMapperWheel.create
56
- @wheel_3 = MongoMapperWheel.create
57
- end
58
-
59
- should "be able to set the associated objects by their ids" do
60
- @car.mongo_mapper_wheel_ids = [@wheel_1.id, @wheel_2.id, @wheel_3.id]
61
- @car.save
62
- assert_set_equal [@wheel_1, @wheel_2, @wheel_3], ActiveRecordCar.find(@car.id).mongo_mapper_wheels
63
- assert_set_equal [@wheel_1.id.to_s, @wheel_2.id.to_s, @wheel_3.id.to_s], ActiveRecordCar.find(@car.id).mongo_mapper_wheel_ids
64
- end
65
-
66
- context "that works with associated objects" do
5
+ for_each_orm_extension_combination do |source, target|
6
+ context "A #{source} class with a has_many association to a #{target} class" do
67
7
  setup do
68
- @car.mongo_mapper_wheels = [@wheel_1, @wheel_2, @wheel_3]
69
- @car.save
70
- end
71
-
72
- should "be able to set the associated objects" do
73
- assert_set_equal [@wheel_1, @wheel_2, @wheel_3], ActiveRecordCar.find(@car.id).mongo_mapper_wheels
74
- end
75
-
76
- should "be able to add an associated object using the << operator" do
77
- wheel_4 = MongoMapperWheel.create
78
- @car.mongo_mapper_wheels << wheel_4
79
- @car.save
80
- assert_set_equal [@wheel_1, @wheel_2, @wheel_3, wheel_4], ActiveRecordCar.find(@car.id).mongo_mapper_wheels
81
- end
82
-
83
- should "be able to remove an associated object using the delete method" do
84
- @car.mongo_mapper_wheels.delete(@wheel_3)
85
- @car.save
86
- assert_set_equal [@wheel_1, @wheel_2], ActiveRecordCar.find(@car.id).mongo_mapper_wheels
87
- end
88
-
89
- should "be able to clear all associated objects using the clear method" do
90
- @car.mongo_mapper_wheels.clear
91
- @car.save
92
- assert_equal [], ActiveRecordCar.find(@car.id).mongo_mapper_wheels
93
- end
94
-
95
- should "return an empty array if the association is not set" do
96
- car = ActiveRecordCar.create
97
- assert_set_equal [], ActiveRecordCar.find(car.id).mongo_mapper_wheels
98
- end
99
- end
100
- end
101
-
102
- context "A MongoMapper class with a has_many association to an ActiveRecord class" do
103
- setup do
104
- setup_fixtures
105
- @wheel = MongoMapperWheel.create
106
- @nut_1 = ActiveRecordNut.create
107
- @nut_2 = ActiveRecordNut.create
108
- @nut_3 = ActiveRecordNut.create
109
- end
110
-
111
- should "be able to set the associated objects by their ids" do
112
- @wheel.active_record_nut_ids = [@nut_1.id, @nut_2.id, @nut_3.id]
113
- @wheel.save
114
- assert_set_equal [@nut_1, @nut_2, @nut_3], MongoMapperWheel.find(@wheel.id).active_record_nuts
115
- assert_set_equal [@nut_1.id.to_s, @nut_2.id.to_s, @nut_3.id.to_s], MongoMapperWheel.find(@wheel.id).active_record_nut_ids
116
- end
117
-
118
- context "that works with associated objects" do
119
- setup do
120
- @wheel.active_record_nuts = [@nut_1, @nut_2, @nut_3]
121
- @wheel.save
122
- end
123
-
124
- should "be able to set the associated objects" do
125
- assert_set_equal [@nut_1, @nut_2, @nut_3], MongoMapperWheel.find(@wheel.id).active_record_nuts
126
- end
127
-
128
- should "be able to add an associated object using the << operator" do
129
- nut_4 = ActiveRecordNut.create
130
- @wheel.active_record_nuts << nut_4
131
- @wheel.save
132
- assert_set_equal [@nut_1, @nut_2, @nut_3, nut_4], MongoMapperWheel.find(@wheel.id).active_record_nuts
133
- end
134
-
135
- should "be able to remove an associated object using the delete method" do
136
- @wheel.active_record_nuts.delete(@nut_3)
137
- @wheel.save
138
- assert_set_equal [@nut_1, @nut_2], MongoMapperWheel.find(@wheel.id).active_record_nuts
139
- end
140
-
141
- should "be able to clear all associated objects using the clear method" do
142
- @wheel.active_record_nuts.clear
143
- @wheel.save
144
- assert_set_equal [], MongoMapperWheel.find(@wheel.id).active_record_nuts
145
- end
146
-
147
- should "return an empty array if the association is not set" do
148
- wheel = MongoMapperWheel.create
149
- assert_set_equal [], MongoMapperWheel.find(wheel.id).active_record_nuts
150
- end
151
- end
152
- end
153
-
154
- context "A CouchRest class with a has_many association to a MongoMapper class" do
155
- setup do
156
- setup_all_fixtures
157
- @radio = CouchRestRadio.create({})
158
- @button_1 = MongoMapperButton.create
159
- @button_2 = MongoMapperButton.create
160
- @button_3 = MongoMapperButton.create
161
- end
162
-
163
- should "be able to set the associated objects by their ids" do
164
- @radio.mongo_mapper_button_ids = [@button_1.id, @button_2.id, @button_3.id]
165
- @radio.save
166
- assert_set_equal [@button_1, @button_2, @button_3], CouchRestRadio.get(@radio.id).mongo_mapper_buttons
167
- assert_set_equal [@button_1.id.to_s, @button_2.id.to_s, @button_3.id.to_s], CouchRestRadio.get(@radio.id).mongo_mapper_button_ids
168
- end
169
-
170
- context "that works with associated objects" do
171
- setup do
172
- @radio.mongo_mapper_buttons = [@button_1, @button_2, @button_3]
173
- @radio.save
174
- end
175
-
176
- should "be able to set the associated objects" do
177
- assert_set_equal [@button_1, @button_2, @button_3], CouchRestRadio.get(@radio.id).mongo_mapper_buttons
178
- end
179
-
180
- should "be able to add an associated object using the << operator" do
181
- button_4 = MongoMapperButton.create
182
- @radio.mongo_mapper_buttons << button_4
183
- @radio.save
184
- assert_set_equal [@button_1, @button_2, @button_3, button_4], CouchRestRadio.get(@radio.id).mongo_mapper_buttons
185
- end
186
-
187
- should "be able to remove an associated object using the delete method" do
188
- @radio.mongo_mapper_buttons.delete(@button_3)
189
- @radio.save
190
- assert_set_equal [@button_1, @button_2], CouchRestRadio.get(@radio.id).mongo_mapper_buttons
191
- end
192
-
193
- should "be able to clear all associated objects using the clear method" do
194
- @radio.mongo_mapper_buttons.clear
195
- @radio.save
196
- assert_equal [], CouchRestRadio.get(@radio.id).mongo_mapper_buttons
197
- end
198
-
199
- should "return an empty array if the association is not set" do
200
- radio = CouchRestRadio.create({})
201
- assert_set_equal [], CouchRestRadio.get(radio.id).mongo_mapper_buttons
8
+ setup_fixtures_for(source, target)
9
+
10
+ @source_class = class_for_extension(source)
11
+ @source = @source_class.create({})
12
+ @target_class = class_for_extension(target, :has_many)
13
+ @target_1 = @target_class.create({})
14
+ @target_2 = @target_class.create({})
15
+ @target_3 = @target_class.create({})
16
+
17
+ @foreign_key = foreign_key_for(target, :has_many)
18
+ @foreign_key_id = foreign_key_id_for(target, :has_many)
19
+ end
20
+
21
+ should "be able to set the associated objects by their ids" do
22
+ @source.send("#{@foreign_key_id}=", [@target_1.id, @target_2.id, @target_3.id])
23
+ @source.save
24
+ [@target_1, @target_2, @target_3].each { |t| t._t_reload }
25
+ assert_set_equal [@target_1, @target_2, @target_3], @source_class._t_find(@source.id.to_s).send(@foreign_key)
26
+ assert_set_equal [@target_1.id.to_s, @target_2.id.to_s, @target_3.id.to_s], @source_class._t_find(@source.id.to_s).send(@foreign_key_id)
27
+ end
28
+
29
+ context "that works with associated objects" do
30
+ setup do
31
+ @source.send("#{@foreign_key}=", [@target_1, @target_2, @target_3])
32
+ @source.save
33
+ end
34
+
35
+ should "be able to set the associated objects" do
36
+ [@target_1, @target_2, @target_3].each { |t| t._t_reload }
37
+ assert_set_equal [@target_1, @target_2, @target_3], @source_class._t_find(@source.id.to_s).send(@foreign_key)
38
+ end
39
+
40
+ should "be able to add an associated object using the << operator" do
41
+ target_4 = @target_class.create({})
42
+ @source.send(@foreign_key) << target_4
43
+ @source.save
44
+ [@target_1, @target_2, @target_3, target_4].each { |t| t._t_reload }
45
+ assert_set_equal [@target_1, @target_2, @target_3, target_4], @source_class._t_find(@source.id.to_s).send(@foreign_key)
46
+ end
47
+
48
+ should "be able to add an associated object using the push method" do
49
+ target_4 = @target_class.create({})
50
+ target_5 = @target_class.create({})
51
+ @source.send(@foreign_key).push(target_4, target_5)
52
+ @source.save
53
+ [@target_1, @target_2, @target_3, target_4, target_5].each { |t| t._t_reload }
54
+ assert_set_equal [@target_1, @target_2, @target_3, target_4, target_5], @source_class._t_find(@source.id.to_s).send(@foreign_key)
55
+ end
56
+
57
+ should "be able to add an associated object using the concat method" do
58
+ target_4 = @target_class.create({})
59
+ target_5 = @target_class.create({})
60
+ @source.send(@foreign_key).concat([target_4, target_5])
61
+ @source.save
62
+ [@target_1, @target_2, @target_3, target_4, target_5].each { |t| t._t_reload }
63
+ assert_set_equal [@target_1, @target_2, @target_3, target_4, target_5], @source_class._t_find(@source.id.to_s).send(@foreign_key)
64
+ end
65
+
66
+ should "be able to remove an associated object using the delete method" do
67
+ @source.send(@foreign_key).delete(@target_3)
68
+ @source.save
69
+ [@target_1, @target_2].each { |t| t._t_reload }
70
+ assert_set_equal [@target_1, @target_2], @source_class._t_find(@source.id.to_s).send(@foreign_key)
71
+ end
72
+
73
+ should "be able to clear all associated objects using the clear method" do
74
+ @source.send(@foreign_key).clear
75
+ @source.save
76
+ assert_set_equal [], @source_class._t_find(@source.id.to_s).send(@foreign_key)
77
+ end
78
+
79
+ should "return an empty array if the association is not set" do
80
+ source = @source_class.create({})
81
+ assert_set_equal [], @source_class._t_find(source.id.to_s).send(@foreign_key)
82
+ end
202
83
  end
203
84
  end
204
85
  end
@@ -2,90 +2,28 @@ require 'test_helper'
2
2
 
3
3
  class HasOneTest < Test::Unit::TestCase
4
4
 
5
- context "A class with a has_one association to another class" do
6
- setup do
7
- setup_fixtures
8
- @climate_control_unit = ActiveRecordClimateControlUnit.create
9
- @dashboard = MongoMapperDashboard.create(:active_record_climate_control_unit => @climate_control_unit)
10
- end
11
-
12
- should "memoize the association" do
13
- assert_equal @climate_control_unit, @dashboard.active_record_climate_control_unit
14
-
15
- other_climate_control_unit = ActiveRecordClimateControlUnit.create
16
- assert_equal @climate_control_unit, MongoMapperDashboard.find(@dashboard.id).active_record_climate_control_unit
17
- ActiveRecordClimateControlUnit.update(@climate_control_unit.id, :mongo_mapper_dashboard_id => nil)
18
- ActiveRecordClimateControlUnit.update(other_climate_control_unit.id, :mongo_mapper_dashboard_id => @dashboard.id)
19
- assert_equal other_climate_control_unit, MongoMapperDashboard.find(@dashboard.id).active_record_climate_control_unit
20
-
21
- assert_equal @climate_control_unit, @dashboard.active_record_climate_control_unit
22
- assert_equal other_climate_control_unit, @dashboard.active_record_climate_control_unit(true)
23
- end
24
-
25
- should "be able to specify the class name of the associated class" do
26
- ash_tray = MongoMapperAshTray.create
27
- dashboard = MongoMapperDashboard.create(:ash_tray => ash_tray)
28
- assert_equal ash_tray, dashboard.ash_tray
29
- end
30
-
31
- should "be able to specify the foreign key to use for the class" do
32
- car = ActiveRecordCar.create
33
- windshield = CouchRestWindshield.create(:active_record_car => car)
34
- assert_equal windshield, car.couch_rest_windshield
35
-
36
- engine = ActiveRecordEngine.create(:active_record_car => car)
37
- assert_equal engine, car.active_record_engine
38
- end
39
- end
40
-
41
- context "A MongoMapper class with a has_one association to an ActiveRecord class" do
42
- setup do
43
- setup_fixtures
44
- @climate_control_unit = ActiveRecordClimateControlUnit.create
45
- @dashboard = MongoMapperDashboard.create
46
- end
47
-
48
- should "be able to set and get the associated object" do
49
- @dashboard.active_record_climate_control_unit = @climate_control_unit
50
- assert_equal @climate_control_unit, MongoMapperDashboard.find(@dashboard.id).active_record_climate_control_unit
51
- end
52
-
53
- should "return nil if no association is set" do
54
- assert_nil MongoMapperDashboard.find(@dashboard.id).active_record_climate_control_unit
55
- end
56
- end
57
-
58
- context "An ActiveRecord class with a has_one association to a MongoMapper class" do
59
- setup do
60
- setup_fixtures
61
- @dashboard = MongoMapperDashboard.create
62
- @car = ActiveRecordCar.create
63
- end
64
-
65
- should "be able to set and get the associated object" do
66
- @car.mongo_mapper_dashboard = @dashboard
67
- assert_equal @dashboard, ActiveRecordCar.find(@car.id).mongo_mapper_dashboard
68
- end
69
-
70
- should "return nil if no association is set" do
71
- assert_nil ActiveRecordCar.find(@car.id).mongo_mapper_dashboard
72
- end
73
- end
74
-
75
- context "A CouchRest class with a has_one association to a MongoMapper class" do
76
- setup do
77
- setup_all_fixtures
78
- @dashboard = MongoMapperDashboard.create
79
- @radio = CouchRestRadio.create({})
80
- end
81
-
82
- should "be able to set and get the associated object" do
83
- @dashboard.couch_rest_radio = @radio
84
- assert_equal @radio, MongoMapperDashboard.find(@dashboard.id).couch_rest_radio
85
- end
86
-
87
- should "return nil if no association is set" do
88
- assert_nil MongoMapperDashboard.find(@dashboard.id).couch_rest_radio
5
+ for_each_orm_extension_combination do |source, target|
6
+ context "A #{source} class with a has_one association to a #{target} class" do
7
+ setup do
8
+ setup_fixtures_for(source, target)
9
+
10
+ @source_class = class_for_extension(source)
11
+ @source = @source_class.create({})
12
+ @target_class = class_for_extension(target, :has_one)
13
+ @target = @target_class.create({})
14
+
15
+ @foreign_key = foreign_key_for(target, :has_one)
16
+ @foreign_key_id = foreign_key_id_for(target, :has_one)
17
+ end
18
+
19
+ should "be able to set and get the associated object" do
20
+ @source.send("#{@foreign_key}=", @target)
21
+ assert_equal @target, @source_class._t_find(@source.id.to_s).send(@foreign_key)
22
+ end
23
+
24
+ should "return nil if no association is set" do
25
+ assert_nil @source_class._t_find(@source.id.to_s).send(@foreign_key)
26
+ end
89
27
  end
90
28
  end
91
29