forked from mongodb/mongoid
/
criteria_spec.rb
622 lines (426 loc) · 14.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
require File.expand_path(File.join(File.dirname(__FILE__), "/../../spec_helper.rb"))
describe Mongoid::Criteria do
before do
@criteria = Mongoid::Criteria.new(:all)
end
describe "#aggregate" do
context "when klass provided" do
before do
@reduce = "function(obj, prev) { prev.count++; }"
@criteria = Mongoid::Criteria.new(:all, 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 "#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(:all, 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 "#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 "#execute" do
context "filtering" do
before do
@collection = mock
Person.expects(:collection).returns(@collection)
@criteria = Mongoid::Criteria.new(:all).extras(:page => 1, :per_page => 20)
@collection.expects(:find).with(@criteria.selector, @criteria.options).returns([])
end
it "filters out unused params" do
@criteria.execute(Person)
@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(:all).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.execute(Person).should == []
@criteria.count.should == 44
end
end
context "when type is :first" 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(:first)
criteria.execute(Person).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(:first)
criteria.execute(Person).should be_nil
end
end
end
context "when type is not :first" do
it "calls find on the collection with the selector and options" do
criteria = Mongoid::Criteria.new(:all)
collection = mock
Person.expects(:collection).returns(collection)
collection.expects(:find).with(@criteria.selector, @criteria.options).returns([])
criteria.execute(Person).should == []
end
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(:all, 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 "#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 "#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(:all).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(:all).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(:all).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(:all)
end
it "returns nil" do
@criteria.offset.should be_nil
@criteria.options[:skip].should be_nil
end
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(:all).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(:all)
end
it "returns 1" do
@criteria.page.should == 1
end
end
end
describe "#per_page" do
context "when the per_page option exists" do
before do
@criteria = Mongoid::Criteria.new(:all).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(:all)
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
it "creates a criteria for an object id" do
id = Mongo::ObjectID.new.to_s
criteria = Mongoid::Criteria.translate(id)
criteria.selector.should == { :_id => id }
end
it "creates a criteria for a string" do
id = Mongo::ObjectID.new.to_s
criteria = Mongoid::Criteria.translate(id)
criteria.selector.should == { :_id => id }
end
end
context "multiple arguments" do
context "when :first, :conditions => {}" do
before do
@criteria = Mongoid::Criteria.translate(:first, :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 type :first" do
@criteria.type.should == :first
end
end
context "when :all, :conditions => {}" do
before do
@criteria = Mongoid::Criteria.translate(:all, :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 type :all" do
@criteria.type.should == :all
end
end
context "when :last, :conditions => {}" do
before do
@criteria = Mongoid::Criteria.translate(:last, :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 type :last" do
@criteria.type.should == :last
end
end
context "when options are provided" do
before do
@criteria = Mongoid::Criteria.translate(:last, :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