mongoid 7.0.3 → 7.0.8
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
- checksums.yaml.gz.sig +2 -0
- data.tar.gz.sig +1 -0
- data/LICENSE +1 -0
- data/README.md +3 -2
- data/Rakefile +12 -0
- data/lib/mongoid.rb +2 -1
- data/lib/mongoid/association/embedded/embeds_many.rb +2 -1
- data/lib/mongoid/association/embedded/embeds_one.rb +2 -1
- data/lib/mongoid/association/proxy.rb +1 -1
- data/lib/mongoid/association/relatable.rb +23 -21
- data/lib/mongoid/atomic.rb +13 -3
- data/lib/mongoid/atomic/paths/embedded.rb +1 -1
- data/lib/mongoid/attributes.rb +28 -20
- data/lib/mongoid/attributes/dynamic.rb +15 -14
- data/lib/mongoid/config/environment.rb +21 -8
- data/lib/mongoid/copyable.rb +5 -1
- data/lib/mongoid/criteria.rb +7 -1
- data/lib/mongoid/criteria/modifiable.rb +13 -2
- data/lib/mongoid/criteria/queryable/extensions/numeric.rb +1 -1
- data/lib/mongoid/criteria/queryable/extensions/regexp.rb +3 -3
- data/lib/mongoid/criteria/queryable/key.rb +67 -8
- data/lib/mongoid/criteria/queryable/mergeable.rb +5 -4
- data/lib/mongoid/criteria/queryable/selectable.rb +3 -4
- data/lib/mongoid/criteria/queryable/selector.rb +9 -31
- data/lib/mongoid/extensions/hash.rb +4 -2
- data/lib/mongoid/extensions/regexp.rb +1 -1
- data/lib/mongoid/extensions/string.rb +5 -3
- data/lib/mongoid/fields.rb +2 -1
- data/lib/mongoid/matchable.rb +14 -15
- data/lib/mongoid/matchable/all.rb +4 -3
- data/lib/mongoid/matchable/default.rb +71 -24
- data/lib/mongoid/matchable/regexp.rb +2 -2
- data/lib/mongoid/persistable/pushable.rb +11 -2
- data/lib/mongoid/persistence_context.rb +6 -6
- data/lib/mongoid/positional.rb +1 -1
- data/lib/mongoid/query_cache.rb +3 -2
- data/lib/mongoid/validatable/macros.rb +1 -1
- data/lib/mongoid/validatable/uniqueness.rb +1 -1
- data/lib/mongoid/version.rb +2 -1
- data/lib/rails/generators/mongoid/model/templates/model.rb.tt +1 -1
- data/spec/README.md +18 -0
- data/spec/app/models/delegating_patient.rb +16 -0
- data/spec/app/models/other_owner_object.rb +2 -0
- data/spec/integration/app_spec.rb +192 -0
- data/spec/integration/associations/embedded_spec.rb +62 -0
- data/spec/integration/criteria/time_with_zone_spec.rb +32 -0
- data/spec/integration/document_spec.rb +22 -0
- data/spec/integration/matchable_spec.rb +680 -0
- data/spec/lite_spec_helper.rb +15 -5
- data/spec/mongoid/association/embedded/embedded_in_spec.rb +58 -0
- data/spec/mongoid/association/embedded/embeds_many_models.rb +53 -0
- data/spec/mongoid/association/embedded/embeds_many_spec.rb +10 -0
- data/spec/mongoid/association/embedded/embeds_one_dnl_models.rb +6 -0
- data/spec/mongoid/association/embedded/embeds_one_models.rb +51 -0
- data/spec/mongoid/association/embedded/embeds_one_spec.rb +46 -0
- data/spec/mongoid/association/referenced/belongs_to_spec.rb +23 -6
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +2 -1
- data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +2 -1
- data/spec/mongoid/association/referenced/has_one_spec.rb +12 -2
- data/spec/mongoid/attributes/dynamic_spec.rb +153 -0
- data/spec/mongoid/attributes_spec.rb +19 -7
- data/spec/mongoid/clients/factory_spec.rb +2 -2
- data/spec/mongoid/clients/options_spec.rb +4 -4
- data/spec/mongoid/clients/sessions_spec.rb +20 -7
- data/spec/mongoid/clients/transactions_spec.rb +36 -15
- data/spec/mongoid/clients_spec.rb +2 -2
- data/spec/mongoid/contextual/atomic_spec.rb +20 -10
- data/spec/mongoid/contextual/geo_near_spec.rb +1 -0
- data/spec/mongoid/contextual/map_reduce_spec.rb +20 -5
- data/spec/mongoid/contextual/mongo_spec.rb +76 -53
- data/spec/mongoid/criteria/modifiable_spec.rb +59 -10
- data/spec/mongoid/criteria/queryable/extensions/numeric_spec.rb +54 -0
- data/spec/mongoid/criteria/queryable/extensions/regexp_spec.rb +7 -7
- data/spec/mongoid/criteria/queryable/extensions/string_spec.rb +1 -1
- data/spec/mongoid/criteria/queryable/key_spec.rb +48 -6
- data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +762 -0
- data/spec/mongoid/criteria/queryable/selectable_spec.rb +5 -224
- data/spec/mongoid/criteria/queryable/selector_spec.rb +37 -0
- data/spec/mongoid/criteria_spec.rb +7 -2
- data/spec/mongoid/document_fields_spec.rb +88 -0
- data/spec/mongoid/document_persistence_context_spec.rb +33 -0
- data/spec/mongoid/extensions/string_spec.rb +35 -7
- data/spec/mongoid/indexable_spec.rb +6 -4
- data/spec/mongoid/matchable/default_spec.rb +10 -3
- data/spec/mongoid/matchable/regexp_spec.rb +2 -2
- data/spec/mongoid/matchable_spec.rb +2 -2
- data/spec/mongoid/persistable/pushable_spec.rb +55 -1
- data/spec/mongoid/query_cache_spec.rb +2 -1
- data/spec/mongoid/relations/proxy_spec.rb +1 -1
- data/spec/mongoid/scopable_spec.rb +2 -1
- data/spec/mongoid/tasks/database_rake_spec.rb +13 -13
- data/spec/mongoid/tasks/database_spec.rb +1 -1
- data/spec/mongoid/validatable/uniqueness_spec.rb +33 -6
- data/spec/spec_helper.rb +4 -37
- data/spec/support/child_process_helper.rb +76 -0
- data/spec/support/cluster_config.rb +158 -0
- data/spec/support/constraints.rb +29 -19
- data/spec/support/expectations.rb +17 -3
- data/spec/support/spec_config.rb +12 -4
- metadata +525 -464
- metadata.gz.sig +2 -0
|
@@ -168,7 +168,8 @@ describe Mongoid::Contextual::Mongo do
|
|
|
168
168
|
end
|
|
169
169
|
end
|
|
170
170
|
|
|
171
|
-
context 'when a collation is specified'
|
|
171
|
+
context 'when a collation is specified' do
|
|
172
|
+
min_server_version '3.4'
|
|
172
173
|
|
|
173
174
|
let(:context) do
|
|
174
175
|
described_class.new(criteria)
|
|
@@ -229,7 +230,8 @@ describe Mongoid::Contextual::Mongo do
|
|
|
229
230
|
expect(deleted).to eq(1)
|
|
230
231
|
end
|
|
231
232
|
|
|
232
|
-
context 'when the criteria has a collation'
|
|
233
|
+
context 'when the criteria has a collation' do
|
|
234
|
+
min_server_version '3.4'
|
|
233
235
|
|
|
234
236
|
let(:criteria) do
|
|
235
237
|
Band.where(name: "DEPECHE MODE").collation(locale: 'en_US', strength: 2)
|
|
@@ -333,7 +335,8 @@ describe Mongoid::Contextual::Mongo do
|
|
|
333
335
|
expect(destroyed).to eq(1)
|
|
334
336
|
end
|
|
335
337
|
|
|
336
|
-
context 'when the criteria has a collation'
|
|
338
|
+
context 'when the criteria has a collation' do
|
|
339
|
+
min_server_version '3.4'
|
|
337
340
|
|
|
338
341
|
let(:criteria) do
|
|
339
342
|
Band.where(name: "DEPECHE MODE").collation(locale: 'en_US', strength: 2)
|
|
@@ -455,7 +458,8 @@ describe Mongoid::Contextual::Mongo do
|
|
|
455
458
|
end
|
|
456
459
|
end
|
|
457
460
|
|
|
458
|
-
context 'when a collation is specified'
|
|
461
|
+
context 'when a collation is specified' do
|
|
462
|
+
min_server_version '3.4'
|
|
459
463
|
|
|
460
464
|
before do
|
|
461
465
|
Band.create(name: 'DEPECHE MODE')
|
|
@@ -493,7 +497,8 @@ describe Mongoid::Contextual::Mongo do
|
|
|
493
497
|
described_class.new(criteria)
|
|
494
498
|
end
|
|
495
499
|
|
|
496
|
-
context 'when the criteria has a collation'
|
|
500
|
+
context 'when the criteria has a collation' do
|
|
501
|
+
min_server_version '3.4'
|
|
497
502
|
|
|
498
503
|
let(:criteria) do
|
|
499
504
|
Band.where(name: "DEPECHE MODE").collation(locale: 'en_US', strength: 2)
|
|
@@ -850,7 +855,8 @@ describe Mongoid::Contextual::Mongo do
|
|
|
850
855
|
end
|
|
851
856
|
end
|
|
852
857
|
|
|
853
|
-
context 'when a collation is specified on the criteria'
|
|
858
|
+
context 'when a collation is specified on the criteria' do
|
|
859
|
+
min_server_version '3.4'
|
|
854
860
|
|
|
855
861
|
let(:criteria) do
|
|
856
862
|
Band.where(name: "DEPECHE MODE").collation(locale: 'en_US', strength: 2)
|
|
@@ -1003,7 +1009,8 @@ describe Mongoid::Contextual::Mongo do
|
|
|
1003
1009
|
end
|
|
1004
1010
|
end
|
|
1005
1011
|
|
|
1006
|
-
context 'when a collation is specified on the criteria'
|
|
1012
|
+
context 'when a collation is specified on the criteria' do
|
|
1013
|
+
min_server_version '3.4'
|
|
1007
1014
|
|
|
1008
1015
|
let(:criteria) do
|
|
1009
1016
|
Band.where(name: "DEPECHE MODE").collation(locale: 'en_US', strength: 2)
|
|
@@ -1077,7 +1084,8 @@ describe Mongoid::Contextual::Mongo do
|
|
|
1077
1084
|
}.to raise_error(Mongoid::Errors::DocumentNotFound)
|
|
1078
1085
|
end
|
|
1079
1086
|
|
|
1080
|
-
context 'when a collation is specified on the criteria'
|
|
1087
|
+
context 'when a collation is specified on the criteria' do
|
|
1088
|
+
min_server_version '3.4'
|
|
1081
1089
|
|
|
1082
1090
|
let(:criteria) do
|
|
1083
1091
|
Band.where(name: "DEPECHE MODE").collation(locale: 'en_US', strength: 2)
|
|
@@ -1149,7 +1157,8 @@ describe Mongoid::Contextual::Mongo do
|
|
|
1149
1157
|
expect(context.send(method)).to eq(depeche_mode)
|
|
1150
1158
|
end
|
|
1151
1159
|
|
|
1152
|
-
context 'when the criteria has a collation'
|
|
1160
|
+
context 'when the criteria has a collation' do
|
|
1161
|
+
min_server_version '3.4'
|
|
1153
1162
|
|
|
1154
1163
|
let(:criteria) do
|
|
1155
1164
|
Band.where(name: "DEPECHE MODE").collation(locale: 'en_US', strength: 2)
|
|
@@ -1553,6 +1562,10 @@ describe Mongoid::Contextual::Mongo do
|
|
|
1553
1562
|
}}
|
|
1554
1563
|
end
|
|
1555
1564
|
|
|
1565
|
+
let(:ordered_results) do
|
|
1566
|
+
results['results'].sort_by { |doc| doc['_id'] }
|
|
1567
|
+
end
|
|
1568
|
+
|
|
1556
1569
|
context "when no selection is provided" do
|
|
1557
1570
|
|
|
1558
1571
|
let(:criteria) do
|
|
@@ -1584,36 +1597,40 @@ describe Mongoid::Contextual::Mongo do
|
|
|
1584
1597
|
end
|
|
1585
1598
|
|
|
1586
1599
|
it "contains the entire raw results" do
|
|
1587
|
-
expect(
|
|
1600
|
+
expect(ordered_results).to eq([
|
|
1588
1601
|
{ "_id" => "Depeche Mode", "value" => { "likes" => 200 }},
|
|
1589
1602
|
{ "_id" => "Tool", "value" => { "likes" => 100 }}
|
|
1590
1603
|
])
|
|
1591
1604
|
end
|
|
1592
1605
|
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
end
|
|
1606
|
+
context 'when statistics are available' do
|
|
1607
|
+
max_server_version '4.2'
|
|
1596
1608
|
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
})
|
|
1601
|
-
end
|
|
1609
|
+
it "contains the execution time" do
|
|
1610
|
+
expect(results.time).to_not be_nil
|
|
1611
|
+
end
|
|
1602
1612
|
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1613
|
+
it "contains the count statistics" do
|
|
1614
|
+
expect(results["counts"]).to eq({
|
|
1615
|
+
"input" => 2, "emit" => 2, "reduce" => 0, "output" => 2
|
|
1616
|
+
})
|
|
1617
|
+
end
|
|
1606
1618
|
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1619
|
+
it "contains the input count" do
|
|
1620
|
+
expect(results.input).to eq(2)
|
|
1621
|
+
end
|
|
1610
1622
|
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1623
|
+
it "contains the emitted count" do
|
|
1624
|
+
expect(results.emitted).to eq(2)
|
|
1625
|
+
end
|
|
1614
1626
|
|
|
1615
|
-
|
|
1616
|
-
|
|
1627
|
+
it "contains the reduced count" do
|
|
1628
|
+
expect(results.reduced).to eq(0)
|
|
1629
|
+
end
|
|
1630
|
+
|
|
1631
|
+
it "contains the output count" do
|
|
1632
|
+
expect(results.output).to eq(2)
|
|
1633
|
+
end
|
|
1617
1634
|
end
|
|
1618
1635
|
end
|
|
1619
1636
|
|
|
@@ -1642,35 +1659,39 @@ describe Mongoid::Contextual::Mongo do
|
|
|
1642
1659
|
end
|
|
1643
1660
|
|
|
1644
1661
|
it "contains the entire raw results" do
|
|
1645
|
-
expect(
|
|
1662
|
+
expect(ordered_results).to eq([
|
|
1646
1663
|
{ "_id" => "Depeche Mode", "value" => { "likes" => 200 }}
|
|
1647
1664
|
])
|
|
1648
1665
|
end
|
|
1649
1666
|
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
end
|
|
1667
|
+
context 'when statistics are available' do
|
|
1668
|
+
max_server_version '4.2'
|
|
1653
1669
|
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
})
|
|
1658
|
-
end
|
|
1670
|
+
it "contains the execution time" do
|
|
1671
|
+
expect(results.time).to_not be_nil
|
|
1672
|
+
end
|
|
1659
1673
|
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1674
|
+
it "contains the count statistics" do
|
|
1675
|
+
expect(results["counts"]).to eq({
|
|
1676
|
+
"input" => 1, "emit" => 1, "reduce" => 0, "output" => 1
|
|
1677
|
+
})
|
|
1678
|
+
end
|
|
1663
1679
|
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1680
|
+
it "contains the input count" do
|
|
1681
|
+
expect(results.input).to eq(1)
|
|
1682
|
+
end
|
|
1667
1683
|
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1684
|
+
it "contains the emitted count" do
|
|
1685
|
+
expect(results.emitted).to eq(1)
|
|
1686
|
+
end
|
|
1671
1687
|
|
|
1672
|
-
|
|
1673
|
-
|
|
1688
|
+
it "contains the reduced count" do
|
|
1689
|
+
expect(results.reduced).to eq(0)
|
|
1690
|
+
end
|
|
1691
|
+
|
|
1692
|
+
it "contains the output count" do
|
|
1693
|
+
expect(results.output).to eq(1)
|
|
1694
|
+
end
|
|
1674
1695
|
end
|
|
1675
1696
|
end
|
|
1676
1697
|
|
|
@@ -1710,7 +1731,7 @@ describe Mongoid::Contextual::Mongo do
|
|
|
1710
1731
|
end
|
|
1711
1732
|
|
|
1712
1733
|
it "contains the entire raw results" do
|
|
1713
|
-
expect(
|
|
1734
|
+
expect(ordered_results).to eq([
|
|
1714
1735
|
{ "_id" => "Depeche Mode", "value" => { "likes" => 200 }},
|
|
1715
1736
|
{ "_id" => "Tool", "value" => { "likes" => 100 }}
|
|
1716
1737
|
])
|
|
@@ -2161,7 +2182,8 @@ describe Mongoid::Contextual::Mongo do
|
|
|
2161
2182
|
end
|
|
2162
2183
|
end
|
|
2163
2184
|
|
|
2164
|
-
context 'when provided array filters'
|
|
2185
|
+
context 'when provided array filters' do
|
|
2186
|
+
min_server_version '3.6'
|
|
2165
2187
|
|
|
2166
2188
|
before do
|
|
2167
2189
|
Band.delete_all
|
|
@@ -2333,7 +2355,8 @@ describe Mongoid::Contextual::Mongo do
|
|
|
2333
2355
|
end
|
|
2334
2356
|
end
|
|
2335
2357
|
|
|
2336
|
-
context 'when provided array filters'
|
|
2358
|
+
context 'when provided array filters' do
|
|
2359
|
+
min_server_version '3.6'
|
|
2337
2360
|
|
|
2338
2361
|
before do
|
|
2339
2362
|
Band.delete_all
|
|
@@ -2371,7 +2394,7 @@ describe Mongoid::Contextual::Mongo do
|
|
|
2371
2394
|
|
|
2372
2395
|
describe '#pipeline' do
|
|
2373
2396
|
|
|
2374
|
-
context 'when the criteria has a selector'
|
|
2397
|
+
context 'when the criteria has a selector' do
|
|
2375
2398
|
|
|
2376
2399
|
before do
|
|
2377
2400
|
Artist.index(name: "text")
|
|
@@ -1643,11 +1643,15 @@ describe Mongoid::Criteria::Modifiable do
|
|
|
1643
1643
|
{ 'username' => 'Turnip' }
|
|
1644
1644
|
end
|
|
1645
1645
|
|
|
1646
|
-
it '
|
|
1647
|
-
expect(Person.create_with(attrs).selector).to
|
|
1646
|
+
it 'does not modify the selector' do
|
|
1647
|
+
expect(Person.create_with(attrs).selector[:username]).to be_nil
|
|
1648
1648
|
end
|
|
1649
1649
|
|
|
1650
|
-
|
|
1650
|
+
it 'create_attrs is modified' do
|
|
1651
|
+
expect(Person.create_with(attrs).create_attrs).to eq(attrs)
|
|
1652
|
+
end
|
|
1653
|
+
|
|
1654
|
+
context 'when a create is chained' do
|
|
1651
1655
|
|
|
1652
1656
|
context 'when a write method is chained' do
|
|
1653
1657
|
|
|
@@ -1671,6 +1675,25 @@ describe Mongoid::Criteria::Modifiable do
|
|
|
1671
1675
|
expect(new_person.age).to eq(50)
|
|
1672
1676
|
end
|
|
1673
1677
|
|
|
1678
|
+
context 'when a matching document is already in the collection' do
|
|
1679
|
+
let(:query) do
|
|
1680
|
+
{ 'username' => 'foo', 'age' => 12 }
|
|
1681
|
+
end
|
|
1682
|
+
|
|
1683
|
+
let(:person) do
|
|
1684
|
+
Person.create!(query)
|
|
1685
|
+
end
|
|
1686
|
+
|
|
1687
|
+
let(:found_person) do
|
|
1688
|
+
Person.create_with(attrs).find_or_create_by(query)
|
|
1689
|
+
end
|
|
1690
|
+
|
|
1691
|
+
it 'finds the matching document' do
|
|
1692
|
+
person
|
|
1693
|
+
expect(found_person.id).to eq(person.id)
|
|
1694
|
+
end
|
|
1695
|
+
end
|
|
1696
|
+
|
|
1674
1697
|
context 'when the attributes are shared with the write method args' do
|
|
1675
1698
|
|
|
1676
1699
|
let(:query) do
|
|
@@ -1681,7 +1704,7 @@ describe Mongoid::Criteria::Modifiable do
|
|
|
1681
1704
|
Person.create_with(attrs).find_or_create_by(query)
|
|
1682
1705
|
end
|
|
1683
1706
|
|
|
1684
|
-
it 'gives the
|
|
1707
|
+
it 'gives the find method args precedence' do
|
|
1685
1708
|
expect(new_person.username).to eq('Beet')
|
|
1686
1709
|
expect(new_person.age).to eq(50)
|
|
1687
1710
|
end
|
|
@@ -1708,8 +1731,12 @@ describe Mongoid::Criteria::Modifiable do
|
|
|
1708
1731
|
{ 'username' => 'Beet', 'age' => 50 }
|
|
1709
1732
|
end
|
|
1710
1733
|
|
|
1734
|
+
it 'does not modify the selector' do
|
|
1735
|
+
expect(criteria.create_with(attrs).selector).to eq(criteria_selector)
|
|
1736
|
+
end
|
|
1737
|
+
|
|
1711
1738
|
it 'overwrites all the original attributes' do
|
|
1712
|
-
expect(criteria.create_with(attrs).
|
|
1739
|
+
expect(criteria.create_with(attrs).create_attrs).to eq(attrs)
|
|
1713
1740
|
end
|
|
1714
1741
|
end
|
|
1715
1742
|
end
|
|
@@ -1720,8 +1747,12 @@ describe Mongoid::Criteria::Modifiable do
|
|
|
1720
1747
|
{ 'username' => 'Beet' }
|
|
1721
1748
|
end
|
|
1722
1749
|
|
|
1750
|
+
it 'does not modify the selector' do
|
|
1751
|
+
expect(criteria.create_with(attrs).selector).to eq(criteria_selector)
|
|
1752
|
+
end
|
|
1753
|
+
|
|
1723
1754
|
it 'only overwrites the shared attributes' do
|
|
1724
|
-
expect(criteria.create_with(attrs).
|
|
1755
|
+
expect(criteria.create_with(attrs).create_attrs).to eq(attrs)
|
|
1725
1756
|
end
|
|
1726
1757
|
end
|
|
1727
1758
|
|
|
@@ -1730,12 +1761,11 @@ describe Mongoid::Criteria::Modifiable do
|
|
|
1730
1761
|
let(:attrs) do
|
|
1731
1762
|
{ 'username' => 'Turnip' }
|
|
1732
1763
|
end
|
|
1733
|
-
|
|
1734
1764
|
let(:query) do
|
|
1735
1765
|
{ 'username' => 'Beet', 'age' => 50 }
|
|
1736
1766
|
end
|
|
1737
1767
|
|
|
1738
|
-
context 'when a
|
|
1768
|
+
context 'when a create method is chained' do
|
|
1739
1769
|
|
|
1740
1770
|
it 'executes the method' do
|
|
1741
1771
|
expect(criteria.create_with(attrs).new.username).to eq('Turnip')
|
|
@@ -1749,9 +1779,28 @@ describe Mongoid::Criteria::Modifiable do
|
|
|
1749
1779
|
criteria.create_with(attrs).find_or_create_by(query)
|
|
1750
1780
|
end
|
|
1751
1781
|
|
|
1752
|
-
it '
|
|
1782
|
+
it 'gives the find method arg precedence' do
|
|
1753
1783
|
expect(new_person.username).to eq('Beet')
|
|
1754
|
-
expect(new_person.age).to
|
|
1784
|
+
expect(new_person.age).to be(50)
|
|
1785
|
+
end
|
|
1786
|
+
|
|
1787
|
+
context 'when a matching document is already in the collection' do
|
|
1788
|
+
let(:query) do
|
|
1789
|
+
{ 'username' => 'foo', 'age' => 12 }
|
|
1790
|
+
end
|
|
1791
|
+
|
|
1792
|
+
let(:person) do
|
|
1793
|
+
Person.create!(query)
|
|
1794
|
+
end
|
|
1795
|
+
|
|
1796
|
+
let(:found_person) do
|
|
1797
|
+
criteria.create_with(attrs).find_or_create_by(query)
|
|
1798
|
+
end
|
|
1799
|
+
|
|
1800
|
+
it 'finds the matching document' do
|
|
1801
|
+
person
|
|
1802
|
+
expect(found_person.id).to eq(person.id)
|
|
1803
|
+
end
|
|
1755
1804
|
end
|
|
1756
1805
|
end
|
|
1757
1806
|
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
# encoding: utf-8
|
|
3
|
+
|
|
4
|
+
require "spec_helper"
|
|
5
|
+
|
|
6
|
+
describe Mongoid::Criteria::Queryable::Extensions::Numeric::ClassMethods do
|
|
7
|
+
|
|
8
|
+
let(:host) do
|
|
9
|
+
Class.new do
|
|
10
|
+
include Mongoid::Criteria::Queryable::Extensions::Numeric::ClassMethods
|
|
11
|
+
end.new
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
describe "#__numeric__" do
|
|
15
|
+
|
|
16
|
+
let(:actual) { host.__numeric__(str) }
|
|
17
|
+
|
|
18
|
+
context "when the string is a whole number" do
|
|
19
|
+
|
|
20
|
+
let(:str) { '123' }
|
|
21
|
+
|
|
22
|
+
it "returns the value as integer" do
|
|
23
|
+
expect(actual).to eq(123)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
context "when the string is a floating point number" do
|
|
28
|
+
|
|
29
|
+
let(:str) { '123.45' }
|
|
30
|
+
|
|
31
|
+
it "returns the value as a float" do
|
|
32
|
+
expect(actual).to eq(123.45)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
context "when the string is a dot only" do
|
|
37
|
+
|
|
38
|
+
let(:str) { '.' }
|
|
39
|
+
|
|
40
|
+
it "returns zero" do
|
|
41
|
+
expect(actual).to eq(0)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
context "when the string is a number with fractional part consisting of zeros" do
|
|
46
|
+
|
|
47
|
+
let(:str) { '12.000' }
|
|
48
|
+
|
|
49
|
+
it "returns the value as integer" do
|
|
50
|
+
expect(actual).to eq(12)
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
@@ -8,7 +8,7 @@ describe Mongoid::Criteria::Queryable::Extensions::Regexp do
|
|
|
8
8
|
context "when provided a regexp" do
|
|
9
9
|
|
|
10
10
|
let(:regexp) do
|
|
11
|
-
|
|
11
|
+
/\A[123]/
|
|
12
12
|
end
|
|
13
13
|
|
|
14
14
|
let(:evolved) do
|
|
@@ -23,7 +23,7 @@ describe Mongoid::Criteria::Queryable::Extensions::Regexp do
|
|
|
23
23
|
context "when provided a string" do
|
|
24
24
|
|
|
25
25
|
let(:regexp) do
|
|
26
|
-
"
|
|
26
|
+
"\\A[123]"
|
|
27
27
|
end
|
|
28
28
|
|
|
29
29
|
let(:evolved) do
|
|
@@ -31,7 +31,7 @@ describe Mongoid::Criteria::Queryable::Extensions::Regexp do
|
|
|
31
31
|
end
|
|
32
32
|
|
|
33
33
|
it "returns the converted regexp" do
|
|
34
|
-
expect(evolved).to eq(
|
|
34
|
+
expect(evolved).to eq(/\A[123]/)
|
|
35
35
|
end
|
|
36
36
|
end
|
|
37
37
|
|
|
@@ -40,7 +40,7 @@ describe Mongoid::Criteria::Queryable::Extensions::Regexp do
|
|
|
40
40
|
context "when the elements are regexps" do
|
|
41
41
|
|
|
42
42
|
let(:regexp) do
|
|
43
|
-
|
|
43
|
+
/\A[123]/
|
|
44
44
|
end
|
|
45
45
|
|
|
46
46
|
let(:array) do
|
|
@@ -63,7 +63,7 @@ describe Mongoid::Criteria::Queryable::Extensions::Regexp do
|
|
|
63
63
|
context "when the elements are strings" do
|
|
64
64
|
|
|
65
65
|
let(:regexp) do
|
|
66
|
-
"
|
|
66
|
+
"\\A[123]"
|
|
67
67
|
end
|
|
68
68
|
|
|
69
69
|
let(:evolved) do
|
|
@@ -71,7 +71,7 @@ describe Mongoid::Criteria::Queryable::Extensions::Regexp do
|
|
|
71
71
|
end
|
|
72
72
|
|
|
73
73
|
it "returns the regexps" do
|
|
74
|
-
expect(evolved).to eq([
|
|
74
|
+
expect(evolved).to eq([ /\A[123]/ ])
|
|
75
75
|
end
|
|
76
76
|
end
|
|
77
77
|
end
|
|
@@ -80,7 +80,7 @@ describe Mongoid::Criteria::Queryable::Extensions::Regexp do
|
|
|
80
80
|
describe "#regexp?" do
|
|
81
81
|
|
|
82
82
|
let(:regexp) do
|
|
83
|
-
|
|
83
|
+
/\A[123]/
|
|
84
84
|
end
|
|
85
85
|
|
|
86
86
|
it "returns true" do
|