mongodoc 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +143 -0
- data/Rakefile +35 -3
- data/VERSION +1 -1
- data/examples/simple_document.rb +35 -0
- data/examples/simple_object.rb +32 -0
- data/features/finders.feature +72 -0
- data/features/mongodoc_base.feature +12 -2
- data/features/named_scopes.feature +66 -0
- data/features/new_record.feature +36 -0
- data/features/partial_updates.feature +105 -0
- data/features/step_definitions/criteria_steps.rb +4 -41
- data/features/step_definitions/document_steps.rb +56 -5
- data/features/step_definitions/documents.rb +14 -3
- data/features/step_definitions/finder_steps.rb +15 -0
- data/features/step_definitions/named_scope_steps.rb +18 -0
- data/features/step_definitions/partial_update_steps.rb +32 -0
- data/features/step_definitions/query_steps.rb +51 -0
- data/features/using_criteria.feature +5 -1
- data/lib/mongodoc/attributes.rb +76 -63
- data/lib/mongodoc/collection.rb +9 -9
- data/lib/mongodoc/criteria.rb +152 -161
- data/lib/mongodoc/cursor.rb +7 -5
- data/lib/mongodoc/document.rb +95 -31
- data/lib/mongodoc/finders.rb +29 -0
- data/lib/mongodoc/named_scope.rb +68 -0
- data/lib/mongodoc/parent_proxy.rb +15 -6
- data/lib/mongodoc/proxy.rb +22 -13
- data/lib/mongodoc.rb +3 -3
- data/mongodoc.gemspec +42 -14
- data/perf/mongodoc_runner.rb +90 -0
- data/perf/ruby_driver_runner.rb +64 -0
- data/spec/attributes_spec.rb +46 -12
- data/spec/collection_spec.rb +23 -23
- data/spec/criteria_spec.rb +124 -187
- data/spec/cursor_spec.rb +21 -17
- data/spec/document_ext.rb +2 -2
- data/spec/document_spec.rb +187 -218
- data/spec/embedded_save_spec.rb +104 -0
- data/spec/finders_spec.rb +81 -0
- data/spec/hash_matchers.rb +27 -0
- data/spec/named_scope_spec.rb +82 -0
- data/spec/new_record_spec.rb +216 -0
- data/spec/parent_proxy_spec.rb +8 -6
- data/spec/proxy_spec.rb +80 -0
- data/spec/spec_helper.rb +2 -0
- metadata +35 -7
- data/README.rdoc +0 -75
data/spec/criteria_spec.rb
CHANGED
@@ -1,32 +1,44 @@
|
|
1
1
|
require File.expand_path(File.join(File.dirname(__FILE__), "spec_helper.rb"))
|
2
2
|
|
3
3
|
describe MongoDoc::Criteria do
|
4
|
-
class CountryCode
|
4
|
+
class CountryCode
|
5
|
+
include MongoDoc::Document
|
6
|
+
|
5
7
|
key :code
|
6
8
|
end
|
7
9
|
|
8
|
-
class Phone
|
10
|
+
class Phone
|
11
|
+
include MongoDoc::Document
|
12
|
+
|
9
13
|
key :number
|
10
14
|
has_one :country_code
|
11
15
|
end
|
12
16
|
|
13
|
-
class Animal
|
17
|
+
class Animal
|
18
|
+
include MongoDoc::Document
|
19
|
+
|
14
20
|
key :name
|
15
21
|
end
|
16
22
|
|
17
|
-
class Address
|
23
|
+
class Address
|
24
|
+
include MongoDoc::Document
|
25
|
+
|
18
26
|
key :street
|
19
27
|
key :city
|
20
28
|
key :state
|
21
29
|
key :post_code
|
22
30
|
end
|
23
31
|
|
24
|
-
class Name
|
32
|
+
class Name
|
33
|
+
include MongoDoc::Document
|
34
|
+
|
25
35
|
key :first_name
|
26
36
|
key :last_name
|
27
37
|
end
|
28
38
|
|
29
|
-
class Person
|
39
|
+
class Person
|
40
|
+
include MongoDoc::Document
|
41
|
+
|
30
42
|
key :title
|
31
43
|
key :terms
|
32
44
|
key :age
|
@@ -50,57 +62,30 @@ describe MongoDoc::Criteria do
|
|
50
62
|
def employer=(emp)
|
51
63
|
self.employer_id = emp.id
|
52
64
|
end
|
53
|
-
|
54
|
-
class << self
|
55
|
-
def accepted
|
56
|
-
criteria.where(:terms => true)
|
57
|
-
end
|
58
|
-
def knight
|
59
|
-
criteria.where(:title => "Sir")
|
60
|
-
end
|
61
|
-
def old
|
62
|
-
criteria.where(:age => { "$gt" => 50 })
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
65
|
end
|
67
66
|
|
68
67
|
before do
|
69
68
|
@criteria = MongoDoc::Criteria.new(Person)
|
70
69
|
end
|
71
70
|
|
72
|
-
describe "#
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
before do
|
77
|
-
@reduce = "function(obj, prev) { prev.count++; }"
|
78
|
-
@collection = mock
|
79
|
-
Animal.should_receive(:collection).and_return(@collection)
|
80
|
-
end
|
81
|
-
|
82
|
-
it "calls group on the collection with the aggregate js" do
|
83
|
-
@collection.should_receive(:group).with([:field1], {}, {:count => 0}, @reduce)
|
84
|
-
@criteria.select(:field1).aggregate(Animal)
|
85
|
-
end
|
86
|
-
|
71
|
+
describe "#all" do
|
72
|
+
it "calls collect" do
|
73
|
+
@criteria.should_receive(:collect).with(no_args)
|
74
|
+
@criteria.all
|
87
75
|
end
|
76
|
+
end
|
88
77
|
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
Person.should_receive(:collection).and_return(@collection)
|
95
|
-
end
|
96
|
-
|
97
|
-
it "calls group on the collection with the aggregate js" do
|
98
|
-
@collection.should_receive(:group).with([:field1], {}, {:count => 0}, @reduce)
|
99
|
-
@criteria.select(:field1).aggregate
|
100
|
-
end
|
101
|
-
|
78
|
+
describe "#aggregate" do
|
79
|
+
before do
|
80
|
+
@reduce = "function(obj, prev) { prev.count++; }"
|
81
|
+
@collection = mock
|
82
|
+
Person.should_receive(:collection).and_return(@collection)
|
102
83
|
end
|
103
84
|
|
85
|
+
it "calls group on the collection with the aggregate js" do
|
86
|
+
@collection.should_receive(:group).with([:field1], {}, {:count => 0}, @reduce, true)
|
87
|
+
@criteria.only(:field1).aggregate
|
88
|
+
end
|
104
89
|
end
|
105
90
|
|
106
91
|
describe "#every" do
|
@@ -110,7 +95,7 @@ describe MongoDoc::Criteria do
|
|
110
95
|
@criteria.selector.should == { :title => { "$all" => ["title1", "title2"] } }
|
111
96
|
end
|
112
97
|
|
113
|
-
it "
|
98
|
+
it "returns self" do
|
114
99
|
@criteria.every(:title => [ "title1" ]).should == @criteria
|
115
100
|
end
|
116
101
|
|
@@ -124,7 +109,7 @@ describe MongoDoc::Criteria do
|
|
124
109
|
end
|
125
110
|
|
126
111
|
it "calls each" do
|
127
|
-
@
|
112
|
+
@criteria.should_receive(:each).and_return(@criteria)
|
128
113
|
@criteria.collect
|
129
114
|
end
|
130
115
|
|
@@ -181,6 +166,12 @@ describe MongoDoc::Criteria do
|
|
181
166
|
@criteria.each
|
182
167
|
end
|
183
168
|
|
169
|
+
it "has memoized the result of execute" do
|
170
|
+
@criteria.stub(:execute).and_return(@cursor)
|
171
|
+
@criteria.each
|
172
|
+
@criteria.collection.should == @cursor
|
173
|
+
end
|
174
|
+
|
184
175
|
it "returns self" do
|
185
176
|
@criteria.stub(:execute).and_return(@cursor)
|
186
177
|
@criteria.each.should == @criteria
|
@@ -188,7 +179,22 @@ describe MongoDoc::Criteria do
|
|
188
179
|
end
|
189
180
|
|
190
181
|
context "when a block is given" do
|
182
|
+
class CursorStub
|
183
|
+
include Enumerable
|
184
|
+
|
185
|
+
attr_accessor :data
|
186
|
+
|
187
|
+
def each(*args, &block)
|
188
|
+
data.each(*args, &block)
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
191
192
|
context "when the criteria has not been executed" do
|
193
|
+
before do
|
194
|
+
@cursor = CursorStub.new
|
195
|
+
@cursor.data = [@person]
|
196
|
+
end
|
197
|
+
|
192
198
|
it "executes the criteria" do
|
193
199
|
@criteria.should_receive(:execute).and_return(@cursor)
|
194
200
|
@criteria.each do |person|
|
@@ -204,6 +210,15 @@ describe MongoDoc::Criteria do
|
|
204
210
|
@result.should == @person
|
205
211
|
end
|
206
212
|
|
213
|
+
it "has memoized the cursor iteration" do
|
214
|
+
@criteria.stub(:execute).and_return(@cursor)
|
215
|
+
@criteria.each do |person|
|
216
|
+
@result = person
|
217
|
+
end
|
218
|
+
@criteria.collection.should == [@person]
|
219
|
+
end
|
220
|
+
|
221
|
+
|
207
222
|
it "returns self" do
|
208
223
|
@criteria.stub(:execute).and_return(@cursor)
|
209
224
|
@criteria.each.should == @criteria
|
@@ -244,7 +259,7 @@ describe MongoDoc::Criteria do
|
|
244
259
|
@criteria.selector.should == { :title => { "$ne" => "Bad Title"}, :text => { "$ne" => "Bad Text" } }
|
245
260
|
end
|
246
261
|
|
247
|
-
it "
|
262
|
+
it "returns self" do
|
248
263
|
@criteria.excludes(:title => "Bad").should == @criteria
|
249
264
|
end
|
250
265
|
|
@@ -290,7 +305,7 @@ describe MongoDoc::Criteria do
|
|
290
305
|
@criteria.options.should == { :skip => 10 }
|
291
306
|
end
|
292
307
|
|
293
|
-
it "
|
308
|
+
it "returns self" do
|
294
309
|
@criteria.extras({}).should == @criteria
|
295
310
|
end
|
296
311
|
|
@@ -303,54 +318,36 @@ describe MongoDoc::Criteria do
|
|
303
318
|
end
|
304
319
|
|
305
320
|
describe "#group" do
|
306
|
-
|
307
321
|
before do
|
308
322
|
@grouping = [{ "title" => "Sir", "group" => [{ "title" => "Sir", "age" => 30 }] }]
|
323
|
+
@reduce = "function(obj, prev) { prev.group.push(obj); }"
|
324
|
+
@collection = mock
|
325
|
+
Person.should_receive(:collection).and_return(@collection)
|
309
326
|
end
|
310
327
|
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
@reduce = "function(obj, prev) { prev.group.push(obj); }"
|
315
|
-
@collection = mock
|
316
|
-
Animal.should_receive(:collection).and_return(@collection)
|
317
|
-
end
|
318
|
-
|
319
|
-
it "calls group on the collection with the aggregate js" do
|
320
|
-
@collection.should_receive(:group).with([:field1], {}, {:group => []}, @reduce).and_return(@grouping)
|
321
|
-
@criteria.select(:field1).group(Animal)
|
322
|
-
end
|
323
|
-
|
324
|
-
end
|
325
|
-
|
326
|
-
context "when klass not provided" do
|
327
|
-
|
328
|
-
before do
|
329
|
-
@reduce = "function(obj, prev) { prev.group.push(obj); }"
|
330
|
-
@collection = mock
|
331
|
-
Person.should_receive(:collection).and_return(@collection)
|
332
|
-
end
|
333
|
-
|
334
|
-
it "calls group on the collection with the aggregate js" do
|
335
|
-
@collection.should_receive(:group).with([:field1], {}, {:group => []}, @reduce).and_return(@grouping)
|
336
|
-
@criteria.select(:field1).group
|
337
|
-
end
|
338
|
-
|
328
|
+
it "calls group on the collection with the aggregate js" do
|
329
|
+
@collection.should_receive(:group).with([:field1], {}, {:group => []}, @reduce, true).and_return(@grouping)
|
330
|
+
@criteria.only(:field1).group
|
339
331
|
end
|
340
|
-
|
341
332
|
end
|
342
333
|
|
343
334
|
describe "#id" do
|
344
335
|
|
345
|
-
it "adds the _id query to the selector" do
|
346
|
-
id = Mongo::ObjectID.new
|
336
|
+
it "adds the ObjectID as the _id query to the selector" do
|
337
|
+
id = Mongo::ObjectID.new
|
347
338
|
@criteria.id(id)
|
348
339
|
@criteria.selector.should == { :_id => id }
|
349
340
|
end
|
350
341
|
|
351
|
-
it "
|
342
|
+
it "adds the string as the _id query to the selector" do
|
352
343
|
id = Mongo::ObjectID.new.to_s
|
353
|
-
@criteria.id(id
|
344
|
+
@criteria.id(id)
|
345
|
+
@criteria.selector.should == { :_id => id }
|
346
|
+
end
|
347
|
+
|
348
|
+
it "returns self" do
|
349
|
+
id = Mongo::ObjectID.new
|
350
|
+
@criteria.id(id).should == @criteria
|
354
351
|
end
|
355
352
|
|
356
353
|
end
|
@@ -362,7 +359,7 @@ describe MongoDoc::Criteria do
|
|
362
359
|
@criteria.selector.should == { :title => { "$in" => ["title1", "title2"] }, :text => { "$in" => ["test"] } }
|
363
360
|
end
|
364
361
|
|
365
|
-
it "
|
362
|
+
it "returns self" do
|
366
363
|
@criteria.in(:title => ["title1"]).should == @criteria
|
367
364
|
end
|
368
365
|
|
@@ -393,7 +390,7 @@ describe MongoDoc::Criteria do
|
|
393
390
|
@collection.should_receive(:find_one).with(@criteria.selector, { :sort => [[:_id, :desc]] }).and_return(nil)
|
394
391
|
end
|
395
392
|
|
396
|
-
it "
|
393
|
+
it "returns nil" do
|
397
394
|
@criteria.last.should be_nil
|
398
395
|
end
|
399
396
|
|
@@ -435,82 +432,12 @@ describe MongoDoc::Criteria do
|
|
435
432
|
|
436
433
|
end
|
437
434
|
|
438
|
-
it "
|
435
|
+
it "returns self" do
|
439
436
|
@criteria.limit.should == @criteria
|
440
437
|
end
|
441
438
|
|
442
439
|
end
|
443
440
|
|
444
|
-
describe "#merge" do
|
445
|
-
|
446
|
-
before do
|
447
|
-
@criteria.where(:title => "Sir", :age => 30).skip(40).limit(20)
|
448
|
-
end
|
449
|
-
|
450
|
-
context "with another criteria" do
|
451
|
-
|
452
|
-
context "when the other has a selector and options" do
|
453
|
-
|
454
|
-
before do
|
455
|
-
@other = MongoDoc::Criteria.new(Person)
|
456
|
-
@other.where(:name => "Chloe").order_by([[:name, :asc]])
|
457
|
-
@selector = { :title => "Sir", :age => 30, :name => "Chloe" }
|
458
|
-
@options = { :skip => 40, :limit => 20, :sort => [[:name, :asc]] }
|
459
|
-
end
|
460
|
-
|
461
|
-
it "merges the selector and options hashes together" do
|
462
|
-
@criteria.merge(@other)
|
463
|
-
@criteria.selector.should == @selector
|
464
|
-
@criteria.options.should == @options
|
465
|
-
end
|
466
|
-
|
467
|
-
end
|
468
|
-
|
469
|
-
context "when the other has no selector or options" do
|
470
|
-
|
471
|
-
before do
|
472
|
-
@other = MongoDoc::Criteria.new(Person)
|
473
|
-
@selector = { :title => "Sir", :age => 30 }
|
474
|
-
@options = { :skip => 40, :limit => 20 }
|
475
|
-
end
|
476
|
-
|
477
|
-
it "merges the selector and options hashes together" do
|
478
|
-
@criteria.merge(@other)
|
479
|
-
@criteria.selector.should == @selector
|
480
|
-
@criteria.options.should == @options
|
481
|
-
end
|
482
|
-
end
|
483
|
-
|
484
|
-
end
|
485
|
-
|
486
|
-
end
|
487
|
-
|
488
|
-
describe "#method_missing" do
|
489
|
-
|
490
|
-
before do
|
491
|
-
@criteria.where(:title => "Sir")
|
492
|
-
end
|
493
|
-
|
494
|
-
it "merges the criteria with the next one" do
|
495
|
-
@new_criteria = @criteria.accepted
|
496
|
-
@new_criteria.selector.should == { :title => "Sir", :terms => true }
|
497
|
-
end
|
498
|
-
|
499
|
-
context "chaining more than one scope" do
|
500
|
-
|
501
|
-
before do
|
502
|
-
@criteria = Person.accepted.old.knight
|
503
|
-
end
|
504
|
-
|
505
|
-
it "and_return the final merged criteria" do
|
506
|
-
@criteria.selector.should ==
|
507
|
-
{ :title => "Sir", :terms => true, :age => { "$gt" => 50 } }
|
508
|
-
end
|
509
|
-
|
510
|
-
end
|
511
|
-
|
512
|
-
end
|
513
|
-
|
514
441
|
describe "#not_in" do
|
515
442
|
|
516
443
|
it "adds the exclusion to the selector" do
|
@@ -518,7 +445,7 @@ describe MongoDoc::Criteria do
|
|
518
445
|
@criteria.selector.should == { :title => { "$nin" => ["title1", "title2"] }, :text => { "$nin" => ["test"] } }
|
519
446
|
end
|
520
447
|
|
521
|
-
it "
|
448
|
+
it "returns self" do
|
522
449
|
@criteria.not_in(:title => ["title1"]).should == @criteria
|
523
450
|
end
|
524
451
|
|
@@ -532,7 +459,7 @@ describe MongoDoc::Criteria do
|
|
532
459
|
@criteria.extras({ :per_page => 20, :page => 3 })
|
533
460
|
end
|
534
461
|
|
535
|
-
it "
|
462
|
+
it "returns the per_page option" do
|
536
463
|
@criteria.offset.should == 40
|
537
464
|
end
|
538
465
|
|
@@ -544,7 +471,7 @@ describe MongoDoc::Criteria do
|
|
544
471
|
@criteria.extras({ :skip => 20 })
|
545
472
|
end
|
546
473
|
|
547
|
-
it "
|
474
|
+
it "returns the skip option" do
|
548
475
|
@criteria.offset.should == 20
|
549
476
|
end
|
550
477
|
|
@@ -558,7 +485,7 @@ describe MongoDoc::Criteria do
|
|
558
485
|
@criteria.extras({ :page => 2 })
|
559
486
|
end
|
560
487
|
|
561
|
-
it "adds the skip option to the options and
|
488
|
+
it "adds the skip option to the options and returns it" do
|
562
489
|
@criteria.offset.should == 20
|
563
490
|
@criteria.options[:skip].should == 20
|
564
491
|
end
|
@@ -567,7 +494,7 @@ describe MongoDoc::Criteria do
|
|
567
494
|
|
568
495
|
context "when page option does not exist" do
|
569
496
|
|
570
|
-
it "
|
497
|
+
it "returns nil" do
|
571
498
|
@criteria.offset.should be_nil
|
572
499
|
@criteria.options[:skip].should be_nil
|
573
500
|
end
|
@@ -621,7 +548,7 @@ describe MongoDoc::Criteria do
|
|
621
548
|
|
622
549
|
end
|
623
550
|
|
624
|
-
it "
|
551
|
+
it "returns self" do
|
625
552
|
@criteria.order_by.should == @criteria
|
626
553
|
end
|
627
554
|
|
@@ -635,7 +562,7 @@ describe MongoDoc::Criteria do
|
|
635
562
|
@criteria.extras({ :page => 5 })
|
636
563
|
end
|
637
564
|
|
638
|
-
it "
|
565
|
+
it "returns the page option" do
|
639
566
|
@criteria.page.should == 5
|
640
567
|
end
|
641
568
|
|
@@ -656,7 +583,7 @@ describe MongoDoc::Criteria do
|
|
656
583
|
before do
|
657
584
|
@collection = mock
|
658
585
|
Person.should_receive(:collection).and_return(@collection)
|
659
|
-
@criteria.
|
586
|
+
@criteria.where(:_id => "1").skip(60).limit(20)
|
660
587
|
@cursor = mock('cursor', :count => 20, :to_a => [])
|
661
588
|
@collection.should_receive(:find).with({:_id => "1"}, :skip => 60, :limit => 20).and_return(@cursor)
|
662
589
|
@results = @criteria.paginate
|
@@ -677,7 +604,7 @@ describe MongoDoc::Criteria do
|
|
677
604
|
@criteria.extras({ :per_page => 10 })
|
678
605
|
end
|
679
606
|
|
680
|
-
it "
|
607
|
+
it "returns the per_page option" do
|
681
608
|
@criteria.per_page.should == 10
|
682
609
|
end
|
683
610
|
|
@@ -693,17 +620,17 @@ describe MongoDoc::Criteria do
|
|
693
620
|
|
694
621
|
end
|
695
622
|
|
696
|
-
describe "#
|
623
|
+
describe "#only" do
|
697
624
|
|
698
625
|
context "when args are provided" do
|
699
626
|
|
700
627
|
it "adds the options for limiting by fields" do
|
701
|
-
@criteria.
|
628
|
+
@criteria.only(:title, :text)
|
702
629
|
@criteria.options.should == { :fields => [ :title, :text ] }
|
703
630
|
end
|
704
631
|
|
705
|
-
it "
|
706
|
-
@criteria.
|
632
|
+
it "returns self" do
|
633
|
+
@criteria.only.should == @criteria
|
707
634
|
end
|
708
635
|
|
709
636
|
end
|
@@ -711,7 +638,7 @@ describe MongoDoc::Criteria do
|
|
711
638
|
context "when no args provided" do
|
712
639
|
|
713
640
|
it "does not add the field option" do
|
714
|
-
@criteria.
|
641
|
+
@criteria.only
|
715
642
|
@criteria.options[:fields].should be_nil
|
716
643
|
end
|
717
644
|
|
@@ -739,7 +666,7 @@ describe MongoDoc::Criteria do
|
|
739
666
|
|
740
667
|
end
|
741
668
|
|
742
|
-
it "
|
669
|
+
it "returns self" do
|
743
670
|
@criteria.skip.should == @criteria
|
744
671
|
end
|
745
672
|
|
@@ -753,12 +680,12 @@ describe MongoDoc::Criteria do
|
|
753
680
|
@id = Mongo::ObjectID.new.to_s
|
754
681
|
@document = stub
|
755
682
|
@criteria = mock
|
756
|
-
MongoDoc::Criteria.should_receive(:new).and_return(@criteria)
|
757
|
-
@criteria.should_receive(:id).with(@id).and_return(@criteria)
|
758
|
-
@criteria.should_receive(:one).and_return(@document)
|
759
683
|
end
|
760
684
|
|
761
685
|
it "creates a criteria for a string" do
|
686
|
+
MongoDoc::Criteria.should_receive(:new).and_return(@criteria)
|
687
|
+
@criteria.should_receive(:id).with(@id).and_return(@criteria)
|
688
|
+
@criteria.should_receive(:one).and_return(@document)
|
762
689
|
MongoDoc::Criteria.translate(Person, @id)
|
763
690
|
end
|
764
691
|
|
@@ -772,11 +699,11 @@ describe MongoDoc::Criteria do
|
|
772
699
|
@criteria = MongoDoc::Criteria.translate(Person, :conditions => { :title => "Test" })
|
773
700
|
end
|
774
701
|
|
775
|
-
it "
|
702
|
+
it "returns a criteria with a selector from the conditions" do
|
776
703
|
@criteria.selector.should == { :title => "Test" }
|
777
704
|
end
|
778
705
|
|
779
|
-
it "
|
706
|
+
it "returns a criteria with klass Person" do
|
780
707
|
@criteria.klass.should == Person
|
781
708
|
end
|
782
709
|
|
@@ -788,11 +715,11 @@ describe MongoDoc::Criteria do
|
|
788
715
|
@criteria = MongoDoc::Criteria.translate(Person, :conditions => { :title => "Test" })
|
789
716
|
end
|
790
717
|
|
791
|
-
it "
|
718
|
+
it "returns a criteria with a selector from the conditions" do
|
792
719
|
@criteria.selector.should == { :title => "Test" }
|
793
720
|
end
|
794
721
|
|
795
|
-
it "
|
722
|
+
it "returns a criteria with klass Person" do
|
796
723
|
@criteria.klass.should == Person
|
797
724
|
end
|
798
725
|
|
@@ -804,11 +731,11 @@ describe MongoDoc::Criteria do
|
|
804
731
|
@criteria = MongoDoc::Criteria.translate(Person, :conditions => { :title => "Test" })
|
805
732
|
end
|
806
733
|
|
807
|
-
it "
|
734
|
+
it "returns a criteria with a selector from the conditions" do
|
808
735
|
@criteria.selector.should == { :title => "Test" }
|
809
736
|
end
|
810
737
|
|
811
|
-
it "
|
738
|
+
it "returns a criteria with klass Person" do
|
812
739
|
@criteria.klass.should == Person
|
813
740
|
end
|
814
741
|
end
|
@@ -823,11 +750,8 @@ describe MongoDoc::Criteria do
|
|
823
750
|
@criteria.selector.should == { :title => "Test" }
|
824
751
|
@criteria.options.should == { :skip => 10 }
|
825
752
|
end
|
826
|
-
|
827
753
|
end
|
828
|
-
|
829
754
|
end
|
830
|
-
|
831
755
|
end
|
832
756
|
|
833
757
|
describe "#where" do
|
@@ -837,10 +761,23 @@ describe MongoDoc::Criteria do
|
|
837
761
|
@criteria.selector.should == { :title => "Title", :text => "Text" }
|
838
762
|
end
|
839
763
|
|
840
|
-
it "
|
764
|
+
it "accepts a js where clause" do
|
765
|
+
@criteria.where("this.a > 3")
|
766
|
+
@criteria.selector.should == { '$where' => "this.a > 3" }
|
767
|
+
end
|
768
|
+
|
769
|
+
it "and return self" do
|
841
770
|
@criteria.where.should == @criteria
|
842
771
|
end
|
843
772
|
|
844
|
-
|
773
|
+
it "#and is an alias for where" do
|
774
|
+
@criteria.and(:title => "Title", :text => "Text")
|
775
|
+
@criteria.selector.should == { :title => "Title", :text => "Text" }
|
776
|
+
end
|
845
777
|
|
778
|
+
it "#conditions is an alias for where" do
|
779
|
+
@criteria.conditions(:title => "Title", :text => "Text")
|
780
|
+
@criteria.selector.should == { :title => "Title", :text => "Text" }
|
781
|
+
end
|
782
|
+
end
|
846
783
|
end
|
data/spec/cursor_spec.rb
CHANGED
@@ -6,10 +6,14 @@ describe "MongoDoc::Cursor" do
|
|
6
6
|
@cursor = MongoDoc::Cursor.new(@mongo_cursor)
|
7
7
|
end
|
8
8
|
|
9
|
+
it "is Enumerable" do
|
10
|
+
Enumerable.should === @cursor
|
11
|
+
end
|
12
|
+
|
9
13
|
it ".new wraps a Mongo::Cursor" do
|
10
14
|
@cursor._cursor.should == @mongo_cursor
|
11
15
|
end
|
12
|
-
|
16
|
+
|
13
17
|
context "with the underlying cursor" do
|
14
18
|
%w(close closed? count explain limit query_options_hash query_opts skip sort).each do |delegated_method|
|
15
19
|
it "delegates #{delegated_method} to the Mongo::Cursor" do
|
@@ -18,7 +22,7 @@ describe "MongoDoc::Cursor" do
|
|
18
22
|
end
|
19
23
|
end
|
20
24
|
end
|
21
|
-
|
25
|
+
|
22
26
|
context "#each" do
|
23
27
|
it "delegates to the cursor" do
|
24
28
|
@mongo_cursor.should_receive(:each)
|
@@ -31,7 +35,7 @@ describe "MongoDoc::Cursor" do
|
|
31
35
|
MongoDoc::BSON.should_receive(:decode).with(bson)
|
32
36
|
@cursor.each {}
|
33
37
|
end
|
34
|
-
|
38
|
+
|
35
39
|
it "calls the block with the decoded return" do
|
36
40
|
result = stub('bson')
|
37
41
|
@cursor.stub(:_cursor).and_return([result])
|
@@ -40,42 +44,42 @@ describe "MongoDoc::Cursor" do
|
|
40
44
|
@obj.should == result
|
41
45
|
end
|
42
46
|
end
|
43
|
-
|
44
|
-
context "#
|
47
|
+
|
48
|
+
context "#next_document" do
|
45
49
|
it "delegates to the cursor" do
|
46
|
-
@mongo_cursor.should_receive(:
|
47
|
-
@cursor.
|
50
|
+
@mongo_cursor.should_receive(:next_document)
|
51
|
+
@cursor.next_document
|
48
52
|
end
|
49
|
-
|
53
|
+
|
50
54
|
it "decodes the return from the delegate" do
|
51
55
|
bson = stub('bson')
|
52
|
-
@mongo_cursor.stub(:
|
56
|
+
@mongo_cursor.stub(:next_document).and_return(bson)
|
53
57
|
MongoDoc::BSON.should_receive(:decode).with(bson)
|
54
|
-
@cursor.
|
58
|
+
@cursor.next_document
|
55
59
|
end
|
56
|
-
|
60
|
+
|
57
61
|
it "returns nil if the delegate returns nil" do
|
58
|
-
@mongo_cursor.stub(:
|
59
|
-
@cursor.
|
62
|
+
@mongo_cursor.stub(:next_document)
|
63
|
+
@cursor.next_document.should be_nil
|
60
64
|
end
|
61
65
|
end
|
62
|
-
|
66
|
+
|
63
67
|
context "#to_a" do
|
64
68
|
it "delegates to the cursor" do
|
65
69
|
@mongo_cursor.should_receive(:to_a)
|
66
70
|
@cursor.to_a
|
67
71
|
end
|
68
|
-
|
72
|
+
|
69
73
|
it "decodes the return from the delegate" do
|
70
74
|
array = stub('array')
|
71
75
|
@mongo_cursor.stub(:to_a).and_return(array)
|
72
76
|
MongoDoc::BSON.should_receive(:decode).with(array)
|
73
77
|
@cursor.to_a
|
74
78
|
end
|
75
|
-
|
79
|
+
|
76
80
|
it "returns [] if the delegate returns []" do
|
77
81
|
@mongo_cursor.stub(:to_a).and_return([])
|
78
82
|
@cursor.to_a.should == []
|
79
83
|
end
|
80
84
|
end
|
81
|
-
end
|
85
|
+
end
|