mongoid-pre 2.0.0.beta1 → 2.0.0.pre
Sign up to get free protection for your applications and to get access to all the features.
- data/.watchr +15 -10
- data/HISTORY +342 -0
- data/README.rdoc +8 -1
- data/Rakefile +2 -3
- data/VERSION +1 -1
- data/lib/mongoid/associations/{embedded_in.rb → belongs_to.rb} +4 -4
- data/lib/mongoid/associations/belongs_to_related.rb +9 -6
- data/lib/mongoid/associations/{embeds_many.rb → has_many.rb} +20 -33
- data/lib/mongoid/associations/has_many_related.rb +4 -28
- data/lib/mongoid/associations/{embeds_one.rb → has_one.rb} +6 -6
- data/lib/mongoid/associations/options.rb +6 -1
- data/lib/mongoid/associations.rb +32 -41
- data/lib/mongoid/attributes.rb +6 -13
- data/lib/mongoid/collection.rb +1 -2
- data/lib/mongoid/commands/delete.rb +1 -1
- data/lib/mongoid/commands/delete_all.rb +1 -4
- data/lib/mongoid/commands/destroy.rb +1 -1
- data/lib/mongoid/commands/destroy_all.rb +1 -3
- data/lib/mongoid/commands/save.rb +0 -1
- data/lib/mongoid/commands.rb +13 -2
- data/lib/mongoid/components.rb +1 -6
- data/lib/mongoid/config.rb +1 -5
- data/lib/mongoid/contexts/enumerable.rb +17 -54
- data/lib/mongoid/contexts/mongo.rb +38 -101
- data/lib/mongoid/contexts/paging.rb +2 -2
- data/lib/mongoid/contexts.rb +0 -21
- data/lib/mongoid/criteria.rb +73 -15
- data/lib/mongoid/criterion/inclusion.rb +0 -2
- data/lib/mongoid/criterion/optional.rb +2 -10
- data/lib/mongoid/document.rb +30 -41
- data/lib/mongoid/extensions.rb +0 -15
- data/lib/mongoid/field.rb +7 -20
- data/lib/mongoid/fields.rb +10 -15
- data/lib/mongoid/finders.rb +98 -10
- data/lib/mongoid/identity.rb +2 -8
- data/lib/mongoid/named_scope.rb +0 -2
- data/lib/mongoid/validations/associated.rb +8 -3
- data/lib/mongoid/validations/uniqueness.rb +7 -2
- data/lib/mongoid/validations.rb +2 -2
- data/lib/mongoid/versioning.rb +1 -1
- data/lib/mongoid.rb +5 -21
- data/mongoid.gemspec +19 -59
- data/spec/integration/mongoid/associations_spec.rb +3 -42
- data/spec/integration/mongoid/attributes_spec.rb +2 -2
- data/spec/integration/mongoid/commands_spec.rb +13 -27
- data/spec/integration/mongoid/contexts/enumerable_spec.rb +0 -13
- data/spec/integration/mongoid/criteria_spec.rb +3 -50
- data/spec/integration/mongoid/document_spec.rb +5 -72
- data/spec/integration/mongoid/finders_spec.rb +28 -85
- data/spec/models/address.rb +3 -3
- data/spec/models/animal.rb +2 -2
- data/spec/models/country_code.rb +2 -2
- data/spec/models/game.rb +1 -2
- data/spec/models/inheritance.rb +5 -5
- data/spec/models/location.rb +2 -2
- data/spec/models/name.rb +3 -3
- data/spec/models/namespacing.rb +2 -2
- data/spec/models/patient.rb +0 -2
- data/spec/models/person.rb +4 -6
- data/spec/models/pet.rb +3 -3
- data/spec/models/pet_owner.rb +3 -3
- data/spec/models/phone.rb +3 -3
- data/spec/models/post.rb +1 -1
- data/spec/models/translation.rb +2 -2
- data/spec/models/vet_visit.rb +2 -2
- data/spec/spec_helper.rb +2 -2
- data/spec/unit/mongoid/associations/belongs_to_related_spec.rb +0 -4
- data/spec/unit/mongoid/associations/{embedded_in_spec.rb → belongs_to_spec.rb} +11 -11
- data/spec/unit/mongoid/associations/has_many_related_spec.rb +14 -45
- data/spec/unit/mongoid/associations/{embeds_many_spec.rb → has_many_spec.rb} +34 -79
- data/spec/unit/mongoid/associations/{embeds_one_spec.rb → has_one_spec.rb} +18 -18
- data/spec/unit/mongoid/associations/options_spec.rb +19 -20
- data/spec/unit/mongoid/associations_spec.rb +12 -74
- data/spec/unit/mongoid/attributes_spec.rb +51 -83
- data/spec/unit/mongoid/collection_spec.rb +0 -46
- data/spec/unit/mongoid/commands/delete_all_spec.rb +8 -9
- data/spec/unit/mongoid/commands/delete_spec.rb +3 -6
- data/spec/unit/mongoid/commands/destroy_all_spec.rb +2 -0
- data/spec/unit/mongoid/commands/destroy_spec.rb +3 -10
- data/spec/unit/mongoid/commands_spec.rb +11 -20
- data/spec/unit/mongoid/config_spec.rb +0 -18
- data/spec/unit/mongoid/contexts/enumerable_spec.rb +11 -198
- data/spec/unit/mongoid/contexts/mongo_spec.rb +54 -357
- data/spec/unit/mongoid/criteria_spec.rb +78 -107
- data/spec/unit/mongoid/criterion/exclusion_spec.rb +13 -3
- data/spec/unit/mongoid/criterion/inclusion_spec.rb +19 -25
- data/spec/unit/mongoid/criterion/optional_spec.rb +18 -25
- data/spec/unit/mongoid/document_spec.rb +34 -71
- data/spec/unit/mongoid/extensions/array/conversions_spec.rb +2 -2
- data/spec/unit/mongoid/extensions/datetime/conversions_spec.rb +3 -0
- data/spec/unit/mongoid/extensions/object/conversions_spec.rb +2 -16
- data/spec/unit/mongoid/extensions/proc/scoping_spec.rb +1 -1
- data/spec/unit/mongoid/field_spec.rb +0 -62
- data/spec/unit/mongoid/fields_spec.rb +0 -33
- data/spec/unit/mongoid/finders_spec.rb +1 -37
- data/spec/unit/mongoid/identity_spec.rb +6 -47
- data/spec/unit/mongoid/named_scope_spec.rb +2 -15
- data/spec/unit/mongoid/scope_spec.rb +1 -1
- data/spec/unit/mongoid/validations/uniqueness_spec.rb +3 -3
- data/spec/unit/mongoid_spec.rb +2 -11
- metadata +14 -64
- data/lib/mongoid/associations/meta_data.rb +0 -28
- data/lib/mongoid/contexts/ids.rb +0 -25
- data/lib/mongoid/deprecation.rb +0 -22
- data/lib/mongoid/dirty.rb +0 -203
- data/lib/mongoid/extensions/big_decimal/conversions.rb +0 -19
- data/lib/mongoid/extensions/binary/conversions.rb +0 -17
- data/lib/mongoid/extensions/objectid/conversions.rb +0 -15
- data/lib/mongoid/extras.rb +0 -61
- data/lib/mongoid/javascript/functions.yml +0 -37
- data/lib/mongoid/javascript.rb +0 -21
- data/lib/mongoid/observable.rb +0 -30
- data/lib/mongoid/paths.rb +0 -54
- data/lib/mongoid/persistence/command.rb +0 -20
- data/lib/mongoid/persistence/insert.rb +0 -71
- data/lib/mongoid/persistence/update.rb +0 -78
- data/lib/mongoid/persistence.rb +0 -27
- data/lib/mongoid/state.rb +0 -32
- data/spec/integration/mongoid/dirty_spec.rb +0 -70
- data/spec/integration/mongoid/persistence/update_spec.rb +0 -46
- data/spec/models/callbacks.rb +0 -18
- data/spec/unit/mongoid/associations/meta_data_spec.rb +0 -88
- data/spec/unit/mongoid/contexts_spec.rb +0 -25
- data/spec/unit/mongoid/deprecation_spec.rb +0 -24
- data/spec/unit/mongoid/dirty_spec.rb +0 -286
- data/spec/unit/mongoid/extensions/big_decimal/conversions_spec.rb +0 -22
- data/spec/unit/mongoid/extensions/binary/conversions_spec.rb +0 -22
- data/spec/unit/mongoid/extras_spec.rb +0 -102
- data/spec/unit/mongoid/javascript_spec.rb +0 -48
- data/spec/unit/mongoid/observable_spec.rb +0 -46
- data/spec/unit/mongoid/paths_spec.rb +0 -124
- data/spec/unit/mongoid/persistence/insert_spec.rb +0 -175
- data/spec/unit/mongoid/persistence/update_spec.rb +0 -148
- data/spec/unit/mongoid/persistence_spec.rb +0 -40
- data/spec/unit/mongoid/state_spec.rb +0 -83
@@ -5,9 +5,9 @@ describe Mongoid::Contexts::Mongo do
|
|
5
5
|
describe "#aggregate" do
|
6
6
|
|
7
7
|
before do
|
8
|
-
@
|
9
|
-
@
|
10
|
-
@context = Mongoid::Contexts::Mongo.new(@
|
8
|
+
@selector = {}
|
9
|
+
@options = { :fields => [:field1] }
|
10
|
+
@context = Mongoid::Contexts::Mongo.new(@selector, @options, Person)
|
11
11
|
end
|
12
12
|
|
13
13
|
context "when klass not provided" do
|
@@ -19,78 +19,20 @@ describe Mongoid::Contexts::Mongo do
|
|
19
19
|
end
|
20
20
|
|
21
21
|
it "calls group on the collection with the aggregate js" do
|
22
|
-
@collection.expects(:group).with([:field1], {
|
22
|
+
@collection.expects(:group).with([:field1], {}, {:count => 0}, @reduce, true)
|
23
23
|
@context.aggregate
|
24
24
|
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
describe "#avg" do
|
29
|
-
|
30
|
-
before do
|
31
|
-
@reduce = Mongoid::Javascript.sum.gsub("[field]", "age")
|
32
|
-
@collection = mock
|
33
|
-
Person.expects(:collection).twice.returns(@collection)
|
34
|
-
@criteria = Mongoid::Criteria.new(Person)
|
35
|
-
@context = Mongoid::Contexts::Mongo.new(@criteria)
|
36
|
-
end
|
37
|
-
|
38
|
-
it "calls group on the collection with the aggregate js" do
|
39
|
-
@collection.expects(:group).with(
|
40
|
-
nil,
|
41
|
-
{:_type => {'$in' => ['Doctor', 'Person']}},
|
42
|
-
{:sum => "start"},
|
43
|
-
@reduce
|
44
|
-
).returns([{"sum" => 100.0}])
|
45
|
-
@cursor = mock(:count => 10)
|
46
|
-
@collection.expects(:find).returns(@cursor)
|
47
|
-
@context.avg(:age).should == 10
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
describe "blank?" do
|
52
|
-
|
53
|
-
before do
|
54
|
-
@criteria = Mongoid::Criteria.new(Game)
|
55
|
-
@criteria.where(:test => 'Testing')
|
56
|
-
@context = Mongoid::Contexts::Mongo.new(@criteria)
|
57
|
-
end
|
58
|
-
|
59
|
-
context "when a document exists" do
|
60
|
-
|
61
|
-
before do
|
62
|
-
@doc = mock
|
63
|
-
@collection = mock
|
64
|
-
Game.expects(:collection).returns(@collection)
|
65
|
-
@collection.expects(:find_one).with({ :test => "Testing" }, { :fields => [ :_id ] }).returns(@doc)
|
66
|
-
end
|
67
25
|
|
68
|
-
it "returns false" do
|
69
|
-
@context.blank?.should be_false
|
70
|
-
end
|
71
26
|
end
|
72
27
|
|
73
|
-
context "when a document does not exist" do
|
74
|
-
|
75
|
-
before do
|
76
|
-
@doc = mock
|
77
|
-
@collection = mock
|
78
|
-
Game.expects(:collection).returns(@collection)
|
79
|
-
@collection.expects(:find_one).with({ :test => "Testing" }, { :fields => [ :_id ] }).returns(nil)
|
80
|
-
end
|
81
|
-
|
82
|
-
it "returns true" do
|
83
|
-
@context.blank?.should be_true
|
84
|
-
end
|
85
|
-
end
|
86
28
|
end
|
87
29
|
|
88
30
|
describe "#count" do
|
89
31
|
|
90
32
|
before do
|
91
|
-
@
|
92
|
-
@
|
93
|
-
@context = Mongoid::Contexts::Mongo.new(@
|
33
|
+
@selector = { :_type => { "$in" => ["Doctor", "Person"] }, :test => "Testing" }
|
34
|
+
@options = {}
|
35
|
+
@context = Mongoid::Contexts::Mongo.new(@selector, @options, Person)
|
94
36
|
end
|
95
37
|
|
96
38
|
context "when criteria has not been executed" do
|
@@ -114,7 +56,7 @@ describe Mongoid::Contexts::Mongo do
|
|
114
56
|
end
|
115
57
|
|
116
58
|
it "returns the count from the cursor without creating the documents" do
|
117
|
-
@collection.expects(:find).with(@
|
59
|
+
@collection.expects(:find).with(@selector, {}).returns(@cursor)
|
118
60
|
@cursor.expects(:count).returns(10)
|
119
61
|
@context.count.should == 10
|
120
62
|
end
|
@@ -123,22 +65,6 @@ describe Mongoid::Contexts::Mongo do
|
|
123
65
|
|
124
66
|
end
|
125
67
|
|
126
|
-
describe "#distinct" do
|
127
|
-
|
128
|
-
before do
|
129
|
-
@criteria = Mongoid::Criteria.new(Person)
|
130
|
-
@criteria.where(:test => 'Testing')
|
131
|
-
@context = Mongoid::Contexts::Mongo.new(@criteria)
|
132
|
-
@collection = mock
|
133
|
-
Person.expects(:collection).returns(@collection)
|
134
|
-
end
|
135
|
-
|
136
|
-
it "returns delegates to distinct on the collection" do
|
137
|
-
@collection.expects(:distinct).with(:title, @criteria.selector).returns(["Sir"])
|
138
|
-
@context.distinct(:title).should == ["Sir"]
|
139
|
-
end
|
140
|
-
end
|
141
|
-
|
142
68
|
describe "#execute" do
|
143
69
|
|
144
70
|
let(:selector) { { :field => "value" } }
|
@@ -147,16 +73,8 @@ describe Mongoid::Contexts::Mongo do
|
|
147
73
|
before do
|
148
74
|
@cursor = stub(:count => 500)
|
149
75
|
@collection = mock
|
150
|
-
@klass = stub(
|
151
|
-
|
152
|
-
:hereditary => false,
|
153
|
-
:instantiate => @person,
|
154
|
-
:enslaved? => false,
|
155
|
-
:cached? => false
|
156
|
-
)
|
157
|
-
@criteria = Mongoid::Criteria.new(@klass)
|
158
|
-
@criteria.where(selector).skip(20)
|
159
|
-
@context = Mongoid::Contexts::Mongo.new(@criteria)
|
76
|
+
@klass = stub(:collection => @collection, :hereditary => false, :instantiate => @person)
|
77
|
+
@context = Mongoid::Contexts::Mongo.new(selector, options, @klass)
|
160
78
|
end
|
161
79
|
|
162
80
|
it "calls find on the collection" do
|
@@ -179,7 +97,7 @@ describe Mongoid::Contexts::Mongo do
|
|
179
97
|
context "when _type not in the field list" do
|
180
98
|
|
181
99
|
before do
|
182
|
-
|
100
|
+
options[:fields] = [ :title ]
|
183
101
|
@expected_options = { :skip => 20, :fields => [ :title, :_type ] }
|
184
102
|
end
|
185
103
|
|
@@ -197,10 +115,10 @@ describe Mongoid::Contexts::Mongo do
|
|
197
115
|
describe "#group" do
|
198
116
|
|
199
117
|
before do
|
200
|
-
@
|
201
|
-
@
|
118
|
+
@selector = { :_type => { "$in" => ["Doctor", "Person"] } }
|
119
|
+
@options = { :fields => [ :field1 ] }
|
202
120
|
@grouping = [{ "title" => "Sir", "group" => [{ "title" => "Sir", "age" => 30, "_type" => "Person" }] }]
|
203
|
-
@context = Mongoid::Contexts::Mongo.new(@
|
121
|
+
@context = Mongoid::Contexts::Mongo.new(@selector, @options, Person)
|
204
122
|
end
|
205
123
|
|
206
124
|
context "when klass provided" do
|
@@ -212,12 +130,7 @@ describe Mongoid::Contexts::Mongo do
|
|
212
130
|
end
|
213
131
|
|
214
132
|
it "calls group on the collection with the aggregate js" do
|
215
|
-
@collection.expects(:group).with(
|
216
|
-
[:field1],
|
217
|
-
{:_type => { "$in" => ["Doctor", "Person"] }},
|
218
|
-
{:group => []},
|
219
|
-
@reduce
|
220
|
-
).returns(@grouping)
|
133
|
+
@collection.expects(:group).with([:field1], {:_type => { "$in" => ["Doctor", "Person"] }}, {:group => []}, @reduce, true).returns(@grouping)
|
221
134
|
@context.group
|
222
135
|
end
|
223
136
|
|
@@ -230,118 +143,21 @@ describe Mongoid::Contexts::Mongo do
|
|
230
143
|
let(:selector) { { :field => "value" } }
|
231
144
|
let(:options) { { :skip => 20 } }
|
232
145
|
let(:klass) { Person }
|
233
|
-
let(:criteria) { Mongoid::Criteria.new(klass) }
|
234
|
-
let(:context) { Mongoid::Contexts::Mongo.new(criteria) }
|
235
146
|
|
236
147
|
before do
|
237
|
-
|
148
|
+
@context = Mongoid::Contexts::Mongo.new(selector, options, klass)
|
238
149
|
end
|
239
150
|
|
240
151
|
it "sets the selector" do
|
241
|
-
context.selector.should ==
|
152
|
+
@context.selector.should == selector
|
242
153
|
end
|
243
154
|
|
244
155
|
it "sets the options" do
|
245
|
-
context.options.should == options
|
156
|
+
@context.options.should == options
|
246
157
|
end
|
247
158
|
|
248
159
|
it "sets the klass" do
|
249
|
-
context.klass.should == klass
|
250
|
-
end
|
251
|
-
|
252
|
-
context "when persisting type" do
|
253
|
-
|
254
|
-
it "set the selector to query across the _type when it is hereditary" do
|
255
|
-
context.selector[:_type].should == {'$in' => Person._types}
|
256
|
-
end
|
257
|
-
|
258
|
-
end
|
259
|
-
|
260
|
-
context "when not persisting type" do
|
261
|
-
|
262
|
-
before do
|
263
|
-
Mongoid.persist_types = false
|
264
|
-
end
|
265
|
-
|
266
|
-
after do
|
267
|
-
Mongoid.persist_types = true
|
268
|
-
end
|
269
|
-
|
270
|
-
it "does not add the type to the selector" do
|
271
|
-
context.selector[:_type].should be_nil
|
272
|
-
end
|
273
|
-
|
274
|
-
end
|
275
|
-
|
276
|
-
context "enslaved and cached classes" do
|
277
|
-
|
278
|
-
let(:klass) { Game }
|
279
|
-
|
280
|
-
it "enslaves the criteria" do
|
281
|
-
context.criteria.should be_enslaved
|
282
|
-
end
|
283
|
-
|
284
|
-
it "caches the criteria" do
|
285
|
-
context.criteria.should be_cached
|
286
|
-
end
|
287
|
-
end
|
288
|
-
end
|
289
|
-
|
290
|
-
describe "#iterate" do
|
291
|
-
before do
|
292
|
-
@criteria = Mongoid::Criteria.new(Person)
|
293
|
-
@context = Mongoid::Contexts::Mongo.new(@criteria)
|
294
|
-
@person = Person.new(:title => "Sir")
|
295
|
-
@cursor = stub('cursor')
|
296
|
-
@cursor.stubs(:each).yields(@person)
|
297
|
-
end
|
298
|
-
|
299
|
-
context "when not caching" do
|
300
|
-
|
301
|
-
it "executes the criteria" do
|
302
|
-
@context.expects(:execute).returns(@cursor)
|
303
|
-
@context.iterate do |person|
|
304
|
-
person.should == @person
|
305
|
-
end
|
306
|
-
|
307
|
-
end
|
308
|
-
|
309
|
-
end
|
310
|
-
|
311
|
-
context "when caching" do
|
312
|
-
before do
|
313
|
-
@criteria.cache
|
314
|
-
end
|
315
|
-
|
316
|
-
it "executes the criteria" do
|
317
|
-
@context.expects(:execute).returns(@cursor)
|
318
|
-
@context.iterate do |person|
|
319
|
-
person.should == @person
|
320
|
-
end
|
321
|
-
end
|
322
|
-
|
323
|
-
it "executes only once and it caches the result" do
|
324
|
-
@context.expects(:execute).once.returns(@cursor)
|
325
|
-
@context.iterate do |person|
|
326
|
-
person.should == @person
|
327
|
-
end
|
328
|
-
@context.iterate do |person|
|
329
|
-
person.should == @person
|
330
|
-
end
|
331
|
-
end
|
332
|
-
|
333
|
-
it "executes even if there is no block" do
|
334
|
-
@context.expects(:execute).once.returns(@cursor)
|
335
|
-
@context.iterate
|
336
|
-
end
|
337
|
-
|
338
|
-
it "caches even if there is no block" do
|
339
|
-
@context.expects(:execute).once.returns(@cursor)
|
340
|
-
@context.iterate
|
341
|
-
@context.iterate do |person|
|
342
|
-
person.should == @person
|
343
|
-
end
|
344
|
-
end
|
160
|
+
@context.klass.should == klass
|
345
161
|
end
|
346
162
|
|
347
163
|
end
|
@@ -356,13 +172,10 @@ describe Mongoid::Contexts::Mongo do
|
|
356
172
|
context "when documents exist" do
|
357
173
|
|
358
174
|
before do
|
359
|
-
@
|
360
|
-
@
|
361
|
-
@context = Mongoid::Contexts::Mongo.new(@
|
362
|
-
@collection.expects(:find_one).with(
|
363
|
-
{:_type => {'$in' => ['Doctor', 'Person']}},
|
364
|
-
{ :sort => [[:title, :desc]] }
|
365
|
-
).returns(
|
175
|
+
@selector = {}
|
176
|
+
@options = { :sort => [[:title, :asc]] }
|
177
|
+
@context = Mongoid::Contexts::Mongo.new(@selector, @options, Person)
|
178
|
+
@collection.expects(:find_one).with(@selector, { :sort => [[:title, :desc]] }).returns(
|
366
179
|
{ "title" => "Sir", "_type" => "Person" }
|
367
180
|
)
|
368
181
|
end
|
@@ -376,13 +189,10 @@ describe Mongoid::Contexts::Mongo do
|
|
376
189
|
context "when no documents exist" do
|
377
190
|
|
378
191
|
before do
|
379
|
-
@
|
380
|
-
@
|
381
|
-
@context = Mongoid::Contexts::Mongo.new(@
|
382
|
-
@collection.expects(:find_one).with(
|
383
|
-
{:_type => {'$in' => ['Doctor', 'Person']}},
|
384
|
-
{ :sort => [[:_id, :desc]] }
|
385
|
-
).returns(nil)
|
192
|
+
@selector = {}
|
193
|
+
@options = { :sort => [[:_id, :asc]] }
|
194
|
+
@context = Mongoid::Contexts::Mongo.new(@selector, @options, Person)
|
195
|
+
@collection.expects(:find_one).with(@selector, { :sort => [[:_id, :desc]] }).returns(nil)
|
386
196
|
end
|
387
197
|
|
388
198
|
it "returns nil" do
|
@@ -394,13 +204,10 @@ describe Mongoid::Contexts::Mongo do
|
|
394
204
|
context "when no sorting options provided" do
|
395
205
|
|
396
206
|
before do
|
397
|
-
@
|
398
|
-
@
|
399
|
-
@context = Mongoid::Contexts::Mongo.new(@
|
400
|
-
@collection.expects(:find_one).with(
|
401
|
-
{:_type => {'$in' => ['Doctor', 'Person']}},
|
402
|
-
{ :sort => [[:_id, :desc]] }
|
403
|
-
).returns(
|
207
|
+
@selector = {}
|
208
|
+
@options = { :sort => [[:_id, :asc]] }
|
209
|
+
@context = Mongoid::Contexts::Mongo.new(@selector, @options, Person)
|
210
|
+
@collection.expects(:find_one).with(@selector, { :sort => [[:_id, :desc]] }).returns(
|
404
211
|
{ "title" => "Sir", "_type" => "Person" }
|
405
212
|
)
|
406
213
|
end
|
@@ -416,19 +223,19 @@ describe Mongoid::Contexts::Mongo do
|
|
416
223
|
describe "#max" do
|
417
224
|
|
418
225
|
before do
|
419
|
-
@reduce = Mongoid::
|
226
|
+
@reduce = Mongoid::Contexts::Mongo::MAX_REDUCE.gsub("[field]", "age")
|
420
227
|
@collection = mock
|
421
228
|
Person.expects(:collection).returns(@collection)
|
422
|
-
@
|
423
|
-
@context = Mongoid::Contexts::Mongo.new(@criteria)
|
229
|
+
@context = Mongoid::Contexts::Mongo.new({}, {}, Person)
|
424
230
|
end
|
425
231
|
|
426
232
|
it "calls group on the collection with the aggregate js" do
|
427
233
|
@collection.expects(:group).with(
|
428
234
|
nil,
|
429
|
-
{
|
235
|
+
{},
|
430
236
|
{:max => "start"},
|
431
|
-
@reduce
|
237
|
+
@reduce,
|
238
|
+
true
|
432
239
|
).returns([{"max" => 200.0}])
|
433
240
|
@context.max(:age).should == 200.0
|
434
241
|
end
|
@@ -438,19 +245,19 @@ describe Mongoid::Contexts::Mongo do
|
|
438
245
|
describe "#min" do
|
439
246
|
|
440
247
|
before do
|
441
|
-
@reduce = Mongoid::
|
248
|
+
@reduce = Mongoid::Contexts::Mongo::MIN_REDUCE.gsub("[field]", "age")
|
442
249
|
@collection = mock
|
443
250
|
Person.expects(:collection).returns(@collection)
|
444
|
-
@
|
445
|
-
@context = Mongoid::Contexts::Mongo.new(@criteria)
|
251
|
+
@context = Mongoid::Contexts::Mongo.new({}, {}, Person)
|
446
252
|
end
|
447
253
|
|
448
254
|
it "calls group on the collection with the aggregate js" do
|
449
255
|
@collection.expects(:group).with(
|
450
256
|
nil,
|
451
|
-
{
|
257
|
+
{},
|
452
258
|
{:min => "start"},
|
453
|
-
@reduce
|
259
|
+
@reduce,
|
260
|
+
true
|
454
261
|
).returns([{"min" => 4.0}])
|
455
262
|
@context.min(:age).should == 4.0
|
456
263
|
end
|
@@ -462,10 +269,10 @@ describe Mongoid::Contexts::Mongo do
|
|
462
269
|
context "when documents exist" do
|
463
270
|
|
464
271
|
before do
|
272
|
+
@collection = mock
|
465
273
|
Person.expects(:collection).returns(@collection)
|
466
|
-
@
|
467
|
-
@
|
468
|
-
@collection.expects(:find_one).with({:_type => {'$in' => ['Doctor', 'Person']}}, {}).returns(
|
274
|
+
@context = Mongoid::Contexts::Mongo.new({}, {}, Person)
|
275
|
+
@collection.expects(:find_one).with({}, {}).returns(
|
469
276
|
{ "title"=> "Sir", "_type" => "Person" }
|
470
277
|
)
|
471
278
|
end
|
@@ -481,9 +288,8 @@ describe Mongoid::Contexts::Mongo do
|
|
481
288
|
before do
|
482
289
|
@collection = mock
|
483
290
|
Person.expects(:collection).returns(@collection)
|
484
|
-
@
|
485
|
-
@
|
486
|
-
@collection.expects(:find_one).with({:_type => {'$in' => ['Doctor', 'Person']}}, {}).returns(nil)
|
291
|
+
@context = Mongoid::Contexts::Mongo.new({}, {}, Person)
|
292
|
+
@collection.expects(:find_one).with({}, {}).returns(nil)
|
487
293
|
end
|
488
294
|
|
489
295
|
it "returns nil" do
|
@@ -500,7 +306,7 @@ describe Mongoid::Contexts::Mongo do
|
|
500
306
|
|
501
307
|
before do
|
502
308
|
@criteria = Mongoid::Criteria.new(Person).extras({ :page => 5 })
|
503
|
-
@context = Mongoid::Contexts::Mongo.new(@criteria)
|
309
|
+
@context = Mongoid::Contexts::Mongo.new({}, @criteria.options, Person)
|
504
310
|
end
|
505
311
|
|
506
312
|
it "returns the page option" do
|
@@ -513,7 +319,7 @@ describe Mongoid::Contexts::Mongo do
|
|
513
319
|
|
514
320
|
before do
|
515
321
|
@criteria = Mongoid::Criteria.new(Person)
|
516
|
-
@context = Mongoid::Contexts::Mongo.new(@criteria)
|
322
|
+
@context = Mongoid::Contexts::Mongo.new({}, @criteria.options, Person)
|
517
323
|
end
|
518
324
|
|
519
325
|
it "returns 1" do
|
@@ -530,7 +336,7 @@ describe Mongoid::Contexts::Mongo do
|
|
530
336
|
@collection = mock
|
531
337
|
Person.expects(:collection).returns(@collection)
|
532
338
|
@criteria = Person.where(:_id => "1").skip(60).limit(20)
|
533
|
-
@context = Mongoid::Contexts::Mongo.new(@criteria)
|
339
|
+
@context = Mongoid::Contexts::Mongo.new(@criteria.selector, @criteria.options, Person)
|
534
340
|
@collection.expects(:find).with(
|
535
341
|
{:_type => { "$in" => ["Doctor", "Person"] }, :_id => "1"}, :skip => 60, :limit => 20
|
536
342
|
).returns([])
|
@@ -549,19 +355,19 @@ describe Mongoid::Contexts::Mongo do
|
|
549
355
|
context "when klass not provided" do
|
550
356
|
|
551
357
|
before do
|
552
|
-
@reduce = Mongoid::
|
358
|
+
@reduce = Mongoid::Contexts::Mongo::SUM_REDUCE.gsub("[field]", "age")
|
553
359
|
@collection = mock
|
554
|
-
@
|
555
|
-
@context = Mongoid::Contexts::Mongo.new(@criteria)
|
360
|
+
@context = Mongoid::Contexts::Mongo.new({}, {}, Person)
|
556
361
|
Person.expects(:collection).returns(@collection)
|
557
362
|
end
|
558
363
|
|
559
364
|
it "calls group on the collection with the aggregate js" do
|
560
365
|
@collection.expects(:group).with(
|
561
366
|
nil,
|
562
|
-
{
|
367
|
+
{},
|
563
368
|
{:sum => "start"},
|
564
|
-
@reduce
|
369
|
+
@reduce,
|
370
|
+
true
|
565
371
|
).returns([{"sum" => 50.0}])
|
566
372
|
@context.sum(:age).should == 50.0
|
567
373
|
end
|
@@ -570,113 +376,4 @@ describe Mongoid::Contexts::Mongo do
|
|
570
376
|
|
571
377
|
end
|
572
378
|
|
573
|
-
context "#id_criteria" do
|
574
|
-
|
575
|
-
let(:criteria) { Mongoid::Criteria.new(Person) }
|
576
|
-
let(:context) { criteria.context }
|
577
|
-
|
578
|
-
context "with a single argument" do
|
579
|
-
|
580
|
-
let(:id) { Mongo::ObjectID.new.to_s }
|
581
|
-
|
582
|
-
before do
|
583
|
-
criteria.expects(:id).with(id).returns(criteria)
|
584
|
-
end
|
585
|
-
|
586
|
-
context "when the document is found" do
|
587
|
-
|
588
|
-
let(:document) { stub }
|
589
|
-
|
590
|
-
it "returns a matching document" do
|
591
|
-
context.expects(:one).returns(document)
|
592
|
-
document.expects(:blank? => false)
|
593
|
-
context.id_criteria(id).should == document
|
594
|
-
end
|
595
|
-
|
596
|
-
end
|
597
|
-
|
598
|
-
context "when the document is not found" do
|
599
|
-
|
600
|
-
it "raises an error" do
|
601
|
-
context.expects(:one).returns(nil)
|
602
|
-
lambda { context.id_criteria(id) }.should raise_error
|
603
|
-
end
|
604
|
-
|
605
|
-
end
|
606
|
-
|
607
|
-
end
|
608
|
-
|
609
|
-
context "multiple arguments" do
|
610
|
-
|
611
|
-
context "when an array of ids" do
|
612
|
-
|
613
|
-
let(:ids) do
|
614
|
-
(0..2).inject([]) { |ary, i| ary << Mongo::ObjectID.new.to_s }
|
615
|
-
end
|
616
|
-
|
617
|
-
context "when documents are found" do
|
618
|
-
|
619
|
-
let(:docs) do
|
620
|
-
(0..2).inject([]) { |ary, i| ary << stub }
|
621
|
-
end
|
622
|
-
|
623
|
-
before do
|
624
|
-
criteria.expects(:id).with(ids).returns(criteria)
|
625
|
-
end
|
626
|
-
|
627
|
-
it "returns matching documents" do
|
628
|
-
context.expects(:execute).returns(docs)
|
629
|
-
context.id_criteria(ids).should == docs
|
630
|
-
end
|
631
|
-
|
632
|
-
end
|
633
|
-
|
634
|
-
context "when documents are not found" do
|
635
|
-
|
636
|
-
it "raises an error" do
|
637
|
-
context.expects(:execute).returns([])
|
638
|
-
lambda { context.id_criteria(ids) }.should raise_error
|
639
|
-
end
|
640
|
-
|
641
|
-
end
|
642
|
-
|
643
|
-
end
|
644
|
-
|
645
|
-
context "when an array of object ids" do
|
646
|
-
|
647
|
-
let(:ids) do
|
648
|
-
(0..2).inject([]) { |ary, i| ary << Mongo::ObjectID.new }
|
649
|
-
end
|
650
|
-
|
651
|
-
context "when documents are found" do
|
652
|
-
|
653
|
-
let(:docs) do
|
654
|
-
(0..2).inject([]) { |ary, i| ary << stub }
|
655
|
-
end
|
656
|
-
|
657
|
-
before do
|
658
|
-
criteria.expects(:id).with(ids).returns(criteria)
|
659
|
-
end
|
660
|
-
|
661
|
-
it "returns matching documents" do
|
662
|
-
context.expects(:execute).returns(docs)
|
663
|
-
context.id_criteria(ids).should == docs
|
664
|
-
end
|
665
|
-
|
666
|
-
end
|
667
|
-
|
668
|
-
context "when documents are not found" do
|
669
|
-
|
670
|
-
it "raises an error" do
|
671
|
-
context.expects(:execute).returns([])
|
672
|
-
lambda { context.id_criteria(ids) }.should raise_error
|
673
|
-
end
|
674
|
-
|
675
|
-
end
|
676
|
-
|
677
|
-
end
|
678
|
-
end
|
679
|
-
|
680
|
-
end
|
681
|
-
|
682
379
|
end
|