dm-mongo-adapter 0.2.0.pre3 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +27 -0
- data/README.rdoc +16 -35
- data/Rakefile +6 -16
- data/VERSION.yml +2 -2
- data/dm-mongo-adapter.gemspec +100 -121
- data/lib/mongo_adapter.rb +19 -62
- data/lib/mongo_adapter/adapter.rb +49 -36
- data/lib/mongo_adapter/conditions.rb +0 -5
- data/lib/mongo_adapter/migrations.rb +17 -23
- data/lib/mongo_adapter/model.rb +3 -74
- data/lib/mongo_adapter/modifier.rb +1 -1
- data/lib/mongo_adapter/property/array.rb +10 -0
- data/lib/mongo_adapter/property/db_ref.rb +17 -0
- data/lib/mongo_adapter/property/hash.rb +27 -0
- data/lib/mongo_adapter/property/object_id.rb +47 -0
- data/lib/mongo_adapter/query.rb +42 -45
- data/lib/mongo_adapter/rails/storage.rb +15 -0
- data/lib/mongo_adapter/resource.rb +0 -130
- data/lib/mongo_adapter/support/class.rb +11 -0
- data/lib/mongo_adapter/support/date.rb +11 -0
- data/lib/mongo_adapter/support/date_time.rb +12 -0
- data/lib/mongo_adapter/support/object.rb +9 -0
- data/spec/legacy/adapter_spec.rb +9 -6
- data/spec/legacy/associations_spec.rb +37 -29
- data/spec/legacy/property_spec.rb +4 -3
- data/spec/legacy/sti_spec.rb +1 -2
- data/spec/lib/cleanup_models.rb +0 -1
- data/spec/public/aggregates_spec.rb +171 -0
- data/spec/public/model_spec.rb +8 -22
- data/spec/public/modifier_spec.rb +109 -0
- data/spec/public/properties/db_ref_spec.rb +17 -0
- data/spec/public/properties/object_id_spec.rb +15 -0
- data/spec/public/resource_spec.rb +2 -383
- data/spec/public/shared/object_id_shared_spec.rb +16 -39
- data/spec/spec_helper.rb +0 -4
- metadata +87 -117
- data/.gitignore +0 -9
- data/lib/mongo_adapter/embedded_model.rb +0 -187
- data/lib/mongo_adapter/embedded_resource.rb +0 -134
- data/lib/mongo_adapter/embedments/one_to_many.rb +0 -144
- data/lib/mongo_adapter/embedments/one_to_one.rb +0 -57
- data/lib/mongo_adapter/embedments/relationship.rb +0 -258
- data/lib/mongo_adapter/model/embedment.rb +0 -215
- data/lib/mongo_adapter/types/date.rb +0 -24
- data/lib/mongo_adapter/types/date_time.rb +0 -28
- data/lib/mongo_adapter/types/db_ref.rb +0 -65
- data/lib/mongo_adapter/types/discriminator.rb +0 -32
- data/lib/mongo_adapter/types/object_id.rb +0 -72
- data/lib/mongo_adapter/types/objects.rb +0 -31
- data/spec/legacy/embedded_resource_spec.rb +0 -157
- data/spec/legacy/embedments_spec.rb +0 -177
- data/spec/legacy/modifier_spec.rb +0 -81
- data/spec/public/embedded_collection_spec.rb +0 -61
- data/spec/public/embedded_resource_spec.rb +0 -220
- data/spec/public/model/embedment_spec.rb +0 -186
- data/spec/public/shared/model_embedments_spec.rb +0 -338
- data/spec/public/types/df_ref_spec.rb +0 -6
- data/spec/public/types/discriminator_spec.rb +0 -118
- data/spec/public/types/embedded_array_spec.rb +0 -55
- data/spec/public/types/embedded_hash_spec.rb +0 -83
- data/spec/public/types/object_id_spec.rb +0 -6
- data/spec/semipublic/embedded_model_spec.rb +0 -43
- data/spec/semipublic/model/embedment_spec.rb +0 -42
- data/spec/semipublic/resource_spec.rb +0 -70
@@ -1,157 +0,0 @@
|
|
1
|
-
require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
|
2
|
-
|
3
|
-
describe DataMapper::Mongo::EmbeddedModel do
|
4
|
-
|
5
|
-
before(:all) do
|
6
|
-
class ::User
|
7
|
-
include Resource
|
8
|
-
|
9
|
-
property :id, ObjectID
|
10
|
-
property :name, String
|
11
|
-
property :age, Integer
|
12
|
-
end
|
13
|
-
|
14
|
-
class ::Address
|
15
|
-
include EmbeddedResource
|
16
|
-
|
17
|
-
property :street, String
|
18
|
-
property :post_code, String
|
19
|
-
property :phone, String
|
20
|
-
end
|
21
|
-
|
22
|
-
class ::Car
|
23
|
-
include EmbeddedResource
|
24
|
-
|
25
|
-
property :name, String
|
26
|
-
end
|
27
|
-
|
28
|
-
User.embeds 1, :address, :model => Address
|
29
|
-
User.has User.n, :cars
|
30
|
-
end
|
31
|
-
|
32
|
-
describe "#new" do
|
33
|
-
it "should not need a key" do
|
34
|
-
lambda {
|
35
|
-
class ::Thing
|
36
|
-
include DataMapper::Mongo::EmbeddedResource
|
37
|
-
property :name, String
|
38
|
-
end
|
39
|
-
|
40
|
-
Thing.new
|
41
|
-
}.should_not raise_error
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
# @done
|
46
|
-
describe "creating new resources" do
|
47
|
-
# @done
|
48
|
-
it "should save via parent" do
|
49
|
-
user = User.new :address => Address.new(:street => 'Blank 0')
|
50
|
-
user.save.should be(true)
|
51
|
-
user.new?.should be(false)
|
52
|
-
end
|
53
|
-
|
54
|
-
# @done
|
55
|
-
it "should create via parent" do
|
56
|
-
user = User.create(:address => Address.new(:street => 'Blank 0'))
|
57
|
-
user.new?.should be(false)
|
58
|
-
end
|
59
|
-
|
60
|
-
# @done
|
61
|
-
it "should save an embedded resource" do
|
62
|
-
user = User.new :address => Address.new(:street => 'Blank 0')
|
63
|
-
user.address.save.should be(true)
|
64
|
-
user.new?.should be(false)
|
65
|
-
user.address.new?.should be(false)
|
66
|
-
end
|
67
|
-
|
68
|
-
# @done
|
69
|
-
it "should not allow to create an embedded resource without a parent" do
|
70
|
-
address = Address.new(:street => 'Blank 0')
|
71
|
-
lambda { address.save }.should raise_error(EmbeddedResource::MissingParentError)
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
# @done
|
76
|
-
describe "updating resources" do
|
77
|
-
before :all do
|
78
|
-
@user = User.create(:name => 'john', :address => Address.new)
|
79
|
-
end
|
80
|
-
|
81
|
-
# @done
|
82
|
-
it "should update embedded resource" do
|
83
|
-
@user.update(:address => { :street => 'Something 1' }).should be(true)
|
84
|
-
|
85
|
-
@user.reload
|
86
|
-
|
87
|
-
@user.address.street.should_not be_nil
|
88
|
-
@user.address.street.should eql('Something 1')
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
describe "#dirty?" do
|
93
|
-
before :all do
|
94
|
-
@user = User.new
|
95
|
-
end
|
96
|
-
|
97
|
-
# @done
|
98
|
-
it "should return false when new" do
|
99
|
-
address = Address.new
|
100
|
-
address.dirty?.should be(false)
|
101
|
-
end
|
102
|
-
|
103
|
-
# @done
|
104
|
-
it "should return true if changed" do
|
105
|
-
address = Address.new(:street => "Some Street 1234")
|
106
|
-
address.dirty?.should be(true)
|
107
|
-
end
|
108
|
-
|
109
|
-
# @done
|
110
|
-
it "should return false for a clean parent" do
|
111
|
-
@user.dirty?.should be(false)
|
112
|
-
end
|
113
|
-
|
114
|
-
# @done
|
115
|
-
it "should return true with one-to-one" do
|
116
|
-
@user.address = Address.new(:street => 'Some Street 1234')
|
117
|
-
@user.dirty?.should be(true)
|
118
|
-
@user.address.dirty?.should be(true)
|
119
|
-
end
|
120
|
-
|
121
|
-
it "should include embedded resource attributes in the dirty attributes list" do
|
122
|
-
@user.address = Address.new
|
123
|
-
@user.address.street = 'Some Street 1234'
|
124
|
-
|
125
|
-
dirty_attributes = @user.dirty_attributes
|
126
|
-
|
127
|
-
dirty_attributes.should_not be_nil
|
128
|
-
|
129
|
-
address_dirty_attrs = dirty_attributes[@user.model.embedments[:address]]
|
130
|
-
|
131
|
-
address_dirty_attrs.should_not be_nil
|
132
|
-
address_dirty_attrs.keys.size.should eql(1)
|
133
|
-
address_dirty_attrs.values.include?('Some Street 1234').should be(true)
|
134
|
-
end
|
135
|
-
|
136
|
-
describe "with saved resource" do
|
137
|
-
before :all do
|
138
|
-
@user.name = 'john'
|
139
|
-
@user.address = Address.new(:street => 'A Street 101')
|
140
|
-
@user.save
|
141
|
-
end
|
142
|
-
|
143
|
-
# @done
|
144
|
-
it "should return true with one-to-many" do
|
145
|
-
@user.cars << Car.new
|
146
|
-
@user.dirty?.should be(true)
|
147
|
-
end
|
148
|
-
|
149
|
-
# @done
|
150
|
-
it "should return false with a loaded resource" do
|
151
|
-
user = User.get(@user.id)
|
152
|
-
user.dirty_embedments?.should be(false)
|
153
|
-
user.dirty?.should be(false)
|
154
|
-
end
|
155
|
-
end
|
156
|
-
end
|
157
|
-
end
|
@@ -1,177 +0,0 @@
|
|
1
|
-
require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
|
2
|
-
|
3
|
-
describe DataMapper::Mongo::Model::Embedment do
|
4
|
-
before :all do
|
5
|
-
# let's start with an empty collection
|
6
|
-
$db.drop_collection('users')
|
7
|
-
|
8
|
-
class ::User
|
9
|
-
include Resource
|
10
|
-
|
11
|
-
property :id, ObjectID
|
12
|
-
property :name, String
|
13
|
-
property :age, Integer
|
14
|
-
end
|
15
|
-
|
16
|
-
class ::Address
|
17
|
-
include EmbeddedResource
|
18
|
-
|
19
|
-
property :street, String
|
20
|
-
property :post_code, String
|
21
|
-
property :phone, String
|
22
|
-
end
|
23
|
-
|
24
|
-
class ::Car
|
25
|
-
include EmbeddedResource
|
26
|
-
|
27
|
-
property :name, String
|
28
|
-
end
|
29
|
-
|
30
|
-
@user_attributes = {:name => 'piotr', :age => 26, :address => {:street => '1st ave', :post_code => '123-45'}}
|
31
|
-
end
|
32
|
-
|
33
|
-
describe "Resource" do
|
34
|
-
# @done
|
35
|
-
it "should respond to #embeds" do
|
36
|
-
User.should respond_to(:embeds)
|
37
|
-
end
|
38
|
-
|
39
|
-
it "should respond to #embedded_in" do
|
40
|
-
User.should respond_to(:embedded_in)
|
41
|
-
end
|
42
|
-
|
43
|
-
# @done
|
44
|
-
it "should respond to #embedments" do
|
45
|
-
User.should respond_to(:embedments)
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
describe "#embeds" do
|
50
|
-
describe "One-To-One Relationship" do
|
51
|
-
before :all do
|
52
|
-
User.embeds(1, :address, :model => Address)
|
53
|
-
end
|
54
|
-
|
55
|
-
describe "#dirty?" do
|
56
|
-
# @done
|
57
|
-
it "should return false without dirty attributes and without an embedded resource" do
|
58
|
-
u = User.new
|
59
|
-
u.dirty?.should be_false
|
60
|
-
end
|
61
|
-
|
62
|
-
# @done
|
63
|
-
it "should return true with a dirty attributes and with an embedded resource" do
|
64
|
-
u = User.new(@user_attributes.except(:address))
|
65
|
-
u.dirty?.should be_true
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
# @done
|
70
|
-
it "should create a new embedment" do
|
71
|
-
User.embedments[:address].class.should be(Embedments::OneToOne::Relationship)
|
72
|
-
end
|
73
|
-
|
74
|
-
# @done
|
75
|
-
it "should create readers and writers for the embedded resource" do
|
76
|
-
user = User.new
|
77
|
-
|
78
|
-
user.should respond_to("address")
|
79
|
-
user.should respond_to("address=")
|
80
|
-
end
|
81
|
-
|
82
|
-
# @done
|
83
|
-
it "should not require embedded resource to save the parent" do
|
84
|
-
user = User.new(@user_attributes.except(:address))
|
85
|
-
user.save.should be_true
|
86
|
-
end
|
87
|
-
|
88
|
-
# @done
|
89
|
-
it "should set the embedded resource" do
|
90
|
-
user = User.new
|
91
|
-
address = Address.new
|
92
|
-
|
93
|
-
user.address = address
|
94
|
-
user.address.should be(address)
|
95
|
-
|
96
|
-
address.parent.should be(user)
|
97
|
-
end
|
98
|
-
|
99
|
-
# @done
|
100
|
-
it "should save the embedded resource" do
|
101
|
-
user = User.new(@user_attributes)
|
102
|
-
user.save.should be(true)
|
103
|
-
user.address.new?.should be(false)
|
104
|
-
end
|
105
|
-
|
106
|
-
# @done
|
107
|
-
it "should load parent and the embedded resource" do
|
108
|
-
_id = $db.collection('users').insert(@user_attributes)
|
109
|
-
|
110
|
-
user = User.get(_id)
|
111
|
-
|
112
|
-
user.address.should_not be_nil
|
113
|
-
end
|
114
|
-
|
115
|
-
# @done
|
116
|
-
#
|
117
|
-
# Spec now fails since a OneToOne relationship which hasn't been set
|
118
|
-
# should return nil.
|
119
|
-
#
|
120
|
-
# it "should load parent if the embedded resource is nil" do
|
121
|
-
# _id = $db.collection('users').insert(:name => 'john')
|
122
|
-
#
|
123
|
-
# user = User.get(_id)
|
124
|
-
# user.address.should_not be_nil
|
125
|
-
# end
|
126
|
-
end
|
127
|
-
|
128
|
-
describe "One-to-Many Relationship" do
|
129
|
-
before :all do
|
130
|
-
User.embeds User.n, :cars
|
131
|
-
end
|
132
|
-
|
133
|
-
it "should create a new embedment" do
|
134
|
-
User.embedments[:cars].class.should be(Embedments::OneToMany::Relationship)
|
135
|
-
end
|
136
|
-
|
137
|
-
it "should create readers and writers for the embedded resource" do
|
138
|
-
user = User.new
|
139
|
-
|
140
|
-
user.should respond_to("cars")
|
141
|
-
user.should respond_to("cars=")
|
142
|
-
end
|
143
|
-
|
144
|
-
it "should set the embedded collection" do
|
145
|
-
user = User.new
|
146
|
-
|
147
|
-
3.times { user.cars << Car.new }
|
148
|
-
|
149
|
-
user.cars.size.should eql(3)
|
150
|
-
user.cars.all?{ |c| c.parent == user }.should be(true)
|
151
|
-
end
|
152
|
-
|
153
|
-
it "should save the embedded resources" do
|
154
|
-
user = User.new @user_attributes.except(:address)
|
155
|
-
|
156
|
-
['ford', 'honda', 'volvo'].each { |name| user.cars << Car.new(:name => name) }
|
157
|
-
|
158
|
-
user.save.should be(true)
|
159
|
-
user.cars.all? { |car| car.saved? }.should be(true)
|
160
|
-
end
|
161
|
-
|
162
|
-
it "should load parent with its embedded collection" do
|
163
|
-
_id = $db.collection('users').insert(
|
164
|
-
"name"=>"piotr", "cars"=>[{"name"=>"ford"}, {"name"=>"honda"}, {"name"=>"volvo"}], "age"=>26)
|
165
|
-
|
166
|
-
user = User.get(_id)
|
167
|
-
|
168
|
-
user.cars.should_not be_nil
|
169
|
-
user.cars.size.should eql(3)
|
170
|
-
end
|
171
|
-
end
|
172
|
-
|
173
|
-
describe "Many-To-One Relationship" do
|
174
|
-
it "should be implemented"
|
175
|
-
end
|
176
|
-
end
|
177
|
-
end
|
@@ -1,81 +0,0 @@
|
|
1
|
-
require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
|
2
|
-
|
3
|
-
# @done
|
4
|
-
describe "updates with mongodb modifiers" do
|
5
|
-
# @done
|
6
|
-
before :all do
|
7
|
-
$db.drop_collection("posts")
|
8
|
-
class ::Post
|
9
|
-
include DataMapper::Mongo::Resource
|
10
|
-
property :id, ObjectID
|
11
|
-
property :comment_count, Integer
|
12
|
-
property :body, Text
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
# @done
|
17
|
-
it "should increment" do
|
18
|
-
post = Post.create(:comment_count => 1)
|
19
|
-
post.increment(:comment_count, 1)
|
20
|
-
post.comment_count.should == 2
|
21
|
-
Post.get(post.id).comment_count.should == 2
|
22
|
-
end
|
23
|
-
|
24
|
-
# @done
|
25
|
-
it "should decrement" do
|
26
|
-
post = Post.create(:comment_count => 10)
|
27
|
-
post.decrement(:comment_count, 5)
|
28
|
-
post.comment_count.should == 5
|
29
|
-
Post.get(post.id).comment_count.should == 5
|
30
|
-
end
|
31
|
-
|
32
|
-
# @done
|
33
|
-
it "should set" do
|
34
|
-
# post = Post.create(:body => "This needs to be edited", :comment_count => 2)
|
35
|
-
# post.set(:body => "This was edited", :comment_count => 3)
|
36
|
-
# post.body.should == "This was edited"
|
37
|
-
# post.comment_count.should == 3
|
38
|
-
# fresh_post = Post.get(post.id)
|
39
|
-
# fresh_post.body.should == "This was edited"
|
40
|
-
# fresh_post.comment_count.should == 3
|
41
|
-
pending
|
42
|
-
end
|
43
|
-
|
44
|
-
# @done
|
45
|
-
it "should unset" do
|
46
|
-
#post = Post.create(:body => "This needs to be removed", :comment_count => 2)
|
47
|
-
#post.unset(:body, :comment_count)
|
48
|
-
#post.body.should be_nil
|
49
|
-
#post.comment_count.should be_nil
|
50
|
-
#fresh_post = Post.get(post.id)
|
51
|
-
#fresh_post.body.should be_nil
|
52
|
-
#fresh_post.comment_count.should be_nil
|
53
|
-
pending
|
54
|
-
end
|
55
|
-
|
56
|
-
# @done
|
57
|
-
it "should push" do
|
58
|
-
pending
|
59
|
-
end
|
60
|
-
|
61
|
-
# @done
|
62
|
-
it "should push_all" do
|
63
|
-
pending
|
64
|
-
end
|
65
|
-
|
66
|
-
# @done
|
67
|
-
it "should pop" do
|
68
|
-
pending
|
69
|
-
end
|
70
|
-
|
71
|
-
# @done
|
72
|
-
it "should pull" do
|
73
|
-
pending
|
74
|
-
end
|
75
|
-
|
76
|
-
# @done
|
77
|
-
it "should pull_all" do
|
78
|
-
pending
|
79
|
-
end
|
80
|
-
|
81
|
-
end
|
@@ -1,61 +0,0 @@
|
|
1
|
-
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
|
2
|
-
|
3
|
-
describe DataMapper::Mongo::EmbeddedResource do
|
4
|
-
before :all do
|
5
|
-
class ::Student
|
6
|
-
include DataMapper::Mongo::Resource
|
7
|
-
property :id, ObjectID
|
8
|
-
property :name, String
|
9
|
-
|
10
|
-
embeds n, :scores
|
11
|
-
end
|
12
|
-
|
13
|
-
class ::Score
|
14
|
-
include DataMapper::Mongo::EmbeddedResource
|
15
|
-
property :value, Float
|
16
|
-
property :course, String
|
17
|
-
end
|
18
|
-
|
19
|
-
Student.all.destroy!
|
20
|
-
|
21
|
-
@student = Student.new
|
22
|
-
end
|
23
|
-
|
24
|
-
describe "#new" do
|
25
|
-
it "should create a new instance and add it to the collection" do
|
26
|
-
score = @student.scores.new
|
27
|
-
score.should be_kind_of(Score)
|
28
|
-
score.parent.should be(@student)
|
29
|
-
end
|
30
|
-
|
31
|
-
it "should set a new instance with attributes" do
|
32
|
-
attrs = { :value => 5.0, :course => 'MongoDB' }
|
33
|
-
score = @student.scores.new(attrs)
|
34
|
-
score.attributes.should == attrs
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
describe "#dirty?" do
|
39
|
-
it "should return true when includes dirty resources" do
|
40
|
-
@student.scores << Score.new(:value => 6.0)
|
41
|
-
@student.scores.dirty?.should be(true)
|
42
|
-
end
|
43
|
-
|
44
|
-
it "should return false when it doesn't include dirty resources" do
|
45
|
-
@student.scores << Score.new
|
46
|
-
@student.scores.dirty?.should be(true)
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
describe "#save" do
|
51
|
-
it "should call save on parent" do
|
52
|
-
expected = Score.new(:value => 7.0)
|
53
|
-
|
54
|
-
@student.scores << expected
|
55
|
-
@student.scores.save.should be(true)
|
56
|
-
@student.clean?.should be(true)
|
57
|
-
|
58
|
-
@student.reload.scores.should == [expected]
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|