forked from mongodb/mongoid
-
Notifications
You must be signed in to change notification settings - Fork 22
/
criteria_spec.rb
835 lines (579 loc) · 19.4 KB
/
criteria_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
require "spec_helper"
describe Mongoid::Criteria do
before do
@criteria = Mongoid::Criteria.new(Person)
end
describe "#aggregate" do
context "when klass provided" do
before do
@reduce = "function(obj, prev) { prev.count++; }"
@criteria = Mongoid::Criteria.new(Person)
@collection = mock
Person.expects(:collection).returns(@collection)
end
it "calls group on the collection with the aggregate js" do
@collection.expects(:group).with([:field1], {}, {:count => 0}, @reduce)
@criteria.select(:field1).aggregate
end
end
context "when klass not provided" do
before do
@reduce = "function(obj, prev) { prev.count++; }"
@collection = mock
Person.expects(:collection).returns(@collection)
end
it "calls group on the collection with the aggregate js" do
@collection.expects(:group).with([:field1], {}, {:count => 0}, @reduce)
@criteria.select(:field1).aggregate(Person)
end
end
end
describe "#all" do
it "adds the $all query to the selector" do
@criteria.all(:title => ["title1", "title2"])
@criteria.selector.should == { :title => { "$all" => ["title1", "title2"] } }
end
it "returns self" do
@criteria.all(:title => [ "title1" ]).should == @criteria
end
end
describe "#collect" do
context "filtering" do
before do
@collection = mock
Person.expects(:collection).returns(@collection)
@criteria = Mongoid::Criteria.new(Person).extras(:page => 1, :per_page => 20)
@collection.expects(:find).with(@criteria.selector, @criteria.options).returns([])
end
it "filters out unused params" do
@criteria.collect
@criteria.options[:page].should be_nil
@criteria.options[:per_page].should be_nil
end
end
context "when type is :all" do
before do
@collection = mock
Person.expects(:collection).returns(@collection)
@criteria = Mongoid::Criteria.new(Person).extras(:page => 1, :per_page => 20)
@cursor = stub(:count => 44, :collect => [])
@collection.expects(:find).with(@criteria.selector, @criteria.options).returns(@cursor)
end
it "adds the count instance variable" do
@criteria.collect.should == []
@criteria.count.should == 44
end
end
context "when type is :first" do
end
context "when type is not :first" do
it "calls find on the collection with the selector and options" do
criteria = Mongoid::Criteria.new(Person)
collection = mock
Person.expects(:collection).returns(collection)
collection.expects(:find).with(@criteria.selector, @criteria.options).returns([])
criteria.collect.should == []
end
end
end
describe "#count" do
context "when criteria has not been executed" do
before do
@criteria.instance_variable_set(:@count, 34)
end
it "returns a count from the cursor" do
@criteria.count.should == 34
end
end
context "when criteria has been executed" do
before do
@criteria = Mongoid::Criteria.new(Person)
@selector = { :test => "Testing" }
@criteria.where(@selector)
@collection = mock
@cursor = mock
Person.expects(:collection).returns(@collection)
end
it "returns the count from the cursor without creating the documents" do
@collection.expects(:find).with(@selector, {}).returns(@cursor)
@cursor.expects(:count).returns(10)
@criteria.count.should == 10
end
end
end
describe "#each" do
before do
@criteria.where(:title => "Sir")
@collection = stub
@person = Person.new(:title => "Sir")
@cursor = stub(:count => 10, :collect => [@person])
end
context "when the criteria has not been executed" do
before do
Person.expects(:collection).returns(@collection)
@collection.expects(:find).with({ :title => "Sir" }, {}).returns(@cursor)
end
it "executes the criteria" do
@criteria.each do |person|
person.should == @person
end
end
end
context "when the criteria has been executed" do
before do
Person.expects(:collection).returns(@collection)
@collection.expects(:find).with({ :title => "Sir" }, {}).returns(@cursor)
end
it "calls each on the existing results" do
@criteria.each
@criteria.each do |person|
person.should == @person
end
end
end
end
describe "#excludes" do
it "adds the $ne query to the selector" do
@criteria.excludes(:title => "Bad Title", :text => "Bad Text")
@criteria.selector.should == { :title => { "$ne" => "Bad Title"}, :text => { "$ne" => "Bad Text" } }
end
it "returns self" do
@criteria.excludes(:title => "Bad").should == @criteria
end
end
describe "#extras" do
context "filtering" do
context "when page is provided" do
it "sets the limit and skip options" do
@criteria.extras({ :page => "2" })
@criteria.page.should == 2
@criteria.options.should == { :skip => 20, :limit => 20 }
end
end
context "when per_page is provided" do
it "sets the limit and skip options" do
@criteria.extras({ :per_page => 45 })
@criteria.options.should == { :skip => 0, :limit => 45 }
end
end
context "when page and per_page both provided" do
it "sets the limit and skip options" do
@criteria.extras({ :per_page => 30, :page => "4" })
@criteria.options.should == { :skip => 90, :limit => 30 }
@criteria.page.should == 4
end
end
end
it "adds the extras to the options" do
@criteria.extras({ :skip => 10 })
@criteria.options.should == { :skip => 10 }
end
it "returns self" do
@criteria.extras({}).should == @criteria
end
end
describe "#group" do
before do
@grouping = [{ "title" => "Sir", "group" => [{ "title" => "Sir", "age" => 30 }] }]
end
context "when klass provided" do
before do
@reduce = "function(obj, prev) { prev.group.push(obj); }"
@criteria = Mongoid::Criteria.new(Person)
@collection = mock
Person.expects(:collection).returns(@collection)
end
it "calls group on the collection with the aggregate js" do
@collection.expects(:group).with([:field1], {}, {:group => []}, @reduce).returns(@grouping)
@criteria.select(:field1).group
end
end
context "when klass not provided" do
before do
@reduce = "function(obj, prev) { prev.group.push(obj); }"
@collection = mock
Person.expects(:collection).returns(@collection)
end
it "calls group on the collection with the aggregate js" do
@collection.expects(:group).with([:field1], {}, {:group => []}, @reduce).returns(@grouping)
@criteria.select(:field1).group(Person)
end
end
end
describe "#id" do
it "adds the _id query to the selector" do
id = Mongo::ObjectID.new.to_s
@criteria.id(id)
@criteria.selector.should == { :_id => id }
end
it "returns self" do
id = Mongo::ObjectID.new.to_s
@criteria.id(id.to_s).should == @criteria
end
end
describe "#in" do
it "adds the $in clause to the selector" do
@criteria.in(:title => ["title1", "title2"], :text => ["test"])
@criteria.selector.should == { :title => { "$in" => ["title1", "title2"] }, :text => { "$in" => ["test"] } }
end
it "returns self" do
@criteria.in(:title => ["title1"]).should == @criteria
end
end
describe "#last" do
context "when documents exist" do
before do
@collection = mock
Person.expects(:collection).returns(@collection)
@collection.expects(:find_one).with(@criteria.selector, { :sort => [[:title, :desc]] }).returns({ :title => "Sir" })
end
it "calls find on the collection with the selector and sort options reversed" do
criteria = Mongoid::Criteria.new(Person)
criteria.order_by([[:title, :asc]])
criteria.last.should be_a_kind_of(Person)
end
end
context "when no documents exist" do
before do
@collection = mock
Person.expects(:collection).returns(@collection)
@collection.expects(:find_one).with(@criteria.selector, { :sort => [[:_id, :desc]] }).returns(nil)
end
it "returns nil" do
criteria = Mongoid::Criteria.new(Person)
criteria.last.should be_nil
end
end
context "when no sorting options provided" do
before do
@collection = mock
Person.expects(:collection).returns(@collection)
@collection.expects(:find_one).with(@criteria.selector, { :sort => [[:_id, :desc]] }).returns({ :title => "Sir" })
end
it "defaults to sort by id" do
criteria = Mongoid::Criteria.new(Person)
criteria.last
end
end
end
describe "#limit" do
context "when value provided" do
it "adds the limit to the options" do
@criteria.limit(100)
@criteria.options.should == { :limit => 100 }
end
end
context "when value not provided" do
it "defaults to 20" do
@criteria.limit
@criteria.options.should == { :limit => 20 }
end
end
it "returns self" do
@criteria.limit.should == @criteria
end
end
describe "#merge" do
before do
@criteria.where(:title => "Sir", :age => 30).skip(40).limit(20)
end
context "with another criteria" do
context "when the other has a selector and options" do
before do
@other = Mongoid::Criteria.new(Person)
@other.where(:name => "Chloe").order_by([[:name, :asc]])
@selector = { :title => "Sir", :age => 30, :name => "Chloe" }
@options = { :skip => 40, :limit => 20, :sort => [[:name, :asc]] }
end
it "merges the selector and options hashes together" do
@criteria.merge(@other)
@criteria.selector.should == @selector
@criteria.options.should == @options
end
end
context "when the other has no selector or options" do
before do
@other = Mongoid::Criteria.new(Person)
@selector = { :title => "Sir", :age => 30 }
@options = { :skip => 40, :limit => 20 }
end
it "merges the selector and options hashes together" do
@criteria.merge(@other)
@criteria.selector.should == @selector
@criteria.options.should == @options
end
end
end
end
describe "#method_missing" do
before do
@criteria = Mongoid::Criteria.new(Person)
@criteria.where(:title => "Sir")
end
it "merges the criteria with the next one" do
@new_criteria = @criteria.accepted
@new_criteria.selector.should == { :title => "Sir", :terms => true }
end
context "chaining more than one scope" do
before do
@criteria = Person.accepted.old.knight
end
it "returns the final merged criteria" do
@criteria.selector.should ==
{ :title => "Sir", :terms => true, :age => { "$gt" => 50 } }
end
end
context "when expecting behaviour of an array" do
before do
@array = mock
@document = mock
end
describe "#[]" do
it "collects the criteria and calls []" do
@criteria.expects(:collect).returns([@document])
@criteria[0].should == @document
end
end
describe "#rand" do
it "collects the criteria and call rand" do
@criteria.expects(:collect).returns(@array)
@array.expects(:send).with(:rand).returns(@document)
@criteria.rand
end
end
end
end
describe "#not_in" do
it "adds the exclusion to the selector" do
@criteria.not_in(:title => ["title1", "title2"], :text => ["test"])
@criteria.selector.should == { :title => { "$nin" => ["title1", "title2"] }, :text => { "$nin" => ["test"] } }
end
it "returns self" do
@criteria.not_in(:title => ["title1"]).should == @criteria
end
end
describe "#offset" do
context "when the per_page option exists" do
before do
@criteria = Mongoid::Criteria.new(Person).extras({ :per_page => 20, :page => 3 })
end
it "returns the per_page option" do
@criteria.offset.should == 40
end
end
context "when the skip option exists" do
before do
@criteria = Mongoid::Criteria.new(Person).extras({ :skip => 20 })
end
it "returns the skip option" do
@criteria.offset.should == 20
end
end
context "when no option exists" do
context "when page option exists" do
before do
@criteria = Mongoid::Criteria.new(Person).extras({ :page => 2 })
end
it "adds the skip option to the options and returns it" do
@criteria.offset.should == 20
@criteria.options[:skip].should == 20
end
end
context "when page option does not exist" do
before do
@criteria = Mongoid::Criteria.new(Person)
end
it "returns nil" do
@criteria.offset.should be_nil
@criteria.options[:skip].should be_nil
end
end
end
end
describe "#one" do
context "when documents exist" do
before do
@collection = mock
Person.expects(:collection).returns(@collection)
@collection.expects(:find_one).with(@criteria.selector, @criteria.options).returns({ :title => "Sir" })
end
it "calls find on the collection with the selector and options" do
criteria = Mongoid::Criteria.new(Person)
criteria.one.should be_a_kind_of(Person)
end
end
context "when no documents exist" do
before do
@collection = mock
Person.expects(:collection).returns(@collection)
@collection.expects(:find_one).with(@criteria.selector, @criteria.options).returns(nil)
end
it "returns nil" do
criteria = Mongoid::Criteria.new(Person)
criteria.one.should be_nil
end
end
end
describe "#order_by" do
context "when field names and direction specified" do
it "adds the sort to the options" do
@criteria.order_by([[:title, :asc], [:text, :desc]])
@criteria.options.should == { :sort => [[:title, :asc], [:text, :desc]] }
end
end
it "returns self" do
@criteria.order_by.should == @criteria
end
end
describe "#page" do
context "when the page option exists" do
before do
@criteria = Mongoid::Criteria.new(Person).extras({ :page => 5 })
end
it "returns the page option" do
@criteria.page.should == 5
end
end
context "when the page option does not exist" do
before do
@criteria = Mongoid::Criteria.new(Person)
end
it "returns 1" do
@criteria.page.should == 1
end
end
end
describe "#paginate" do
before do
@collection = mock
Person.expects(:collection).returns(@collection)
@criteria = Person.select.where(:_id => "1").skip(60).limit(20)
@collection.expects(:find).with({:_id => "1"}, :skip => 60, :limit => 20).returns([])
@results = @criteria.paginate
end
it "executes and paginates the results" do
@results.current_page.should == 4
@results.per_page.should == 20
end
end
describe "#per_page" do
context "when the per_page option exists" do
before do
@criteria = Mongoid::Criteria.new(Person).extras({ :per_page => 10 })
end
it "returns the per_page option" do
@criteria.per_page.should == 10
end
end
context "when the per_page option does not exist" do
before do
@criteria = Mongoid::Criteria.new(Person)
end
it "returns 1" do
@criteria.per_page.should == 20
end
end
end
describe "#select" do
context "when args are provided" do
it "adds the options for limiting by fields" do
@criteria.select(:title, :text)
@criteria.options.should == { :fields => [ :title, :text ] }
end
it "returns self" do
@criteria.select.should == @criteria
end
end
context "when no args provided" do
it "does not add the field option" do
@criteria.select
@criteria.options[:fields].should be_nil
end
end
end
describe "#skip" do
context "when value provided" do
it "adds the skip value to the options" do
@criteria.skip(20)
@criteria.options.should == { :skip => 20 }
end
end
context "when value not provided" do
it "defaults to zero" do
@criteria.skip
@criteria.options.should == { :skip => 0 }
end
end
it "returns self" do
@criteria.skip.should == @criteria
end
end
describe ".translate" do
context "with a single argument" do
before do
@id = Mongo::ObjectID.new.to_s
@document = stub
@criteria = mock
Mongoid::Criteria.expects(:new).returns(@criteria)
@criteria.expects(:id).with(@id).returns(@criteria)
@criteria.expects(:one).returns(@document)
end
it "creates a criteria for a string" do
Mongoid::Criteria.translate(Person, @id)
end
end
context "multiple arguments" do
context "when Person, :conditions => {}" do
before do
@criteria = Mongoid::Criteria.translate(Person, :conditions => { :title => "Test" })
end
it "returns a criteria with a selector from the conditions" do
@criteria.selector.should == { :title => "Test" }
end
it "returns a criteria with klass Person" do
@criteria.klass.should == Person
end
end
context "when :all, :conditions => {}" do
before do
@criteria = Mongoid::Criteria.translate(Person, :conditions => { :title => "Test" })
end
it "returns a criteria with a selector from the conditions" do
@criteria.selector.should == { :title => "Test" }
end
it "returns a criteria with klass Person" do
@criteria.klass.should == Person
end
end
context "when :last, :conditions => {}" do
before do
@criteria = Mongoid::Criteria.translate(Person, :conditions => { :title => "Test" })
end
it "returns a criteria with a selector from the conditions" do
@criteria.selector.should == { :title => "Test" }
end
it "returns a criteria with klass Person" do
@criteria.klass.should == Person
end
end
context "when options are provided" do
before do
@criteria = Mongoid::Criteria.translate(Person, :conditions => { :title => "Test" }, :skip => 10)
end
it "adds the criteria and the options" do
@criteria.selector.should == { :title => "Test" }
@criteria.options.should == { :skip => 10 }
end
end
end
end
describe "#where" do
it "adds the clause to the selector" do
@criteria.where(:title => "Title", :text => "Text")
@criteria.selector.should == { :title => "Title", :text => "Text" }
end
it "returns self" do
@criteria.where.should == @criteria
end
end
end