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
@@ -0,0 +1,44 @@
1
+ require 'datamapper'
2
+ require 'dm-migrations'
3
+ require 'dm-migrations/migration_runner'
4
+
5
+ DataMapper.setup(:default, {
6
+ :adapter => 'sqlite3',
7
+ :database => ':memory:'})
8
+
9
+ migration 1, :create_join_tables do
10
+ up do
11
+ create_table :active_record_has_many_targets_data_mapper_objects do
12
+ column :data_mapper_object_id, Integer
13
+ column :active_record_has_many_target_id, String
14
+ end
15
+
16
+ create_table :data_mapper_objects_mongo_mapper_has_many_targets do
17
+ column :data_mapper_object_id, Integer
18
+ column :mongo_mapper_has_many_target_id, String
19
+ end
20
+
21
+ create_table :couch_rest_has_many_targets_data_mapper_objects do
22
+ column :data_mapper_object_id, Integer
23
+ column :couch_rest_has_many_target_id, String
24
+ end
25
+
26
+ create_table :data_mapper_has_many_targets_data_mapper_objects do
27
+ column :data_mapper_object_id, Integer
28
+ column :data_mapper_has_many_target_id, String
29
+ end
30
+
31
+ create_table :data_mapper_objects_mongoid_has_many_targets do
32
+ column :data_mapper_object_id, Integer
33
+ column :mongoid_has_many_target_id, String
34
+ end
35
+
36
+ create_table :data_mapper_objects_sequel_has_many_targets do
37
+ column :data_mapper_object_id, Integer
38
+ column :sequel_has_many_target_id, String
39
+ end
40
+ end
41
+ end
42
+
43
+ migrate_up!
44
+
@@ -0,0 +1,21 @@
1
+ def require_mongoid
2
+ begin
3
+ require 'mongoid'
4
+ yield
5
+ rescue LoadError
6
+ end
7
+ end
8
+
9
+ begin
10
+ require 'mongoid'
11
+
12
+ Mongoid.configure do |config|
13
+ name = "tenacity_test"
14
+ host = "localhost"
15
+ config.master = Mongo::Connection.new.db(name)
16
+ config.persist_in_safe_mode = false
17
+ end
18
+ rescue LoadError
19
+ puts "WARNING: Mongoid could not be loaded. Skipping tests."
20
+ end
21
+
@@ -0,0 +1,60 @@
1
+ require 'sequel'
2
+
3
+ DB = Sequel.sqlite
4
+
5
+ DB.create_table :sequel_objects do
6
+ primary_key :id
7
+ end
8
+
9
+ DB.create_table :sequel_has_one_targets do
10
+ primary_key :id
11
+
12
+ String :active_record_object_id
13
+ String :couch_rest_object_id
14
+ String :data_mapper_object_id
15
+ String :mongo_mapper_object_id
16
+ String :mongoid_object_id
17
+ String :sequel_object_id
18
+ end
19
+
20
+ DB.create_table :sequel_has_many_targets do
21
+ primary_key :id
22
+
23
+ String :active_record_object_id
24
+ String :couch_rest_object_id
25
+ String :data_mapper_object_id
26
+ String :mongo_mapper_object_id
27
+ String :mongoid_object_id
28
+ String :sequel_object_id
29
+ end
30
+
31
+ DB.create_table :active_record_has_many_targets_sequel_objects do
32
+ Integer :sequel_object_id
33
+ String :active_record_has_many_target_id
34
+ end
35
+
36
+ DB.create_table :mongo_mapper_has_many_targets_sequel_objects do
37
+ Integer :sequel_object_id
38
+ String :mongo_mapper_has_many_target_id
39
+ end
40
+
41
+ DB.create_table :couch_rest_has_many_targets_sequel_objects do
42
+ Integer :sequel_object_id
43
+ String :couch_rest_has_many_target_id
44
+ end
45
+
46
+ DB.create_table :data_mapper_has_many_targets_sequel_objects do
47
+ Integer :sequel_object_id
48
+ String :data_mapper_has_many_target_id
49
+ end
50
+
51
+ DB.create_table :mongoid_has_many_targets_sequel_objects do
52
+ Integer :sequel_object_id
53
+ String :mongoid_has_many_target_id
54
+ end
55
+
56
+ DB.create_table :sequel_has_many_targets_sequel_objects do
57
+ Integer :sequel_object_id
58
+ String :sequel_has_many_target_id
59
+ end
60
+
@@ -8,85 +8,105 @@ class ActiveRecordTest < Test::Unit::TestCase
8
8
  end
9
9
 
10
10
  should "be able to find the object in the database" do
11
- car = ActiveRecordCar.create
12
- assert_equal car, ActiveRecordCar._t_find(car.id)
11
+ object = ActiveRecordObject.create
12
+ assert_equal object, ActiveRecordObject._t_find(object.id)
13
13
  end
14
14
 
15
15
  should "return nil if the specified object cannot be found in the database" do
16
- assert_nil ActiveRecordCar._t_find(989782)
16
+ assert_nil ActiveRecordObject._t_find(989782)
17
17
  end
18
18
 
19
19
  should "be able to find multiple objects in the database" do
20
- car = ActiveRecordCar.create
21
- car_2 = ActiveRecordCar.create
22
- assert_set_equal [car, car_2], ActiveRecordCar._t_find_bulk([car.id, car_2.id, 989823])
20
+ object = ActiveRecordObject.create
21
+ object_2 = ActiveRecordObject.create
22
+ assert_set_equal [object, object_2], ActiveRecordObject._t_find_bulk([object.id, object_2.id, 989823])
23
23
  end
24
24
 
25
25
  should "return an empty array if none of the specified ids could be found in the database" do
26
- assert_set_equal [], ActiveRecordNut._t_find_bulk([989823, 992111, 989771])
26
+ assert_set_equal [], ActiveRecordObject._t_find_bulk([989823, 992111, 989771])
27
27
  end
28
28
 
29
29
  should "be able to find the first associate of an object" do
30
- mongo_mapper_dashboard = MongoMapperDashboard.create
31
- climate_control_unit = ActiveRecordClimateControlUnit.create(:mongo_mapper_dashboard_id => mongo_mapper_dashboard.id)
32
- assert_equal climate_control_unit, ActiveRecordClimateControlUnit._t_find_first_by_associate(:mongo_mapper_dashboard_id, mongo_mapper_dashboard.id)
30
+ object = ActiveRecordObject.create
31
+ has_one_target = ActiveRecordHasOneTarget.create(:active_record_object_id => object.id)
32
+ assert_equal has_one_target, ActiveRecordHasOneTarget._t_find_first_by_associate(:active_record_object_id, object.id)
33
33
  end
34
34
 
35
35
  should "return nil if unable to find the first associate of an object" do
36
- assert_nil ActiveRecordClimateControlUnit._t_find_first_by_associate(:mongo_mapper_dashboard_id, 'abc123')
36
+ assert_nil ActiveRecordHasOneTarget._t_find_first_by_associate(:active_record_object_id, '9999999')
37
37
  end
38
38
 
39
39
  should "be able to find all associates of an object" do
40
- nut_1 = ActiveRecordNut.create(:mongo_mapper_wheel_id => 'abc123')
41
- nut_2 = ActiveRecordNut.create(:mongo_mapper_wheel_id => 'abc123')
42
- nut_3 = ActiveRecordNut.create(:mongo_mapper_wheel_id => 'xyz456')
43
- assert_set_equal [nut_1, nut_2], ActiveRecordNut._t_find_all_by_associate(:mongo_mapper_wheel_id, 'abc123')
40
+ object = ActiveRecordObject.create
41
+ has_many_target_1 = ActiveRecordHasManyTarget.create(:active_record_object_id => object.id)
42
+ has_many_target_2 = ActiveRecordHasManyTarget.create(:active_record_object_id => object.id)
43
+ has_many_target_3 = ActiveRecordHasManyTarget.create(:active_record_object_id => '9999999')
44
+ assert_set_equal [has_many_target_1, has_many_target_2], ActiveRecordHasManyTarget._t_find_all_by_associate(:active_record_object_id, object.id)
44
45
  end
45
46
 
46
47
  should "return an empty array able to find the associates of an object" do
47
- assert_set_equal [], ActiveRecordNut._t_find_all_by_associate(:mongo_mapper_wheel_id, 'abc123')
48
+ assert_set_equal [], ActiveRecordHasManyTarget._t_find_all_by_associate(:active_record_object_id, '9999999999')
48
49
  end
49
50
 
50
51
  should "be able to reload an object from the database" do
51
- nut = ActiveRecordNut.create
52
- nut.mongo_mapper_wheel_id = 'abc123'
53
- nut.reload
54
- assert_equal '', nut.mongo_mapper_wheel_id
52
+ object = ActiveRecordHasOneTarget.create
53
+ object.mongo_mapper_object_id = 'abc123'
54
+ object.reload
55
+ assert_equal '', object.mongo_mapper_object_id
55
56
  end
56
57
 
57
58
  should "be able to clear the associates of a given object" do
58
- nut = ActiveRecordNut.create
59
- nut._t_associate_many(association, ['abc123', 'def456', 'ghi789'])
60
- nut.save
61
- nut._t_clear_associates(association)
62
- assert_set_equal [], nut._t_get_associate_ids(association)
59
+ object = ActiveRecordObject.create
60
+ object._t_associate_many(association, ['abc123', 'def456', 'ghi789'])
61
+ object.save
62
+ object._t_clear_associates(association)
63
+ assert_set_equal [], object._t_get_associate_ids(association)
63
64
  end
64
65
 
65
66
  should "be able to associate many objects with the given object" do
66
- nut = ActiveRecordNut.create
67
- nut._t_associate_many(association, ['abc123', 'def456', 'ghi789'])
68
- rows = ActiveRecordNut.connection.execute("select mongo_mapper_wheel_id from nuts_and_wheels where nut_id = #{nut.id}")
67
+ object = ActiveRecordObject.create
68
+ object._t_associate_many(association, ['abc123', 'def456', 'ghi789'])
69
+ rows = ActiveRecordObject.connection.execute("select mongo_mapper_has_many_target_id from active_record_objects_mongo_mapper_has_many_targets where active_record_object_id = #{object.id}")
69
70
  ids = []; rows.each { |r| ids << r[0] }; ids
70
71
  assert_set_equal ['abc123', 'def456', 'ghi789'], ids
71
72
  end
72
73
 
73
74
  should "be able to get the ids of the objects associated with the given object" do
74
- nut = ActiveRecordNut.create
75
- nut._t_associate_many(association, ['abc123', 'def456', 'ghi789'])
76
- assert_set_equal ['abc123', 'def456', 'ghi789'], nut._t_get_associate_ids(association)
75
+ object = ActiveRecordObject.create
76
+ object._t_associate_many(association, ['abc123', 'def456', 'ghi789'])
77
+ assert_set_equal ['abc123', 'def456', 'ghi789'], object._t_get_associate_ids(association)
77
78
  end
78
79
 
79
80
  should "return an empty array if there are no objects associated with the given object ids" do
80
- nut = ActiveRecordNut.create
81
- assert_set_equal [], nut._t_get_associate_ids(association)
81
+ object = ActiveRecordObject.create
82
+ assert_set_equal [], object._t_get_associate_ids(association)
83
+ end
84
+
85
+ should "be able to delete a set of objects, issuing their callbacks" do
86
+ object_1 = ActiveRecordObject.create
87
+ object_2 = ActiveRecordObject.create
88
+ object_3 = ActiveRecordObject.create
89
+
90
+ old_count = ActiveRecordObject.count
91
+ ActiveRecordObject._t_delete([object_1.id, object_2.id, object_3.id])
92
+ assert_equal old_count - 3, ActiveRecordObject.count
93
+ end
94
+
95
+ should "be able to delete a setup of objects, without issuing their callbacks" do
96
+ object_1 = ActiveRecordObject.create
97
+ object_2 = ActiveRecordObject.create
98
+ object_3 = ActiveRecordObject.create
99
+
100
+ old_count = ActiveRecordObject.count
101
+ ActiveRecordObject._t_delete([object_1.id, object_2.id, object_3.id], false)
102
+ assert_equal old_count - 3, ActiveRecordObject.count
82
103
  end
83
104
  end
84
105
 
85
106
  private
86
107
 
87
108
  def association
88
- Tenacity::Association.new(:t_has_many, :mongo_mapper_wheels, ActiveRecordNut, :join_table => :nuts_and_wheels,
89
- :association_key => :nut_id)
109
+ Tenacity::Association.new(:t_has_many, :mongo_mapper_has_many_targets, ActiveRecordObject)
90
110
  end
91
111
 
92
112
  end
@@ -8,95 +8,115 @@ class CouchRestTest < Test::Unit::TestCase
8
8
  end
9
9
 
10
10
  should "be able to find the object in the database" do
11
- radio = CouchRestRadio.create({})
12
- assert_equal radio, CouchRestRadio._t_find(radio.id)
11
+ object = CouchRestObject.create({})
12
+ assert_equal object, CouchRestObject._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 CouchRestRadio._t_find("abc123")
16
+ assert_nil CouchRestObject._t_find("abc123")
17
17
  end
18
18
 
19
19
  should "be able to find multiple objects in the database" do
20
- radio_1 = CouchRestRadio.create({})
21
- radio_2 = CouchRestRadio.create({})
22
- assert_set_equal [radio_1, radio_2], CouchRestRadio._t_find_bulk([radio_1.id, radio_2.id, "abc123"])
20
+ object_1 = CouchRestObject.create({})
21
+ object_2 = CouchRestObject.create({})
22
+ assert_set_equal [object_1, object_2], CouchRestObject._t_find_bulk([object_1.id, object_2.id, "abc123"])
23
23
  end
24
24
 
25
25
  should "return an empty array if unable to find the specified objects in the database" do
26
- assert_equal [], CouchRestRadio._t_find_bulk(["abc123", "abc456", "abc789"])
26
+ assert_equal [], CouchRestObject._t_find_bulk(["abc123", "abc456", "abc789"])
27
27
  end
28
28
 
29
29
  should "be able to find the first associate of an object" do
30
- dashboard = MongoMapperDashboard.create
31
- radio = CouchRestRadio.create(:mongo_mapper_dashboard_id => dashboard.id)
32
- assert_equal radio, CouchRestRadio._t_find_first_by_associate(:mongo_mapper_dashboard_id, dashboard.id)
30
+ object = CouchRestObject.create({})
31
+ target = CouchRestHasOneTarget.create(:couch_rest_object_id => object.id)
32
+ assert_equal target, CouchRestHasOneTarget._t_find_first_by_associate(:couch_rest_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 CouchRestRadio._t_find_first_by_associate(:mongo_mapper_dashboard_id, 12345)
36
+ assert_nil CouchRestHasOneTarget._t_find_first_by_associate(:couch_rest_object_id, 12345)
37
37
  end
38
38
 
39
39
  should "be able to find all associates of an object" do
40
- dashboard = MongoMapperDashboard.create
41
- radio_1 = CouchRestRadio.create(:mongo_mapper_dashboard_id => dashboard.id)
42
- radio_2 = CouchRestRadio.create(:mongo_mapper_dashboard_id => dashboard.id)
43
- radio_3 = CouchRestRadio.create(:mongo_mapper_dashboard_id => 'abc123')
44
- assert_set_equal [radio_1, radio_2], CouchRestRadio._t_find_all_by_associate(:mongo_mapper_dashboard_id, dashboard.id)
40
+ object = CouchRestObject.create({})
41
+ target_1 = CouchRestHasManyTarget.create(:couch_rest_object_id => object.id)
42
+ target_2 = CouchRestHasManyTarget.create(:couch_rest_object_id => object.id)
43
+ target_3 = CouchRestHasManyTarget.create(:couch_rest_object_id => 'abc123')
44
+ assert_set_equal [target_1, target_2], CouchRestHasManyTarget._t_find_all_by_associate(:couch_rest_object_id, object.id)
45
45
  end
46
46
 
47
47
  should "return an empty array if unable to find the associates of an object" do
48
- assert_equal [], CouchRestRadio._t_find_all_by_associate(:mongo_mapper_dashboard_id, 'abc123')
48
+ assert_set_equal [], CouchRestHasManyTarget._t_find_all_by_associate(:couch_rest_object_id, 'abc123')
49
49
  end
50
50
 
51
51
  should "be able to reload an object from the database" do
52
- radio = CouchRestRadio.create({"abc" => "123"})
53
- assert_equal "123", radio["abc"]
54
- radio["abc"] = "456"
55
- assert_equal "456", radio["abc"]
56
- radio._t_reload
57
- assert_equal "123", radio["abc"]
52
+ object = CouchRestObject.create({"abc" => "123"})
53
+ assert_equal "123", object["abc"]
54
+ object["abc"] = "456"
55
+ assert_equal "456", object["abc"]
56
+ object._t_reload
57
+ assert_equal "123", object["abc"]
58
58
  end
59
59
 
60
60
  should "be able to associate many objects with the given object" do
61
- button_1 = MongoMapperButton.create
62
- button_2 = MongoMapperButton.create
63
- button_3 = MongoMapperButton.create
64
- radio = CouchRestRadio.create({})
65
- radio._t_associate_many(association, [button_1.id, button_2.id, button_3.id])
66
- assert_set_equal [button_1.id.to_s, button_2.id.to_s, button_3.id.to_s], radio.t_mongo_mapper_button_ids
61
+ target_1 = CouchRestHasManyTarget.create({})
62
+ target_2 = CouchRestHasManyTarget.create({})
63
+ target_3 = CouchRestHasManyTarget.create({})
64
+ object = CouchRestObject.create({})
65
+ object._t_associate_many(association, [target_1.id, target_2.id, target_3.id])
66
+ assert_set_equal [target_1.id.to_s, target_2.id.to_s, target_3.id.to_s], object.t_couch_rest_has_many_target_ids
67
67
  end
68
68
 
69
69
  should "be able to get the ids of the objects associated with the given object" do
70
- button_1 = MongoMapperButton.create
71
- button_2 = MongoMapperButton.create
72
- button_3 = MongoMapperButton.create
73
- radio = CouchRestRadio.create({})
70
+ target_1 = CouchRestHasManyTarget.create({})
71
+ target_2 = CouchRestHasManyTarget.create({})
72
+ target_3 = CouchRestHasManyTarget.create({})
73
+ object = CouchRestObject.create({})
74
74
 
75
- radio._t_associate_many(association, [button_1.id, button_2.id, button_3.id])
76
- assert_set_equal [button_1.id.to_s, button_2.id.to_s, button_3.id.to_s], radio._t_get_associate_ids(association)
75
+ object._t_associate_many(association, [target_1.id, target_2.id, target_3.id])
76
+ assert_set_equal [target_1.id.to_s, target_2.id.to_s, target_3.id.to_s], object._t_get_associate_ids(association)
77
77
  end
78
78
 
79
79
  should "return an empty array when trying to fetch associate ids for an object with no associates" do
80
- radio = CouchRestRadio.create({})
81
- assert_equal [], radio._t_get_associate_ids(association)
80
+ object = CouchRestObject.create({})
81
+ assert_equal [], object._t_get_associate_ids(association)
82
82
  end
83
83
 
84
84
  should "be able to clear the associates of an object" do
85
- button_1 = MongoMapperButton.create
86
- button_2 = MongoMapperButton.create
87
- button_3 = MongoMapperButton.create
88
- radio = CouchRestRadio.create({})
85
+ target_1 = CouchRestHasManyTarget.create({})
86
+ target_2 = CouchRestHasManyTarget.create({})
87
+ target_3 = CouchRestHasManyTarget.create({})
88
+ object = CouchRestObject.create({})
89
+
90
+ object._t_associate_many(association, [target_1.id, target_2.id, target_3.id])
91
+ assert_set_equal [target_1.id.to_s, target_2.id.to_s, target_3.id.to_s], object._t_get_associate_ids(association)
92
+ object._t_clear_associates(association)
93
+ assert_equal [], object._t_get_associate_ids(association)
94
+ end
95
+
96
+ should "be able to delete a set of objects, issuing their callbacks" do
97
+ object_1 = CouchRestObject.create({})
98
+ object_2 = CouchRestObject.create({})
99
+ object_3 = CouchRestObject.create({})
100
+
101
+ old_count = CouchRestObject.count
102
+ CouchRestObject._t_delete([object_1.id, object_2.id, object_3.id])
103
+ assert_equal old_count - 3, CouchRestObject.count
104
+ end
105
+
106
+ should "be able to delete a setup of objects, without issuing their callbacks" do
107
+ object_1 = CouchRestObject.create({})
108
+ object_2 = CouchRestObject.create({})
109
+ object_3 = CouchRestObject.create({})
89
110
 
90
- radio._t_associate_many(association, [button_1.id, button_2.id, button_3.id])
91
- assert_set_equal [button_1.id.to_s, button_2.id.to_s, button_3.id.to_s], radio._t_get_associate_ids(association)
92
- radio._t_clear_associates(association)
93
- assert_equal [], radio._t_get_associate_ids(association)
111
+ old_count = CouchRestObject.count
112
+ CouchRestObject._t_delete([object_1.id, object_2.id, object_3.id], false)
113
+ assert_equal old_count - 3, CouchRestObject.count
94
114
  end
95
115
  end
96
116
 
97
117
  private
98
118
 
99
119
  def association
100
- Tenacity::Association.new(:t_has_many, :mongo_mapper_buttons, CouchRestRadio)
120
+ Tenacity::Association.new(:t_has_many, :couch_rest_has_many_targets, CouchRestObject)
101
121
  end
102
122
  end
@@ -0,0 +1,112 @@
1
+ require 'test_helper'
2
+
3
+ class DataMapperTest < Test::Unit::TestCase
4
+
5
+ context "The DataMapper 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 = DataMapperObject.create
12
+ assert_equal object, DataMapperObject._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 DataMapperObject._t_find(989782)
17
+ end
18
+
19
+ should "be able to find multiple objects in the database" do
20
+ object = DataMapperObject.create
21
+ object_2 = DataMapperObject.create
22
+ assert_set_equal [object, object_2], DataMapperObject._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 [], DataMapperObject._t_find_bulk([989823, 992111, 989771])
27
+ end
28
+
29
+ should "be able to find the first associate of an object" do
30
+ object = DataMapperObject.create
31
+ has_one_target = DataMapperHasOneTarget.create(:data_mapper_object_id => object.id)
32
+ assert_equal has_one_target, DataMapperHasOneTarget._t_find_first_by_associate(:data_mapper_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 DataMapperHasOneTarget._t_find_first_by_associate(:data_mapper_object_id, '9999999')
37
+ end
38
+
39
+ should "be able to find all associates of an object" do
40
+ object = DataMapperObject.create
41
+ has_many_target_1 = DataMapperHasManyTarget.create(:data_mapper_object_id => object.id)
42
+ has_many_target_2 = DataMapperHasManyTarget.create(:data_mapper_object_id => object.id)
43
+ has_many_target_3 = DataMapperHasManyTarget.create(:data_mapper_object_id => '9999999')
44
+ assert_set_equal [has_many_target_1, has_many_target_2], DataMapperHasManyTarget._t_find_all_by_associate(:data_mapper_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 [], DataMapperHasManyTarget._t_find_all_by_associate(:data_mapper_object_id, '9999999999')
49
+ end
50
+
51
+ should "be able to reload an object from the database" do
52
+ object = DataMapperHasOneTarget.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 = DataMapperObject.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 = DataMapperObject.create
69
+ object._t_associate_many(association, ['abc123', 'def456', 'ghi789'])
70
+ rows = DataMapperObject.repository.adapter.select("select mongo_mapper_has_many_target_id from data_mapper_objects_mongo_mapper_has_many_targets where data_mapper_object_id = #{object.id}")
71
+ assert_set_equal ['abc123', 'def456', 'ghi789'], rows
72
+ end
73
+
74
+ should "be able to get the ids of the objects associated with the given object" do
75
+ object = DataMapperObject.create
76
+ object._t_associate_many(association, ['abc123', 'def456', 'ghi789'])
77
+ assert_set_equal ['abc123', 'def456', 'ghi789'], object._t_get_associate_ids(association)
78
+ end
79
+
80
+ should "return an empty array if there are no objects associated with the given object ids" do
81
+ object = DataMapperObject.create
82
+ assert_set_equal [], object._t_get_associate_ids(association)
83
+ end
84
+
85
+ should "be able to delete a set of objects, issuing their callbacks" do
86
+ object_1 = DataMapperObject.create
87
+ object_2 = DataMapperObject.create
88
+ object_3 = DataMapperObject.create
89
+
90
+ old_count = DataMapperObject.count
91
+ DataMapperObject._t_delete([object_1.id, object_2.id, object_3.id])
92
+ assert_equal old_count - 3, DataMapperObject.count
93
+ end
94
+
95
+ should "be able to delete a setup of objects, without issuing their callbacks" do
96
+ object_1 = DataMapperObject.create
97
+ object_2 = DataMapperObject.create
98
+ object_3 = DataMapperObject.create
99
+
100
+ old_count = DataMapperObject.count
101
+ DataMapperObject._t_delete([object_1.id, object_2.id, object_3.id], false)
102
+ assert_equal old_count - 3, DataMapperObject.count
103
+ end
104
+ end
105
+
106
+ private
107
+
108
+ def association
109
+ Tenacity::Association.new(:t_has_many, :mongo_mapper_has_many_targets, DataMapperObject)
110
+ end
111
+
112
+ end