mongo_mapper 0.17.0 → 0.18.1
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 +4 -4
- data/README.md +7 -3
- data/lib/mongo_mapper/connection.rb +20 -0
- data/lib/mongo_mapper/document.rb +1 -0
- data/lib/mongo_mapper/exceptions.rb +7 -1
- data/lib/mongo_mapper/extensions/symbol.rb +3 -0
- data/lib/mongo_mapper/plugins/associations/base.rb +3 -9
- data/lib/mongo_mapper/plugins/embedded_document.rb +10 -0
- data/lib/mongo_mapper/plugins/global_id.rb +8 -0
- data/lib/mongo_mapper/plugins/keys/key.rb +14 -0
- data/lib/mongo_mapper/plugins/keys/static.rb +3 -2
- data/lib/mongo_mapper/plugins/keys.rb +58 -7
- data/lib/mongo_mapper/plugins/scopes.rb +57 -27
- data/lib/mongo_mapper/plugins.rb +30 -8
- data/lib/mongo_mapper/railtie.rb +1 -2
- data/lib/mongo_mapper/version.rb +1 -1
- data/lib/mongo_mapper.rb +6 -0
- data/spec/config/alias.yml +14 -0
- data/spec/config/erb.yml +10 -0
- data/spec/config/simple.yml +11 -0
- data/spec/examples.txt +1722 -1689
- data/spec/functional/embedded_document_spec.rb +10 -0
- data/spec/functional/global_id_spec.rb +21 -0
- data/spec/functional/keys_spec.rb +80 -0
- data/spec/functional/scopes_spec.rb +170 -4
- data/spec/functional/static_keys_spec.rb +25 -1
- data/spec/functional/stats_spec.rb +59 -13
- data/spec/spec_helper.rb +2 -0
- data/spec/unit/associations/proxy_spec.rb +1 -1
- data/spec/unit/key_spec.rb +19 -0
- data/spec/unit/mongo_mapper_spec.rb +56 -8
- data/spec/unit/plugins_spec.rb +44 -0
- metadata +11 -6
|
@@ -346,4 +346,14 @@ describe "EmbeddedDocument" do
|
|
|
346
346
|
person.pets.first.flag.should be_falsey
|
|
347
347
|
end
|
|
348
348
|
end
|
|
349
|
+
|
|
350
|
+
it "should raise an error when no root" do
|
|
351
|
+
lambda {
|
|
352
|
+
@pet_klass.new.save
|
|
353
|
+
}.should raise_error(MongoMapper::NoRootDocument, "Can not save embedded document Pet without a root document.")
|
|
354
|
+
|
|
355
|
+
lambda {
|
|
356
|
+
@pet_klass.new.save!
|
|
357
|
+
}.should raise_error(MongoMapper::NoRootDocument, "Can not save embedded document Pet without a root document.")
|
|
358
|
+
end
|
|
349
359
|
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
require "spec_helper"
|
|
2
|
+
|
|
3
|
+
describe "GlobalID" do
|
|
4
|
+
before do
|
|
5
|
+
@document = stub_const("TestDocument", Doc("TestDocument") do
|
|
6
|
+
key :name, String
|
|
7
|
+
end)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
it "generates a valid GlobalID" do
|
|
11
|
+
doc = @document.create!(name: "Test")
|
|
12
|
+
doc.to_global_id.should be_a(GlobalID)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
it "can be found by GlobalID" do
|
|
16
|
+
doc = @document.create!(name: "Test")
|
|
17
|
+
global_id = doc.to_global_id
|
|
18
|
+
found_doc = GlobalID::Locator.locate(global_id)
|
|
19
|
+
found_doc.should == doc
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -188,6 +188,86 @@ describe "Keys" do
|
|
|
188
188
|
doc.new.good_name.should be_nil
|
|
189
189
|
lambda { doc.new.method("good_name") }.should_not raise_error
|
|
190
190
|
end
|
|
191
|
+
|
|
192
|
+
it "should warn but keep working when a key clashes with an internal method" do
|
|
193
|
+
doc = Doc {}
|
|
194
|
+
Kernel.should_receive(:warn).with(/\[DEPRECATION\] Key name `valid` clashes with an internal MongoMapper method \(valid\?\) and will raise in a future release/)
|
|
195
|
+
doc.class_eval do
|
|
196
|
+
key :valid, Boolean
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
instance = doc.new(:valid => false)
|
|
200
|
+
instance.valid.should == false
|
|
201
|
+
# Validations#valid? is included later and still answers validation, which is
|
|
202
|
+
# why a key named `valid` is confusing enough to deprecate.
|
|
203
|
+
instance.valid?.should be_truthy
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
it "should warn when only the predicate accessor clashes" do
|
|
207
|
+
doc = Doc {}
|
|
208
|
+
Kernel.should_receive(:warn).with(/\[DEPRECATION\] Key name `valid` clashes with an internal MongoMapper method \(valid\?\) and will raise in a future release/)
|
|
209
|
+
doc.class_eval do
|
|
210
|
+
key :valid, Boolean, :accessors => :predicate
|
|
211
|
+
end
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
it "should not warn when the clashing accessor is not created" do
|
|
215
|
+
doc = Doc {}
|
|
216
|
+
Kernel.should_not_receive(:warn)
|
|
217
|
+
doc.class_eval do
|
|
218
|
+
key :valid, Boolean, :accessors => :read
|
|
219
|
+
end
|
|
220
|
+
doc.new(:valid => false).valid.should == false
|
|
221
|
+
doc.new.valid?.should be_truthy
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
it "should not warn for a __dynamic key that clashes" do
|
|
225
|
+
doc = Doc {}
|
|
226
|
+
Kernel.should_not_receive(:warn)
|
|
227
|
+
doc.class_eval do
|
|
228
|
+
key :valid, Boolean, :__dynamic => true
|
|
229
|
+
end
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
it "should not warn when accessors are skipped" do
|
|
233
|
+
doc = Doc {}
|
|
234
|
+
Kernel.should_not_receive(:warn)
|
|
235
|
+
doc.class_eval do
|
|
236
|
+
key :valid, Boolean, :accessors => :skip
|
|
237
|
+
key :errors, String, :accessors => :none
|
|
238
|
+
end
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
it "should still raise for reserved key names" do
|
|
242
|
+
doc = Doc {}
|
|
243
|
+
lambda {
|
|
244
|
+
doc.class_eval { key :id, String }
|
|
245
|
+
}.should raise_error(MongoMapper::InvalidKey, /reserved/)
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
it "should warn when a key clashes with an ActiveModel method" do
|
|
249
|
+
doc = Doc {}
|
|
250
|
+
Kernel.should_receive(:warn).with(/\[DEPRECATION\] Key name `errors` clashes with an internal MongoMapper method \(errors\) and will raise in a future release/)
|
|
251
|
+
doc.class_eval do
|
|
252
|
+
key :errors, String
|
|
253
|
+
end
|
|
254
|
+
# ActiveModel may prepend `errors`, so the key reader does not always
|
|
255
|
+
# win. The value is still stored on the key.
|
|
256
|
+
doc.new(:errors => "kept")[:errors].should == "kept"
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
it "should not warn when redefining the _id key" do
|
|
260
|
+
Kernel.should_not_receive(:warn)
|
|
261
|
+
Doc() do
|
|
262
|
+
key :_id, Integer
|
|
263
|
+
end
|
|
264
|
+
end
|
|
265
|
+
|
|
266
|
+
it "should not warn when a subclass receives the _type key" do
|
|
267
|
+
Kernel.should_not_receive(:warn)
|
|
268
|
+
parent = Doc("ParentType")
|
|
269
|
+
Subclass(parent, "ChildType")
|
|
270
|
+
end
|
|
191
271
|
end
|
|
192
272
|
|
|
193
273
|
it "should handle loading dynamic fields from the database that have bad names" do
|
|
@@ -139,6 +139,17 @@ describe "Scopes" do
|
|
|
139
139
|
docs.first.age.should == 50
|
|
140
140
|
end
|
|
141
141
|
|
|
142
|
+
it "should work with scoped queries swapped" do
|
|
143
|
+
u1 = @document.create(:name => 'John', :age => 60)
|
|
144
|
+
u2 = @document.create(:name => 'John', :age => 50)
|
|
145
|
+
u3 = @document.create(:name => 'Bill', :age => 50)
|
|
146
|
+
|
|
147
|
+
q1 = @document.by_name('John')
|
|
148
|
+
q2 = @document.by_age(50)
|
|
149
|
+
q1.all.should == [u1, u2]
|
|
150
|
+
q2.all.should == [u2, u3]
|
|
151
|
+
end
|
|
152
|
+
|
|
142
153
|
context "with model methods" do
|
|
143
154
|
it "should work if method returns a query" do
|
|
144
155
|
@document.create(:name => 'John', :age => 10)
|
|
@@ -253,6 +264,109 @@ describe "Scopes" do
|
|
|
253
264
|
@klass.all.should include(u2)
|
|
254
265
|
end
|
|
255
266
|
|
|
267
|
+
it "should work in nested" do
|
|
268
|
+
u1 = @klass.create!(:first_name => "Scott", :age => 10)
|
|
269
|
+
u2 = @klass.create!(:first_name => "Scott", :age => 20)
|
|
270
|
+
u3 = @klass.create!(:first_name => "Andrew", :age => 20)
|
|
271
|
+
|
|
272
|
+
run = false
|
|
273
|
+
|
|
274
|
+
@klass.with_scope(:first_name => "Scott") do
|
|
275
|
+
@klass.with_scope(:age => 20) do
|
|
276
|
+
run = true
|
|
277
|
+
@klass.all.should == [u2]
|
|
278
|
+
end
|
|
279
|
+
|
|
280
|
+
@klass.all.should == [u1, u2]
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
run.should == true
|
|
284
|
+
|
|
285
|
+
@klass.all.should == [u1, u2, u3]
|
|
286
|
+
end
|
|
287
|
+
|
|
288
|
+
it "should work with nested two class scopes" do
|
|
289
|
+
other = Doc("Other") do
|
|
290
|
+
end
|
|
291
|
+
|
|
292
|
+
u1 = @klass.create!(:first_name => "Scott")
|
|
293
|
+
u2 = @klass.create!(:first_name => "Andrew")
|
|
294
|
+
|
|
295
|
+
a1 = other.create!(:first_name => "Scott")
|
|
296
|
+
a2 = other.create!(:first_name => "Andrew")
|
|
297
|
+
|
|
298
|
+
run = false
|
|
299
|
+
|
|
300
|
+
@klass.with_scope(:first_name => "Scott") do
|
|
301
|
+
other.with_scope(:first_name => "Andrew") do
|
|
302
|
+
run = true
|
|
303
|
+
@klass.all.should == [u1]
|
|
304
|
+
other.all.should == [a2]
|
|
305
|
+
end
|
|
306
|
+
|
|
307
|
+
@klass.all.should == [u1]
|
|
308
|
+
other.all.should == [a1, a2]
|
|
309
|
+
end
|
|
310
|
+
|
|
311
|
+
run.should == true
|
|
312
|
+
|
|
313
|
+
@klass.all.should == [u1, u2]
|
|
314
|
+
other.all.should == [a1, a2]
|
|
315
|
+
end
|
|
316
|
+
|
|
317
|
+
it "should work with default scope" do
|
|
318
|
+
u1 = @klass.create!(:first_name => "Scott", :age => 10)
|
|
319
|
+
u2 = @klass.create!(:first_name => "Scott", :age => 20)
|
|
320
|
+
u3 = @klass.create!(:first_name => "Andrew", :age => 20)
|
|
321
|
+
|
|
322
|
+
@klass.default_scope { where(age: 20) }
|
|
323
|
+
|
|
324
|
+
result = @klass.with_scope(:first_name => "Scott") do
|
|
325
|
+
@klass.all
|
|
326
|
+
end
|
|
327
|
+
|
|
328
|
+
result.should == [u2]
|
|
329
|
+
end
|
|
330
|
+
|
|
331
|
+
it "should work with scope chain" do
|
|
332
|
+
u1 = @klass.create!(:first_name => "Scott", :age => 10)
|
|
333
|
+
u2 = @klass.create!(:first_name => "Scott", :age => 20)
|
|
334
|
+
u3 = @klass.create!(:first_name => "Andrew", :age => 20)
|
|
335
|
+
|
|
336
|
+
@klass.scope :by_age, lambda { |age| where(age: age) }
|
|
337
|
+
|
|
338
|
+
result = @klass.by_age(20).with_scope(:first_name => "Scott") do
|
|
339
|
+
@klass.all
|
|
340
|
+
end
|
|
341
|
+
|
|
342
|
+
result.should == [u2]
|
|
343
|
+
end
|
|
344
|
+
|
|
345
|
+
it "should keep query options on the query inside with_scope" do
|
|
346
|
+
younger = @klass.create!(:first_name => "Scott", :age => 10)
|
|
347
|
+
@klass.create!(:first_name => "Scott", :age => 20)
|
|
348
|
+
@klass.create!(:first_name => "Andrew", :age => 30)
|
|
349
|
+
|
|
350
|
+
@klass.with_scope(:first_name => "Scott") do
|
|
351
|
+
scoped = @klass.query(:age => 10, :limit => 1, :sort => :age)
|
|
352
|
+
scoped.criteria_hash[:first_name].should == "Scott"
|
|
353
|
+
scoped.criteria_hash[:age].should == 10
|
|
354
|
+
scoped.criteria_hash.key?(:limit).should be_falsey
|
|
355
|
+
scoped.criteria_hash.key?(:sort).should be_falsey
|
|
356
|
+
scoped.options_hash[:limit].should == 1
|
|
357
|
+
scoped.all.should == [younger]
|
|
358
|
+
|
|
359
|
+
# A later query in the same scope must not inherit that limit.
|
|
360
|
+
@klass.all.map(&:age).sort.should == [10, 20]
|
|
361
|
+
end
|
|
362
|
+
end
|
|
363
|
+
|
|
364
|
+
it "should not mutate the hash passed to query" do
|
|
365
|
+
options = {:first_name => "Scott", :unscoped => true}
|
|
366
|
+
@klass.query(options)
|
|
367
|
+
options.should == {:first_name => "Scott", :unscoped => true}
|
|
368
|
+
end
|
|
369
|
+
|
|
256
370
|
it "should be able to use an unscoped query" do
|
|
257
371
|
u1 = @klass.create!(:first_name => "Scott")
|
|
258
372
|
u2 = @klass.create!(:first_name => "Andrew")
|
|
@@ -368,6 +482,33 @@ describe "Scopes" do
|
|
|
368
482
|
@subclass.default_scopes.length.should == 2
|
|
369
483
|
@klass.default_scopes.length.should == 1
|
|
370
484
|
end
|
|
485
|
+
|
|
486
|
+
it "should be cleared in unscoped" do
|
|
487
|
+
@klass.key :active, Boolean, default: true
|
|
488
|
+
|
|
489
|
+
normal = @klass.create!
|
|
490
|
+
inactive = @klass.create!(active: false)
|
|
491
|
+
|
|
492
|
+
@klass.default_scope { where(active: true) }
|
|
493
|
+
|
|
494
|
+
@klass.unscoped do
|
|
495
|
+
@klass.all.should == [normal, inactive]
|
|
496
|
+
end
|
|
497
|
+
end
|
|
498
|
+
|
|
499
|
+
it "should allow a default scope proc that queries the class" do
|
|
500
|
+
@klass.key :active, Boolean, :default => true
|
|
501
|
+
|
|
502
|
+
normal = @klass.create!
|
|
503
|
+
@klass.create!(:active => false)
|
|
504
|
+
|
|
505
|
+
# The proc is instance_exec'd on the query, so it must close over the
|
|
506
|
+
# model. Calling where on the class re-enters query and must not recurse.
|
|
507
|
+
model = @klass
|
|
508
|
+
@klass.default_scope -> { model.where(:active => true) }
|
|
509
|
+
|
|
510
|
+
@klass.all.should == [normal]
|
|
511
|
+
end
|
|
371
512
|
end
|
|
372
513
|
end
|
|
373
514
|
|
|
@@ -473,8 +614,8 @@ describe "Scopes" do
|
|
|
473
614
|
@threads.each { |t| t.join }
|
|
474
615
|
end
|
|
475
616
|
|
|
476
|
-
it "should not taint another thread's
|
|
477
|
-
|
|
617
|
+
it "should not taint another thread's current_context" do
|
|
618
|
+
is_current_context_nil = nil
|
|
478
619
|
second_thread_run = false
|
|
479
620
|
|
|
480
621
|
make_thread do
|
|
@@ -487,13 +628,13 @@ describe "Scopes" do
|
|
|
487
628
|
end
|
|
488
629
|
|
|
489
630
|
make_thread do
|
|
490
|
-
|
|
631
|
+
is_current_context_nil = @klass.send(:current_context).nil?
|
|
491
632
|
second_thread_run = true
|
|
492
633
|
end
|
|
493
634
|
|
|
494
635
|
wait_for_threads!
|
|
495
636
|
|
|
496
|
-
|
|
637
|
+
is_current_context_nil.should == true
|
|
497
638
|
end
|
|
498
639
|
|
|
499
640
|
it "should not taint another thread's count method" do
|
|
@@ -521,5 +662,30 @@ describe "Scopes" do
|
|
|
521
662
|
|
|
522
663
|
klass_count.should == 2
|
|
523
664
|
end
|
|
665
|
+
|
|
666
|
+
it "should not taint another thread's default_scopes" do
|
|
667
|
+
@klass.default_scope { where }
|
|
668
|
+
|
|
669
|
+
count_default_scopes = nil
|
|
670
|
+
second_thread_run = false
|
|
671
|
+
|
|
672
|
+
make_thread do
|
|
673
|
+
@klass.unscoped do
|
|
674
|
+
loop do
|
|
675
|
+
sleep Float::EPSILON
|
|
676
|
+
break if second_thread_run
|
|
677
|
+
end
|
|
678
|
+
end
|
|
679
|
+
end
|
|
680
|
+
|
|
681
|
+
make_thread do
|
|
682
|
+
count_default_scopes = @klass.default_scopes.length
|
|
683
|
+
second_thread_run = true
|
|
684
|
+
end
|
|
685
|
+
|
|
686
|
+
wait_for_threads!
|
|
687
|
+
|
|
688
|
+
count_default_scopes.should == 1
|
|
689
|
+
end
|
|
524
690
|
end
|
|
525
691
|
end
|
|
@@ -18,6 +18,10 @@ describe MongoMapper::Plugins::Keys::Static do
|
|
|
18
18
|
|
|
19
19
|
describe "a class with static keys" do
|
|
20
20
|
before do
|
|
21
|
+
@other_klass = EDoc('Other') do
|
|
22
|
+
key :key, String
|
|
23
|
+
end
|
|
24
|
+
|
|
21
25
|
@klass = Class.new do
|
|
22
26
|
include MongoMapper::Document
|
|
23
27
|
|
|
@@ -28,6 +32,8 @@ describe MongoMapper::Plugins::Keys::Static do
|
|
|
28
32
|
key "valid_key", String
|
|
29
33
|
end
|
|
30
34
|
|
|
35
|
+
@klass.many :other, class: @other_klass
|
|
36
|
+
|
|
31
37
|
@obj = @klass.new
|
|
32
38
|
end
|
|
33
39
|
|
|
@@ -100,6 +106,24 @@ describe MongoMapper::Plugins::Keys::Static do
|
|
|
100
106
|
@obj.foo
|
|
101
107
|
}.should raise_error(NoMethodError)
|
|
102
108
|
end
|
|
109
|
+
|
|
110
|
+
it "should allow saving embedded documents" do
|
|
111
|
+
@obj.other.build(key: "foo")
|
|
112
|
+
@obj.save!
|
|
113
|
+
|
|
114
|
+
db_obj = @klass.collection.find(_id: @obj.id).first
|
|
115
|
+
db_obj["other"].should be_present
|
|
116
|
+
db_obj["other"][0]["key"].should == "foo"
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
it "should allow loading embedded documents" do
|
|
120
|
+
@obj.other.build(key: "foo")
|
|
121
|
+
@obj.save!
|
|
122
|
+
|
|
123
|
+
@obj.reload
|
|
124
|
+
@obj.other.count.should == 1
|
|
125
|
+
@obj.other.first.key.should == "foo"
|
|
126
|
+
end
|
|
103
127
|
end
|
|
104
128
|
|
|
105
129
|
describe "a class without static keys" do
|
|
@@ -150,4 +174,4 @@ describe MongoMapper::Plugins::Keys::Static do
|
|
|
150
174
|
}.should raise_error(MongoMapper::Plugins::Keys::Static::MissingKeyError)
|
|
151
175
|
end
|
|
152
176
|
end
|
|
153
|
-
end
|
|
177
|
+
end
|
|
@@ -31,6 +31,18 @@ describe "Stats" do
|
|
|
31
31
|
MongoMapper.database.command(:collstats => 'docs').documents[0]
|
|
32
32
|
end
|
|
33
33
|
|
|
34
|
+
def assert_positive_size(value)
|
|
35
|
+
value.should be_a(Numeric)
|
|
36
|
+
value.should be > 0
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def assert_index_sizes_consistent(stats)
|
|
40
|
+
return unless stats.respond_to?(:index_sizes) && stats.index_sizes
|
|
41
|
+
|
|
42
|
+
stats.index_sizes.size.should == stats.nindexes
|
|
43
|
+
stats.index_sizes.values.inject(0, :+).should == stats.total_index_size
|
|
44
|
+
end
|
|
45
|
+
|
|
34
46
|
it "should have the correct count" do
|
|
35
47
|
Docs.stats.count.should == get_stats['count']
|
|
36
48
|
end
|
|
@@ -39,16 +51,21 @@ describe "Stats" do
|
|
|
39
51
|
Docs.stats.ns.should == get_stats['ns']
|
|
40
52
|
end
|
|
41
53
|
|
|
42
|
-
|
|
43
|
-
|
|
54
|
+
# WiredTiger size stats (data/index/storage sizes) can change between
|
|
55
|
+
# collStats calls as data is checkpointed, so size fields are asserted
|
|
56
|
+
# from a single response rather than compared across two commands.
|
|
57
|
+
it "should have a positive data size" do
|
|
58
|
+
assert_positive_size(Docs.stats.size)
|
|
44
59
|
end
|
|
45
60
|
|
|
46
|
-
it "should have
|
|
47
|
-
Docs.stats.storage_size
|
|
61
|
+
it "should have a positive storage size" do
|
|
62
|
+
assert_positive_size(Docs.stats.storage_size)
|
|
48
63
|
end
|
|
49
64
|
|
|
50
|
-
it "should have
|
|
51
|
-
Docs.stats
|
|
65
|
+
it "should have a positive average object size consistent with size and count" do
|
|
66
|
+
stats = Docs.stats
|
|
67
|
+
assert_positive_size(stats.avg_obj_size)
|
|
68
|
+
stats.avg_obj_size.should be_within(1).of(stats.size.to_f / stats.count)
|
|
52
69
|
end
|
|
53
70
|
|
|
54
71
|
it "should have the correct number of extents" do
|
|
@@ -61,9 +78,10 @@ describe "Stats" do
|
|
|
61
78
|
Docs.stats.nindexes.should == get_stats['nindexes']
|
|
62
79
|
end
|
|
63
80
|
|
|
64
|
-
it "should have
|
|
65
|
-
|
|
66
|
-
|
|
81
|
+
it "should have a last extent size when the storage engine reports one" do
|
|
82
|
+
stats = Docs.stats
|
|
83
|
+
if stats.respond_to?(:last_extent_size)
|
|
84
|
+
assert_positive_size(stats.last_extent_size)
|
|
67
85
|
end
|
|
68
86
|
end
|
|
69
87
|
|
|
@@ -79,13 +97,41 @@ describe "Stats" do
|
|
|
79
97
|
end
|
|
80
98
|
end
|
|
81
99
|
|
|
82
|
-
it "should have
|
|
83
|
-
Docs.stats
|
|
100
|
+
it "should have a positive total index size consistent with per-index sizes" do
|
|
101
|
+
stats = Docs.stats
|
|
102
|
+
assert_positive_size(stats.total_index_size)
|
|
103
|
+
stats.nindexes.should be > 0
|
|
104
|
+
assert_index_sizes_consistent(stats)
|
|
105
|
+
stats.scale_factor.should == 1 if stats.respond_to?(:scale_factor)
|
|
84
106
|
end
|
|
85
107
|
|
|
86
108
|
context 'with scale keyword argument' do
|
|
87
|
-
it "should
|
|
88
|
-
|
|
109
|
+
it "should scale size fields in a single collStats response" do
|
|
110
|
+
scale = 1_024
|
|
111
|
+
scaled = Docs.stats(scale: scale)
|
|
112
|
+
|
|
113
|
+
# Deterministic fields stay exact even against a separate unscaled read.
|
|
114
|
+
unscaled = Docs.stats
|
|
115
|
+
scaled.count.should == unscaled.count
|
|
116
|
+
scaled.ns.should == unscaled.ns
|
|
117
|
+
scaled.nindexes.should == unscaled.nindexes
|
|
118
|
+
|
|
119
|
+
# Size fields come from the scaled response only. WiredTiger can
|
|
120
|
+
# change index/storage sizes between calls, so we do not require
|
|
121
|
+
# scaled.total_index_size == unscaled.total_index_size / scale.
|
|
122
|
+
scaled.total_index_size.should be_a(Numeric)
|
|
123
|
+
scaled.total_index_size.should be >= 0
|
|
124
|
+
scaled.size.should be_a(Numeric)
|
|
125
|
+
scaled.size.should be >= 0
|
|
126
|
+
scaled.storage_size.should be_a(Numeric)
|
|
127
|
+
scaled.storage_size.should be >= 0
|
|
128
|
+
|
|
129
|
+
# scaleFactor proves the keyword was forwarded to MongoDB.
|
|
130
|
+
if scaled.respond_to?(:scale_factor)
|
|
131
|
+
scaled.scale_factor.should == scale
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
assert_index_sizes_consistent(scaled)
|
|
89
135
|
end
|
|
90
136
|
end
|
|
91
137
|
end
|
data/spec/spec_helper.rb
CHANGED
|
@@ -105,7 +105,7 @@ describe "Proxy" do
|
|
|
105
105
|
end
|
|
106
106
|
end)
|
|
107
107
|
|
|
108
|
-
lambda { @proxy.private_foo }.should raise_error(NoMethodError, /private method `private_foo' called/)
|
|
108
|
+
lambda { @proxy.private_foo }.should raise_error(NoMethodError, /private method ['`]private_foo' called/)
|
|
109
109
|
end
|
|
110
110
|
end
|
|
111
111
|
|
data/spec/unit/key_spec.rb
CHANGED
|
@@ -63,6 +63,25 @@ describe "Key" do
|
|
|
63
63
|
lambda { Key.new(:id) }.should raise_error(/reserved/)
|
|
64
64
|
end
|
|
65
65
|
|
|
66
|
+
it "should not warn for a clashing name without a model class" do
|
|
67
|
+
Kernel.should_not_receive(:warn)
|
|
68
|
+
lambda { Key.new(:valid) }.should_not raise_error
|
|
69
|
+
lambda { Key.new(:valid, Boolean, :accessors => :predicate) }.should_not raise_error
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
it "should permit reserved names if __dynamic" do
|
|
73
|
+
Kernel.should_not_receive(:warn)
|
|
74
|
+
lambda { Key.new(:id, :__dynamic => true) }.should_not raise_error
|
|
75
|
+
lambda { Key.new(:valid, :__dynamic => true) }.should_not raise_error
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
it "should permit reserved names if it is not to create accessors" do
|
|
79
|
+
Kernel.should_not_receive(:warn)
|
|
80
|
+
lambda { Key.new(:id, :accessors => :skip) }.should_not raise_error
|
|
81
|
+
lambda { Key.new(:valid, :accessors => :skip) }.should_not raise_error
|
|
82
|
+
lambda { Key.new(:valid, :accessors => :none) }.should_not raise_error
|
|
83
|
+
end
|
|
84
|
+
|
|
66
85
|
it "should not permit bad names" do
|
|
67
86
|
lambda { Key.new(:"id.bar") }.should raise_error(/must match/)
|
|
68
87
|
end
|
|
@@ -46,7 +46,7 @@ describe "MongoMapper" do
|
|
|
46
46
|
MongoMapper.config = {
|
|
47
47
|
'development' => {'uri' => 'mongodb://127.0.0.1:27017/test'}
|
|
48
48
|
}
|
|
49
|
-
Mongo::Client.should_receive(:new).with('mongodb://127.0.0.1:27017/test', {})
|
|
49
|
+
Mongo::Client.should_receive(:new).with('mongodb://127.0.0.1:27017/test', { :wrapping_libraries=>[{:name=>"MongoMapper", :version=>::MongoMapper::Version}] })
|
|
50
50
|
MongoMapper.connect('development')
|
|
51
51
|
end
|
|
52
52
|
|
|
@@ -54,7 +54,7 @@ describe "MongoMapper" do
|
|
|
54
54
|
MongoMapper.config = {
|
|
55
55
|
'development' => {'hosts' => ['127.0.0.1:27017']}
|
|
56
56
|
}
|
|
57
|
-
Mongo::Client.should_receive(:new).with(['127.0.0.1:27017'], {})
|
|
57
|
+
Mongo::Client.should_receive(:new).with(['127.0.0.1:27017'], { :wrapping_libraries=>[{:name=>"MongoMapper", :version=>::MongoMapper::Version}] })
|
|
58
58
|
MongoMapper.connect('development')
|
|
59
59
|
end
|
|
60
60
|
|
|
@@ -62,7 +62,7 @@ describe "MongoMapper" do
|
|
|
62
62
|
MongoMapper.config = {
|
|
63
63
|
'development' => {'host' => '127.0.0.1:27017'}
|
|
64
64
|
}
|
|
65
|
-
Mongo::Client.should_receive(:new).with(['127.0.0.1:27017'], {})
|
|
65
|
+
Mongo::Client.should_receive(:new).with(['127.0.0.1:27017'], { :wrapping_libraries=>[{:name=>"MongoMapper", :version=>::MongoMapper::Version}] })
|
|
66
66
|
MongoMapper.connect('development')
|
|
67
67
|
end
|
|
68
68
|
|
|
@@ -71,7 +71,7 @@ describe "MongoMapper" do
|
|
|
71
71
|
'development' => {'hosts' => ['127.0.0.1:27017'], 'database' => 'test', 'read' => 'primary'}
|
|
72
72
|
}
|
|
73
73
|
logger = double('logger')
|
|
74
|
-
Mongo::Client.should_receive(:new).with(['127.0.0.1:27017'], { :logger => logger, :read => :primary, :database => 'test' })
|
|
74
|
+
Mongo::Client.should_receive(:new).with(['127.0.0.1:27017'], { :logger => logger, :read => :primary, :database => 'test', :wrapping_libraries=>[{:name=>"MongoMapper", :version=>::MongoMapper::Version}] })
|
|
75
75
|
MongoMapper.connect('development', :logger => logger)
|
|
76
76
|
end
|
|
77
77
|
|
|
@@ -80,7 +80,7 @@ describe "MongoMapper" do
|
|
|
80
80
|
'development' => {'hosts' => ['192.168.1.1:2222'], 'database' => 'test', 'safe' => true}
|
|
81
81
|
}
|
|
82
82
|
logger = double('logger')
|
|
83
|
-
Mongo::Client.should_receive(:new).with(['192.168.1.1:2222'], { :logger => logger, :safe => true, :database => 'test' })
|
|
83
|
+
Mongo::Client.should_receive(:new).with(['192.168.1.1:2222'], { :logger => logger, :safe => true, :database => 'test', :wrapping_libraries=>[{:name=>"MongoMapper", :version=>::MongoMapper::Version}] })
|
|
84
84
|
MongoMapper.connect('development', :logger => logger)
|
|
85
85
|
end
|
|
86
86
|
|
|
@@ -89,7 +89,7 @@ describe "MongoMapper" do
|
|
|
89
89
|
'development' => {'uri' => 'mongodb://127.0.0.1:27017/test', 'options'=> {:foo => 1}}
|
|
90
90
|
}
|
|
91
91
|
logger = double('logger')
|
|
92
|
-
Mongo::Client.should_receive(:new).with('mongodb://127.0.0.1:27017/test', { :logger => logger, :foo => 1 })
|
|
92
|
+
Mongo::Client.should_receive(:new).with('mongodb://127.0.0.1:27017/test', { :logger => logger, :foo => 1, :wrapping_libraries=>[{:name=>"MongoMapper", :version=>::MongoMapper::Version}] })
|
|
93
93
|
MongoMapper.connect('development', :logger => logger)
|
|
94
94
|
end
|
|
95
95
|
|
|
@@ -97,7 +97,7 @@ describe "MongoMapper" do
|
|
|
97
97
|
MongoMapper.config = {
|
|
98
98
|
'development' => {'hosts' => ['127.0.0.1:27017'], 'database' => 'test', 'user' => 'john', 'password' => 'secret'}
|
|
99
99
|
}
|
|
100
|
-
Mongo::Client.should_receive(:new).with(['127.0.0.1:27017'], { :database => 'test', :user => 'john', :password => 'secret' })
|
|
100
|
+
Mongo::Client.should_receive(:new).with(['127.0.0.1:27017'], { :database => 'test', :user => 'john', :password => 'secret', :wrapping_libraries=>[{:name=>"MongoMapper", :version=>::MongoMapper::Version}] })
|
|
101
101
|
MongoMapper.connect('development')
|
|
102
102
|
end
|
|
103
103
|
|
|
@@ -105,7 +105,7 @@ describe "MongoMapper" do
|
|
|
105
105
|
MongoMapper.config = {
|
|
106
106
|
'development' => {'uri' => 'mongodb://john:secret@127.0.0.1:27017/test'}
|
|
107
107
|
}
|
|
108
|
-
Mongo::Client.should_receive(:new).with('mongodb://john:secret@127.0.0.1:27017/test', {})
|
|
108
|
+
Mongo::Client.should_receive(:new).with('mongodb://john:secret@127.0.0.1:27017/test', { :wrapping_libraries=>[{:name=>"MongoMapper", :version=>::MongoMapper::Version}]})
|
|
109
109
|
MongoMapper.connect('development')
|
|
110
110
|
end
|
|
111
111
|
|
|
@@ -140,6 +140,54 @@ describe "MongoMapper" do
|
|
|
140
140
|
end
|
|
141
141
|
end
|
|
142
142
|
|
|
143
|
+
context "setup_with_config_file" do
|
|
144
|
+
it "should work with config/simple.yml" do
|
|
145
|
+
logger = double('logger')
|
|
146
|
+
path = File.expand_path('../../config/simple.yml', __FILE__)
|
|
147
|
+
MongoMapper.should_receive(:config=).with({
|
|
148
|
+
"production" => { "host" => "127.0.0.1:21017", "database" => "production" },
|
|
149
|
+
"development" => { "host" => "127.0.0.1:21017", "database" => "development" },
|
|
150
|
+
"test" => { "host" => "127.0.0.1:21017", "database" => "test" },
|
|
151
|
+
})
|
|
152
|
+
MongoMapper.should_receive(:connect).with('development', { :logger => logger })
|
|
153
|
+
MongoMapper.should_receive(:handle_passenger_forking).once
|
|
154
|
+
MongoMapper.setup_with_config_file(path, 'development', :logger => logger)
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
it "should work with config/alias.yml" do
|
|
158
|
+
logger = double('logger')
|
|
159
|
+
path = File.expand_path('../../config/alias.yml', __FILE__)
|
|
160
|
+
MongoMapper.should_receive(:config=).with({
|
|
161
|
+
"default" => { "host" => "127.0.0.1:21017" },
|
|
162
|
+
"production" => { "host" => "127.0.0.1:21017", "database" => "production" },
|
|
163
|
+
"development" => { "host" => "127.0.0.1:21017", "database" => "development" },
|
|
164
|
+
"test" => { "host" => "127.0.0.1:21017", "database" => "test" },
|
|
165
|
+
})
|
|
166
|
+
MongoMapper.should_receive(:connect).with('development', { :logger => logger })
|
|
167
|
+
MongoMapper.should_receive(:handle_passenger_forking).once
|
|
168
|
+
MongoMapper.setup_with_config_file(path, 'development', :logger => logger)
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
it "should work with config/erb.yml" do
|
|
172
|
+
begin
|
|
173
|
+
old_mongodb_uri, ENV["MONGODB_URI"] = ENV["MONGODB_URI"], "mongodb://127.0.0.1:21017/production"
|
|
174
|
+
|
|
175
|
+
logger = double('logger')
|
|
176
|
+
path = File.expand_path('../../config/erb.yml', __FILE__)
|
|
177
|
+
MongoMapper.should_receive(:config=).with({
|
|
178
|
+
"production" => { "uri" => "mongodb://127.0.0.1:21017/production" },
|
|
179
|
+
"development" => { "host" => "127.0.0.1:21017", "database" => "development" },
|
|
180
|
+
"test" => { "host" => "127.0.0.1:21017", "database" => "test" },
|
|
181
|
+
})
|
|
182
|
+
MongoMapper.should_receive(:connect).with('development', { :logger => logger })
|
|
183
|
+
MongoMapper.should_receive(:handle_passenger_forking).once
|
|
184
|
+
MongoMapper.setup_with_config_file(path, 'development', :logger => logger)
|
|
185
|
+
ensure
|
|
186
|
+
ENV["MONGODB_URI"] = old_mongodb_uri
|
|
187
|
+
end
|
|
188
|
+
end
|
|
189
|
+
end
|
|
190
|
+
|
|
143
191
|
context "options" do
|
|
144
192
|
it "should sets/returns filtered_attributes correctly" do
|
|
145
193
|
MongoMapper.filter_attributes.should == []
|