mongomodel 0.4.6 → 0.4.7
Sign up to get free protection for your applications and to get access to all the features.
- data/Appraisals +12 -0
- data/bin/console +1 -2
- data/gemfiles/mongo_mapper.gemfile +12 -0
- data/gemfiles/mongoid.gemfile +12 -0
- data/lib/mongomodel.rb +3 -0
- data/lib/mongomodel/compatibility/mongo_mapper.rb +23 -0
- data/lib/mongomodel/compatibility/mongoid.rb +17 -0
- data/lib/mongomodel/concerns/properties.rb +5 -0
- data/lib/mongomodel/concerns/validations.rb +5 -3
- data/lib/mongomodel/support/core_extensions.rb +4 -4
- data/lib/mongomodel/support/mongo_options.rb +4 -2
- data/lib/mongomodel/support/mongo_order.rb +4 -0
- data/lib/mongomodel/support/scope.rb +1 -1
- data/lib/mongomodel/version.rb +1 -1
- data/spec/mongomodel/attributes/store_spec.rb +12 -12
- data/spec/mongomodel/concerns/associations/belongs_to_spec.rb +13 -13
- data/spec/mongomodel/concerns/associations/has_many_by_foreign_key_spec.rb +25 -25
- data/spec/mongomodel/concerns/associations/has_many_by_ids_spec.rb +25 -25
- data/spec/mongomodel/concerns/attribute_methods/before_type_cast_spec.rb +5 -5
- data/spec/mongomodel/concerns/attribute_methods/dirty_spec.rb +21 -21
- data/spec/mongomodel/concerns/attribute_methods/multi_parameter_assignment_spec.rb +3 -3
- data/spec/mongomodel/concerns/attribute_methods/protected_spec.rb +10 -10
- data/spec/mongomodel/concerns/attribute_methods/query_spec.rb +6 -6
- data/spec/mongomodel/concerns/attribute_methods/read_spec.rb +6 -6
- data/spec/mongomodel/concerns/attribute_methods/write_spec.rb +5 -5
- data/spec/mongomodel/concerns/attribute_methods_spec.rb +5 -5
- data/spec/mongomodel/concerns/attributes_spec.rb +13 -13
- data/spec/mongomodel/concerns/callbacks_spec.rb +11 -11
- data/spec/mongomodel/concerns/logging_spec.rb +2 -2
- data/spec/mongomodel/concerns/observing_spec.rb +3 -3
- data/spec/mongomodel/concerns/pretty_inspect_spec.rb +5 -5
- data/spec/mongomodel/concerns/properties_spec.rb +6 -6
- data/spec/mongomodel/concerns/serialization/json_serialization_spec.rb +6 -6
- data/spec/mongomodel/concerns/timestamps_spec.rb +10 -10
- data/spec/mongomodel/concerns/translation_spec.rb +1 -1
- data/spec/mongomodel/concerns/validations_spec.rb +12 -4
- data/spec/mongomodel/document/callbacks_spec.rb +10 -10
- data/spec/mongomodel/document/collection_modifiers_spec.rb +1 -1
- data/spec/mongomodel/document/dynamic_finders_spec.rb +5 -5
- data/spec/mongomodel/document/finders_spec.rb +9 -9
- data/spec/mongomodel/document/indexes_spec.rb +19 -19
- data/spec/mongomodel/document/optimistic_locking_spec.rb +8 -8
- data/spec/mongomodel/document/persistence_spec.rb +38 -38
- data/spec/mongomodel/document/scopes_spec.rb +8 -8
- data/spec/mongomodel/document/validations/uniqueness_spec.rb +9 -9
- data/spec/mongomodel/document/validations_spec.rb +16 -16
- data/spec/mongomodel/document_spec.rb +11 -11
- data/spec/mongomodel/embedded_document_spec.rb +13 -13
- data/spec/mongomodel/mongomodel_spec.rb +4 -4
- data/spec/mongomodel/support/collection_spec.rb +40 -40
- data/spec/mongomodel/support/map_spec.rb +41 -41
- data/spec/mongomodel/support/mongo_operator_spec.rb +11 -9
- data/spec/mongomodel/support/mongo_options_spec.rb +17 -17
- data/spec/mongomodel/support/mongo_order_spec.rb +22 -22
- data/spec/mongomodel/support/property_spec.rb +21 -12
- data/spec/mongomodel/support/scope_spec.rb +134 -134
- data/spec/spec_helper.rb +1 -0
- data/spec/specdoc.opts +0 -3
- metadata +7 -5
@@ -6,27 +6,27 @@ module MongoModel
|
|
6
6
|
context "no options" do
|
7
7
|
subject { Property.new(:name, String) }
|
8
8
|
|
9
|
-
it "
|
9
|
+
it "sets property name" do
|
10
10
|
subject.name.should == :name
|
11
11
|
end
|
12
12
|
|
13
|
-
it "
|
13
|
+
it "sets property type" do
|
14
14
|
subject.type.should == String
|
15
15
|
end
|
16
16
|
|
17
|
-
it "
|
17
|
+
it "sets default as value from name" do
|
18
18
|
subject.as.should == 'name'
|
19
19
|
end
|
20
20
|
|
21
|
-
it "
|
21
|
+
it "defaults to nil" do
|
22
22
|
subject.default(mock('document instance')).should be_nil
|
23
23
|
end
|
24
24
|
|
25
|
-
it "
|
25
|
+
it "equals a property with the same name and type" do
|
26
26
|
subject.should == Property.new(:name, String)
|
27
27
|
end
|
28
28
|
|
29
|
-
it "
|
29
|
+
it "does not equal properties with different name, type and options" do
|
30
30
|
subject.should_not == Property.new(:address, String)
|
31
31
|
subject.should_not == Property.new(:name, Float)
|
32
32
|
subject.should_not == Property.new(:name, String, :default => 'Anonymous')
|
@@ -38,23 +38,23 @@ module MongoModel
|
|
38
38
|
context "with options" do
|
39
39
|
subject { Property.new(:age, Integer, :as => '_record_age', :default => 21) }
|
40
40
|
|
41
|
-
it "
|
41
|
+
it "sets property options" do
|
42
42
|
subject.options.should == { :as => '_record_age', :default => 21 }
|
43
43
|
end
|
44
44
|
|
45
|
-
it "
|
45
|
+
it "sets custom as value" do
|
46
46
|
subject.as.should == '_record_age'
|
47
47
|
end
|
48
48
|
|
49
|
-
it "
|
49
|
+
it "defaults to custom default" do
|
50
50
|
subject.default(mock('document instance')).should == 21
|
51
51
|
end
|
52
52
|
|
53
|
-
it "
|
53
|
+
it "equals a property with the same name, type and options" do
|
54
54
|
subject.should == Property.new(:age, Integer, :as => '_record_age', :default => 21)
|
55
55
|
end
|
56
56
|
|
57
|
-
it "
|
57
|
+
it "does not equal properties with different name, type and options" do
|
58
58
|
subject.should_not == Property.new(:address, String)
|
59
59
|
subject.should_not == Property.new(:name, Float)
|
60
60
|
subject.should_not == Property.new(:name, String, :default => 'Anonymous')
|
@@ -63,7 +63,7 @@ module MongoModel
|
|
63
63
|
context "with callable default" do
|
64
64
|
subject { Property.new(:age, Integer, :default => lambda { |doc| doc.answer }) }
|
65
65
|
|
66
|
-
it "
|
66
|
+
it "calls lambda with given instance" do
|
67
67
|
subject.default(mock('document instance', :answer => 42)).should == 42
|
68
68
|
end
|
69
69
|
end
|
@@ -78,6 +78,15 @@ module MongoModel
|
|
78
78
|
it { should be_internal }
|
79
79
|
end
|
80
80
|
end
|
81
|
+
|
82
|
+
it "does not validate if options[:validate] is false" do
|
83
|
+
Property.new(:age, Integer, :validate => false).validate?.should be_false
|
84
|
+
end
|
85
|
+
|
86
|
+
it "validates when options[:validate] is true or not provided" do
|
87
|
+
Property.new(:age, Integer, :validate => true).validate?.should be_true
|
88
|
+
Property.new(:age, Integer).validate?.should be_true
|
89
|
+
end
|
81
90
|
end
|
82
91
|
end
|
83
92
|
end
|
@@ -40,37 +40,37 @@ module MongoModel
|
|
40
40
|
end
|
41
41
|
|
42
42
|
describe "#to_a" do
|
43
|
-
it "
|
43
|
+
it "finds and return documents matching conditions" do
|
44
44
|
model.should_find(finder_options, posts) do
|
45
45
|
subject.to_a.should == posts
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
49
|
-
it "
|
49
|
+
it "loads the scope" do
|
50
50
|
subject.to_a
|
51
51
|
subject.should be_loaded
|
52
52
|
end
|
53
53
|
|
54
|
-
it "
|
54
|
+
it "caches the documents" do
|
55
55
|
subject.to_a
|
56
56
|
model.should_not_find { subject.to_a }
|
57
57
|
end
|
58
58
|
end
|
59
59
|
|
60
60
|
describe "#as_json" do
|
61
|
-
it "
|
61
|
+
it "delegates to #to_a" do
|
62
62
|
subject.as_json.should == subject.to_a.as_json
|
63
63
|
end
|
64
64
|
end
|
65
65
|
|
66
66
|
describe "#count" do
|
67
|
-
it "
|
67
|
+
it "counts documents matching conditions and return the result" do
|
68
68
|
model.should_count(finder_options, 4) do
|
69
69
|
subject.count
|
70
70
|
end
|
71
71
|
end
|
72
72
|
|
73
|
-
it "
|
73
|
+
it "does not cache the result" do
|
74
74
|
subject.count
|
75
75
|
model.should_count(finder_options, 4) do
|
76
76
|
subject.count.should == 4
|
@@ -82,21 +82,21 @@ module MongoModel
|
|
82
82
|
before(:each) { model.stub_find(posts) }
|
83
83
|
|
84
84
|
subject_loaded do
|
85
|
-
it "
|
85
|
+
it "returns the number of matching documents" do
|
86
86
|
subject.size.should == 5
|
87
87
|
end
|
88
88
|
|
89
|
-
it "
|
89
|
+
it "does not perform a count on the collection" do
|
90
90
|
model.should_not_count { subject.size }
|
91
91
|
end
|
92
92
|
end
|
93
93
|
|
94
94
|
subject_not_loaded do
|
95
|
-
it "
|
95
|
+
it "returns the number of matching documents" do
|
96
96
|
subject.size.should == 5
|
97
97
|
end
|
98
98
|
|
99
|
-
it "
|
99
|
+
it "performs a count on the collection" do
|
100
100
|
model.should_count(finder_options, 9) do
|
101
101
|
subject.size.should == 9
|
102
102
|
end
|
@@ -122,7 +122,7 @@ module MongoModel
|
|
122
122
|
end
|
123
123
|
|
124
124
|
subject_loaded do
|
125
|
-
it "
|
125
|
+
it "does not perform a count on the collection" do
|
126
126
|
model.should_not_count { subject.empty? }
|
127
127
|
end
|
128
128
|
end
|
@@ -130,7 +130,7 @@ module MongoModel
|
|
130
130
|
|
131
131
|
describe "#any?" do
|
132
132
|
context "when no block given" do
|
133
|
-
it "
|
133
|
+
it "performs a count on the collection" do
|
134
134
|
model.should_count(finder_options, 1) { subject.any? }
|
135
135
|
end
|
136
136
|
|
@@ -152,7 +152,7 @@ module MongoModel
|
|
152
152
|
end
|
153
153
|
|
154
154
|
context "when block given" do
|
155
|
-
it "
|
155
|
+
it "delegates block to to_a" do
|
156
156
|
blk = lambda { |*args| true }
|
157
157
|
subject.to_a.should_receive(:any?).with(&blk)
|
158
158
|
subject.any?(&blk)
|
@@ -162,11 +162,11 @@ module MongoModel
|
|
162
162
|
|
163
163
|
describe "#reset" do
|
164
164
|
always do
|
165
|
-
it "
|
165
|
+
it "returns itself" do
|
166
166
|
subject.reset.should equal(subject)
|
167
167
|
end
|
168
168
|
|
169
|
-
it "
|
169
|
+
it "is not loaded" do
|
170
170
|
subject.reset
|
171
171
|
subject.should_not be_loaded
|
172
172
|
end
|
@@ -175,21 +175,21 @@ module MongoModel
|
|
175
175
|
|
176
176
|
describe "#reload" do
|
177
177
|
always do
|
178
|
-
it "
|
178
|
+
it "returns itself" do
|
179
179
|
subject.reload.should equal(subject)
|
180
180
|
end
|
181
181
|
|
182
|
-
it "
|
182
|
+
it "is loaded after calling" do
|
183
183
|
subject.reload
|
184
184
|
subject.should be_loaded
|
185
185
|
end
|
186
186
|
|
187
|
-
it "
|
187
|
+
it "resets its finder options" do
|
188
188
|
old_finder_options = subject.finder_options
|
189
189
|
subject.reload.finder_options.should_not equal(old_finder_options)
|
190
190
|
end
|
191
191
|
|
192
|
-
it "
|
192
|
+
it "resets its options for create" do
|
193
193
|
old_options_for_create = subject.options_for_create
|
194
194
|
subject.reload.options_for_create.should_not equal(old_options_for_create)
|
195
195
|
end
|
@@ -199,22 +199,22 @@ module MongoModel
|
|
199
199
|
describe "#==" do
|
200
200
|
before(:each) { model.stub_find(posts) }
|
201
201
|
|
202
|
-
it "
|
202
|
+
it "is equal to an array with matching results" do
|
203
203
|
subject.should == posts
|
204
204
|
end
|
205
205
|
|
206
|
-
it "
|
206
|
+
it "is not equal to an array with different results" do
|
207
207
|
subject.should_not == []
|
208
208
|
end
|
209
209
|
end
|
210
210
|
|
211
211
|
describe "array methods" do
|
212
|
-
it "
|
212
|
+
it "forwards [] to to_a" do
|
213
213
|
subject.to_a.should_receive(:[]).with(0)
|
214
214
|
subject[0]
|
215
215
|
end
|
216
216
|
|
217
|
-
it "
|
217
|
+
it "forwards each to to_a" do
|
218
218
|
blk = lambda { |*args| true }
|
219
219
|
subject.to_a.should_receive(:each).with(&blk)
|
220
220
|
subject.each(&blk)
|
@@ -222,113 +222,113 @@ module MongoModel
|
|
222
222
|
end
|
223
223
|
|
224
224
|
describe "#where" do
|
225
|
-
it "
|
225
|
+
it "returns a new scope" do
|
226
226
|
subject.where(:author => "Sam").should be_an_instance_of(Scope)
|
227
227
|
end
|
228
228
|
|
229
|
-
it "
|
229
|
+
it "is not loaded" do
|
230
230
|
subject.to_a
|
231
231
|
subject.where(:author => "Sam").should_not be_loaded
|
232
232
|
end
|
233
233
|
|
234
|
-
it "
|
234
|
+
it "adds individual where values" do
|
235
235
|
where_scope = subject.where(:author => "Sam")
|
236
236
|
where_scope.where_values.should == subject.where_values + [{ :author => "Sam" }]
|
237
237
|
end
|
238
238
|
|
239
|
-
it "
|
239
|
+
it "adds multiple where values" do
|
240
240
|
where_scope = subject.where({ :author => "Sam" }, { :published => false })
|
241
241
|
where_scope.where_values.should == subject.where_values + [{ :author => "Sam" }, { :published => false }]
|
242
242
|
end
|
243
243
|
end
|
244
244
|
|
245
245
|
describe "#where!" do
|
246
|
-
it "
|
246
|
+
it "overwrites where values" do
|
247
247
|
where_scope = subject.where!(:author => "Sam")
|
248
248
|
where_scope.where_values.should == [{ :author => "Sam" }]
|
249
249
|
end
|
250
250
|
end
|
251
251
|
|
252
252
|
describe "#order" do
|
253
|
-
it "
|
253
|
+
it "returns a new scope" do
|
254
254
|
subject.order(:author.asc).should be_an_instance_of(Scope)
|
255
255
|
end
|
256
256
|
|
257
|
-
it "
|
257
|
+
it "is not loaded" do
|
258
258
|
subject.to_a
|
259
259
|
subject.order(:author.asc).should_not be_loaded
|
260
260
|
end
|
261
261
|
|
262
|
-
it "
|
262
|
+
it "adds individual order values" do
|
263
263
|
order_scope = subject.order(:author.asc)
|
264
264
|
order_scope.order_values.should == subject.order_values + [:author.asc]
|
265
265
|
end
|
266
266
|
|
267
|
-
it "
|
267
|
+
it "adds multiple order values" do
|
268
268
|
order_scope = subject.order(:author.asc, :published.desc)
|
269
269
|
order_scope.order_values.should == subject.order_values + [:author.asc, :published.desc]
|
270
270
|
end
|
271
271
|
end
|
272
272
|
|
273
273
|
describe "#order!" do
|
274
|
-
it "
|
274
|
+
it "overwrites order values" do
|
275
275
|
order_scope = subject.order!(:author.asc)
|
276
276
|
order_scope.order_values.should == [:author.asc]
|
277
277
|
end
|
278
278
|
end
|
279
279
|
|
280
280
|
describe "#select" do
|
281
|
-
it "
|
281
|
+
it "returns a new scope" do
|
282
282
|
subject.select(:author).should be_an_instance_of(Scope)
|
283
283
|
end
|
284
284
|
|
285
|
-
it "
|
285
|
+
it "is not loaded" do
|
286
286
|
subject.to_a
|
287
287
|
subject.select(:author).should_not be_loaded
|
288
288
|
end
|
289
289
|
|
290
|
-
it "
|
290
|
+
it "adds individual select values" do
|
291
291
|
select_scope = subject.select(:author)
|
292
292
|
select_scope.select_values.should == subject.select_values + [:author]
|
293
293
|
end
|
294
294
|
|
295
|
-
it "
|
295
|
+
it "adds multiple select values" do
|
296
296
|
select_scope = subject.select(:author, :published)
|
297
297
|
select_scope.select_values.should == subject.select_values + [:author, :published]
|
298
298
|
end
|
299
299
|
end
|
300
300
|
|
301
301
|
describe "#select!" do
|
302
|
-
it "
|
302
|
+
it "overwrites select values" do
|
303
303
|
select_scope = subject.select!(:author)
|
304
304
|
select_scope.select_values.should == [:author]
|
305
305
|
end
|
306
306
|
end
|
307
307
|
|
308
308
|
describe "#limit" do
|
309
|
-
it "
|
309
|
+
it "returns a new scope" do
|
310
310
|
subject.limit(10).should be_an_instance_of(Scope)
|
311
311
|
end
|
312
312
|
|
313
|
-
it "
|
313
|
+
it "is not loaded" do
|
314
314
|
subject.limit(10).should_not be_loaded
|
315
315
|
end
|
316
316
|
|
317
|
-
it "
|
317
|
+
it "overrides previous limit value" do
|
318
318
|
subject.limit(10).limit_value.should == 10
|
319
319
|
end
|
320
320
|
end
|
321
321
|
|
322
322
|
describe "#offset" do
|
323
|
-
it "
|
323
|
+
it "returns a new scope" do
|
324
324
|
subject.offset(10).should be_an_instance_of(Scope)
|
325
325
|
end
|
326
326
|
|
327
|
-
it "
|
327
|
+
it "is not loaded" do
|
328
328
|
subject.offset(10).should_not be_loaded
|
329
329
|
end
|
330
330
|
|
331
|
-
it "
|
331
|
+
it "overrides previous offset value" do
|
332
332
|
subject.offset(10).offset_value.should == 10
|
333
333
|
end
|
334
334
|
end
|
@@ -336,19 +336,19 @@ module MongoModel
|
|
336
336
|
describe "#from" do
|
337
337
|
define_class(:NotAPost, Document)
|
338
338
|
|
339
|
-
it "
|
339
|
+
it "returns a new scope" do
|
340
340
|
subject.from(NotAPost.collection).should be_an_instance_of(Scope)
|
341
341
|
end
|
342
342
|
|
343
|
-
it "
|
343
|
+
it "is not loaded" do
|
344
344
|
subject.from(NotAPost.collection).should_not be_loaded
|
345
345
|
end
|
346
346
|
|
347
|
-
it "
|
347
|
+
it "overrides collection" do
|
348
348
|
subject.from(NotAPost.collection).collection.should == NotAPost.collection
|
349
349
|
end
|
350
350
|
|
351
|
-
it "
|
351
|
+
it "allows collection to be set using string" do
|
352
352
|
subject.from(NotAPost.collection.name).collection.name.should == NotAPost.collection.name
|
353
353
|
end
|
354
354
|
end
|
@@ -359,19 +359,19 @@ module MongoModel
|
|
359
359
|
before(:each) { model.stub_find([]) }
|
360
360
|
|
361
361
|
always do
|
362
|
-
it "
|
362
|
+
it "returns an empty array" do
|
363
363
|
subject.first(3).should == []
|
364
364
|
end
|
365
365
|
end
|
366
366
|
|
367
367
|
subject_loaded do
|
368
|
-
it "
|
368
|
+
it "does not perform a find" do
|
369
369
|
model.should_not_find { subject.first(3) }
|
370
370
|
end
|
371
371
|
end
|
372
372
|
|
373
373
|
subject_not_loaded do
|
374
|
-
it "
|
374
|
+
it "finds with a limit of 3" do
|
375
375
|
model.should_find(finder_options.merge(:limit => 3), []) { subject.first(3) }
|
376
376
|
end
|
377
377
|
end
|
@@ -381,7 +381,7 @@ module MongoModel
|
|
381
381
|
before(:each) { model.stub_find([posts[0], posts[1], posts[2]]) }
|
382
382
|
|
383
383
|
always do
|
384
|
-
it "
|
384
|
+
it "returns the first documents in an array" do
|
385
385
|
subject.first(3).should == [posts[0], posts[1], posts[2]]
|
386
386
|
end
|
387
387
|
end
|
@@ -393,19 +393,19 @@ module MongoModel
|
|
393
393
|
before(:each) { model.stub_find([]) }
|
394
394
|
|
395
395
|
always do
|
396
|
-
it "
|
396
|
+
it "returns nil" do
|
397
397
|
subject.first.should be_nil
|
398
398
|
end
|
399
399
|
end
|
400
400
|
|
401
401
|
subject_loaded do
|
402
|
-
it "
|
402
|
+
it "does not perform a find" do
|
403
403
|
model.should_not_find { subject.first }
|
404
404
|
end
|
405
405
|
end
|
406
406
|
|
407
407
|
subject_not_loaded do
|
408
|
-
it "
|
408
|
+
it "finds with a limit of 1" do
|
409
409
|
model.should_find(finder_options.merge(:limit => 1), []) { subject.first }
|
410
410
|
end
|
411
411
|
end
|
@@ -415,7 +415,7 @@ module MongoModel
|
|
415
415
|
before(:each) { model.stub_find([posts[0]]) }
|
416
416
|
|
417
417
|
always do
|
418
|
-
it "
|
418
|
+
it "returns the first document" do
|
419
419
|
subject.first.should == posts[0]
|
420
420
|
end
|
421
421
|
end
|
@@ -434,19 +434,19 @@ module MongoModel
|
|
434
434
|
before(:each) { model.stub_find([]) }
|
435
435
|
|
436
436
|
always do
|
437
|
-
it "
|
437
|
+
it "returns an empty array" do
|
438
438
|
subject.last(2).should == []
|
439
439
|
end
|
440
440
|
end
|
441
441
|
|
442
442
|
subject_loaded do
|
443
|
-
it "
|
443
|
+
it "does not perform a find" do
|
444
444
|
model.should_not_find { subject.last(2) }
|
445
445
|
end
|
446
446
|
end
|
447
447
|
|
448
448
|
subject_not_loaded do
|
449
|
-
it "
|
449
|
+
it "finds with a limit of 2" do
|
450
450
|
model.should_find(reversed_finder_options.merge(:limit => 2), []) { subject.last(2) }
|
451
451
|
end
|
452
452
|
end
|
@@ -456,7 +456,7 @@ module MongoModel
|
|
456
456
|
before(:each) { model.stub_find([posts[0], posts[1]]) }
|
457
457
|
|
458
458
|
always do
|
459
|
-
it "
|
459
|
+
it "returns the last documents in an array" do
|
460
460
|
subject.last(2).should == [posts[0], posts[1]]
|
461
461
|
end
|
462
462
|
end
|
@@ -468,19 +468,19 @@ module MongoModel
|
|
468
468
|
before(:each) { model.stub_find([]) }
|
469
469
|
|
470
470
|
always do
|
471
|
-
it "
|
471
|
+
it "returns nil" do
|
472
472
|
subject.last.should be_nil
|
473
473
|
end
|
474
474
|
end
|
475
475
|
|
476
476
|
subject_loaded do
|
477
|
-
it "
|
477
|
+
it "does not perform a find" do
|
478
478
|
model.should_not_find { subject.last }
|
479
479
|
end
|
480
480
|
end
|
481
481
|
|
482
482
|
subject_not_loaded do
|
483
|
-
it "
|
483
|
+
it "finds with a limit of 1" do
|
484
484
|
model.should_find(reversed_finder_options.merge(:limit => 1), []) { subject.last }
|
485
485
|
end
|
486
486
|
end
|
@@ -491,7 +491,7 @@ module MongoModel
|
|
491
491
|
before(:each) { model.stub_find([post]) }
|
492
492
|
|
493
493
|
always do
|
494
|
-
it "
|
494
|
+
it "returns the last document" do
|
495
495
|
subject.last.should == post
|
496
496
|
end
|
497
497
|
end
|
@@ -500,7 +500,7 @@ module MongoModel
|
|
500
500
|
end
|
501
501
|
|
502
502
|
describe "#all" do
|
503
|
-
it "
|
503
|
+
it "returns all documents" do
|
504
504
|
model.should_find(finder_options, posts) do
|
505
505
|
subject.all.should == posts
|
506
506
|
end
|
@@ -511,7 +511,7 @@ module MongoModel
|
|
511
511
|
context "with single id" do
|
512
512
|
let(:post) { posts.first }
|
513
513
|
|
514
|
-
it "
|
514
|
+
it "performs find on collection" do
|
515
515
|
model.should_find(finder_options.deep_merge(:conditions => { :id => post.id }, :limit => 1), [post]) do
|
516
516
|
subject.find(post.id)
|
517
517
|
end
|
@@ -520,7 +520,7 @@ module MongoModel
|
|
520
520
|
context "when document exists" do
|
521
521
|
before(:each) { model.stub_find([post]) }
|
522
522
|
|
523
|
-
it "
|
523
|
+
it "finds and return document" do
|
524
524
|
subject.find(post.id).should == post
|
525
525
|
end
|
526
526
|
end
|
@@ -528,7 +528,7 @@ module MongoModel
|
|
528
528
|
context "when document does not exist" do
|
529
529
|
before(:each) { model.stub_find([]) }
|
530
530
|
|
531
|
-
it "
|
531
|
+
it "raises a DocumentNotFound exception" do
|
532
532
|
lambda {
|
533
533
|
subject.find('missing')
|
534
534
|
}.should raise_error(MongoModel::DocumentNotFound)
|
@@ -540,7 +540,7 @@ module MongoModel
|
|
540
540
|
let(:post1) { posts.first }
|
541
541
|
let(:post2) { posts.last }
|
542
542
|
|
543
|
-
it "
|
543
|
+
it "performs find on collection" do
|
544
544
|
model.should_find(finder_options.deep_merge(:conditions => { :id.in => [post2.id, post1.id] }), [post1, post2]) do
|
545
545
|
subject.find(post2.id, post1.id)
|
546
546
|
end
|
@@ -549,7 +549,7 @@ module MongoModel
|
|
549
549
|
context "when all documents exist" do
|
550
550
|
before(:each) { model.stub_find([post2, post1]) }
|
551
551
|
|
552
|
-
it "
|
552
|
+
it "returns documents in order given" do
|
553
553
|
subject.find(post2.id, post1.id).should == [post2, post1]
|
554
554
|
end
|
555
555
|
end
|
@@ -557,7 +557,7 @@ module MongoModel
|
|
557
557
|
context "when some documents do not exist" do
|
558
558
|
before(:each) { model.stub_find([post1]) }
|
559
559
|
|
560
|
-
it "
|
560
|
+
it "raises a DocumentNotFound exception" do
|
561
561
|
lambda {
|
562
562
|
subject.find(post1.id, 'missing')
|
563
563
|
}.should raise_error(MongoModel::DocumentNotFound)
|
@@ -569,7 +569,7 @@ module MongoModel
|
|
569
569
|
describe "#exists?" do
|
570
570
|
let(:post) { posts.first }
|
571
571
|
|
572
|
-
it "
|
572
|
+
it "performs a count on the collection" do
|
573
573
|
model.should_count(finder_options.deep_merge(:conditions => { :id => post.id }), 1) do
|
574
574
|
subject.exists?(post.id)
|
575
575
|
end
|
@@ -578,7 +578,7 @@ module MongoModel
|
|
578
578
|
context "when the document exists" do
|
579
579
|
before(:each) { model.stub_find([post])}
|
580
580
|
|
581
|
-
it "
|
581
|
+
it "returns true" do
|
582
582
|
subject.exists?(post.id).should be_true
|
583
583
|
end
|
584
584
|
end
|
@@ -586,21 +586,21 @@ module MongoModel
|
|
586
586
|
context "when the document does not exist" do
|
587
587
|
before(:each) { model.stub_find([])}
|
588
588
|
|
589
|
-
it "
|
589
|
+
it "returns false" do
|
590
590
|
subject.exists?('missing').should be_false
|
591
591
|
end
|
592
592
|
end
|
593
593
|
end
|
594
594
|
|
595
595
|
describe "#delete_all" do
|
596
|
-
it "
|
596
|
+
it "removes all matching documents from collection" do
|
597
597
|
model.should_delete(finder_conditions) do
|
598
598
|
subject.delete_all
|
599
599
|
end
|
600
600
|
end
|
601
601
|
|
602
602
|
subject_loaded do
|
603
|
-
it "
|
603
|
+
it "resets the scope" do
|
604
604
|
subject.delete_all
|
605
605
|
subject.should_not be_loaded
|
606
606
|
end
|
@@ -609,14 +609,14 @@ module MongoModel
|
|
609
609
|
|
610
610
|
describe "#delete" do
|
611
611
|
context "by single id" do
|
612
|
-
it "
|
612
|
+
it "removes the document from the collection" do
|
613
613
|
model.should_delete(finder_conditions.merge(:id => "the-id")) do
|
614
614
|
subject.delete("the-id")
|
615
615
|
end
|
616
616
|
end
|
617
617
|
|
618
618
|
subject_loaded do
|
619
|
-
it "
|
619
|
+
it "resets the scope" do
|
620
620
|
subject.delete("the-id")
|
621
621
|
subject.should_not be_loaded
|
622
622
|
end
|
@@ -624,14 +624,14 @@ module MongoModel
|
|
624
624
|
end
|
625
625
|
|
626
626
|
context "by multiple ids" do
|
627
|
-
it "
|
627
|
+
it "removes the document from the collection" do
|
628
628
|
model.should_delete(finder_conditions.merge(:id.in => ["first-id", "second-id"])) do
|
629
629
|
subject.delete("first-id", "second-id")
|
630
630
|
end
|
631
631
|
end
|
632
632
|
|
633
633
|
subject_loaded do
|
634
|
-
it "
|
634
|
+
it "resets the scope" do
|
635
635
|
subject.delete("first-id", "second-id")
|
636
636
|
subject.should_not be_loaded
|
637
637
|
end
|
@@ -645,14 +645,14 @@ module MongoModel
|
|
645
645
|
|
646
646
|
before(:each) { model.stub_find([post1, post2]) }
|
647
647
|
|
648
|
-
it "
|
648
|
+
it "destroys all matching documents individually" do
|
649
649
|
Post.should_delete(:id => post1.id)
|
650
650
|
Post.should_delete(:id => post2.id)
|
651
651
|
subject.destroy_all
|
652
652
|
end
|
653
653
|
|
654
654
|
subject_loaded do
|
655
|
-
it "
|
655
|
+
it "resets the scope" do
|
656
656
|
subject.destroy_all
|
657
657
|
subject.should_not be_loaded
|
658
658
|
end
|
@@ -665,13 +665,13 @@ module MongoModel
|
|
665
665
|
|
666
666
|
before(:each) { model.stub_find([post]) }
|
667
667
|
|
668
|
-
it "
|
668
|
+
it "destroys the retrieved document" do
|
669
669
|
Post.should_delete(:id => post.id)
|
670
670
|
subject.destroy(post.id)
|
671
671
|
end
|
672
672
|
|
673
673
|
subject_loaded do
|
674
|
-
it "
|
674
|
+
it "resets the scope" do
|
675
675
|
subject.destroy(post.id)
|
676
676
|
subject.should_not be_loaded
|
677
677
|
end
|
@@ -684,14 +684,14 @@ module MongoModel
|
|
684
684
|
|
685
685
|
before(:each) { model.stub_find([post1, post2]) }
|
686
686
|
|
687
|
-
it "
|
687
|
+
it "destroys the documents individually" do
|
688
688
|
Post.should_delete(:id => post1.id)
|
689
689
|
Post.should_delete(:id => post2.id)
|
690
690
|
subject.destroy(post1.id, post2.id)
|
691
691
|
end
|
692
692
|
|
693
693
|
subject_loaded do
|
694
|
-
it "
|
694
|
+
it "resets the scope" do
|
695
695
|
subject.destroy(post1.id, post2.id)
|
696
696
|
subject.should_not be_loaded
|
697
697
|
end
|
@@ -700,13 +700,13 @@ module MongoModel
|
|
700
700
|
end
|
701
701
|
|
702
702
|
describe "#update_all" do
|
703
|
-
it "
|
703
|
+
it "updates all matching documents" do
|
704
704
|
model.should_update(finder_conditions, { :name => "New name" })
|
705
705
|
subject.update_all(:name => "New name")
|
706
706
|
end
|
707
707
|
|
708
708
|
subject_loaded do
|
709
|
-
it "
|
709
|
+
it "resets the scope" do
|
710
710
|
subject.update_all(:name => "New name")
|
711
711
|
subject.should_not be_loaded
|
712
712
|
end
|
@@ -717,13 +717,13 @@ module MongoModel
|
|
717
717
|
context "by single id" do
|
718
718
|
let(:post) { posts.first }
|
719
719
|
|
720
|
-
it "
|
720
|
+
it "updates the document with the given id" do
|
721
721
|
model.should_update(finder_conditions.merge(:id => post.id), { :name => "New name" })
|
722
722
|
subject.update(post.id, :name => "New name")
|
723
723
|
end
|
724
724
|
|
725
725
|
subject_loaded do
|
726
|
-
it "
|
726
|
+
it "resets the scope" do
|
727
727
|
subject.update(post.id, {})
|
728
728
|
subject.should_not be_loaded
|
729
729
|
end
|
@@ -734,13 +734,13 @@ module MongoModel
|
|
734
734
|
let(:post1) { posts.first }
|
735
735
|
let(:post2) { posts.last }
|
736
736
|
|
737
|
-
it "
|
737
|
+
it "updates the documents with the given ids" do
|
738
738
|
model.should_update(finder_conditions.merge(:id.in => [post1.id, post2.id]), { :name => "New name" })
|
739
739
|
subject.update([post1.id, post2.id], :name => "New name")
|
740
740
|
end
|
741
741
|
|
742
742
|
subject_loaded do
|
743
|
-
it "
|
743
|
+
it "resets the scope" do
|
744
744
|
subject.update([post1.id, post2.id], {})
|
745
745
|
subject.should_not be_loaded
|
746
746
|
end
|
@@ -749,25 +749,25 @@ module MongoModel
|
|
749
749
|
end
|
750
750
|
|
751
751
|
describe "#paginate" do
|
752
|
-
it "
|
752
|
+
it "loads the first page of results by default" do
|
753
753
|
model.should_find(finder_options.merge(:offset => 0, :limit => 20), posts) {
|
754
754
|
subject.paginate
|
755
755
|
}
|
756
756
|
end
|
757
757
|
|
758
|
-
it "
|
758
|
+
it "loads a specified page of results" do
|
759
759
|
model.should_find(finder_options.merge(:offset => 40, :limit => 20), posts) {
|
760
760
|
subject.paginate(:page => 3)
|
761
761
|
}
|
762
762
|
end
|
763
763
|
|
764
|
-
it "
|
764
|
+
it "allows the per_page option to be set" do
|
765
765
|
model.should_find(finder_options.merge(:offset => 7, :limit => 7), posts) {
|
766
766
|
subject.paginate(:per_page => 7, :page => 2)
|
767
767
|
}
|
768
768
|
end
|
769
769
|
|
770
|
-
it "
|
770
|
+
it "auto s-detect total entries where possible" do
|
771
771
|
paginator = nil
|
772
772
|
|
773
773
|
model.should_find(finder_options.merge(:offset => 0, :limit => 20), posts) {
|
@@ -777,7 +777,7 @@ module MongoModel
|
|
777
777
|
paginator.total_entries.should == 5
|
778
778
|
end
|
779
779
|
|
780
|
-
it "
|
780
|
+
it "loads total entries using count when auto-detection not possible" do
|
781
781
|
paginator = nil
|
782
782
|
|
783
783
|
subject.stub!(:count).and_return(57)
|
@@ -790,7 +790,7 @@ module MongoModel
|
|
790
790
|
end
|
791
791
|
|
792
792
|
describe "#in_batches" do
|
793
|
-
it "
|
793
|
+
it "yields documents in groups of given size" do
|
794
794
|
model.should_find(finder_options.merge(:offset => 0, :limit => 3), posts.first(3))
|
795
795
|
model.should_find(finder_options.merge(:offset => 3, :limit => 3), posts.last(2))
|
796
796
|
|
@@ -827,14 +827,14 @@ module MongoModel
|
|
827
827
|
|
828
828
|
it_should_behave_like "all scopes"
|
829
829
|
|
830
|
-
it "
|
830
|
+
it "uses collection from class" do
|
831
831
|
subject.collection.should == Post.collection
|
832
832
|
end
|
833
833
|
|
834
834
|
describe "#inspect" do
|
835
835
|
before(:each) { Post.stub_find(posts) }
|
836
836
|
|
837
|
-
it "
|
837
|
+
it "delegates to to_a" do
|
838
838
|
subject.inspect.should == posts.inspect
|
839
839
|
end
|
840
840
|
end
|
@@ -842,11 +842,11 @@ module MongoModel
|
|
842
842
|
describe "#==" do
|
843
843
|
define_class(:NotAPost, Document)
|
844
844
|
|
845
|
-
it "
|
845
|
+
it "is equal to a new scope for the same class" do
|
846
846
|
subject.should == Scope.new(Post)
|
847
847
|
end
|
848
848
|
|
849
|
-
it "
|
849
|
+
it "is not equal to a scope for a different class" do
|
850
850
|
subject.should_not == Scope.new(NotAPost)
|
851
851
|
end
|
852
852
|
end
|
@@ -854,66 +854,66 @@ module MongoModel
|
|
854
854
|
describe "#reverse_order" do
|
855
855
|
subject { basic_scope.reverse_order }
|
856
856
|
|
857
|
-
it "
|
857
|
+
it "returns a new scope" do
|
858
858
|
subject.should be_an_instance_of(Scope)
|
859
859
|
end
|
860
860
|
|
861
|
-
it "
|
861
|
+
it "is not loaded" do
|
862
862
|
basic_scope.to_a # Load parent scope
|
863
863
|
subject.should_not be_loaded
|
864
864
|
end
|
865
865
|
|
866
|
-
it "
|
866
|
+
it "sets the order value to descending by id" do
|
867
867
|
subject.order_values.should == [:id.desc]
|
868
868
|
end
|
869
869
|
end
|
870
870
|
|
871
871
|
describe "#build" do
|
872
|
-
it "
|
872
|
+
it "returns a new document" do
|
873
873
|
subject.build.should be_an_instance_of(Post)
|
874
874
|
end
|
875
875
|
|
876
|
-
it "
|
876
|
+
it "is aliased as #new" do
|
877
877
|
subject.new(:id => '123').should == subject.build(:id => '123')
|
878
878
|
end
|
879
879
|
end
|
880
880
|
|
881
881
|
describe "#create" do
|
882
|
-
it "
|
882
|
+
it "returns a new document" do
|
883
883
|
subject.create.should be_an_instance_of(Post)
|
884
884
|
end
|
885
885
|
|
886
|
-
it "
|
886
|
+
it "saves the document" do
|
887
887
|
subject.create.should_not be_a_new_record
|
888
888
|
end
|
889
889
|
end
|
890
890
|
|
891
891
|
describe "#apply_finder_options" do
|
892
|
-
it "
|
892
|
+
it "returns a new scope" do
|
893
893
|
subject.apply_finder_options({}).should be_an_instance_of(Scope)
|
894
894
|
end
|
895
895
|
|
896
|
-
it "
|
896
|
+
it "sets where values from options" do
|
897
897
|
scope = subject.apply_finder_options({ :conditions => { :author => "John" } })
|
898
898
|
scope.where_values.should == [{ :author => "John" }]
|
899
899
|
end
|
900
900
|
|
901
|
-
it "
|
901
|
+
it "sets order values from options" do
|
902
902
|
scope = subject.apply_finder_options({ :order => :author.desc })
|
903
903
|
scope.order_values.should == [:author.desc]
|
904
904
|
end
|
905
905
|
|
906
|
-
it "
|
906
|
+
it "sets select values from options" do
|
907
907
|
scope = subject.apply_finder_options({ :select => [:id, :author] })
|
908
908
|
scope.select_values.should == [:id, :author]
|
909
909
|
end
|
910
910
|
|
911
|
-
it "
|
911
|
+
it "sets offset value from options" do
|
912
912
|
scope = subject.apply_finder_options({ :offset => 40 })
|
913
913
|
scope.offset_value.should == 40
|
914
914
|
end
|
915
915
|
|
916
|
-
it "
|
916
|
+
it "sets limit value from options" do
|
917
917
|
scope = subject.apply_finder_options({ :limit => 50 })
|
918
918
|
scope.limit_value.should == 50
|
919
919
|
end
|
@@ -961,7 +961,7 @@ module MongoModel
|
|
961
961
|
it_should_behave_like "all scopes"
|
962
962
|
|
963
963
|
describe "#build" do
|
964
|
-
it "
|
964
|
+
it "uses equality where conditions as attributes" do
|
965
965
|
doc = subject.build
|
966
966
|
doc.author.should == "Sam"
|
967
967
|
doc.published.should be_true
|
@@ -970,7 +970,7 @@ module MongoModel
|
|
970
970
|
end
|
971
971
|
|
972
972
|
describe "#create" do
|
973
|
-
it "
|
973
|
+
it "uses equality where conditions as attributes" do
|
974
974
|
doc = subject.create
|
975
975
|
doc.author.should == "Sam"
|
976
976
|
doc.published.should be_true
|
@@ -981,14 +981,14 @@ module MongoModel
|
|
981
981
|
describe "#reverse_order" do
|
982
982
|
subject { scoped.reverse_order }
|
983
983
|
|
984
|
-
it "
|
985
|
-
subject.order_values.should == [:author.desc, :published.asc]
|
984
|
+
it "sets the order values to the reverse order" do
|
985
|
+
subject.order_values.should == MongoOrder.parse([:author.desc, :published.asc]).to_a
|
986
986
|
end
|
987
987
|
end
|
988
988
|
|
989
989
|
describe "#except" do
|
990
990
|
context "given :where" do
|
991
|
-
it "
|
991
|
+
it "returns a new scope without where values" do
|
992
992
|
s = subject.except(:where)
|
993
993
|
s.where_values.should be_empty
|
994
994
|
s.order_values.should == [:author.asc, :published.desc]
|
@@ -1000,7 +1000,7 @@ module MongoModel
|
|
1000
1000
|
end
|
1001
1001
|
|
1002
1002
|
context "given :order" do
|
1003
|
-
it "
|
1003
|
+
it "returns a new scope without order values" do
|
1004
1004
|
s = subject.except(:order)
|
1005
1005
|
s.where_values.should == [{ :author => "Sam" }, { :published => true }, { :date.lt => timestamp }]
|
1006
1006
|
s.order_values.should be_empty
|
@@ -1012,7 +1012,7 @@ module MongoModel
|
|
1012
1012
|
end
|
1013
1013
|
|
1014
1014
|
context "given :select" do
|
1015
|
-
it "
|
1015
|
+
it "returns a new scope without select values" do
|
1016
1016
|
s = subject.except(:select)
|
1017
1017
|
s.where_values.should == [{ :author => "Sam" }, { :published => true }, { :date.lt => timestamp }]
|
1018
1018
|
s.order_values.should == [:author.asc, :published.desc]
|
@@ -1024,7 +1024,7 @@ module MongoModel
|
|
1024
1024
|
end
|
1025
1025
|
|
1026
1026
|
context "given :offset" do
|
1027
|
-
it "
|
1027
|
+
it "returns a new scope without offset value" do
|
1028
1028
|
s = subject.except(:offset)
|
1029
1029
|
s.where_values.should == [{ :author => "Sam" }, { :published => true }, { :date.lt => timestamp }]
|
1030
1030
|
s.order_values.should == [:author.asc, :published.desc]
|
@@ -1036,7 +1036,7 @@ module MongoModel
|
|
1036
1036
|
end
|
1037
1037
|
|
1038
1038
|
context "given :limit" do
|
1039
|
-
it "
|
1039
|
+
it "returns a new scope without limit value" do
|
1040
1040
|
s = subject.except(:limit)
|
1041
1041
|
s.where_values.should == [{ :author => "Sam" }, { :published => true }, { :date.lt => timestamp }]
|
1042
1042
|
s.order_values.should == [:author.asc, :published.desc]
|
@@ -1048,7 +1048,7 @@ module MongoModel
|
|
1048
1048
|
end
|
1049
1049
|
|
1050
1050
|
context "given :from" do
|
1051
|
-
it "
|
1051
|
+
it "returns a new scope with default collection" do
|
1052
1052
|
s = subject.except(:from)
|
1053
1053
|
s.where_values.should == [{ :author => "Sam" }, { :published => true }, { :date.lt => timestamp }]
|
1054
1054
|
s.order_values.should == [:author.asc, :published.desc]
|
@@ -1070,7 +1070,7 @@ module MongoModel
|
|
1070
1070
|
end
|
1071
1071
|
let(:result) { subject.merge(merged) }
|
1072
1072
|
|
1073
|
-
it "
|
1073
|
+
it "combines where values from scopes" do
|
1074
1074
|
result.where_values.should == [
|
1075
1075
|
{ :author => "Sam" },
|
1076
1076
|
{ :published => true },
|
@@ -1079,22 +1079,22 @@ module MongoModel
|
|
1079
1079
|
]
|
1080
1080
|
end
|
1081
1081
|
|
1082
|
-
it "
|
1082
|
+
it "combines order values from scopes" do
|
1083
1083
|
result.order_values.should == [:author.asc, :published.desc, :date.desc]
|
1084
1084
|
end
|
1085
1085
|
|
1086
|
-
it "
|
1086
|
+
it "combines select values from scopes" do
|
1087
1087
|
result.select_values.should == [:author, :published, :date]
|
1088
1088
|
end
|
1089
1089
|
|
1090
|
-
it "
|
1090
|
+
it "preserves on load proc" do
|
1091
1091
|
result.on_load_proc.should == on_load_proc
|
1092
1092
|
end
|
1093
1093
|
|
1094
1094
|
context "merged scope has offset value" do
|
1095
1095
|
let(:merged) { basic_scope.offset(10) }
|
1096
1096
|
|
1097
|
-
it "
|
1097
|
+
it "uses offset value from merged scope" do
|
1098
1098
|
result.offset_value.should == 10
|
1099
1099
|
end
|
1100
1100
|
end
|
@@ -1102,7 +1102,7 @@ module MongoModel
|
|
1102
1102
|
context "merged scope has no offset value set" do
|
1103
1103
|
let(:merged) { basic_scope }
|
1104
1104
|
|
1105
|
-
it "
|
1105
|
+
it "uses offset value from original scope" do
|
1106
1106
|
result.offset_value.should == 15
|
1107
1107
|
end
|
1108
1108
|
end
|
@@ -1110,7 +1110,7 @@ module MongoModel
|
|
1110
1110
|
context "merged scope has limit value" do
|
1111
1111
|
let(:merged) { basic_scope.limit(50) }
|
1112
1112
|
|
1113
|
-
it "
|
1113
|
+
it "uses limit value from merged scope" do
|
1114
1114
|
result.limit_value.should == 50
|
1115
1115
|
end
|
1116
1116
|
end
|
@@ -1118,12 +1118,12 @@ module MongoModel
|
|
1118
1118
|
context "merged scope has no limit value set" do
|
1119
1119
|
let(:merged) { basic_scope }
|
1120
1120
|
|
1121
|
-
it "
|
1121
|
+
it "uses limit value from original scope" do
|
1122
1122
|
result.limit_value.should == 7
|
1123
1123
|
end
|
1124
1124
|
end
|
1125
1125
|
|
1126
|
-
it "
|
1126
|
+
it "uses from value (collection) from merged scope" do
|
1127
1127
|
merged = basic_scope.from(Post.collection)
|
1128
1128
|
subject.merge(merged).collection.should == Post.collection
|
1129
1129
|
end
|