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.
Files changed (64) hide show
  1. data/Gemfile +27 -0
  2. data/README.rdoc +16 -35
  3. data/Rakefile +6 -16
  4. data/VERSION.yml +2 -2
  5. data/dm-mongo-adapter.gemspec +100 -121
  6. data/lib/mongo_adapter.rb +19 -62
  7. data/lib/mongo_adapter/adapter.rb +49 -36
  8. data/lib/mongo_adapter/conditions.rb +0 -5
  9. data/lib/mongo_adapter/migrations.rb +17 -23
  10. data/lib/mongo_adapter/model.rb +3 -74
  11. data/lib/mongo_adapter/modifier.rb +1 -1
  12. data/lib/mongo_adapter/property/array.rb +10 -0
  13. data/lib/mongo_adapter/property/db_ref.rb +17 -0
  14. data/lib/mongo_adapter/property/hash.rb +27 -0
  15. data/lib/mongo_adapter/property/object_id.rb +47 -0
  16. data/lib/mongo_adapter/query.rb +42 -45
  17. data/lib/mongo_adapter/rails/storage.rb +15 -0
  18. data/lib/mongo_adapter/resource.rb +0 -130
  19. data/lib/mongo_adapter/support/class.rb +11 -0
  20. data/lib/mongo_adapter/support/date.rb +11 -0
  21. data/lib/mongo_adapter/support/date_time.rb +12 -0
  22. data/lib/mongo_adapter/support/object.rb +9 -0
  23. data/spec/legacy/adapter_spec.rb +9 -6
  24. data/spec/legacy/associations_spec.rb +37 -29
  25. data/spec/legacy/property_spec.rb +4 -3
  26. data/spec/legacy/sti_spec.rb +1 -2
  27. data/spec/lib/cleanup_models.rb +0 -1
  28. data/spec/public/aggregates_spec.rb +171 -0
  29. data/spec/public/model_spec.rb +8 -22
  30. data/spec/public/modifier_spec.rb +109 -0
  31. data/spec/public/properties/db_ref_spec.rb +17 -0
  32. data/spec/public/properties/object_id_spec.rb +15 -0
  33. data/spec/public/resource_spec.rb +2 -383
  34. data/spec/public/shared/object_id_shared_spec.rb +16 -39
  35. data/spec/spec_helper.rb +0 -4
  36. metadata +87 -117
  37. data/.gitignore +0 -9
  38. data/lib/mongo_adapter/embedded_model.rb +0 -187
  39. data/lib/mongo_adapter/embedded_resource.rb +0 -134
  40. data/lib/mongo_adapter/embedments/one_to_many.rb +0 -144
  41. data/lib/mongo_adapter/embedments/one_to_one.rb +0 -57
  42. data/lib/mongo_adapter/embedments/relationship.rb +0 -258
  43. data/lib/mongo_adapter/model/embedment.rb +0 -215
  44. data/lib/mongo_adapter/types/date.rb +0 -24
  45. data/lib/mongo_adapter/types/date_time.rb +0 -28
  46. data/lib/mongo_adapter/types/db_ref.rb +0 -65
  47. data/lib/mongo_adapter/types/discriminator.rb +0 -32
  48. data/lib/mongo_adapter/types/object_id.rb +0 -72
  49. data/lib/mongo_adapter/types/objects.rb +0 -31
  50. data/spec/legacy/embedded_resource_spec.rb +0 -157
  51. data/spec/legacy/embedments_spec.rb +0 -177
  52. data/spec/legacy/modifier_spec.rb +0 -81
  53. data/spec/public/embedded_collection_spec.rb +0 -61
  54. data/spec/public/embedded_resource_spec.rb +0 -220
  55. data/spec/public/model/embedment_spec.rb +0 -186
  56. data/spec/public/shared/model_embedments_spec.rb +0 -338
  57. data/spec/public/types/df_ref_spec.rb +0 -6
  58. data/spec/public/types/discriminator_spec.rb +0 -118
  59. data/spec/public/types/embedded_array_spec.rb +0 -55
  60. data/spec/public/types/embedded_hash_spec.rb +0 -83
  61. data/spec/public/types/object_id_spec.rb +0 -6
  62. data/spec/semipublic/embedded_model_spec.rb +0 -43
  63. data/spec/semipublic/model/embedment_spec.rb +0 -42
  64. data/spec/semipublic/resource_spec.rb +0 -70
@@ -1,37 +1,23 @@
1
1
  require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
2
2
 
3
3
  describe DataMapper::Mongo::Model do
4
-
5
4
  before(:all) do
6
5
  class ::PropertyTest
7
6
  include DataMapper::Mongo::Resource
8
- property :array, Array
9
- property :hash, Hash
10
- property :date, Date
11
- property :date_time, DateTime
7
+ property :array_attr, Array
8
+ property :hash_attr, Hash
12
9
  end
13
10
  end
14
11
 
15
12
  describe '#property' do
16
- it 'should cast Array types to EmbeddedArray' do
17
- prop = PropertyTest.properties[:array]
18
- prop.type.should == DataMapper::Mongo::Types::EmbeddedArray
19
- end
20
-
21
- it 'should cast Hash types to EmbeddedHash' do
22
- prop = PropertyTest.properties[:hash]
23
- prop.type.should == DataMapper::Mongo::Types::EmbeddedHash
13
+ it 'should cast Array class to DataMapper::Mongo::Property::Array' do
14
+ prop = PropertyTest.properties[:array_attr]
15
+ prop.should be_kind_of(DataMapper::Mongo::Property::Array)
24
16
  end
25
17
 
26
- it 'should cast Hash types to Types::Date' do
27
- prop = PropertyTest.properties[:date]
28
- prop.type.should == DataMapper::Mongo::Types::Date
29
- end
30
-
31
- it 'should cast Hash types to Types::DateTime' do
32
- prop = PropertyTest.properties[:date_time]
33
- prop.type.should == DataMapper::Mongo::Types::DateTime
18
+ it 'should cast Hash class to DataMapper::Mongo::Property::Hash' do
19
+ prop = PropertyTest.properties[:hash_attr]
20
+ prop.should be_kind_of(DataMapper::Mongo::Property::Hash)
34
21
  end
35
22
  end
36
-
37
23
  end
@@ -0,0 +1,109 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
2
+
3
+ describe DataMapper::Mongo::Resource do
4
+ before :all do
5
+ class ::Post
6
+ include DataMapper::Mongo::Resource
7
+
8
+ property :id, ObjectId
9
+ property :comment_count, Integer
10
+ property :body, Text
11
+ end
12
+
13
+ Post.all.destroy!
14
+ end
15
+
16
+ describe "#increment" do
17
+ before :all do
18
+ @post = Post.create(:comment_count => 1)
19
+ #@post.increment(:comment_count, 1)
20
+ end
21
+
22
+ it "should update the given property with the incremented value" do
23
+ pending "not implemented yet" do
24
+ @post.comment_count.should == 2
25
+ Post.get(@post.id).comment_count.should == 2
26
+ end
27
+ end
28
+
29
+ it "should reload the updated resource" do
30
+ @post.dirty?.should be_false
31
+ end
32
+ end
33
+
34
+ describe "#decrement" do
35
+ before :all do
36
+ @post = Post.create(:comment_count => 10)
37
+ #@post.decrement(:comment_count, 5)
38
+ end
39
+
40
+ it "should update the given property with the decremented value" do
41
+ pending "not implemented yet" do
42
+ @post.comment_count.should == 5
43
+ Post.get(@post.id).comment_count.should == 5
44
+ end
45
+ end
46
+
47
+ it "should reload the updated resource" do
48
+ @post.dirty?.should be_false
49
+ end
50
+ end
51
+
52
+ describe "#set" do
53
+ it "should set the value of a property" do
54
+ post = Post.create(:body => "This needs to be edited", :comment_count => 2)
55
+
56
+ post.set(:body => "This was edited", :comment_count => 3)
57
+ post.body.should == "This was edited"
58
+ post.comment_count.should == 3
59
+ fresh_post = Post.get(post.id)
60
+ fresh_post.body.should == "This was edited"
61
+ fresh_post.comment_count.should == 3
62
+ end
63
+ end
64
+
65
+ describe "#unset" do
66
+ it "should unset the value of a property" do
67
+ post = Post.create(:body => "This needs to be removed", :comment_count => 2)
68
+
69
+ pending "not implemented yet" do
70
+ post.unset(:body, :comment_count)
71
+ post.body.should be_nil
72
+ post.comment_count.should be_nil
73
+ fresh_post = Post.get(post.id)
74
+ fresh_post.body.should be_nil
75
+ fresh_post.comment_count.should be_nil
76
+ end
77
+ end
78
+ end
79
+
80
+ describe "#push" do
81
+ it "should be implemented" do
82
+ pending
83
+ end
84
+ end
85
+
86
+ describe "#push_all" do
87
+ it "should be implemented" do
88
+ pending
89
+ end
90
+ end
91
+
92
+ describe "#pop" do
93
+ it "should be implemented" do
94
+ pending
95
+ end
96
+ end
97
+
98
+ describe "#pull" do
99
+ it "should be implemented" do
100
+ pending
101
+ end
102
+ end
103
+
104
+ describe "#pull_all" do
105
+ it "should be implemented" do
106
+ pending
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,17 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper'))
2
+
3
+ describe DataMapper::Mongo::Property::DBRef do
4
+ before(:all) do
5
+ class User
6
+ include DataMapper::Mongo::Resource
7
+
8
+ property :id, ObjectId
9
+ property :group_id, DBRef
10
+ end
11
+
12
+ @property_class = DataMapper::Mongo::Property::DBRef
13
+ @property = User.properties[:group_id]
14
+ end
15
+
16
+ it_should_behave_like 'An ObjectId Type'
17
+ end
@@ -0,0 +1,15 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper'))
2
+
3
+ describe DataMapper::Mongo::Property::ObjectId do
4
+ before(:all) do
5
+ class User
6
+ include DataMapper::Mongo::Resource
7
+ property :id, ObjectId
8
+ end
9
+
10
+ @property_class = DataMapper::Mongo::Property::ObjectId
11
+ @property = User.properties[:id]
12
+ end
13
+
14
+ it_should_behave_like 'An ObjectId Type'
15
+ end
@@ -3,26 +3,14 @@ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
3
3
  describe DataMapper::Mongo::Resource do
4
4
 
5
5
  before(:all) do
6
- class ::Address
7
- include DataMapper::Mongo::EmbeddedResource
8
- property :city, String
9
- property :state, String
10
- end
11
-
12
- class ::Location < ::Address
13
- property :country, String
14
- property :continent, String
15
- end
16
-
17
6
  class ::User
18
7
  include DataMapper::Mongo::Resource
19
- property :id, ObjectID
8
+
9
+ property :id, ObjectId
20
10
  property :name, String
21
11
  property :tags, Array
22
12
  property :metadata, Hash
23
13
  property :created_at, DateTime
24
- embeds 1, :address, :model => Address
25
- embeds n, :locations
26
14
  end
27
15
  end
28
16
 
@@ -69,55 +57,6 @@ describe DataMapper::Mongo::Resource do
69
57
  expected = User.create(:name => 'Two')
70
58
  User.all(:name => 'Two').should == [expected]
71
59
  end
72
-
73
- describe 'including conditions for an embedded has 1 resource' do
74
- before(:all) do
75
- User.all.destroy!
76
- User.create(:name => 'Boston guy', :address => { :city => 'Boston' })
77
- @expected = User.create(:name => 'NY guy', :address => { :city => 'New York' })
78
- end
79
-
80
- it 'should return a collection' do
81
- collection = User.all('address.city' => 'Washington')
82
- collection.should be_kind_of(DataMapper::Collection)
83
- end
84
-
85
- it 'should return an empty collection when there are no matching resources' do
86
- collection = User.all('address.city' => 'Washington')
87
- collection.should be_empty
88
- end
89
-
90
- it 'should return specific resources' do
91
- User.all('address.city' => 'New York').should == [@expected]
92
- end
93
- end # including conditions for an embedded has 1 resource
94
-
95
- describe 'including conditions for an embedded has n resource' do
96
- before(:all) do
97
- User.all.destroy!
98
- User.create(:name => 'Canada Guy', :locations => [{ :country => 'Canada' }])
99
-
100
- @expected = User.create(:name => 'John Kowalski', :locations => [
101
- { :country => 'US', :continent => 'North America' },
102
- { :country => 'Poland', :continent => 'Europe' }])
103
- end
104
-
105
- it 'should return a collection' do
106
- collection = User.all('locations.country' => 'Canada')
107
- collection.should be_kind_of(DataMapper::Collection)
108
- end
109
-
110
- it 'should return an empty collection when there are no matching resources' do
111
- collection = User.all('locations.country' => 'Brazil')
112
- collection.should be_empty
113
- end
114
-
115
- it 'should return specific resources' do
116
- User.all('locations.country' => 'US').should == [@expected]
117
- User.all('locations.country' => 'Poland', 'locations.continent' => 'Europe').should == [@expected]
118
- end
119
- end # including conditions for an embedded has n resource
120
-
121
60
  end
122
61
  end
123
62
 
@@ -154,195 +93,6 @@ describe DataMapper::Mongo::Resource do
154
93
  end
155
94
  end
156
95
 
157
- #
158
- # aggregations
159
- #
160
- describe 'aggregations' do
161
- before(:all) do
162
- class ::Student
163
- include DataMapper::Mongo::Resource
164
-
165
- property :id, ObjectID
166
- property :name, String
167
- property :school, String
168
- property :score, Float
169
- end
170
-
171
- Student.all.destroy!
172
-
173
- @student_one = Student.create(:school => 'School 1', :name => 'One', :score => 3.0)
174
- @student_two = Student.create(:school => 'School 2', :name => 'Two', :score => 3.5)
175
- @student_three = Student.create(:school => 'School 2', :name => 'Three', :score => 4.5)
176
- end
177
-
178
- #
179
- # count
180
- #
181
- describe "#count" do
182
- describe 'with no query' do
183
- it 'should return number of all resources' do
184
- Student.count.should == 3
185
- end
186
- end
187
-
188
- describe 'with a query' do
189
- it 'should return number of resources matching conditions' do
190
- Student.count(:name => /one|two/i).should == 2
191
- end
192
- end
193
- end
194
-
195
- #
196
- # aggregate
197
- #
198
-
199
- describe "#aggregate" do
200
-
201
- #
202
- # simple aggregation without any operators
203
- #
204
-
205
- describe "without operators" do
206
- describe "without conditions" do
207
- it "should return array of hashes based on all records" do
208
- result = Student.aggregate(:school, :score)
209
-
210
- result.should == [
211
- {:school => "School 1", :score => 3.0},
212
- {:school => "School 2", :score => 3.5},
213
- {:school => "School 2", :score => 4.5}]
214
- end
215
- end
216
-
217
- describe "with conditions" do
218
- it "should return array of hashes based on records that match conditions" do
219
- result = Student.aggregate(:school, :score, :score.gt => 3.0)
220
-
221
- result.should == [
222
- {:school => "School 2", :score => 3.5},
223
- {:school => "School 2", :score => 4.5}]
224
- end
225
- end
226
- end
227
-
228
- #
229
- # count
230
- #
231
-
232
- describe "count operator" do
233
- describe "without conditions" do
234
- it "should get correct results based on all records" do
235
- result = Student.aggregate(:school, :score.count)
236
-
237
- result.size.should == 2
238
-
239
- school_1, school_2 = result
240
-
241
- school_1[:school].should == 'School 1'
242
- school_2[:school].should == 'School 2'
243
-
244
- school_1[:score].should == 1
245
- school_2[:score].should == 2
246
- end
247
- end
248
-
249
- describe "with conditions" do
250
- it "should get correct results based on records that match conditions" do
251
- result = Student.aggregate(:school, :score.count, :name => /two|three/i)
252
-
253
- result.size.should == 1
254
- result.first[:score].should == 2
255
- result.first[:school].should == 'School 2'
256
- end
257
- end
258
- end
259
-
260
- #
261
- # avg
262
- #
263
- # TODO: add spec for #avg with conditions
264
-
265
- describe "avg operator" do
266
- describe 'without conditions' do
267
- it 'should return an avarage value of the given field' do
268
- result = Student.aggregate(:school, :score.avg)
269
-
270
- school_1, school_2 = result
271
-
272
- school_1[:school].should == 'School 1'
273
- school_2[:school].should == 'School 2'
274
-
275
- school_1[:score].should == 3.0
276
- school_2[:score].should == 4.0
277
- end
278
- end
279
- end
280
-
281
- #
282
- # min
283
- #
284
- # TODO: add spec for #min with conditions
285
-
286
- describe "min operator" do
287
- describe 'without conditions' do
288
- it 'should return the minimum value of the given field' do
289
- result = Student.aggregate(:school, :score.min)
290
-
291
- school_1, school_2 = result
292
-
293
- school_1[:school].should == 'School 1'
294
- school_2[:school].should == 'School 2'
295
-
296
- school_1[:score].should == 3.0
297
- school_2[:score].should == 3.5
298
- end
299
- end
300
- end
301
-
302
- #
303
- # max
304
- #
305
- # TODO: add spec for #max with conditions
306
-
307
- describe "max operator" do
308
- describe 'without conditions' do
309
- it 'should return the maximum value of the given field' do
310
- result = Student.aggregate(:school, :score.max)
311
-
312
- school_1, school_2 = result
313
-
314
- school_1[:school].should == 'School 1'
315
- school_2[:school].should == 'School 2'
316
-
317
- school_1[:score].should == 3.0
318
- school_2[:score].should == 4.5
319
- end
320
- end
321
- end
322
-
323
- #
324
- # max
325
- #
326
- # TODO: add spec for #sum with conditions
327
-
328
- describe "sum operator" do
329
- describe 'without conditions' do
330
- it 'should return the maximum value of the given field' do
331
- result = Student.aggregate(:school, :score.sum)
332
-
333
- school_1, school_2 = result
334
-
335
- school_1[:school].should == 'School 1'
336
- school_2[:school].should == 'School 2'
337
-
338
- school_1[:score].should == 3.0
339
- school_2[:score].should == 8.0
340
- end
341
- end
342
- end
343
- end
344
- end
345
-
346
96
  #
347
97
  # dirty?
348
98
  #
@@ -353,32 +103,6 @@ describe DataMapper::Mongo::Resource do
353
103
  User.new(:name => 'Mongo').should be_dirty
354
104
  end
355
105
  end
356
-
357
- describe 'when the resource has no changes' do
358
- it 'should return true if a one-to-one embedment has a change' do
359
- user = User.new(:address => Address.new(:city => 'Rock Ridge'))
360
- user.should be_dirty
361
- end
362
-
363
- it 'should return false having just been saved' do
364
- user = User.new(:address => Address.new(:city => 'Rock Ridge'))
365
- user.save
366
- user.should_not be_dirty
367
- end
368
-
369
- it 'should return true if a one-to-many embedment has a change' do
370
- user = User.new
371
- user.locations << Address.new(:city => 'Rock Ridge')
372
- user.should be_dirty
373
- end
374
-
375
- it 'should return false if no embedments have changes' do
376
- user = User.new(:address => Address.new(:city => 'Rock Ridge'))
377
- user.locations << Address.new(:city => 'Rock Ridge')
378
- user.save
379
- user.should_not be_dirty
380
- end
381
- end
382
106
  end
383
107
 
384
108
  #
@@ -455,109 +179,4 @@ describe DataMapper::Mongo::Resource do
455
179
  end
456
180
  end
457
181
 
458
- describe "Modifier" do
459
- before :all do
460
- class ::Post
461
- include DataMapper::Mongo::Resource
462
-
463
- property :id, ObjectID
464
- property :comment_count, Integer
465
- property :body, Text
466
- end
467
-
468
- Post.all.destroy!
469
- end
470
-
471
- describe "#increment" do
472
- before :all do
473
- @post = Post.create(:comment_count => 1)
474
- @post.increment(:comment_count, 1)
475
- end
476
-
477
- it "should update the given property with the incremented value" do
478
- @post.comment_count.should == 2
479
- Post.get(@post.id).comment_count.should == 2
480
- end
481
-
482
- it "should reload the updated resource" do
483
- @post.dirty?.should be_false
484
- end
485
- end
486
-
487
- describe "#decrement" do
488
- before :all do
489
- @post = Post.create(:comment_count => 10)
490
- @post.decrement(:comment_count, 5)
491
- end
492
-
493
- it "should update the given property with the decremented value" do
494
- @post.comment_count.should == 5
495
- Post.get(@post.id).comment_count.should == 5
496
- end
497
-
498
- it "should reload the updated resource" do
499
- @post.dirty?.should be_false
500
- end
501
- end
502
-
503
- describe "#set" do
504
- it "should set the value of a property" do
505
- post = Post.create(:body => "This needs to be edited", :comment_count => 2)
506
-
507
- pending "not implemented yet" do
508
- post.set(:body => "This was edited", :comment_count => 3)
509
- post.body.should == "This was edited"
510
- post.comment_count.should == 3
511
- fresh_post = Post.get(post.id)
512
- fresh_post.body.should == "This was edited"
513
- fresh_post.comment_count.should == 3
514
- end
515
- end
516
- end
517
-
518
- describe "#unset" do
519
- it "should unset the value of a property" do
520
- post = Post.create(:body => "This needs to be removed", :comment_count => 2)
521
-
522
- pending "not implemented yet" do
523
- post.unset(:body, :comment_count)
524
- post.body.should be_nil
525
- post.comment_count.should be_nil
526
- fresh_post = Post.get(post.id)
527
- fresh_post.body.should be_nil
528
- fresh_post.comment_count.should be_nil
529
- end
530
- end
531
- end
532
-
533
- describe "#push" do
534
- it "should be implemented" do
535
- pending
536
- end
537
- end
538
-
539
- describe "#push_all" do
540
- it "should be implemented" do
541
- pending
542
- end
543
- end
544
-
545
- describe "#pop" do
546
- it "should be implemented" do
547
- pending
548
- end
549
- end
550
-
551
- describe "#pull" do
552
- it "should be implemented" do
553
- pending
554
- end
555
- end
556
-
557
- describe "#pull_all" do
558
- it "should be implemented" do
559
- pending
560
- end
561
- end
562
- end
563
182
  end