tenacity 0.2.0 → 0.3.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.
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
@@ -8,92 +8,112 @@ class MongoMapperTest < Test::Unit::TestCase
8
8
  end
9
9
 
10
10
  should "be able to find the object in the database" do
11
- wheel = MongoMapperWheel.create
12
- assert_equal wheel, MongoMapperWheel._t_find(wheel.id)
11
+ object = MongoMapperObject.create
12
+ assert_equal object, MongoMapperObject._t_find(object.id)
13
13
  end
14
14
 
15
15
  should "return nil if the specified id could not be found in the database" do
16
- assert_nil MongoMapperWheel._t_find('4d0e1224b28cdbfb72000042')
16
+ assert_nil MongoMapperObject._t_find('4d0e1224b28cdbfb72000042')
17
17
  end
18
18
 
19
19
  should "be able to find multiple objects in the database" do
20
- wheel_1 = MongoMapperWheel.create
21
- wheel_2 = MongoMapperWheel.create
22
- assert_set_equal [wheel_1, wheel_2], MongoMapperWheel._t_find_bulk([wheel_1.id, wheel_2.id, '4d0e1224b28cdbfb72000042'])
20
+ object_1 = MongoMapperObject.create
21
+ object_2 = MongoMapperObject.create
22
+ assert_set_equal [object_1, object_2], MongoMapperObject._t_find_bulk([object_1.id, object_2.id, '4d0e1224b28cdbfb72000042'])
23
23
  end
24
24
 
25
25
  should "return an empty array if none of the specified object ids could be found in the database" do
26
- assert_equal [], MongoMapperWheel._t_find_bulk(['4d0e1224b28cdbfb72000042', '4d0e1224b28cdbfb72000043', '4d0e1224b28cdbfb72000044'])
26
+ assert_equal [], MongoMapperObject._t_find_bulk(['4d0e1224b28cdbfb72000042', '4d0e1224b28cdbfb72000043', '4d0e1224b28cdbfb72000044'])
27
27
  end
28
28
 
29
29
  should "be able to find the first associate of an object" do
30
- car = ActiveRecordCar.create
31
- wheel = MongoMapperWheel.create(:active_record_car_id => car.id.to_s)
32
- assert_equal wheel, MongoMapperWheel._t_find_first_by_associate(:active_record_car_id, car.id)
30
+ object = MongoMapperObject.create
31
+ target = MongoMapperHasOneTarget.create(:mongo_mapper_object_id => object.id.to_s)
32
+ assert_equal target, MongoMapperHasOneTarget._t_find_first_by_associate(:mongo_mapper_object_id, object.id)
33
33
  end
34
34
 
35
35
  should "return nil if the first associate of an object could not be found" do
36
- assert_nil MongoMapperWheel._t_find_first_by_associate(:active_record_car_id, 12345)
36
+ assert_nil MongoMapperHasOneTarget._t_find_first_by_associate(:mongo_mapper_object_id, 12345)
37
37
  end
38
38
 
39
39
  should "be able to find the associates of an object" do
40
- wheel_1 = MongoMapperWheel.create(:active_record_car_id => '101')
41
- wheel_2 = MongoMapperWheel.create(:active_record_car_id => '101')
42
- wheel_3 = MongoMapperWheel.create(:active_record_car_id => '102')
43
- assert_set_equal [wheel_1, wheel_2], MongoMapperWheel._t_find_all_by_associate(:active_record_car_id, '101')
40
+ target_1 = MongoMapperHasOneTarget.create(:mongo_mapper_object_id => '101')
41
+ target_2 = MongoMapperHasOneTarget.create(:mongo_mapper_object_id => '101')
42
+ target_3 = MongoMapperHasOneTarget.create(:mongo_mapper_object_id => '102')
43
+ assert_set_equal [target_1, target_2], MongoMapperHasOneTarget._t_find_all_by_associate(:mongo_mapper_object_id, '101')
44
44
  end
45
45
 
46
46
  should "return an empty array if the object has no associates" do
47
- assert_equal [], MongoMapperWheel._t_find_all_by_associate(:active_record_car_id, 1234)
47
+ assert_equal [], MongoMapperHasOneTarget._t_find_all_by_associate(:mongo_mapper_object_id, '1234')
48
48
  end
49
49
 
50
50
  should "be able to reload an object from the database" do
51
- wheel = MongoMapperWheel.create
52
- wheel.active_record_car_id = 101
53
- assert_equal 101, wheel.active_record_car_id.to_i
54
- wheel.reload
55
- assert_equal '', wheel.active_record_car_id
51
+ target = MongoMapperHasOneTarget.create
52
+ target.mongo_mapper_object_id = 101
53
+ assert_equal '101', target.mongo_mapper_object_id
54
+ target.reload
55
+ assert_equal '', target.mongo_mapper_object_id
56
56
  end
57
57
 
58
58
  should "be able to associate many objects with the given object" do
59
- nut_1 = ActiveRecordNut.create
60
- nut_2 = ActiveRecordNut.create
61
- nut_3 = ActiveRecordNut.create
62
- wheel = MongoMapperWheel.create
63
- wheel._t_associate_many(association, [nut_1.id, nut_2.id, nut_3.id])
64
- assert_set_equal [nut_1.id.to_s, nut_2.id.to_s, nut_3.id.to_s], wheel.t_active_record_nut_ids
59
+ target_1 = MongoMapperHasManyTarget.create
60
+ target_2 = MongoMapperHasManyTarget.create
61
+ target_3 = MongoMapperHasManyTarget.create
62
+ object = MongoMapperObject.create
63
+ object._t_associate_many(association, [target_1.id, target_2.id, target_3.id])
64
+ assert_set_equal [target_1.id.to_s, target_2.id.to_s, target_3.id.to_s], object.t_mongo_mapper_has_many_target_ids
65
65
  end
66
66
 
67
67
  should "be able to get the ids of the objects associated with the given object" do
68
- nut_1 = ActiveRecordNut.create
69
- nut_2 = ActiveRecordNut.create
70
- nut_3 = ActiveRecordNut.create
71
- wheel = MongoMapperWheel.create
68
+ target_1 = MongoMapperHasManyTarget.create
69
+ target_2 = MongoMapperHasManyTarget.create
70
+ target_3 = MongoMapperHasManyTarget.create
71
+ object = MongoMapperObject.create
72
72
 
73
- wheel._t_associate_many(association, [nut_1.id, nut_2.id, nut_3.id])
74
- assert_set_equal [nut_1.id.to_s, nut_2.id.to_s, nut_3.id.to_s], wheel._t_get_associate_ids(association)
73
+ object._t_associate_many(association, [target_1.id, target_2.id, target_3.id])
74
+ assert_set_equal [target_1.id.to_s, target_2.id.to_s, target_3.id.to_s], object._t_get_associate_ids(association)
75
75
  end
76
76
 
77
77
  should "return an empty array when trying to fetch associate ids for an object with no associates" do
78
- wheel = MongoMapperWheel.create
79
- assert_equal [], wheel._t_get_associate_ids(association)
78
+ object = MongoMapperObject.create
79
+ assert_equal [], object._t_get_associate_ids(association)
80
80
  end
81
81
 
82
82
  should "be able to clear the associates of an object" do
83
- nut_1 = ActiveRecordNut.create
84
- nut_2 = ActiveRecordNut.create
85
- nut_3 = ActiveRecordNut.create
86
- wheel = MongoMapperWheel.create
83
+ target_1 = MongoMapperHasManyTarget.create
84
+ target_2 = MongoMapperHasManyTarget.create
85
+ target_3 = MongoMapperHasManyTarget.create
86
+ object = MongoMapperObject.create
87
+
88
+ object._t_associate_many(association, [target_1.id, target_2.id, target_3.id])
89
+ assert_set_equal [target_1.id.to_s, target_2.id.to_s, target_3.id.to_s], object._t_get_associate_ids(association)
90
+ object._t_clear_associates(association)
91
+ assert_equal [], object._t_get_associate_ids(association)
92
+ end
93
+
94
+ should "be able to delete a set of objects, issuing their callbacks" do
95
+ object_1 = MongoMapperObject.create
96
+ object_2 = MongoMapperObject.create
97
+ object_3 = MongoMapperObject.create
98
+
99
+ old_count = MongoMapperObject.count
100
+ MongoMapperObject._t_delete([object_1.id, object_2.id, object_3.id])
101
+ assert_equal old_count - 3, MongoMapperObject.count
102
+ end
103
+
104
+ should "be able to delete a setup of objects, without issuing their callbacks" do
105
+ object_1 = MongoMapperObject.create
106
+ object_2 = MongoMapperObject.create
107
+ object_3 = MongoMapperObject.create
87
108
 
88
- wheel._t_associate_many(association, [nut_1.id, nut_2.id, nut_3.id])
89
- assert_set_equal [nut_1.id.to_s, nut_2.id.to_s, nut_3.id.to_s], wheel._t_get_associate_ids(association)
90
- wheel._t_clear_associates(association)
91
- assert_equal [], wheel._t_get_associate_ids(association)
109
+ old_count = MongoMapperObject.count
110
+ MongoMapperObject._t_delete([object_1.id, object_2.id, object_3.id], false)
111
+ assert_equal old_count - 3, MongoMapperObject.count
92
112
  end
93
113
  end
94
114
 
95
115
  def association
96
- Tenacity::Association.new(:t_has_many, :active_record_nuts, MongoMapperWheel)
116
+ Tenacity::Association.new(:t_has_many, :mongo_mapper_has_many_targets, MongoMapperObject)
97
117
  end
98
118
 
99
119
  end
@@ -0,0 +1,121 @@
1
+ require 'test_helper'
2
+
3
+ require_mongoid do
4
+ class MongoidTest < Test::Unit::TestCase
5
+
6
+ context "The Mongoid extension" do
7
+ setup do
8
+ setup_fixtures
9
+ end
10
+
11
+ should "be able to find the object in the database" do
12
+ object = MongoidObject.create
13
+ assert_equal object, MongoidObject._t_find(object.id)
14
+ end
15
+
16
+ should "return nil if the specified id could not be found in the database" do
17
+ assert_nil MongoidObject._t_find('4d0e1224b28cdbfb72000042')
18
+ end
19
+
20
+ should "be able to find multiple objects in the database" do
21
+ object_1 = MongoidObject.create
22
+ object_2 = MongoidObject.create
23
+ assert_set_equal [object_1, object_2], MongoidObject._t_find_bulk([object_1.id, object_2.id, '4d0e1224b28cdbfb72000042'])
24
+ end
25
+
26
+ should "return an empty array if none of the specified object ids could be found in the database" do
27
+ assert_equal [], MongoidObject._t_find_bulk(['4d0e1224b28cdbfb72000042', '4d0e1224b28cdbfb72000043', '4d0e1224b28cdbfb72000044'])
28
+ end
29
+
30
+ should "be able to find the first associate of an object" do
31
+ object = MongoidObject.create
32
+ target = MongoidHasOneTarget.create(:mongoid_object_id => object.id.to_s)
33
+ assert_equal target, MongoidHasOneTarget._t_find_first_by_associate(:mongoid_object_id, object.id)
34
+ end
35
+
36
+ should "return nil if the first associate of an object could not be found" do
37
+ assert_nil MongoidHasOneTarget._t_find_first_by_associate(:mongoid_object_id, 12345)
38
+ end
39
+
40
+ should "be able to find the associates of an object" do
41
+ target_1 = MongoidHasOneTarget.create(:mongoid_object_id => '101')
42
+ target_2 = MongoidHasOneTarget.create(:mongoid_object_id => '101')
43
+ target_3 = MongoidHasOneTarget.create(:mongoid_object_id => '102')
44
+ assert_set_equal [target_1, target_2], MongoidHasOneTarget._t_find_all_by_associate(:mongoid_object_id, '101')
45
+ end
46
+
47
+ should "return an empty array if the object has no associates" do
48
+ assert_equal [], MongoidHasOneTarget._t_find_all_by_associate(:mongoid_object_id, '1234')
49
+ end
50
+
51
+ should "be able to reload an object from the database" do
52
+ target = MongoidHasOneTarget.create
53
+ target.mongoid_object_id = 101
54
+ assert_equal '101', target.mongoid_object_id
55
+ target.reload
56
+ assert_equal '', target.mongoid_object_id
57
+ end
58
+
59
+ should "be able to associate many objects with the given object" do
60
+ target_1 = MongoidHasManyTarget.create
61
+ target_2 = MongoidHasManyTarget.create
62
+ target_3 = MongoidHasManyTarget.create
63
+ object = MongoidObject.create
64
+ object._t_associate_many(association, [target_1.id, target_2.id, target_3.id])
65
+ assert_set_equal [target_1.id.to_s, target_2.id.to_s, target_3.id.to_s], object.t_mongoid_has_many_target_ids
66
+ end
67
+
68
+ should "be able to get the ids of the objects associated with the given object" do
69
+ target_1 = MongoidHasManyTarget.create
70
+ target_2 = MongoidHasManyTarget.create
71
+ target_3 = MongoidHasManyTarget.create
72
+ object = MongoidObject.create
73
+
74
+ object._t_associate_many(association, [target_1.id, target_2.id, target_3.id])
75
+ assert_set_equal [target_1.id.to_s, target_2.id.to_s, target_3.id.to_s], object._t_get_associate_ids(association)
76
+ end
77
+
78
+ should "return an empty array when trying to fetch associate ids for an object with no associates" do
79
+ object = MongoidObject.create
80
+ assert_equal [], object._t_get_associate_ids(association)
81
+ end
82
+
83
+ should "be able to clear the associates of an object" do
84
+ target_1 = MongoidHasManyTarget.create
85
+ target_2 = MongoidHasManyTarget.create
86
+ target_3 = MongoidHasManyTarget.create
87
+ object = MongoidObject.create
88
+
89
+ object._t_associate_many(association, [target_1.id, target_2.id, target_3.id])
90
+ assert_set_equal [target_1.id.to_s, target_2.id.to_s, target_3.id.to_s], object._t_get_associate_ids(association)
91
+ object._t_clear_associates(association)
92
+ assert_equal [], object._t_get_associate_ids(association)
93
+ end
94
+
95
+ should "be able to delete a set of objects, issuing their callbacks" do
96
+ object_1 = MongoidObject.create
97
+ object_2 = MongoidObject.create
98
+ object_3 = MongoidObject.create
99
+
100
+ old_count = MongoidObject.count
101
+ MongoidObject._t_delete([object_1.id, object_2.id, object_3.id])
102
+ assert_equal old_count - 3, MongoidObject.count
103
+ end
104
+
105
+ should "be able to delete a setup of objects, without issuing their callbacks" do
106
+ object_1 = MongoidObject.create
107
+ object_2 = MongoidObject.create
108
+ object_3 = MongoidObject.create
109
+
110
+ old_count = MongoidObject.count
111
+ MongoidObject._t_delete([object_1.id, object_2.id, object_3.id], false)
112
+ assert_equal old_count - 3, MongoidObject.count
113
+ end
114
+ end
115
+
116
+ def association
117
+ Tenacity::Association.new(:t_has_many, :mongoid_has_many_targets, MongoidObject)
118
+ end
119
+
120
+ end
121
+ end
@@ -0,0 +1,113 @@
1
+ require 'test_helper'
2
+
3
+ class SequelTest < Test::Unit::TestCase
4
+
5
+ context "The Sequel extension" do
6
+ setup do
7
+ setup_fixtures
8
+ end
9
+
10
+ should "be able to find the object in the database" do
11
+ object = SequelObject.create
12
+ assert_equal object, SequelObject._t_find(object.id)
13
+ end
14
+
15
+ should "return nil if the specified object cannot be found in the database" do
16
+ assert_nil SequelObject._t_find(989782)
17
+ end
18
+
19
+ should "be able to find multiple objects in the database" do
20
+ object = SequelObject.create
21
+ object_2 = SequelObject.create
22
+ assert_set_equal [object, object_2], SequelObject._t_find_bulk([object.id, object_2.id, 989823])
23
+ end
24
+
25
+ should "return an empty array if none of the specified ids could be found in the database" do
26
+ assert_set_equal [], SequelObject._t_find_bulk([989823, 992111, 989771])
27
+ end
28
+
29
+ should "be able to find the first associate of an object" do
30
+ object = SequelObject.create
31
+ has_one_target = SequelHasOneTarget.create(:sequel_object_id => object.id)
32
+ assert_equal has_one_target, SequelHasOneTarget._t_find_first_by_associate(:sequel_object_id, object.id)
33
+ end
34
+
35
+ should "return nil if unable to find the first associate of an object" do
36
+ assert_nil SequelHasOneTarget._t_find_first_by_associate(:sequel_object_id, '9999999')
37
+ end
38
+
39
+ should "be able to find all associates of an object" do
40
+ object = SequelObject.create
41
+ has_many_target_1 = SequelHasManyTarget.create(:sequel_object_id => object.id)
42
+ has_many_target_2 = SequelHasManyTarget.create(:sequel_object_id => object.id)
43
+ has_many_target_3 = SequelHasManyTarget.create(:sequel_object_id => '9999999')
44
+ assert_set_equal [has_many_target_1, has_many_target_2], SequelHasManyTarget._t_find_all_by_associate(:sequel_object_id, object.id)
45
+ end
46
+
47
+ should "return an empty array able to find the associates of an object" do
48
+ assert_set_equal [], SequelHasManyTarget._t_find_all_by_associate(:sequel_object_id, '9999999999')
49
+ end
50
+
51
+ should "be able to reload an object from the database" do
52
+ object = SequelHasOneTarget.create
53
+ object.mongo_mapper_object_id = 'abc123'
54
+ assert_equal 'abc123', object.mongo_mapper_object_id
55
+ object.reload
56
+ assert_equal '', object.mongo_mapper_object_id
57
+ end
58
+
59
+ should "be able to clear the associates of a given object" do
60
+ object = SequelObject.create
61
+ object._t_associate_many(association, ['abc123', 'def456', 'ghi789'])
62
+ object.save
63
+ object._t_clear_associates(association)
64
+ assert_set_equal [], object._t_get_associate_ids(association)
65
+ end
66
+
67
+ should "be able to associate many objects with the given object" do
68
+ object = SequelObject.create
69
+ object._t_associate_many(association, ['abc123', 'def456', 'ghi789'])
70
+
71
+ rows = DB["select mongo_mapper_has_many_target_id from mongo_mapper_has_many_targets_sequel_objects where sequel_object_id = #{object.id}"].all
72
+ ids = rows.map { |row| row[:mongo_mapper_has_many_target_id] }
73
+ assert_set_equal ['abc123', 'def456', 'ghi789'], ids
74
+ end
75
+
76
+ should "be able to get the ids of the objects associated with the given object" do
77
+ object = SequelObject.create
78
+ object._t_associate_many(association, ['abc123', 'def456', 'ghi789'])
79
+ assert_set_equal ['abc123', 'def456', 'ghi789'], object._t_get_associate_ids(association)
80
+ end
81
+
82
+ should "return an empty array if there are no objects associated with the given object ids" do
83
+ object = SequelObject.create
84
+ assert_set_equal [], object._t_get_associate_ids(association)
85
+ end
86
+
87
+ should "be able to delete a set of objects, issuing their callbacks" do
88
+ object_1 = SequelObject.create
89
+ object_2 = SequelObject.create
90
+ object_3 = SequelObject.create
91
+
92
+ old_count = SequelObject.count
93
+ SequelObject._t_delete([object_1.id, object_2.id, object_3.id])
94
+ assert_equal old_count - 3, SequelObject.count
95
+ end
96
+
97
+ should "be able to delete a setup of objects, without issuing their callbacks" do
98
+ object_1 = SequelObject.create
99
+ object_2 = SequelObject.create
100
+ object_3 = SequelObject.create
101
+
102
+ old_count = SequelObject.count
103
+ SequelObject._t_delete([object_1.id, object_2.id, object_3.id], false)
104
+ assert_equal old_count - 3, SequelObject.count
105
+ end
106
+ end
107
+
108
+ private
109
+
110
+ def association
111
+ Tenacity::Association.new(:t_has_many, :mongo_mapper_has_many_targets, SequelObject)
112
+ end
113
+ end
@@ -12,8 +12,11 @@ require 'test/unit'
12
12
  require 'shoulda'
13
13
 
14
14
  require File.join(File.dirname(__FILE__), 'helpers', 'active_record_test_helper')
15
- require File.join(File.dirname(__FILE__), 'helpers', 'mongo_mapper_test_helper')
16
15
  require File.join(File.dirname(__FILE__), 'helpers', 'couch_rest_test_helper')
16
+ require File.join(File.dirname(__FILE__), 'helpers', 'data_mapper_test_helper')
17
+ require File.join(File.dirname(__FILE__), 'helpers', 'mongo_mapper_test_helper')
18
+ require File.join(File.dirname(__FILE__), 'helpers', 'mongoid_test_helper')
19
+ require File.join(File.dirname(__FILE__), 'helpers', 'sequel_test_helper')
17
20
 
18
21
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
19
22
  $LOAD_PATH.unshift(File.dirname(__FILE__))
@@ -21,18 +24,38 @@ require 'tenacity'
21
24
 
22
25
  Dir[File.join(File.dirname(__FILE__), 'fixtures', '*.rb')].each { |file| require file }
23
26
 
27
+ DataMapper.auto_migrate!
28
+
24
29
  def setup_fixtures
25
- ActiveRecordCar.delete_all
26
- ActiveRecordClimateControlUnit.delete_all
27
- ActiveRecordEngine.delete_all
28
- ActiveRecordClimateControlUnit.delete_all
29
- ActiveRecordNut.delete_all
30
- MongoMapperDashboard.delete_all
31
- MongoMapperWheel.delete_all
30
+ Dir.glob(File.join(File.dirname(__FILE__), 'fixtures', '*.rb')).each do |filename|
31
+ begin
32
+ filename =~ /.*\/(.*)\.rb/
33
+ clazz = Kernel.const_get($1.camelcase)
34
+ if clazz.respond_to?(:delete_all)
35
+ clazz.delete_all
36
+ elsif clazz.respond_to?(:db)
37
+ clazz.db["delete from #{clazz.table_name}"].delete
38
+ elsif clazz.respond_to?(:destroy)
39
+ clazz.destroy
40
+ elsif filename =~ /\/couch_rest/
41
+ # CouchDB fixtures are destroyed with the database
42
+ else
43
+ puts "WARN: Don't know how to clear fixtures for #{clazz}"
44
+ end
45
+ rescue NameError
46
+ end
47
+ end
32
48
 
33
- ActiveRecordCar.connection.execute("delete from active_record_cars_mongo_mapper_wheels")
34
- ActiveRecordCar.connection.execute("delete from active_record_cars_couch_rest_doors")
35
- ActiveRecordCar.connection.execute("delete from nuts_and_wheels")
49
+ join_tables = %w{
50
+ active_record_cars_mongo_mapper_wheels
51
+ active_record_cars_couch_rest_doors
52
+ nuts_and_wheels
53
+ active_record_has_many_targets_active_record_objects
54
+ active_record_objects_mongo_mapper_has_many_targets
55
+ active_record_objects_couch_rest_has_many_targets
56
+ active_record_objects_mongoid_has_many_targets
57
+ }
58
+ join_tables.each { |join_table| ActiveRecordCar.connection.execute("delete from #{join_table}") }
36
59
  end
37
60
 
38
61
  def setup_couchdb_fixtures
@@ -44,6 +67,59 @@ def setup_all_fixtures
44
67
  setup_couchdb_fixtures
45
68
  end
46
69
 
70
+ def setup_fixtures_for(source, target)
71
+ if source == :couch_rest || target == :couch_rest
72
+ setup_all_fixtures
73
+ else
74
+ setup_fixtures
75
+ end
76
+ end
77
+
78
+ def orm_extensions
79
+ extensions = [:active_record, :couch_rest, :data_mapper, :mongo_mapper, :sequel]
80
+ require_mongoid { extensions << :mongoid }
81
+ extensions
82
+ end
83
+
84
+ def for_each_orm_extension_combination
85
+ orm_extensions.each do |source|
86
+ orm_extensions.each do |target|
87
+ yield source, target
88
+ end
89
+ end
90
+ end
91
+
92
+ def class_for_extension(extension, type=nil)
93
+ if type.nil?
94
+ class_name = extension.to_s.camelcase + "Object"
95
+ elsif type == :belongs_to || type == :has_one
96
+ class_name = extension.to_s.camelcase + "HasOneTarget"
97
+ elsif type == :has_many
98
+ class_name = extension.to_s.camelcase + "HasManyTarget"
99
+ end
100
+ Kernel.const_get(class_name)
101
+ end
102
+
103
+ def foreign_key_for(extension, type)
104
+ if type == :belongs_to
105
+ "#{extension}_object"
106
+ elsif type == :has_one
107
+ "#{extension}_has_one_target"
108
+ elsif type == :has_many
109
+ "#{extension}_has_many_targets"
110
+ end
111
+ end
112
+
113
+ def foreign_key_id_for(extension, type)
114
+ if type == :belongs_to
115
+ "#{extension}_object_id"
116
+ elsif type == :has_one
117
+ "#{extension}_has_one_target_id"
118
+ elsif type == :has_many
119
+ "#{extension}_has_many_target_ids"
120
+ end
121
+ end
122
+
47
123
  def assert_set_equal(expecteds, actuals, message = nil)
48
124
  assert_equal expecteds && Set.new(expecteds), actuals && Set.new(actuals), message
49
125
  end