mongo_mapper 0.13.0.beta2 → 0.15.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.
- checksums.yaml +5 -5
- data/LICENSE +1 -1
- data/README.md +64 -0
- data/examples/keys.rb +3 -3
- data/examples/modifiers/set.rb +2 -2
- data/examples/querying.rb +3 -3
- data/examples/safe.rb +2 -2
- data/examples/scopes.rb +1 -1
- data/lib/mongo_mapper.rb +5 -0
- data/lib/mongo_mapper/connection.rb +16 -37
- data/lib/mongo_mapper/document.rb +4 -0
- data/lib/mongo_mapper/extensions/array.rb +14 -6
- data/lib/mongo_mapper/extensions/hash.rb +15 -3
- data/lib/mongo_mapper/extensions/object.rb +4 -0
- data/lib/mongo_mapper/extensions/object_id.rb +5 -1
- data/lib/mongo_mapper/extensions/string.rb +13 -5
- data/lib/mongo_mapper/extensions/symbol.rb +18 -0
- data/lib/mongo_mapper/plugins/accessible.rb +14 -4
- data/lib/mongo_mapper/plugins/associations.rb +7 -6
- data/lib/mongo_mapper/plugins/associations/base.rb +18 -13
- data/lib/mongo_mapper/plugins/associations/belongs_to_association.rb +10 -1
- data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +9 -8
- data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +12 -11
- data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +4 -4
- data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +24 -23
- data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +18 -16
- data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +55 -48
- data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +14 -13
- data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +7 -6
- data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +7 -5
- data/lib/mongo_mapper/plugins/associations/one_as_proxy.rb +17 -14
- data/lib/mongo_mapper/plugins/associations/one_embedded_polymorphic_proxy.rb +14 -13
- data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +9 -9
- data/lib/mongo_mapper/plugins/associations/one_proxy.rb +27 -26
- data/lib/mongo_mapper/plugins/associations/proxy.rb +31 -28
- data/lib/mongo_mapper/plugins/callbacks.rb +14 -1
- data/lib/mongo_mapper/plugins/counter_cache.rb +97 -0
- data/lib/mongo_mapper/plugins/dirty.rb +29 -37
- data/lib/mongo_mapper/plugins/document.rb +1 -1
- data/lib/mongo_mapper/plugins/dynamic_querying.rb +10 -9
- data/lib/mongo_mapper/plugins/dynamic_querying/dynamic_finder.rb +18 -17
- data/lib/mongo_mapper/plugins/embedded_callbacks.rb +2 -1
- data/lib/mongo_mapper/plugins/embedded_document.rb +1 -1
- data/lib/mongo_mapper/plugins/identity_map.rb +1 -1
- data/lib/mongo_mapper/plugins/indexes.rb +37 -2
- data/lib/mongo_mapper/plugins/keys.rb +202 -142
- data/lib/mongo_mapper/plugins/keys/key.rb +22 -13
- data/lib/mongo_mapper/plugins/keys/static.rb +45 -0
- data/lib/mongo_mapper/plugins/modifiers.rb +59 -28
- data/lib/mongo_mapper/plugins/partial_updates.rb +86 -0
- data/lib/mongo_mapper/plugins/persistence.rb +13 -8
- data/lib/mongo_mapper/plugins/protected.rb +6 -5
- data/lib/mongo_mapper/plugins/querying.rb +85 -42
- data/lib/mongo_mapper/plugins/querying/decorated_plucky_query.rb +32 -9
- data/lib/mongo_mapper/plugins/rails.rb +1 -0
- data/lib/mongo_mapper/plugins/safe.rb +10 -4
- data/lib/mongo_mapper/plugins/sci.rb +4 -1
- data/lib/mongo_mapper/plugins/scopes.rb +78 -7
- data/lib/mongo_mapper/plugins/stats.rb +17 -0
- data/lib/mongo_mapper/plugins/timestamps.rb +1 -0
- data/lib/mongo_mapper/plugins/touch.rb +1 -1
- data/lib/mongo_mapper/plugins/validations.rb +7 -2
- data/lib/mongo_mapper/railtie.rb +20 -0
- data/lib/mongo_mapper/railtie/database.rake +1 -1
- data/lib/mongo_mapper/utils.rb +2 -2
- data/lib/mongo_mapper/version.rb +1 -1
- data/lib/rails/generators/mongo_mapper/config/config_generator.rb +12 -13
- data/lib/rails/generators/mongo_mapper/model/model_generator.rb +9 -9
- data/spec/examples.txt +1643 -0
- data/spec/functional/accessible_spec.rb +13 -13
- data/spec/functional/associations/belongs_to_polymorphic_proxy_spec.rb +13 -13
- data/spec/functional/associations/belongs_to_proxy_spec.rb +18 -19
- data/spec/functional/associations/in_array_proxy_spec.rb +10 -10
- data/spec/functional/associations/many_documents_as_proxy_spec.rb +6 -6
- data/spec/functional/associations/many_documents_proxy_spec.rb +85 -14
- data/spec/functional/associations/many_embedded_polymorphic_proxy_spec.rb +13 -13
- data/spec/functional/associations/many_embedded_proxy_spec.rb +1 -1
- data/spec/functional/associations/many_polymorphic_proxy_spec.rb +4 -4
- data/spec/functional/associations/one_as_proxy_spec.rb +10 -10
- data/spec/functional/associations/one_embedded_polymorphic_proxy_spec.rb +9 -9
- data/spec/functional/associations/one_embedded_proxy_spec.rb +3 -3
- data/spec/functional/associations/one_proxy_spec.rb +10 -10
- data/spec/functional/associations_spec.rb +3 -3
- data/spec/functional/binary_spec.rb +2 -2
- data/spec/functional/caching_spec.rb +8 -15
- data/spec/functional/callbacks_spec.rb +89 -2
- data/spec/functional/counter_cache_spec.rb +235 -0
- data/spec/functional/dirty_spec.rb +63 -46
- data/spec/functional/document_spec.rb +30 -2
- data/spec/functional/dumpable_spec.rb +1 -1
- data/spec/functional/embedded_document_spec.rb +18 -18
- data/spec/functional/identity_map_spec.rb +27 -14
- data/spec/functional/indexes_spec.rb +44 -19
- data/spec/functional/keys_spec.rb +117 -15
- data/spec/functional/logger_spec.rb +3 -3
- data/spec/functional/modifiers_spec.rb +67 -19
- data/spec/functional/partial_updates_spec.rb +577 -0
- data/spec/functional/protected_spec.rb +14 -14
- data/spec/functional/querying_spec.rb +55 -28
- data/spec/functional/safe_spec.rb +23 -27
- data/spec/functional/sci_spec.rb +49 -14
- data/spec/functional/scopes_spec.rb +235 -2
- data/spec/functional/static_keys_spec.rb +153 -0
- data/spec/functional/stats_spec.rb +86 -0
- data/spec/functional/touch_spec.rb +6 -6
- data/spec/functional/validations_spec.rb +51 -57
- data/spec/quality_spec.rb +51 -0
- data/spec/spec_helper.rb +37 -9
- data/spec/support/matchers.rb +5 -14
- data/spec/unit/associations/base_spec.rb +12 -12
- data/spec/unit/associations/belongs_to_association_spec.rb +2 -2
- data/spec/unit/associations/many_association_spec.rb +2 -2
- data/spec/unit/associations/one_association_spec.rb +2 -2
- data/spec/unit/associations/proxy_spec.rb +19 -16
- data/spec/unit/clone_spec.rb +1 -1
- data/spec/unit/document_spec.rb +8 -8
- data/spec/unit/dynamic_finder_spec.rb +8 -8
- data/spec/unit/embedded_document_spec.rb +18 -19
- data/spec/unit/extensions_spec.rb +41 -17
- data/spec/unit/identity_map_middleware_spec.rb +65 -96
- data/spec/unit/inspect_spec.rb +4 -4
- data/spec/unit/key_spec.rb +28 -26
- data/spec/unit/keys_spec.rb +10 -10
- data/spec/unit/model_generator_spec.rb +2 -4
- data/spec/unit/mongo_mapper_spec.rb +38 -85
- data/spec/unit/rails_spec.rb +5 -0
- data/spec/unit/serialization_spec.rb +1 -1
- data/spec/unit/time_zones_spec.rb +2 -2
- data/spec/unit/validations_spec.rb +28 -15
- metadata +188 -161
- data/README.rdoc +0 -55
- data/lib/mongo_mapper/extensions/ordered_hash.rb +0 -23
@@ -66,7 +66,7 @@ describe "Scopes" do
|
|
66
66
|
@document.class_eval do
|
67
67
|
scope :age, lambda { |age| {:age => age} }
|
68
68
|
scope :ages, lambda { |low, high| {:age.gte => low, :age.lte => high} }
|
69
|
-
scope :ordered, lambda { |
|
69
|
+
scope :ordered, lambda { |s| sort(s) }
|
70
70
|
end
|
71
71
|
end
|
72
72
|
|
@@ -170,7 +170,7 @@ describe "Scopes" do
|
|
170
170
|
key :title, String
|
171
171
|
key :published_at, Time
|
172
172
|
end
|
173
|
-
Item.collection.
|
173
|
+
Item.collection.drop
|
174
174
|
|
175
175
|
class ::Page < ::Item; end
|
176
176
|
class ::Blog < ::Item
|
@@ -201,4 +201,237 @@ describe "Scopes" do
|
|
201
201
|
end
|
202
202
|
end
|
203
203
|
end
|
204
|
+
|
205
|
+
describe "with_scope" do
|
206
|
+
describe "with a base query" do
|
207
|
+
before do
|
208
|
+
@klass = Doc do
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
it "should scope" do
|
213
|
+
u1 = @klass.create!(:first_name => "Scott")
|
214
|
+
u2 = @klass.create!(:first_name => "Andrew")
|
215
|
+
|
216
|
+
run = false
|
217
|
+
|
218
|
+
@klass.with_scope(:first_name => "Scott") do
|
219
|
+
run = true
|
220
|
+
@klass.all.should == [u1]
|
221
|
+
end
|
222
|
+
|
223
|
+
run.should == true
|
224
|
+
end
|
225
|
+
|
226
|
+
it "should return the scope at the end of the block" do
|
227
|
+
u1 = @klass.create!(:first_name => "Scott")
|
228
|
+
u2 = @klass.create!(:first_name => "Andrew")
|
229
|
+
|
230
|
+
run = false
|
231
|
+
|
232
|
+
@klass.with_scope(:first_name => "Scott") do
|
233
|
+
run = true
|
234
|
+
@klass.all.should == [u1]
|
235
|
+
end
|
236
|
+
|
237
|
+
run.should == true
|
238
|
+
|
239
|
+
@klass.all.should include(u1)
|
240
|
+
@klass.all.should include(u2)
|
241
|
+
end
|
242
|
+
|
243
|
+
it "should be able to use an unscoped query" do
|
244
|
+
u1 = @klass.create!(:first_name => "Scott")
|
245
|
+
u2 = @klass.create!(:first_name => "Andrew")
|
246
|
+
|
247
|
+
run = false
|
248
|
+
|
249
|
+
@klass.with_scope(:first_name => "Scott") do
|
250
|
+
@klass.unscoped do
|
251
|
+
run = true
|
252
|
+
@klass.all.should include(u1)
|
253
|
+
@klass.all.should include(u2)
|
254
|
+
end
|
255
|
+
|
256
|
+
@klass.all.should == [u1]
|
257
|
+
end
|
258
|
+
|
259
|
+
run.should == true
|
260
|
+
|
261
|
+
@klass.all.should include(u1)
|
262
|
+
@klass.all.should include(u2)
|
263
|
+
end
|
264
|
+
|
265
|
+
it "should return the value of the block" do
|
266
|
+
u1 = @klass.create!(:first_name => "Scott")
|
267
|
+
u2 = @klass.create!(:first_name => "Andrew")
|
268
|
+
|
269
|
+
result = @klass.with_scope(:first_name => "Scott") do
|
270
|
+
@klass.all
|
271
|
+
end
|
272
|
+
|
273
|
+
result.should == [u1]
|
274
|
+
end
|
275
|
+
|
276
|
+
it "should have an empty list of default scope" do
|
277
|
+
@klass.default_scopes.should == []
|
278
|
+
end
|
279
|
+
|
280
|
+
it "should apply the default scope to all queries" do
|
281
|
+
@klass.key :active, Boolean, :default => true
|
282
|
+
|
283
|
+
normal = @klass.create!
|
284
|
+
inactive = @klass.create!(:active => false)
|
285
|
+
|
286
|
+
@klass.default_scope do
|
287
|
+
{
|
288
|
+
:active => true
|
289
|
+
}
|
290
|
+
end
|
291
|
+
|
292
|
+
@klass.all.should == [normal]
|
293
|
+
end
|
294
|
+
|
295
|
+
it "should allow multiple default scopes" do
|
296
|
+
@klass.key :active, Boolean, :default => true
|
297
|
+
@klass.key :soft_deleted, Boolean, :default => false
|
298
|
+
|
299
|
+
normal = @klass.create!
|
300
|
+
inactive = @klass.create!(:active => false)
|
301
|
+
deleted = @klass.create!(:soft_deleted => true)
|
302
|
+
|
303
|
+
@klass.default_scope do
|
304
|
+
{
|
305
|
+
:active => true,
|
306
|
+
}
|
307
|
+
end
|
308
|
+
|
309
|
+
@klass.default_scope do
|
310
|
+
{
|
311
|
+
:soft_deleted => false
|
312
|
+
}
|
313
|
+
end
|
314
|
+
|
315
|
+
@klass.all.should == [normal]
|
316
|
+
end
|
317
|
+
|
318
|
+
it "should allow a where inside a default_scope" do
|
319
|
+
@klass.key :active, Boolean, :default => true
|
320
|
+
|
321
|
+
normal = @klass.create!
|
322
|
+
inactive = @klass.create!(:active => false)
|
323
|
+
|
324
|
+
@klass.default_scope do
|
325
|
+
where(:active => true)
|
326
|
+
end
|
327
|
+
|
328
|
+
@klass.all.should == [normal]
|
329
|
+
end
|
330
|
+
|
331
|
+
it "should allow a hash directly" do
|
332
|
+
@klass.key :active, Boolean, :default => true
|
333
|
+
|
334
|
+
normal = @klass.create!
|
335
|
+
inactive = @klass.create!(:active => false)
|
336
|
+
|
337
|
+
@klass.default_scope :active => true
|
338
|
+
|
339
|
+
@klass.all.should == [normal]
|
340
|
+
end
|
341
|
+
|
342
|
+
it "should inherit default scopes, but only downwards" do
|
343
|
+
@klass.default_scope do
|
344
|
+
where(:active => true)
|
345
|
+
end
|
346
|
+
@subclass = Class.new(@klass)
|
347
|
+
@subclass.default_scopes.length.should == 1
|
348
|
+
|
349
|
+
@subclass.default_scope do
|
350
|
+
{
|
351
|
+
:foo => :bar
|
352
|
+
}
|
353
|
+
end
|
354
|
+
|
355
|
+
@subclass.default_scopes.length.should == 2
|
356
|
+
@klass.default_scopes.length.should == 1
|
357
|
+
end
|
358
|
+
end
|
359
|
+
end
|
360
|
+
|
361
|
+
describe "regression - multiple named scopes" do
|
362
|
+
it "should merge the hashes from multiple scopes" do
|
363
|
+
klass = Doc do
|
364
|
+
key :a, Boolean
|
365
|
+
key :b, Boolean
|
366
|
+
key :c, Boolean
|
367
|
+
|
368
|
+
scope :one, where(:a => true, :b => true)
|
369
|
+
scope :two, where(:c => true)
|
370
|
+
end
|
371
|
+
|
372
|
+
obj = klass.create!(:a => false, :b => true, :c => true)
|
373
|
+
klass.one.two.all.should == []
|
374
|
+
|
375
|
+
obj2 = klass.create(:a => true, :b => true, :c => true)
|
376
|
+
klass.one.two.all.should == [obj2]
|
377
|
+
end
|
378
|
+
end
|
379
|
+
|
380
|
+
describe "regression - with multiple scopes that set a key to nil" do
|
381
|
+
it "should still have the query as nil" do
|
382
|
+
klass = Doc do
|
383
|
+
key :a, Boolean
|
384
|
+
|
385
|
+
scope :one, lambda {
|
386
|
+
{
|
387
|
+
:a => nil
|
388
|
+
}
|
389
|
+
}
|
390
|
+
scope :two, lambda {
|
391
|
+
{
|
392
|
+
:a => nil
|
393
|
+
}
|
394
|
+
}
|
395
|
+
end
|
396
|
+
|
397
|
+
obj = klass.create!(:a => nil)
|
398
|
+
klass.one.two.all.should == [obj]
|
399
|
+
end
|
400
|
+
end
|
401
|
+
|
402
|
+
describe "regression - multiple scopes leading to empty query (array bug?)" do
|
403
|
+
before do
|
404
|
+
@klass = Doc do
|
405
|
+
key :send_at, Time
|
406
|
+
|
407
|
+
scope :unsent, lambda {
|
408
|
+
now = Time.now
|
409
|
+
|
410
|
+
{
|
411
|
+
send_at: {
|
412
|
+
'$lte' => now,
|
413
|
+
'$gte' => now - 30.days,
|
414
|
+
},
|
415
|
+
sent_at: nil,
|
416
|
+
}
|
417
|
+
}
|
418
|
+
|
419
|
+
scope :sorted, lambda {
|
420
|
+
sort(send_at: 1)
|
421
|
+
}
|
422
|
+
end
|
423
|
+
end
|
424
|
+
|
425
|
+
it "should filter properly by dates" do
|
426
|
+
old_obj = @klass.create(send_at: 3.months.ago)
|
427
|
+
one_day_ago = @klass.create(send_at: 1.day.ago)
|
428
|
+
two_days_ago = @klass.create(send_at: 2.days.ago)
|
429
|
+
|
430
|
+
all_unsorted = @klass.unsent.all
|
431
|
+
|
432
|
+
all_unsorted.should include(one_day_ago)
|
433
|
+
all_unsorted.should include(two_days_ago)
|
434
|
+
@klass.unsent.sorted.all.should == [two_days_ago, one_day_ago]
|
435
|
+
end
|
436
|
+
end
|
204
437
|
end
|
@@ -0,0 +1,153 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe MongoMapper::Plugins::Keys::Static do
|
4
|
+
before do
|
5
|
+
@klass = Class.new do
|
6
|
+
include MongoMapper::Document
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
it "is off by default" do
|
11
|
+
@klass.static_keys.should == false
|
12
|
+
end
|
13
|
+
|
14
|
+
it "can be turned on" do
|
15
|
+
@klass.static_keys = true
|
16
|
+
@klass.static_keys.should == true
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "a class with static keys" do
|
20
|
+
before do
|
21
|
+
@klass = Class.new do
|
22
|
+
include MongoMapper::Document
|
23
|
+
|
24
|
+
self.static_keys = true
|
25
|
+
|
26
|
+
attr_accessor :an_accessor
|
27
|
+
|
28
|
+
key "valid_key", String
|
29
|
+
end
|
30
|
+
|
31
|
+
@obj = @klass.new
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should be able to assign to a valid key" do
|
35
|
+
@obj.valid_key = "foo"
|
36
|
+
@obj.valid_key.should == "foo"
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should raise a MissingKeyError if trying to assign to a field that does not exist" do
|
40
|
+
lambda {
|
41
|
+
@obj['foo'] = "foo"
|
42
|
+
}.should raise_error(MongoMapper::Plugins::Keys::Static::MissingKeyError)
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should raise a MissingKeyError if trying to read from a field that does not exist" do
|
46
|
+
lambda {
|
47
|
+
@obj['foo']
|
48
|
+
}.should raise_error(MongoMapper::Plugins::Keys::Static::MissingKeyError)
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should raise a MissingKeyError if trying to set attributes= to a field that does not exist" do
|
52
|
+
lambda {
|
53
|
+
@obj.attributes = {:foo => "foo"}
|
54
|
+
}.should raise_error(MongoMapper::Plugins::Keys::Static::MissingKeyError)
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should be able to call a method" do
|
58
|
+
@obj.an_accessor = :bar
|
59
|
+
@obj.an_accessor.should == :bar
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should raise a NoMethodError if trying to call the non-existant key method" do
|
63
|
+
lambda {
|
64
|
+
@obj.foo
|
65
|
+
}.should raise_error(NoMethodError)
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should raise for read_key" do
|
69
|
+
lambda {
|
70
|
+
@obj.read_key('foo')
|
71
|
+
}.should raise_error(MongoMapper::Plugins::Keys::Static::MissingKeyError)
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should raise for attribute" do
|
75
|
+
lambda {
|
76
|
+
@obj.attribute('foo')
|
77
|
+
}.should raise_error(MongoMapper::Plugins::Keys::Static::MissingKeyError)
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should not raise if trying to assign to a field that exists (but with a symbol instead of a string)" do
|
81
|
+
lambda {
|
82
|
+
@obj.write_key(:valid_key, "foo")
|
83
|
+
}.should_not raise_error
|
84
|
+
|
85
|
+
@obj.valid_key.should == "foo"
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should not raise a MissingKeyError if trying to read from a field that exists (but with a symbol instead of a string)" do
|
89
|
+
lambda {
|
90
|
+
@obj[:valid_key]
|
91
|
+
}.should_not raise_error
|
92
|
+
end
|
93
|
+
|
94
|
+
it "should not blow up when loading if there is a key defined in the db that has not been defined (but it should not load it)" do
|
95
|
+
@klass.collection.insert_one({ :foo => "bar", :valid_key => "something" })
|
96
|
+
@obj = @klass.first
|
97
|
+
@obj.valid_key.should == "something"
|
98
|
+
|
99
|
+
lambda {
|
100
|
+
@obj.foo
|
101
|
+
}.should raise_error(NoMethodError)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
describe "a class without static keys" do
|
106
|
+
before do
|
107
|
+
@klass = Class.new do
|
108
|
+
include MongoMapper::Document
|
109
|
+
key :valid_key, String
|
110
|
+
end
|
111
|
+
|
112
|
+
@obj = @klass.new
|
113
|
+
end
|
114
|
+
|
115
|
+
it "should be able to assign to a valid key" do
|
116
|
+
@obj.valid_key = "foo"
|
117
|
+
@obj.valid_key.should == "foo"
|
118
|
+
end
|
119
|
+
|
120
|
+
it "should be able to read and assign a dynamic key" do
|
121
|
+
@obj['foo'].should be_nil
|
122
|
+
@obj['foo'] = "bar"
|
123
|
+
@obj['foo'].should == "bar"
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
describe "with mixed static + non static models" do
|
128
|
+
before do
|
129
|
+
@static_model = Class.new do
|
130
|
+
include MongoMapper::Document
|
131
|
+
self.static_keys = true
|
132
|
+
end
|
133
|
+
|
134
|
+
@non_static_model = Class.new do
|
135
|
+
include MongoMapper::Document
|
136
|
+
self.static_keys = false
|
137
|
+
end
|
138
|
+
|
139
|
+
@static_key_object = @static_model.new
|
140
|
+
@non_static_key_object = @non_static_model.new
|
141
|
+
end
|
142
|
+
|
143
|
+
it "should allow non-static key assignment to one, only static assignment to the other" do
|
144
|
+
@non_static_key_object['foo'] = 'bar'
|
145
|
+
@non_static_key_object.foo.should == 'bar'
|
146
|
+
@non_static_key_object['foo'].should == 'bar'
|
147
|
+
|
148
|
+
lambda {
|
149
|
+
@static_key_object['foo'] = 'bar'
|
150
|
+
}.should raise_error(MongoMapper::Plugins::Keys::Static::MissingKeyError)
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Stats" do
|
4
|
+
before(:each) do
|
5
|
+
class ::Docs
|
6
|
+
include MongoMapper::Document
|
7
|
+
key :title, String
|
8
|
+
key :published_at, Time
|
9
|
+
end
|
10
|
+
|
11
|
+
Docs.collection.drop
|
12
|
+
end
|
13
|
+
|
14
|
+
context "with no documents present" do
|
15
|
+
it "should return nil" do
|
16
|
+
if Docs.stats == nil
|
17
|
+
Docs.stats.should == nil
|
18
|
+
else
|
19
|
+
Docs.stats['count'].should == 0
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context "with documents present" do
|
25
|
+
before do
|
26
|
+
# Make sure that there is at least one document stored
|
27
|
+
Docs.create!
|
28
|
+
end
|
29
|
+
|
30
|
+
def get_stats
|
31
|
+
MongoMapper.database.command(:collstats => 'docs').documents[0]
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should have the correct count" do
|
35
|
+
Docs.stats.count.should == get_stats['count']
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should have the correct namespace" do
|
39
|
+
Docs.stats.ns.should == get_stats['ns']
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should have the correct size" do
|
43
|
+
Docs.stats.size.should == get_stats['size']
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should have the correct storage size" do
|
47
|
+
Docs.stats.storage_size.should == get_stats['storageSize']
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should have the correct average object size" do
|
51
|
+
Docs.stats.avg_obj_size.should == get_stats['avgObjSize']
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should have the correct number of extents" do
|
55
|
+
if get_stats['numExtents']
|
56
|
+
Docs.stats.num_extents.should == get_stats['numExtents']
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should have the correct number of indexes" do
|
61
|
+
Docs.stats.nindexes.should == get_stats['nindexes']
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should have the correct last extent size" do
|
65
|
+
if get_stats['lastExtentSize']
|
66
|
+
Docs.stats.last_extent_size.should == get_stats['lastExtentSize']
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should have the correct padding factor" do
|
71
|
+
if get_stats['paddingFactor']
|
72
|
+
Docs.stats.padding_factor.should == get_stats['paddingFactor']
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should have the correct user flags" do
|
77
|
+
if get_stats['userFlags']
|
78
|
+
Docs.stats.user_flags.should == get_stats['userFlags']
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
it "should have the correct total index size" do
|
83
|
+
Docs.stats.total_index_size.should == get_stats['totalIndexSize']
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|