mongoid 7.0.2 → 7.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/mongoid/association/referenced/eager.rb +4 -1
- data/lib/mongoid/association/referenced/has_and_belongs_to_many/proxy.rb +3 -1
- data/lib/mongoid/association/referenced/has_many/proxy.rb +2 -2
- data/lib/mongoid/association/relatable.rb +90 -10
- data/lib/mongoid/clients/options.rb +6 -4
- data/lib/mongoid/copyable.rb +2 -2
- data/lib/mongoid/criteria/options.rb +2 -2
- data/lib/mongoid/criteria/queryable/selectable.rb +33 -6
- data/lib/mongoid/document.rb +11 -4
- data/lib/mongoid/extensions/big_decimal.rb +1 -1
- data/lib/mongoid/extensions/regexp.rb +1 -0
- data/lib/mongoid/matchable.rb +3 -1
- data/lib/mongoid/matchable/eq.rb +22 -0
- data/lib/mongoid/matchable/ne.rb +1 -1
- data/lib/mongoid/persistence_context.rb +20 -5
- data/lib/mongoid/query_cache.rb +8 -4
- data/lib/mongoid/railtie.rb +17 -0
- data/lib/mongoid/railties/controller_runtime.rb +86 -0
- data/lib/mongoid/scopable.rb +3 -3
- data/lib/mongoid/threaded.rb +36 -0
- data/lib/mongoid/version.rb +1 -1
- data/spec/app/models/minim.rb +7 -0
- data/spec/app/models/store_as_dup_test3.rb +7 -0
- data/spec/app/models/store_as_dup_test4.rb +7 -0
- data/spec/config/mongoid.yml +12 -3
- data/spec/integration/associations/belongs_to_spec.rb +13 -0
- data/spec/lite_spec_helper.rb +56 -0
- data/spec/mongoid/association/referenced/belongs_to/eager_spec.rb +24 -5
- data/spec/mongoid/association/referenced/belongs_to_spec.rb +46 -3
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/eager_spec.rb +21 -2
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_persistence_spec.rb +56 -0
- data/spec/mongoid/association/referenced/has_and_belongs_to_many_models.rb +26 -0
- data/spec/mongoid/association/referenced/has_and_belongs_to_many_spec.rb +3 -3
- data/spec/mongoid/association/referenced/has_many/proxy_query_spec.rb +23 -0
- data/spec/mongoid/association/referenced/has_many_models.rb +37 -0
- data/spec/mongoid/association/referenced/has_many_spec.rb +3 -3
- data/spec/mongoid/association/referenced/has_one_models.rb +48 -0
- data/spec/mongoid/association/referenced/has_one_spec.rb +51 -4
- data/spec/mongoid/clients/factory_spec.rb +24 -18
- data/spec/mongoid/clients/options_spec.rb +40 -37
- data/spec/mongoid/clients_spec.rb +68 -8
- data/spec/mongoid/config_spec.rb +3 -1
- data/spec/mongoid/contextual/mongo_spec.rb +5 -2
- data/spec/mongoid/copyable_spec.rb +40 -6
- data/spec/mongoid/copyable_spec_models.rb +17 -0
- data/spec/mongoid/criteria/queryable/extensions/big_decimal_spec.rb +3 -3
- data/spec/mongoid/criteria/queryable/selectable_spec.rb +42 -3
- data/spec/mongoid/criteria/queryable/selector_spec.rb +2 -2
- data/spec/mongoid/criteria/scopable_spec.rb +81 -0
- data/spec/mongoid/criteria_spec.rb +18 -3
- data/spec/mongoid/document_spec.rb +81 -2
- data/spec/mongoid/extensions/big_decimal_spec.rb +9 -9
- data/spec/mongoid/extensions/regexp_spec.rb +23 -0
- data/spec/mongoid/fields_spec.rb +1 -1
- data/spec/mongoid/findable_spec.rb +1 -1
- data/spec/mongoid/matchable/eq_spec.rb +48 -0
- data/spec/mongoid/persistable/incrementable_spec.rb +6 -6
- data/spec/mongoid/persistence_context_spec.rb +1 -1
- data/spec/mongoid/query_cache_spec.rb +59 -6
- data/spec/mongoid/scopable_spec.rb +13 -0
- data/spec/mongoid/threaded_spec.rb +68 -0
- data/spec/rails/controller_extension/controller_runtime_spec.rb +110 -0
- data/spec/spec_helper.rb +35 -25
- data/spec/support/constraints.rb +101 -0
- data/spec/support/macros.rb +20 -0
- data/spec/support/spec_config.rb +39 -0
- metadata +471 -460
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -2
- metadata.gz.sig +0 -0
@@ -7,7 +7,7 @@ describe BigDecimal do
|
|
7
7
|
context "when provided a big decimal" do
|
8
8
|
|
9
9
|
let(:big_decimal) do
|
10
|
-
BigDecimal
|
10
|
+
BigDecimal("123456.789")
|
11
11
|
end
|
12
12
|
|
13
13
|
it "returns the decimal as a string" do
|
@@ -25,11 +25,11 @@ describe BigDecimal do
|
|
25
25
|
context "when provided an array of big decimals" do
|
26
26
|
|
27
27
|
let(:bd_one) do
|
28
|
-
BigDecimal
|
28
|
+
BigDecimal("123456.789")
|
29
29
|
end
|
30
30
|
|
31
31
|
let(:bd_two) do
|
32
|
-
BigDecimal
|
32
|
+
BigDecimal("123333.789")
|
33
33
|
end
|
34
34
|
|
35
35
|
let(:array) do
|
@@ -1073,6 +1073,26 @@ describe Mongoid::Criteria::Queryable::Selectable do
|
|
1073
1073
|
})
|
1074
1074
|
end
|
1075
1075
|
|
1076
|
+
context "when used with the $box operator ($geoWithin query) " do
|
1077
|
+
let(:selection) do
|
1078
|
+
query.geo_spacial(
|
1079
|
+
:location.within_box => [[ 1, 10 ], [ 2, 10 ]]
|
1080
|
+
)
|
1081
|
+
end
|
1082
|
+
|
1083
|
+
it "adds the $geoIntersects expression" do
|
1084
|
+
expect(selection.selector).to eq({
|
1085
|
+
"location" => {
|
1086
|
+
"$geoWithin" => {
|
1087
|
+
"$box" => [
|
1088
|
+
[ 1, 10 ], [ 2, 10 ]
|
1089
|
+
]
|
1090
|
+
}
|
1091
|
+
}
|
1092
|
+
})
|
1093
|
+
end
|
1094
|
+
end
|
1095
|
+
|
1076
1096
|
it_behaves_like "a cloning selection"
|
1077
1097
|
end
|
1078
1098
|
end
|
@@ -3369,7 +3389,7 @@ describe Mongoid::Criteria::Queryable::Selectable do
|
|
3369
3389
|
end
|
3370
3390
|
|
3371
3391
|
it "constructs a text search document" do
|
3372
|
-
expect(selection.selector).to eq({
|
3392
|
+
expect(selection.selector).to eq({ '$text' => { '$search' => "testing" }})
|
3373
3393
|
end
|
3374
3394
|
|
3375
3395
|
it "returns the cloned selectable" do
|
@@ -3383,16 +3403,35 @@ describe Mongoid::Criteria::Queryable::Selectable do
|
|
3383
3403
|
end
|
3384
3404
|
|
3385
3405
|
it "constructs a text search document" do
|
3386
|
-
expect(selection.selector[
|
3406
|
+
expect(selection.selector['$text']['$search']).to eq("essais")
|
3387
3407
|
end
|
3388
3408
|
|
3389
3409
|
it "add the options to the text search document" do
|
3390
|
-
expect(selection.selector[
|
3410
|
+
expect(selection.selector['$text'][:$language]).to eq("fr")
|
3391
3411
|
end
|
3392
3412
|
|
3393
3413
|
it_behaves_like "a cloning selection"
|
3394
3414
|
end
|
3395
3415
|
end
|
3416
|
+
|
3417
|
+
context 'when given more than once' do
|
3418
|
+
let(:selection) do
|
3419
|
+
query.text_search("one").text_search('two')
|
3420
|
+
end
|
3421
|
+
|
3422
|
+
# MongoDB server can only handle one text expression at a time,
|
3423
|
+
# per https://docs.mongodb.com/manual/reference/operator/query/text/.
|
3424
|
+
# Nonetheless we test that the query is built correctly when
|
3425
|
+
# a user supplies more than one text condition.
|
3426
|
+
it 'merges conditions' do
|
3427
|
+
expect(Mongoid.logger).to receive(:warn)
|
3428
|
+
expect(selection.selector).to eq('$and' => [
|
3429
|
+
{'$text' => {'$search' => 'one'}}
|
3430
|
+
],
|
3431
|
+
'$text' => {'$search' => 'two'},
|
3432
|
+
)
|
3433
|
+
end
|
3434
|
+
end
|
3396
3435
|
end
|
3397
3436
|
|
3398
3437
|
describe "#where" do
|
@@ -370,11 +370,11 @@ describe Mongoid::Criteria::Queryable::Selector do
|
|
370
370
|
context "when providing an array" do
|
371
371
|
|
372
372
|
let(:big_one) do
|
373
|
-
BigDecimal
|
373
|
+
BigDecimal("1.2321")
|
374
374
|
end
|
375
375
|
|
376
376
|
let(:big_two) do
|
377
|
-
BigDecimal
|
377
|
+
BigDecimal("4.2222")
|
378
378
|
end
|
379
379
|
|
380
380
|
let(:array) do
|
@@ -388,4 +388,85 @@ describe Mongoid::Criteria::Scopable do
|
|
388
388
|
end
|
389
389
|
end
|
390
390
|
end
|
391
|
+
|
392
|
+
describe 'scope and where' do
|
393
|
+
class ScopeWhereModel
|
394
|
+
include Mongoid::Document
|
395
|
+
|
396
|
+
scope :foo, -> { where(foo: true) }
|
397
|
+
end
|
398
|
+
|
399
|
+
shared_examples_for 'restricts to both' do
|
400
|
+
it 'restricts to both' do
|
401
|
+
expect(result.selector['foo']).to eq(true)
|
402
|
+
expect(result.selector['hello']).to eq('world')
|
403
|
+
end
|
404
|
+
end
|
405
|
+
|
406
|
+
context 'scope is given first' do
|
407
|
+
let(:result) { ScopeWhereModel.foo.where(hello: 'world') }
|
408
|
+
|
409
|
+
it_behaves_like 'restricts to both'
|
410
|
+
end
|
411
|
+
|
412
|
+
context 'where is given first' do
|
413
|
+
let(:result) { ScopeWhereModel.where(hello: 'world').foo }
|
414
|
+
|
415
|
+
it_behaves_like 'restricts to both'
|
416
|
+
end
|
417
|
+
end
|
418
|
+
|
419
|
+
describe 'scope with argument and where' do
|
420
|
+
class ArgumentScopeWhereModel
|
421
|
+
include Mongoid::Document
|
422
|
+
|
423
|
+
scope :my_text_search, ->(search) { where('$text' => {'$search' => search}) }
|
424
|
+
end
|
425
|
+
|
426
|
+
shared_examples_for 'restricts to both' do
|
427
|
+
it 'restricts to both' do
|
428
|
+
expect(result.selector['$text']).to eq({'$search' => 'bar'})
|
429
|
+
expect(result.selector['hello']).to eq('world')
|
430
|
+
end
|
431
|
+
end
|
432
|
+
|
433
|
+
context 'scope is given first' do
|
434
|
+
let(:result) { ArgumentScopeWhereModel.my_text_search('bar').where(hello: 'world') }
|
435
|
+
|
436
|
+
it_behaves_like 'restricts to both'
|
437
|
+
end
|
438
|
+
|
439
|
+
context 'where is given first' do
|
440
|
+
let(:result) { ArgumentScopeWhereModel.where(hello: 'world').my_text_search('bar') }
|
441
|
+
|
442
|
+
it_behaves_like 'restricts to both'
|
443
|
+
end
|
444
|
+
end
|
445
|
+
|
446
|
+
describe 'built in text search scope and where' do
|
447
|
+
class TextSearchScopeWhereModel
|
448
|
+
include Mongoid::Document
|
449
|
+
|
450
|
+
# using the default text_search scope
|
451
|
+
end
|
452
|
+
|
453
|
+
shared_examples_for 'restricts to both' do
|
454
|
+
it 'restricts to both' do
|
455
|
+
expect(result.selector['$text']).to eq({'$search' => 'bar'})
|
456
|
+
expect(result.selector['hello']).to eq('world')
|
457
|
+
end
|
458
|
+
end
|
459
|
+
|
460
|
+
context 'scope is given first' do
|
461
|
+
let(:result) { TextSearchScopeWhereModel.text_search('bar').where(hello: 'world') }
|
462
|
+
|
463
|
+
it_behaves_like 'restricts to both'
|
464
|
+
end
|
465
|
+
|
466
|
+
context 'where is given first' do
|
467
|
+
let(:result) { TextSearchScopeWhereModel.where(hello: 'world').text_search('bar') }
|
468
|
+
|
469
|
+
it_behaves_like 'restricts to both'
|
470
|
+
end
|
471
|
+
end
|
391
472
|
end
|
@@ -285,8 +285,23 @@ describe Mongoid::Criteria do
|
|
285
285
|
Band.where(name: "Depeche Mode")
|
286
286
|
end
|
287
287
|
|
288
|
-
|
289
|
-
|
288
|
+
# as_json changed in rails 6 to call as_json on serializable_hash.
|
289
|
+
# https://github.com/rails/rails/commit/2e5cb980a448e7f4ab00df6e9ad4c1cc456616aa
|
290
|
+
|
291
|
+
context 'rails < 6' do
|
292
|
+
max_rails_version '5.2'
|
293
|
+
|
294
|
+
it "returns the criteria as a json hash" do
|
295
|
+
expect(criteria.as_json).to eq([ band.serializable_hash ])
|
296
|
+
end
|
297
|
+
end
|
298
|
+
|
299
|
+
context 'rails >= 6' do
|
300
|
+
min_rails_version '6.0'
|
301
|
+
|
302
|
+
it "returns the criteria as a json hash" do
|
303
|
+
expect(criteria.as_json).to eq([ band.serializable_hash.as_json ])
|
304
|
+
end
|
290
305
|
end
|
291
306
|
end
|
292
307
|
|
@@ -3465,7 +3480,7 @@ describe Mongoid::Criteria do
|
|
3465
3480
|
context "when querying on a big decimal" do
|
3466
3481
|
|
3467
3482
|
let(:sales) do
|
3468
|
-
BigDecimal
|
3483
|
+
BigDecimal('0.1')
|
3469
3484
|
end
|
3470
3485
|
|
3471
3486
|
let!(:band) do
|
@@ -15,12 +15,12 @@ describe Mongoid::Document do
|
|
15
15
|
end
|
16
16
|
|
17
17
|
describe "#_destroy" do
|
18
|
-
|
18
|
+
|
19
19
|
it "default to false" do
|
20
20
|
expect(Person.new._destroy).to be false
|
21
21
|
end
|
22
22
|
end
|
23
|
-
|
23
|
+
|
24
24
|
describe ".included" do
|
25
25
|
|
26
26
|
let(:models) do
|
@@ -385,6 +385,27 @@ describe Mongoid::Document do
|
|
385
385
|
Person.new(title: "Sir")
|
386
386
|
end
|
387
387
|
|
388
|
+
describe 'id' do
|
389
|
+
context 'rails < 6' do
|
390
|
+
max_rails_version '5.2'
|
391
|
+
|
392
|
+
it 'is a BSON::ObjectId' do
|
393
|
+
id = person.as_json['_id']
|
394
|
+
expect(id).to be_a(BSON::ObjectId)
|
395
|
+
end
|
396
|
+
end
|
397
|
+
|
398
|
+
context 'rails >= 6' do
|
399
|
+
min_rails_version '6.0'
|
400
|
+
|
401
|
+
it 'is a hash with $oid' do
|
402
|
+
id = person.as_json['_id']
|
403
|
+
expect(id).to be_a(Hash)
|
404
|
+
expect(id['$oid']).to be_a(String)
|
405
|
+
end
|
406
|
+
end
|
407
|
+
end
|
408
|
+
|
388
409
|
context "when no options are provided" do
|
389
410
|
|
390
411
|
it "does not apply any options" do
|
@@ -433,6 +454,64 @@ describe Mongoid::Document do
|
|
433
454
|
end
|
434
455
|
end
|
435
456
|
end
|
457
|
+
|
458
|
+
context ':compact option' do
|
459
|
+
# Since rails 6 differs in how it treats id fields,
|
460
|
+
# run this test on one version of rails. Currently rails 6 is in beta,
|
461
|
+
# when it is released this version should be changed to 6.
|
462
|
+
max_rails_version '5.2'
|
463
|
+
|
464
|
+
before do
|
465
|
+
# These tests require a specific set of defined attributes
|
466
|
+
# on the model
|
467
|
+
expect(church.as_json.keys.sort).to eq(%w(_id location name))
|
468
|
+
end
|
469
|
+
|
470
|
+
context 'there is a nil valued attribute' do
|
471
|
+
let(:church) do
|
472
|
+
Church.create!(name: 'St. Basil')
|
473
|
+
end
|
474
|
+
|
475
|
+
it 'returns non-nil fields and _id only' do
|
476
|
+
actual = church.as_json(compact: true)
|
477
|
+
expect(actual).to eq('_id' => church.id, 'name' => 'St. Basil')
|
478
|
+
end
|
479
|
+
end
|
480
|
+
|
481
|
+
context 'all attrbutes are nil valued' do
|
482
|
+
let(:church) do
|
483
|
+
Church.create!
|
484
|
+
end
|
485
|
+
|
486
|
+
it 'returns a hash with _id only' do
|
487
|
+
actual = church.as_json(compact: true)
|
488
|
+
expect(actual).to eq('_id' => church.id)
|
489
|
+
end
|
490
|
+
end
|
491
|
+
|
492
|
+
context 'there are no nil valued attributes' do
|
493
|
+
let(:church) do
|
494
|
+
Church.create!(name: 'St. Basil', location: {})
|
495
|
+
end
|
496
|
+
|
497
|
+
it 'returns all fields and _id' do
|
498
|
+
actual = church.as_json(compact: true)
|
499
|
+
expect(actual).to eq('_id' => church.id, 'name' => 'St. Basil',
|
500
|
+
'location' => {})
|
501
|
+
end
|
502
|
+
end
|
503
|
+
|
504
|
+
context 'when option is specified as a truthy value' do
|
505
|
+
let(:church) do
|
506
|
+
Church.create!(name: 'St. Basil')
|
507
|
+
end
|
508
|
+
|
509
|
+
it 'returns non-nil fields and _id only' do
|
510
|
+
actual = church.as_json(compact: 1)
|
511
|
+
expect(actual).to eq('_id' => church.id, 'name' => 'St. Basil')
|
512
|
+
end
|
513
|
+
end
|
514
|
+
end
|
436
515
|
end
|
437
516
|
|
438
517
|
describe "#as_document" do
|
@@ -3,7 +3,7 @@ require "spec_helper"
|
|
3
3
|
describe Mongoid::Extensions::BigDecimal do
|
4
4
|
|
5
5
|
let(:big_decimal) do
|
6
|
-
BigDecimal
|
6
|
+
BigDecimal("123456.789")
|
7
7
|
end
|
8
8
|
|
9
9
|
describe ".demongoize" do
|
@@ -26,7 +26,7 @@ describe Mongoid::Extensions::BigDecimal do
|
|
26
26
|
end
|
27
27
|
|
28
28
|
it "returns a BigDecimal" do
|
29
|
-
expect(BigDecimal.demongoize(string)).to eq(BigDecimal
|
29
|
+
expect(BigDecimal.demongoize(string)).to eq(BigDecimal(string))
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
@@ -37,7 +37,7 @@ describe Mongoid::Extensions::BigDecimal do
|
|
37
37
|
end
|
38
38
|
|
39
39
|
it "returns a BigDecimal" do
|
40
|
-
expect(BigDecimal.demongoize(string)).to eq(BigDecimal
|
40
|
+
expect(BigDecimal.demongoize(string)).to eq(BigDecimal(string))
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
@@ -48,7 +48,7 @@ describe Mongoid::Extensions::BigDecimal do
|
|
48
48
|
end
|
49
49
|
|
50
50
|
it "returns a BigDecimal" do
|
51
|
-
expect(BigDecimal.demongoize(string)).to eq(BigDecimal
|
51
|
+
expect(BigDecimal.demongoize(string)).to eq(BigDecimal(string))
|
52
52
|
end
|
53
53
|
end
|
54
54
|
|
@@ -184,7 +184,7 @@ describe Mongoid::Extensions::BigDecimal do
|
|
184
184
|
context "when the value is the BigDecimal zero" do
|
185
185
|
|
186
186
|
let(:big_decimal) do
|
187
|
-
BigDecimal
|
187
|
+
BigDecimal("0.0")
|
188
188
|
end
|
189
189
|
|
190
190
|
it "returns a BigDecimal" do
|
@@ -195,7 +195,7 @@ describe Mongoid::Extensions::BigDecimal do
|
|
195
195
|
context "when the value is the BigDecimal negative zero" do
|
196
196
|
|
197
197
|
let(:big_decimal) do
|
198
|
-
BigDecimal
|
198
|
+
BigDecimal("-0.0")
|
199
199
|
end
|
200
200
|
|
201
201
|
it "returns a BigDecimal" do
|
@@ -326,7 +326,7 @@ describe Mongoid::Extensions::BigDecimal do
|
|
326
326
|
context "when the value is BigDecimal NaN" do
|
327
327
|
|
328
328
|
let(:nan) do
|
329
|
-
BigDecimal
|
329
|
+
BigDecimal("NaN")
|
330
330
|
end
|
331
331
|
|
332
332
|
it "returns a String" do
|
@@ -337,7 +337,7 @@ describe Mongoid::Extensions::BigDecimal do
|
|
337
337
|
context "when the value is BigDecimal Infinity" do
|
338
338
|
|
339
339
|
let(:infinity) do
|
340
|
-
BigDecimal
|
340
|
+
BigDecimal("Infinity")
|
341
341
|
end
|
342
342
|
|
343
343
|
it "returns a String" do
|
@@ -348,7 +348,7 @@ describe Mongoid::Extensions::BigDecimal do
|
|
348
348
|
context "when the value is BigDecimal negative Infinity" do
|
349
349
|
|
350
350
|
let(:neg_infinity) do
|
351
|
-
BigDecimal
|
351
|
+
BigDecimal("-Infinity")
|
352
352
|
end
|
353
353
|
|
354
354
|
it "returns a String" do
|
@@ -35,6 +35,29 @@ describe Mongoid::Extensions::Regexp do
|
|
35
35
|
it "returns the provided value as a regex" do
|
36
36
|
expect(value).to eq(/[^abc]/)
|
37
37
|
end
|
38
|
+
|
39
|
+
|
40
|
+
context "when the string is empty" do
|
41
|
+
|
42
|
+
let(:value) do
|
43
|
+
Regexp.mongoize("")
|
44
|
+
end
|
45
|
+
|
46
|
+
it "returns an empty regex" do
|
47
|
+
expect(value).to eq(//)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context "when the value is nil" do
|
53
|
+
|
54
|
+
let(:value) do
|
55
|
+
Regexp.mongoize(nil)
|
56
|
+
end
|
57
|
+
|
58
|
+
it "returns the nil" do
|
59
|
+
expect(value).to be_nil
|
60
|
+
end
|
38
61
|
end
|
39
62
|
end
|
40
63
|
|