mongo_mapper 0.18.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.
@@ -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
@@ -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 active scopes" do
477
- count_active_scopes = nil
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
- count_active_scopes = @klass.active_scopes.length
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
- count_active_scopes.should == 0
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
@@ -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
- it "should have the correct size" do
43
- Docs.stats.size.should == get_stats['size']
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 the correct storage size" do
47
- Docs.stats.storage_size.should == get_stats['storageSize']
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 the correct average object size" do
51
- Docs.stats.avg_obj_size.should == get_stats['avgObjSize']
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 the correct last extent size" do
65
- if get_stats['lastExtentSize']
66
- Docs.stats.last_extent_size.should == get_stats['lastExtentSize']
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 the correct total index size" do
83
- Docs.stats.total_index_size.should == get_stats['totalIndexSize']
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 have the correct total index size (with scale)" do
88
- Docs.stats(scale: 1_024).total_index_size.should == (get_stats['totalIndexSize'] / 1_024)
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
@@ -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
@@ -5,6 +5,50 @@ describe "Plugins" do
5
5
  Class.new { extend MongoMapper::Plugins }.plugins.should == []
6
6
  end
7
7
 
8
+ context "direct_descendants" do
9
+ def build_and_forget(count)
10
+ count.times.map { yield.object_id }
11
+ end
12
+
13
+ def live_descendant_ids(mod, ids)
14
+ mod.direct_descendants.map(&:object_id) & ids
15
+ end
16
+
17
+ # Ruby 3.1 and 3.2 keep a fixed-size global method call cache (1023 slots)
18
+ # that the GC treats as a root. It pins the most recently defined model
19
+ # classes through their singleton classes, so they can't be collected even
20
+ # though nothing else references them. Defining singleton methods on lots of
21
+ # throwaway classes evicts those entries. Ruby 3.3+ no longer marks the
22
+ # cache, so this is skipped there.
23
+ def flush_global_method_cache
24
+ return unless RUBY_ENGINE == "ruby" && RUBY_VERSION >= "3.1" && RUBY_VERSION < "3.3"
25
+
26
+ 10_000.times { Class.new.define_singleton_method(:flush) {} }
27
+ end
28
+
29
+ [
30
+ ["Document", MongoMapper::Document, lambda { Doc() }],
31
+ ["EmbeddedDocument", MongoMapper::EmbeddedDocument, lambda { EDoc() }],
32
+ ].each do |label, mod, builder|
33
+ it "should not retain #{label} models that are no longer referenced" do
34
+ skip "GC is not deterministic on JRuby" if RUBY_ENGINE == "jruby"
35
+
36
+ ids = build_and_forget(20) { builder.call }
37
+
38
+ flush_global_method_cache
39
+
40
+ 3.times { GC.start(full_mark: true, immediate_sweep: true) }
41
+
42
+ live_descendant_ids(mod, ids).size.should < 20
43
+ end
44
+ end
45
+
46
+ it "should track models that are still referenced" do
47
+ model = Doc()
48
+ MongoMapper::Document.direct_descendants.should include(model)
49
+ end
50
+ end
51
+
8
52
  context "a plugin" do
9
53
  module MyConcern
10
54
  extend ActiveSupport::Concern
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongo_mapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.18.0
4
+ version: 0.18.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Nunemaker
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2026-10-26 00:00:00.000000000 Z
13
+ date: 2026-11-25 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: mongo
@@ -314,9 +314,9 @@ licenses:
314
314
  - MIT
315
315
  metadata:
316
316
  bug_tracker_uri: https://github.com/mongomapper/mongomapper/issues
317
- changelog_uri: https://github.com/mongomapper/mongomapper/releases/tag/v0.18.0
318
- documentation_uri: https://www.rubydoc.info/gems/mongomapper/0.18.0
319
- source_code_uri: https://github.com/mongomapper/mongomapper/tree/v0.18.0
317
+ changelog_uri: https://github.com/mongomapper/mongomapper/releases/tag/v0.18.1
318
+ documentation_uri: https://www.rubydoc.info/gems/mongomapper/0.18.1
319
+ source_code_uri: https://github.com/mongomapper/mongomapper/tree/v0.18.1
320
320
  post_install_message:
321
321
  rdoc_options: []
322
322
  require_paths: