mongoid_monkey 0.2.5 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,17 @@
1
+ class Video
2
+ include Mongoid::Document
3
+ field :title, type: String
4
+ field :year, type: Integer
5
+ field :release_dates, type: Set
6
+ field :genres, type: Array
7
+
8
+ embedded_in :person
9
+ belongs_to :post
10
+ belongs_to :game
11
+
12
+ default_scope asc(:title)
13
+
14
+ attr_accessible :title, as: [ :default, :admin ]
15
+ attr_accessible :year, as: [ :default ]
16
+ attr_accessible :person_attributes, as: [ :default ]
17
+ end
@@ -0,0 +1,495 @@
1
+ require "spec_helper"
2
+
3
+ if Mongoid::VERSION =~ /\A3\./
4
+
5
+ describe Mongoid::Contextual::Aggregable::Mongo do
6
+
7
+ before(:each) do
8
+ Mongoid.purge!
9
+ end
10
+
11
+ describe "#aggregates" do
12
+
13
+ context "when provided a single field" do
14
+
15
+ let!(:depeche) do
16
+ Band.create(name: "Depeche Mode", likes: 1000, years: 1000)
17
+ end
18
+
19
+ let!(:tool) do
20
+ Band.create(name: "Tool", likes: 500, years: 800)
21
+ end
22
+
23
+ let(:criteria) do
24
+ Band.all
25
+ end
26
+
27
+ let(:context) do
28
+ Mongoid::Contextual::Mongo.new(criteria)
29
+ end
30
+
31
+ context "when aggregating on a field that exists" do
32
+
33
+ context "when aggregating on an aliased field" do
34
+
35
+ let(:aggregates) do
36
+ context.aggregates(:years)
37
+ end
38
+
39
+ it "returns an avg" do
40
+ aggregates["avg"].should eq(900)
41
+ end
42
+
43
+ it "returns a count" do
44
+ aggregates["count"].should eq(2)
45
+ end
46
+
47
+ it "returns a max" do
48
+ aggregates["max"].should eq(1000)
49
+ end
50
+
51
+ it "returns a min" do
52
+ aggregates["min"].should eq(800)
53
+ end
54
+
55
+ it "returns a sum" do
56
+ aggregates["sum"].should eq(1800)
57
+ end
58
+ end
59
+
60
+ context "when more than 1 document is emitted" do
61
+
62
+ let(:aggregates) do
63
+ context.aggregates(:likes)
64
+ end
65
+
66
+ it "returns an avg" do
67
+ aggregates["avg"].should eq(750)
68
+ end
69
+
70
+ it "returns a count" do
71
+ aggregates["count"].should eq(2)
72
+ end
73
+
74
+ it "returns a max" do
75
+ aggregates["max"].should eq(1000)
76
+ end
77
+
78
+ it "returns a min" do
79
+ aggregates["min"].should eq(500)
80
+ end
81
+
82
+ it "returns a sum" do
83
+ aggregates["sum"].should eq(1500)
84
+ end
85
+ end
86
+
87
+ context "when only 1 document is emitted" do
88
+
89
+ let(:criteria) do
90
+ Band.where(name: "Depeche Mode")
91
+ end
92
+
93
+ let(:aggregates) do
94
+ context.aggregates(:likes)
95
+ end
96
+
97
+ it "returns an avg" do
98
+ aggregates["avg"].should eq(1000)
99
+ end
100
+
101
+ it "returns a count" do
102
+ aggregates["count"].should eq(1)
103
+ end
104
+
105
+ it "returns a max" do
106
+ aggregates["max"].should eq(1000)
107
+ end
108
+
109
+ it "returns a min" do
110
+ aggregates["min"].should eq(1000)
111
+ end
112
+
113
+ it "returns a sum" do
114
+ aggregates["sum"].should eq(1000)
115
+ end
116
+ end
117
+
118
+ context "when only 1 document is emitted because of sorting, skip and limit" do
119
+
120
+ let(:criteria) do
121
+ Band.desc(:name).skip(1).limit(1)
122
+ end
123
+
124
+ let(:aggregates) do
125
+ context.aggregates(:likes)
126
+ end
127
+
128
+ it "returns an avg" do
129
+ expect(aggregates["avg"]).to eq(1000)
130
+ end
131
+
132
+ it "returns a count of documents with that field" do
133
+ expect(aggregates["count"]).to eq(1)
134
+ end
135
+
136
+ it "returns a max" do
137
+ expect(aggregates["max"]).to eq(1000)
138
+ end
139
+
140
+ it "returns a min" do
141
+ expect(aggregates["min"]).to eq(1000)
142
+ end
143
+
144
+ it "returns a sum" do
145
+ expect(aggregates["sum"]).to eq(1000)
146
+ end
147
+ end
148
+
149
+ end
150
+
151
+ context "when the field does not exist" do
152
+
153
+ let(:aggregates) do
154
+ context.aggregates(:non_existant)
155
+ end
156
+
157
+ it "returns an avg" do
158
+ aggregates["avg"].should eq(0)
159
+ end
160
+
161
+ it "returns a count" do
162
+ aggregates["count"].should eq(2)
163
+ end
164
+
165
+ it "returns a max" do
166
+ aggregates["max"].should be_nil
167
+ end
168
+
169
+ it "returns a min" do
170
+ aggregates["min"].should be_nil
171
+ end
172
+
173
+ it "returns a sum" do
174
+ aggregates["sum"].should eq(0)
175
+ end
176
+ end
177
+
178
+ context "when the field sometimes exists" do
179
+ let!(:oasis) do
180
+ Band.create(name: "Oasis", likes: 50)
181
+ end
182
+
183
+ let!(:radiohead) do
184
+ Band.create(name: "Radiohead")
185
+ end
186
+
187
+ context "and the field doesn't exist on the last document" do
188
+ let(:criteria) do
189
+ Band.all
190
+ end
191
+
192
+ let(:context) do
193
+ Mongoid::Contextual::Mongo.new(criteria)
194
+ end
195
+
196
+ let(:aggregates) do
197
+ context.aggregates(:likes)
198
+ end
199
+
200
+ it "returns a min" do
201
+ aggregates["min"].should eq(50)
202
+ end
203
+ end
204
+
205
+ context "and the field doesn't exist on the before-last document" do
206
+ let!(:u2) do
207
+ Band.create(name: "U2", likes: 100)
208
+ end
209
+
210
+ let(:criteria) do
211
+ Band.all
212
+ end
213
+
214
+ let(:context) do
215
+ Mongoid::Contextual::Mongo.new(criteria)
216
+ end
217
+
218
+ let(:aggregates) do
219
+ context.aggregates(:likes)
220
+ end
221
+
222
+ it "returns a min" do
223
+ aggregates["min"].should eq(50)
224
+ end
225
+ end
226
+ end
227
+
228
+ context "when there are no matching documents" do
229
+
230
+ let(:criteria) do
231
+ Band.where(name: "New Order")
232
+ end
233
+
234
+ let(:aggregates) do
235
+ context.aggregates(:non_existant)
236
+ end
237
+
238
+ it "returns nil" do
239
+ aggregates.should eq({ "count" => 0 })
240
+ end
241
+ end
242
+ end
243
+ end
244
+
245
+ describe "#avg" do
246
+
247
+ context "when provided a single field" do
248
+
249
+ context "when there are matching documents" do
250
+
251
+ let!(:depeche) do
252
+ Band.create(name: "Depeche Mode", likes: 1000)
253
+ end
254
+
255
+ let!(:tool) do
256
+ Band.create(name: "Tool", likes: 500)
257
+ end
258
+
259
+ let(:criteria) do
260
+ Band.all
261
+ end
262
+
263
+ let(:context) do
264
+ Mongoid::Contextual::Mongo.new(criteria)
265
+ end
266
+
267
+ let(:avg) do
268
+ context.avg(:likes)
269
+ end
270
+
271
+ it "returns the avg of the provided field" do
272
+ avg.should eq(750)
273
+ end
274
+ end
275
+
276
+ context "when no documents match" do
277
+
278
+ let!(:depeche) do
279
+ Band.create(name: "Depeche Mode", likes: 1000)
280
+ end
281
+
282
+ let(:criteria) do
283
+ Band.where(name: "New Order")
284
+ end
285
+
286
+ let(:context) do
287
+ Mongoid::Contextual::Mongo.new(criteria)
288
+ end
289
+
290
+ let(:avg) do
291
+ context.avg(:likes)
292
+ end
293
+
294
+ it "returns nil" do
295
+ avg.should be_nil
296
+ end
297
+ end
298
+ end
299
+ end
300
+
301
+ describe "#max" do
302
+
303
+ context "when provided a single field" do
304
+
305
+ let!(:depeche) do
306
+ Band.create(name: "Depeche Mode", likes: 1000)
307
+ end
308
+
309
+ let!(:tool) do
310
+ Band.create(name: "Tool", likes: 500)
311
+ end
312
+
313
+ let(:criteria) do
314
+ Band.all
315
+ end
316
+
317
+ let(:context) do
318
+ Mongoid::Contextual::Mongo.new(criteria)
319
+ end
320
+
321
+ context "when provided a symbol" do
322
+
323
+ let(:max) do
324
+ context.max(:likes)
325
+ end
326
+
327
+ it "returns the max of the provided field" do
328
+ max.should eq(1000)
329
+ end
330
+
331
+ context "when no documents match" do
332
+
333
+ let(:criteria) do
334
+ Band.where(name: "New Order")
335
+ end
336
+
337
+ let(:context) do
338
+ Mongoid::Contextual::Mongo.new(criteria)
339
+ end
340
+
341
+ let(:max) do
342
+ context.max(:likes)
343
+ end
344
+
345
+ it "returns nil" do
346
+ max.should be_nil
347
+ end
348
+ end
349
+ end
350
+
351
+ context "when provided a block" do
352
+
353
+ let(:max) do
354
+ context.max do |a, b|
355
+ a.likes <=> b.likes
356
+ end
357
+ end
358
+
359
+ it "returns the document with the max value for the field" do
360
+ max.should eq(depeche)
361
+ end
362
+ end
363
+ end
364
+ end
365
+
366
+ describe "#min" do
367
+
368
+ context "when provided a single field" do
369
+
370
+ let!(:depeche) do
371
+ Band.create(name: "Depeche Mode", likes: 1000)
372
+ end
373
+
374
+ let!(:tool) do
375
+ Band.create(name: "Tool", likes: 500)
376
+ end
377
+
378
+ let(:criteria) do
379
+ Band.all
380
+ end
381
+
382
+ let(:context) do
383
+ Mongoid::Contextual::Mongo.new(criteria)
384
+ end
385
+
386
+ context "when provided a symbol" do
387
+
388
+ let(:min) do
389
+ context.min(:likes)
390
+ end
391
+
392
+ it "returns the min of the provided field" do
393
+ min.should eq(500)
394
+ end
395
+
396
+ context "when no documents match" do
397
+
398
+ let(:criteria) do
399
+ Band.where(name: "New Order")
400
+ end
401
+
402
+ let(:context) do
403
+ Mongoid::Contextual::Mongo.new(criteria)
404
+ end
405
+
406
+ let(:min) do
407
+ context.min(:likes)
408
+ end
409
+
410
+ it "returns nil" do
411
+ min.should be_nil
412
+ end
413
+ end
414
+ end
415
+
416
+ context "when provided a block" do
417
+
418
+ let(:min) do
419
+ context.min do |a, b|
420
+ a.likes <=> b.likes
421
+ end
422
+ end
423
+
424
+ it "returns the document with the min value for the field" do
425
+ min.should eq(tool)
426
+ end
427
+ end
428
+ end
429
+ end
430
+
431
+ describe "#sum" do
432
+
433
+ context "when provided a single field" do
434
+
435
+ let!(:depeche) do
436
+ Band.create(name: "Depeche Mode", likes: 1000)
437
+ end
438
+
439
+ let!(:tool) do
440
+ Band.create(name: "Tool", likes: 500)
441
+ end
442
+
443
+ let(:criteria) do
444
+ Band.all
445
+ end
446
+
447
+ let(:context) do
448
+ Mongoid::Contextual::Mongo.new(criteria)
449
+ end
450
+
451
+ context "when provided a symbol" do
452
+
453
+ let(:sum) do
454
+ context.sum(:likes)
455
+ end
456
+
457
+ it "returns the sum of the provided field" do
458
+ sum.should eq(1500)
459
+ end
460
+
461
+ context "when no documents match" do
462
+
463
+ let(:criteria) do
464
+ Band.where(name: "New Order")
465
+ end
466
+
467
+ let(:context) do
468
+ Mongoid::Contextual::Mongo.new(criteria)
469
+ end
470
+
471
+ let(:sum) do
472
+ context.sum(:likes)
473
+ end
474
+
475
+ it "returns zero" do
476
+ sum.should eq(0)
477
+ end
478
+ end
479
+ end
480
+
481
+ context "when provided a block" do
482
+
483
+ let(:sum) do
484
+ context.sum(&:likes)
485
+ end
486
+
487
+ it "returns the sum for the provided block" do
488
+ sum.should eq(1500)
489
+ end
490
+ end
491
+ end
492
+ end
493
+ end
494
+
495
+ end