tenacity 0.4.1 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/EXTEND.rdoc +18 -21
- data/Gemfile +0 -2
- data/README.rdoc +3 -1
- data/Rakefile +7 -0
- data/history.txt +17 -0
- data/lib/tenacity.rb +4 -0
- data/lib/tenacity/associates_proxy.rb +5 -7
- data/lib/tenacity/association.rb +9 -47
- data/lib/tenacity/associations/belongs_to.rb +1 -2
- data/lib/tenacity/associations/has_many.rb +27 -21
- data/lib/tenacity/associations/has_one.rb +3 -2
- data/lib/tenacity/class_methods.rb +14 -60
- data/lib/tenacity/errors.rb +8 -0
- data/lib/tenacity/instance_methods.rb +42 -12
- data/lib/tenacity/orm_ext/activerecord.rb +11 -32
- data/lib/tenacity/orm_ext/couchrest.rb +14 -22
- data/lib/tenacity/orm_ext/datamapper.rb +14 -31
- data/lib/tenacity/orm_ext/helpers.rb +3 -3
- data/lib/tenacity/orm_ext/mongo_mapper.rb +16 -22
- data/lib/tenacity/orm_ext/mongoid.rb +10 -18
- data/lib/tenacity/orm_ext/ripple.rb +270 -0
- data/lib/tenacity/orm_ext/sequel.rb +21 -33
- data/lib/tenacity/orm_ext/toystore.rb +114 -0
- data/lib/tenacity/version.rb +1 -1
- data/tenacity.gemspec +10 -3
- data/test/association_features/belongs_to_test.rb +12 -0
- data/test/association_features/has_many_test.rb +32 -2
- data/test/association_features/has_one_test.rb +18 -4
- data/test/associations/has_one_test.rb +0 -1
- data/test/core/classmethods_test.rb +7 -0
- data/test/fixtures/active_record_has_many_target.rb +4 -0
- data/test/fixtures/active_record_has_one_target.rb +4 -0
- data/test/fixtures/active_record_object.rb +8 -0
- data/test/fixtures/couch_rest_has_many_target.rb +4 -0
- data/test/fixtures/couch_rest_has_one_target.rb +4 -0
- data/test/fixtures/couch_rest_object.rb +8 -0
- data/test/fixtures/data_mapper_has_many_target.rb +10 -0
- data/test/fixtures/data_mapper_has_one_target.rb +10 -0
- data/test/fixtures/data_mapper_object.rb +8 -0
- data/test/fixtures/mongo_mapper_has_many_target.rb +4 -0
- data/test/fixtures/mongo_mapper_has_one_target.rb +4 -0
- data/test/fixtures/mongo_mapper_object.rb +8 -0
- data/test/fixtures/mongoid_has_many_target.rb +4 -0
- data/test/fixtures/mongoid_has_one_target.rb +4 -0
- data/test/fixtures/mongoid_object.rb +8 -0
- data/test/fixtures/no_associations.rb +4 -0
- data/test/fixtures/ripple_has_many_target.rb +24 -0
- data/test/fixtures/ripple_has_one_target.rb +24 -0
- data/test/fixtures/ripple_object.rb +42 -0
- data/test/fixtures/sequel_has_many_target.rb +4 -0
- data/test/fixtures/sequel_has_one_target.rb +4 -0
- data/test/fixtures/sequel_object.rb +8 -0
- data/test/fixtures/toystore_has_many_target.rb +28 -0
- data/test/fixtures/toystore_has_one_target.rb +28 -0
- data/test/fixtures/toystore_object.rb +46 -0
- data/test/helpers/active_record_test_helper.rb +12 -95
- data/test/helpers/data_mapper_test_helper.rb +0 -64
- data/test/helpers/ripple_test_helper.rb +19 -0
- data/test/helpers/sequel_test_helper.rb +13 -60
- data/test/helpers/toystore_test_helper.rb +17 -0
- data/test/orm_ext/activerecord_test.rb +16 -26
- data/test/orm_ext/couchrest_test.rb +10 -29
- data/test/orm_ext/datamapper_test.rb +16 -29
- data/test/orm_ext/mongo_mapper_test.rb +11 -29
- data/test/orm_ext/mongoid_test.rb +11 -29
- data/test/orm_ext/ripple_test.rb +140 -0
- data/test/orm_ext/sequel_test.rb +15 -26
- data/test/orm_ext/toystore_test.rb +103 -0
- data/test/test_helper.rb +35 -23
- metadata +99 -133
@@ -0,0 +1,17 @@
|
|
1
|
+
def require_toystore
|
2
|
+
begin
|
3
|
+
require 'toystore'
|
4
|
+
yield
|
5
|
+
rescue LoadError
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
begin
|
10
|
+
require 'toystore'
|
11
|
+
|
12
|
+
FileUtils.mkdir_p("log")
|
13
|
+
Toy.logger = ::Logger.new(File.join("log", "toystore.log"))
|
14
|
+
rescue LoadError
|
15
|
+
puts "WARNING: Toystore could not be loaded. Skipping tests."
|
16
|
+
end
|
17
|
+
|
@@ -37,11 +37,12 @@ class ActiveRecordTest < Test::Unit::TestCase
|
|
37
37
|
end
|
38
38
|
|
39
39
|
should "be able to find all associates of an object" do
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
40
|
+
object_1 = ActiveRecordObject.create
|
41
|
+
object_2 = ActiveRecordObject.create
|
42
|
+
has_many_target_1 = ActiveRecordHasManyTarget.create(:active_record_object_id => object_1.id)
|
43
|
+
has_many_target_2 = ActiveRecordHasManyTarget.create(:active_record_object_id => object_1.id)
|
44
|
+
has_many_target_3 = ActiveRecordHasManyTarget.create(:active_record_object_id => object_2.id)
|
45
|
+
assert_set_equal [has_many_target_1, has_many_target_2], ActiveRecordHasManyTarget._t_find_all_by_associate(:active_record_object_id, object_1.id)
|
45
46
|
end
|
46
47
|
|
47
48
|
should "return an empty array able to find the associates of an object" do
|
@@ -51,35 +52,24 @@ class ActiveRecordTest < Test::Unit::TestCase
|
|
51
52
|
should "be able to reload an object from the database" do
|
52
53
|
object = ActiveRecordHasOneTarget.create
|
53
54
|
object.mongo_mapper_object_id = 'abc123'
|
54
|
-
object.
|
55
|
+
object._t_reload
|
55
56
|
assert_nil object.mongo_mapper_object_id
|
56
57
|
end
|
57
58
|
|
58
|
-
should "be able to
|
59
|
-
object = ActiveRecordObject.create
|
60
|
-
|
59
|
+
should "be able to get the ids of the objects associated with the given object" do
|
60
|
+
object = ActiveRecordObject.create!
|
61
|
+
has_many_target_1 = ActiveRecordHasManyTarget.create!
|
62
|
+
has_many_target_2 = ActiveRecordHasManyTarget.create!
|
63
|
+
has_many_target_3 = ActiveRecordHasManyTarget.create!
|
64
|
+
object.mongo_mapper_has_many_targets = [has_many_target_1, has_many_target_2, has_many_target_3]
|
61
65
|
object.save
|
62
|
-
object._t_clear_associates(association)
|
63
|
-
assert_set_equal [], object._t_get_associate_ids(association)
|
64
|
-
end
|
65
66
|
|
66
|
-
|
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}")
|
70
|
-
ids = []; rows.each { |r| ids << r[0] }; ids
|
71
|
-
assert_set_equal ['abc123', 'def456', 'ghi789'], ids
|
72
|
-
end
|
73
|
-
|
74
|
-
should "be able to get the ids of the objects associated with the given object" do
|
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)
|
67
|
+
assert_set_equal [has_many_target_1.id, has_many_target_2.id, has_many_target_3.id], ActiveRecordHasManyTarget._t_find_all_ids_by_associate("active_record_object_id", object.id)
|
78
68
|
end
|
79
69
|
|
80
70
|
should "return an empty array if there are no objects associated with the given object ids" do
|
81
71
|
object = ActiveRecordObject.create
|
82
|
-
assert_set_equal [], object.
|
72
|
+
assert_set_equal [], ActiveRecordHasManyTarget._t_find_all_ids_by_associate("active_record_object_id", object.id)
|
83
73
|
end
|
84
74
|
|
85
75
|
should "be able to delete a set of objects, issuing their callbacks" do
|
@@ -106,7 +96,7 @@ class ActiveRecordTest < Test::Unit::TestCase
|
|
106
96
|
private
|
107
97
|
|
108
98
|
def association
|
109
|
-
Tenacity::Association.new(:t_has_many, :
|
99
|
+
Tenacity::Association.new(:t_has_many, :active_record_has_many_targets, ActiveRecordObject)
|
110
100
|
end
|
111
101
|
|
112
102
|
end
|
@@ -37,11 +37,12 @@ class CouchRestTest < Test::Unit::TestCase
|
|
37
37
|
end
|
38
38
|
|
39
39
|
should "be able to find all associates of an object" do
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
40
|
+
object_1 = CouchRestObject.create({})
|
41
|
+
object_2 = CouchRestObject.create({})
|
42
|
+
target_1 = CouchRestHasManyTarget.create(:couch_rest_object_id => object_1.id)
|
43
|
+
target_2 = CouchRestHasManyTarget.create(:couch_rest_object_id => object_1.id)
|
44
|
+
target_3 = CouchRestHasManyTarget.create(:couch_rest_object_id => object_2.id)
|
45
|
+
assert_set_equal [target_1, target_2], CouchRestHasManyTarget._t_find_all_by_associate(:couch_rest_object_id, object_1.id)
|
45
46
|
end
|
46
47
|
|
47
48
|
should "return an empty array if unable to find the associates of an object" do
|
@@ -57,40 +58,20 @@ class CouchRestTest < Test::Unit::TestCase
|
|
57
58
|
assert_equal "123", object["abc"]
|
58
59
|
end
|
59
60
|
|
60
|
-
should "be able to associate many objects with the given object" do
|
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
|
-
end
|
68
|
-
|
69
61
|
should "be able to get the ids of the objects associated with the given object" do
|
70
62
|
target_1 = CouchRestHasManyTarget.create({})
|
71
63
|
target_2 = CouchRestHasManyTarget.create({})
|
72
64
|
target_3 = CouchRestHasManyTarget.create({})
|
73
65
|
object = CouchRestObject.create({})
|
74
66
|
|
75
|
-
object.
|
76
|
-
|
67
|
+
object.couch_rest_has_many_targets = [target_1, target_2, target_3]
|
68
|
+
object.save
|
69
|
+
assert_set_equal [target_1.id, target_2.id, target_3.id], CouchRestHasManyTarget._t_find_all_ids_by_associate("couch_rest_object_id", object.id)
|
77
70
|
end
|
78
71
|
|
79
72
|
should "return an empty array when trying to fetch associate ids for an object with no associates" do
|
80
73
|
object = CouchRestObject.create({})
|
81
|
-
assert_equal [], object.
|
82
|
-
end
|
83
|
-
|
84
|
-
should "be able to clear the associates of an object" do
|
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)
|
74
|
+
assert_equal [], CouchRestHasManyTarget._t_find_all_ids_by_associate("couch_rest_object_id", object.id)
|
94
75
|
end
|
95
76
|
|
96
77
|
should "be able to delete a set of objects, issuing their callbacks" do
|
@@ -37,11 +37,12 @@ class DataMapperTest < Test::Unit::TestCase
|
|
37
37
|
end
|
38
38
|
|
39
39
|
should "be able to find all associates of an object" do
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
40
|
+
object_1 = DataMapperObject.create
|
41
|
+
object_2 = DataMapperObject.create
|
42
|
+
has_many_target_1 = DataMapperHasManyTarget.create(:data_mapper_object_id => object_1.id)
|
43
|
+
has_many_target_2 = DataMapperHasManyTarget.create(:data_mapper_object_id => object_1.id)
|
44
|
+
has_many_target_3 = DataMapperHasManyTarget.create(:data_mapper_object_id => object_2.id)
|
45
|
+
assert_set_equal [has_many_target_1, has_many_target_2], DataMapperHasManyTarget._t_find_all_by_associate(:data_mapper_object_id, object_1.id)
|
45
46
|
end
|
46
47
|
|
47
48
|
should "return an empty array able to find the associates of an object" do
|
@@ -50,16 +51,16 @@ class DataMapperTest < Test::Unit::TestCase
|
|
50
51
|
|
51
52
|
should "be able to reload an object from the database" do
|
52
53
|
object = DataMapperHasOneTarget.create
|
53
|
-
other_object =
|
54
|
+
other_object = DataMapperObject.create
|
54
55
|
object.mongo_mapper_object_id = other_object.id
|
55
56
|
assert_equal other_object.id.to_s, object.mongo_mapper_object_id
|
56
|
-
object.
|
57
|
+
object._t_reload
|
57
58
|
assert_nil object.mongo_mapper_object_id
|
58
59
|
end
|
59
60
|
|
60
61
|
should "return an empty array if there are no objects associated with the given object ids" do
|
61
62
|
object = DataMapperObject.create
|
62
|
-
assert_set_equal [], object.
|
63
|
+
assert_set_equal [], DataMapperHasManyTarget._t_find_all_ids_by_associate("data_mapper_object_id", object.id)
|
63
64
|
end
|
64
65
|
|
65
66
|
should "be able to delete a set of objects, issuing their callbacks" do
|
@@ -84,30 +85,16 @@ class DataMapperTest < Test::Unit::TestCase
|
|
84
85
|
|
85
86
|
context "that works with t_has_many associations" do
|
86
87
|
setup do
|
87
|
-
@has_many_target_1 =
|
88
|
-
@has_many_target_2 =
|
89
|
-
@has_many_target_3 =
|
90
|
-
end
|
91
|
-
|
92
|
-
should "be able to clear the associates of a given object" do
|
93
|
-
object = DataMapperObject.create
|
94
|
-
object._t_associate_many(association, [@has_many_target_1.id, @has_many_target_2.id, @has_many_target_3.id])
|
95
|
-
object.save
|
96
|
-
object._t_clear_associates(association)
|
97
|
-
assert_set_equal [], object._t_get_associate_ids(association)
|
98
|
-
end
|
99
|
-
|
100
|
-
should "be able to associate many objects with the given object" do
|
101
|
-
object = DataMapperObject.create
|
102
|
-
object._t_associate_many(association, [@has_many_target_1.id, @has_many_target_2.id, @has_many_target_3.id])
|
103
|
-
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}")
|
104
|
-
assert_set_equal [@has_many_target_1.id.to_s, @has_many_target_2.id.to_s, @has_many_target_3.id.to_s], rows
|
88
|
+
@has_many_target_1 = DataMapperHasManyTarget.create
|
89
|
+
@has_many_target_2 = DataMapperHasManyTarget.create
|
90
|
+
@has_many_target_3 = DataMapperHasManyTarget.create
|
105
91
|
end
|
106
92
|
|
107
93
|
should "be able to get the ids of the objects associated with the given object" do
|
108
94
|
object = DataMapperObject.create
|
109
|
-
object.
|
110
|
-
|
95
|
+
object.mongo_mapper_has_many_targets = [@has_many_target_1, @has_many_target_2, @has_many_target_3]
|
96
|
+
object.save
|
97
|
+
assert_set_equal [@has_many_target_1.id, @has_many_target_2.id, @has_many_target_3.id], DataMapperHasManyTarget._t_find_all_ids_by_associate("data_mapper_object_id", object.id)
|
111
98
|
end
|
112
99
|
end
|
113
100
|
end
|
@@ -115,7 +102,7 @@ class DataMapperTest < Test::Unit::TestCase
|
|
115
102
|
private
|
116
103
|
|
117
104
|
def association
|
118
|
-
Tenacity::Association.new(:t_has_many, :
|
105
|
+
Tenacity::Association.new(:t_has_many, :data_mapper_has_many_targets, DataMapperObject)
|
119
106
|
end
|
120
107
|
|
121
108
|
end
|
@@ -37,10 +37,12 @@ class MongoMapperTest < Test::Unit::TestCase
|
|
37
37
|
end
|
38
38
|
|
39
39
|
should "be able to find the associates of an object" do
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
40
|
+
object_1 = MongoMapperObject.create
|
41
|
+
object_2 = MongoMapperObject.create
|
42
|
+
target_1 = MongoMapperHasOneTarget.create(:mongo_mapper_object_id => object_1.id)
|
43
|
+
target_2 = MongoMapperHasOneTarget.create(:mongo_mapper_object_id => object_1.id)
|
44
|
+
target_3 = MongoMapperHasOneTarget.create(:mongo_mapper_object_id => object_2.id)
|
45
|
+
assert_set_equal [target_1, target_2], MongoMapperHasOneTarget._t_find_all_by_associate(:mongo_mapper_object_id, object_1.id)
|
44
46
|
end
|
45
47
|
|
46
48
|
should "return an empty array if the object has no associates" do
|
@@ -51,44 +53,24 @@ class MongoMapperTest < Test::Unit::TestCase
|
|
51
53
|
target = MongoMapperHasOneTarget.create
|
52
54
|
target.mongo_mapper_object_id = '101'
|
53
55
|
assert_equal '101', target.mongo_mapper_object_id
|
54
|
-
target.
|
56
|
+
target._t_reload
|
55
57
|
assert_nil target.mongo_mapper_object_id
|
56
58
|
end
|
57
59
|
|
58
|
-
should "be able to associate many objects with the given object" do
|
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, target_2.id, target_3.id], object.t_mongo_mapper_has_many_target_ids
|
65
|
-
end
|
66
|
-
|
67
60
|
should "be able to get the ids of the objects associated with the given object" do
|
68
61
|
target_1 = MongoMapperHasManyTarget.create
|
69
62
|
target_2 = MongoMapperHasManyTarget.create
|
70
63
|
target_3 = MongoMapperHasManyTarget.create
|
71
64
|
object = MongoMapperObject.create
|
65
|
+
object.mongo_mapper_has_many_targets = [target_1, target_2, target_3]
|
66
|
+
object.save
|
72
67
|
|
73
|
-
|
74
|
-
assert_set_equal [target_1.id, target_2.id, target_3.id], object._t_get_associate_ids(association)
|
68
|
+
assert_set_equal [target_1.id, target_2.id, target_3.id], MongoMapperHasManyTarget._t_find_all_ids_by_associate("mongo_mapper_object_id", object.id)
|
75
69
|
end
|
76
70
|
|
77
71
|
should "return an empty array when trying to fetch associate ids for an object with no associates" do
|
78
72
|
object = MongoMapperObject.create
|
79
|
-
assert_equal [], object.
|
80
|
-
end
|
81
|
-
|
82
|
-
should "be able to clear the associates of an object" do
|
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, target_2.id, target_3.id], object._t_get_associate_ids(association)
|
90
|
-
object._t_clear_associates(association)
|
91
|
-
assert_equal [], object._t_get_associate_ids(association)
|
73
|
+
assert_equal [], MongoMapperHasManyTarget._t_find_all_ids_by_associate("mongo_mapper_object_id", object.id)
|
92
74
|
end
|
93
75
|
|
94
76
|
should "be able to delete a set of objects, issuing their callbacks" do
|
@@ -38,10 +38,12 @@ require_mongoid do
|
|
38
38
|
end
|
39
39
|
|
40
40
|
should "be able to find the associates of an object" do
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
41
|
+
object_1 = MongoidObject.create
|
42
|
+
object_2 = MongoidObject.create
|
43
|
+
target_1 = MongoidHasOneTarget.create(:mongoid_object_id => object_1.id)
|
44
|
+
target_2 = MongoidHasOneTarget.create(:mongoid_object_id => object_1.id)
|
45
|
+
target_3 = MongoidHasOneTarget.create(:mongoid_object_id => object_2.id)
|
46
|
+
assert_set_equal [target_1, target_2], MongoidHasOneTarget._t_find_all_by_associate(:mongoid_object_id, object_1.id)
|
45
47
|
end
|
46
48
|
|
47
49
|
should "return an empty array if the object has no associates" do
|
@@ -52,44 +54,24 @@ require_mongoid do
|
|
52
54
|
target = MongoidHasOneTarget.create
|
53
55
|
target.mongoid_object_id = '101'
|
54
56
|
assert_equal '101', target.mongoid_object_id
|
55
|
-
target.
|
57
|
+
target._t_reload
|
56
58
|
assert_nil target.mongoid_object_id
|
57
59
|
end
|
58
60
|
|
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, target_2.id, target_3.id], object.t_mongoid_has_many_target_ids
|
66
|
-
end
|
67
|
-
|
68
61
|
should "be able to get the ids of the objects associated with the given object" do
|
69
62
|
target_1 = MongoidHasManyTarget.create
|
70
63
|
target_2 = MongoidHasManyTarget.create
|
71
64
|
target_3 = MongoidHasManyTarget.create
|
72
65
|
object = MongoidObject.create
|
66
|
+
object.mongoid_has_many_targets = [target_1, target_2, target_3]
|
67
|
+
object.save
|
73
68
|
|
74
|
-
|
75
|
-
assert_set_equal [target_1.id, target_2.id, target_3.id], object._t_get_associate_ids(association)
|
69
|
+
assert_set_equal [target_1.id, target_2.id, target_3.id], MongoidHasManyTarget._t_find_all_ids_by_associate("mongoid_object_id", object.id)
|
76
70
|
end
|
77
71
|
|
78
72
|
should "return an empty array when trying to fetch associate ids for an object with no associates" do
|
79
73
|
object = MongoidObject.create
|
80
|
-
assert_equal [], object.
|
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, target_2.id, target_3.id], object._t_get_associate_ids(association)
|
91
|
-
object._t_clear_associates(association)
|
92
|
-
assert_equal [], object._t_get_associate_ids(association)
|
74
|
+
assert_equal [], MongoidHasManyTarget._t_find_all_ids_by_associate("mongoid_object_id", object.id)
|
93
75
|
end
|
94
76
|
|
95
77
|
should "be able to delete a set of objects, issuing their callbacks" do
|
@@ -0,0 +1,140 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
require_ripple do
|
4
|
+
class RippleTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
context "The Ripple extension" do
|
7
|
+
setup do
|
8
|
+
setup_ripple_fixtures
|
9
|
+
end
|
10
|
+
|
11
|
+
should "be able to find the object in the database" do
|
12
|
+
object = RippleObject.create
|
13
|
+
assert_equal object, RippleObject._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 RippleObject._t_find('something')
|
18
|
+
end
|
19
|
+
|
20
|
+
should "be able to find multiple objects in the database" do
|
21
|
+
object_1 = RippleObject.create
|
22
|
+
object_2 = RippleObject.create
|
23
|
+
assert_set_equal [object_1, object_2], RippleObject._t_find_bulk([object_1.id, object_2.id, 'bogus_key'])
|
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 [], RippleObject._t_find_bulk(['bogus_key_1', 'bogus_key_2', 'bogus_key_3'])
|
28
|
+
end
|
29
|
+
|
30
|
+
should "be able to find the first associate of an object" do
|
31
|
+
object = RippleObject.create
|
32
|
+
target = RippleHasOneTarget.create(:ripple_object_id => object.id)
|
33
|
+
assert_equal target, RippleHasOneTarget._t_find_first_by_associate(:ripple_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 RippleHasOneTarget._t_find_first_by_associate(:ripple_object_id, 12345)
|
38
|
+
end
|
39
|
+
|
40
|
+
should "be able to find the associates of an object" do
|
41
|
+
object_1 = RippleObject.create
|
42
|
+
object_2 = RippleObject.create
|
43
|
+
target_1 = RippleHasOneTarget.create(:ripple_object => object_1)
|
44
|
+
target_2 = RippleHasOneTarget.create(:ripple_object => object_1)
|
45
|
+
target_3 = RippleHasOneTarget.create(:ripple_object => object_2)
|
46
|
+
assert_set_equal [target_1, target_2], RippleHasOneTarget._t_find_all_by_associate(:ripple_object_id, object_1.id)
|
47
|
+
end
|
48
|
+
|
49
|
+
should "return an empty array if the object has no associates" do
|
50
|
+
assert_equal [], RippleHasOneTarget._t_find_all_by_associate(:ripple_object_id, '1234')
|
51
|
+
end
|
52
|
+
|
53
|
+
should "be able to reload an object from the database" do
|
54
|
+
target = RippleHasOneTarget.create
|
55
|
+
target.ripple_object_id = '101'
|
56
|
+
assert_equal '101', target.ripple_object_id
|
57
|
+
target._t_reload
|
58
|
+
assert_nil target.ripple_object_id
|
59
|
+
end
|
60
|
+
|
61
|
+
should "be able to get the ids of the objects associated with the given object" do
|
62
|
+
target_1 = RippleHasManyTarget.create
|
63
|
+
target_2 = RippleHasManyTarget.create
|
64
|
+
target_3 = RippleHasManyTarget.create
|
65
|
+
object = RippleObject.create
|
66
|
+
object.ripple_has_many_targets = [target_1, target_2, target_3]
|
67
|
+
object.save
|
68
|
+
|
69
|
+
assert_set_equal [target_1.id, target_2.id, target_3.id], RippleHasManyTarget._t_find_all_ids_by_associate("ripple_object_id", object.id)
|
70
|
+
end
|
71
|
+
|
72
|
+
should "return an empty array when trying to fetch associate ids for an object with no associates" do
|
73
|
+
object = RippleObject.create
|
74
|
+
assert_equal [], RippleHasManyTarget._t_find_all_ids_by_associate("ripple_object_id", object.id)
|
75
|
+
end
|
76
|
+
|
77
|
+
should "be able to delete a set of objects, issuing their callbacks" do
|
78
|
+
object_1 = RippleObject.create
|
79
|
+
object_2 = RippleObject.create
|
80
|
+
object_3 = RippleObject.create
|
81
|
+
|
82
|
+
assert RippleObject.bucket.exist?(object_1.id)
|
83
|
+
assert RippleObject.bucket.exist?(object_2.id)
|
84
|
+
assert RippleObject.bucket.exist?(object_3.id)
|
85
|
+
RippleObject._t_delete([object_1.id, object_2.id, object_3.id])
|
86
|
+
assert !RippleObject.bucket.exist?(object_1.id)
|
87
|
+
assert !RippleObject.bucket.exist?(object_2.id)
|
88
|
+
assert !RippleObject.bucket.exist?(object_3.id)
|
89
|
+
end
|
90
|
+
|
91
|
+
should "be able to delete a setup of objects, without issuing their callbacks" do
|
92
|
+
object_1 = RippleObject.create
|
93
|
+
object_2 = RippleObject.create
|
94
|
+
object_3 = RippleObject.create
|
95
|
+
|
96
|
+
assert RippleObject.bucket.exist?(object_1.id)
|
97
|
+
assert RippleObject.bucket.exist?(object_2.id)
|
98
|
+
assert RippleObject.bucket.exist?(object_3.id)
|
99
|
+
RippleObject._t_delete([object_1.id, object_2.id, object_3.id], false)
|
100
|
+
assert !RippleObject.bucket.exist?(object_1.id)
|
101
|
+
assert !RippleObject.bucket.exist?(object_2.id)
|
102
|
+
assert !RippleObject.bucket.exist?(object_3.id)
|
103
|
+
end
|
104
|
+
|
105
|
+
should "create associate indexes when source object is saved" do
|
106
|
+
target_1 = RippleHasManyTarget.create
|
107
|
+
target_2 = RippleHasManyTarget.create
|
108
|
+
target_3 = RippleHasManyTarget.create
|
109
|
+
object = RippleObject.create
|
110
|
+
object.ripple_has_many_targets = [target_1, target_2, target_3]
|
111
|
+
object.save
|
112
|
+
|
113
|
+
bucket = ::Ripple.client.bucket('tenacity_test_ripple_has_many_target_ripple_object_id')
|
114
|
+
assert_set_equal [target_1.id, target_2.id, target_3.id], bucket.get(object.id).data
|
115
|
+
end
|
116
|
+
|
117
|
+
should "destroy associate indexes when source object is saved" do
|
118
|
+
target_1 = RippleHasManyTarget.create
|
119
|
+
target_2 = RippleHasManyTarget.create
|
120
|
+
target_3 = RippleHasManyTarget.create
|
121
|
+
object = RippleObject.create
|
122
|
+
object.ripple_has_many_targets = [target_1, target_2, target_3]
|
123
|
+
object.save
|
124
|
+
|
125
|
+
bucket = ::Ripple.client.bucket('tenacity_test_ripple_has_many_target_ripple_object_id')
|
126
|
+
assert_set_equal [target_1.id, target_2.id, target_3.id], bucket.get(object.id).data
|
127
|
+
target_2.destroy
|
128
|
+
assert_set_equal [target_1.id, target_3.id], bucket.get(object.id).data
|
129
|
+
|
130
|
+
target_1.destroy
|
131
|
+
target_3.destroy
|
132
|
+
assert_set_equal [], bucket.get(object.id).data
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
def association
|
137
|
+
Tenacity::Association.new(:t_has_many, :ripple_has_many_targets, RippleObject)
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|