mongoid 8.1.1 → 8.1.3

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.
@@ -158,6 +158,16 @@ describe Mongoid::Contextual::Mongo do
158
158
  end
159
159
  end
160
160
  end
161
+
162
+ context 'when for_js is present' do
163
+ let(:context) do
164
+ Band.for_js('this.name == "Depeche Mode"')
165
+ end
166
+
167
+ it 'counts the expected records' do
168
+ expect(context.count).to eq(1)
169
+ end
170
+ end
161
171
  end
162
172
 
163
173
  describe "#estimated_count" do
@@ -1184,33 +1194,49 @@ describe Mongoid::Contextual::Mongo do
1184
1194
  let!(:person2) { Person.create!(ssn: BSON::Decimal128.new("1")) }
1185
1195
  let(:tally) { Person.tally("ssn") }
1186
1196
 
1197
+ let(:tallied_classes) do
1198
+ tally.keys.map(&:class).sort do |a, b|
1199
+ a.to_s.casecmp(b.to_s)
1200
+ end
1201
+ end
1202
+
1187
1203
  context "< BSON 5" do
1188
1204
  max_bson_version '4.99.99'
1189
1205
 
1190
1206
  it "stores the correct types in the database" do
1191
- Person.find(person1.id).attributes["ssn"].should be_a BSON::Regexp::Raw
1192
- Person.find(person2.id).attributes["ssn"].should be_a BSON::Decimal128
1207
+ expect(Person.find(person1.id).attributes["ssn"]).to be_a BSON::Regexp::Raw
1208
+ expect(Person.find(person2.id).attributes["ssn"]).to be_a BSON::Decimal128
1209
+ end
1210
+
1211
+ it "tallies the correct type" do
1212
+ expect(tallied_classes).to be == [ BSON::Decimal128, BSON::Regexp::Raw ]
1213
+ end
1214
+ end
1215
+
1216
+ context '>= BSON 5' do
1217
+ min_bson_version "5.0"
1218
+
1219
+ it "stores the correct types in the database" do
1220
+ expect(Person.find(person1.id).ssn).to be_a BSON::Regexp::Raw
1221
+ expect(Person.find(person2.id).ssn).to be_a BigDecimal
1193
1222
  end
1194
1223
 
1195
1224
  it "tallies the correct type" do
1196
- tally.keys.map(&:class).sort do |a,b|
1197
- a.to_s <=> b.to_s
1198
- end.should == [BSON::Decimal128, BSON::Regexp::Raw]
1225
+ expect(tallied_classes).to be == [ BigDecimal, BSON::Regexp::Raw ]
1199
1226
  end
1200
1227
  end
1201
1228
 
1202
- context ">= BSON 5" do
1229
+ context '>= BSON 5 with decimal128 allowed' do
1203
1230
  min_bson_version "5.0"
1231
+ config_override :allow_bson5_decimal128, true
1204
1232
 
1205
1233
  it "stores the correct types in the database" do
1206
- Person.find(person1.id).ssn.should be_a BSON::Regexp::Raw
1207
- Person.find(person2.id).ssn.should be_a BigDeimal
1234
+ expect(Person.find(person1.id).ssn).to be_a BSON::Regexp::Raw
1235
+ expect(Person.find(person2.id).ssn).to be_a BSON::Decimal128
1208
1236
  end
1209
1237
 
1210
1238
  it "tallies the correct type" do
1211
- tally.keys.map(&:class).sort do |a,b|
1212
- a.to_s <=> b.to_s
1213
- end.should == [BigDecimal, BSON::Regexp::Raw]
1239
+ expect(tallied_classes).to be == [ BSON::Decimal128, BSON::Regexp::Raw ]
1214
1240
  end
1215
1241
  end
1216
1242
  end
@@ -3674,16 +3700,65 @@ describe Mongoid::Contextual::Mongo do
3674
3700
 
3675
3701
  context "when the attributes are in the correct type" do
3676
3702
 
3703
+ context "when operation is $set" do
3704
+
3705
+ before do
3706
+ context.update_all("$set" => { name: "Smiths" })
3707
+ end
3708
+
3709
+ it "updates the first matching document" do
3710
+ expect(depeche_mode.reload.name).to eq("Smiths")
3711
+ end
3712
+
3713
+ it "updates the last matching document" do
3714
+ expect(new_order.reload.name).to eq("Smiths")
3715
+ end
3716
+ end
3717
+
3718
+ context "when operation is $push" do
3719
+
3720
+ before do
3721
+ depeche_mode.update_attribute(:genres, ["electronic"])
3722
+ new_order.update_attribute(:genres, ["electronic"])
3723
+ context.update_all("$push" => { genres: "pop" })
3724
+ end
3725
+
3726
+ it "updates the first matching document" do
3727
+ expect(depeche_mode.reload.genres).to eq(["electronic", "pop"])
3728
+ end
3729
+
3730
+ it "updates the last matching document" do
3731
+ expect(new_order.reload.genres).to eq(["electronic", "pop"])
3732
+ end
3733
+ end
3734
+
3735
+ context "when operation is $addToSet" do
3736
+
3737
+ before do
3738
+ context.update_all("$addToSet" => { genres: "electronic" })
3739
+ end
3740
+
3741
+ it "updates the first matching document" do
3742
+ expect(depeche_mode.reload.genres).to eq(["electronic"])
3743
+ end
3744
+
3745
+ it "updates the last matching document" do
3746
+ expect(new_order.reload.genres).to eq(["electronic"])
3747
+ end
3748
+ end
3749
+ end
3750
+
3751
+ context 'when using aliased field names' do
3677
3752
  before do
3678
- context.update_all("$set" => { name: "Smiths" })
3753
+ context.update_all('$set' => { years: 100 })
3679
3754
  end
3680
3755
 
3681
3756
  it "updates the first matching document" do
3682
- expect(depeche_mode.reload.name).to eq("Smiths")
3757
+ expect(depeche_mode.reload.years).to eq(100)
3683
3758
  end
3684
3759
 
3685
3760
  it "updates the last matching document" do
3686
- expect(new_order.reload.name).to eq("Smiths")
3761
+ expect(new_order.reload.years).to eq(100)
3687
3762
  end
3688
3763
  end
3689
3764
 
@@ -44,7 +44,7 @@ describe Mongoid::Criteria::Queryable::Selector do
44
44
  end
45
45
  end
46
46
 
47
- context "when selector contains a $nin" do
47
+ context "when selector contains a $nin string" do
48
48
 
49
49
  let(:initial) do
50
50
  { "$nin" => ["foo"] }
@@ -72,7 +72,35 @@ describe Mongoid::Criteria::Queryable::Selector do
72
72
  end
73
73
  end
74
74
 
75
- context "when selector contains a $in" do
75
+ context "when selector contains a $nin symbol" do
76
+
77
+ let(:initial) do
78
+ { :$nin => ["foo"] }
79
+ end
80
+
81
+ before do
82
+ selector["field"] = initial
83
+ end
84
+
85
+ context "when merging in a new $nin" do
86
+
87
+ let(:other) do
88
+ { "field" => { :$nin => ["bar"] } }
89
+ end
90
+
91
+ before do
92
+ selector.merge!(other)
93
+ end
94
+
95
+ it "combines the two $nin queries into one" do
96
+ expect(selector).to eq({
97
+ "field" => { :$nin => ["foo", "bar"] }
98
+ })
99
+ end
100
+ end
101
+ end
102
+
103
+ context "when selector contains a $in string" do
76
104
 
77
105
  let(:initial) do
78
106
  { "$in" => [1, 2] }
@@ -117,6 +145,51 @@ describe Mongoid::Criteria::Queryable::Selector do
117
145
  end
118
146
  end
119
147
 
148
+ context "when selector contains a $in symbol" do
149
+
150
+ let(:initial) do
151
+ { :$in => [1, 2] }
152
+ end
153
+
154
+ before do
155
+ selector["field"] = initial
156
+ end
157
+
158
+ context "when merging in a new $in with an intersecting value" do
159
+
160
+ let(:other) do
161
+ { "field" => { :$in => [1] } }
162
+ end
163
+
164
+ before do
165
+ selector.merge!(other)
166
+ end
167
+
168
+ it "intersects the $in values" do
169
+ expect(selector).to eq({
170
+ "field" => { :$in => [1] }
171
+ })
172
+ end
173
+ end
174
+
175
+ context "when merging in a new $in with no intersecting values" do
176
+
177
+ let(:other) do
178
+ { "field" => { :$in => [3] } }
179
+ end
180
+
181
+ before do
182
+ selector.merge!(other)
183
+ end
184
+
185
+ it "intersects the $in values" do
186
+ expect(selector).to eq({
187
+ "field" => { :$in => [] }
188
+ })
189
+ end
190
+ end
191
+ end
192
+
120
193
  context "when selector is not nested" do
121
194
 
122
195
  before do
@@ -210,7 +210,79 @@ describe Mongoid::Criteria::Queryable::Storable do
210
210
  }
211
211
  end
212
212
  end
213
+
214
+ context 'when value is a hash combine values with different operator keys' do
215
+ let(:base) do
216
+ query.add_field_expression('foo', {'$in' => ['bar']})
217
+ end
218
+
219
+ let(:modified) do
220
+ base.add_field_expression('foo', {'$nin' => ['zoom']})
221
+ end
222
+
223
+ it 'combines the conditions using $and' do
224
+ modified.selector.should == {
225
+ 'foo' => {
226
+ '$in' => ['bar'],
227
+ '$nin' => ['zoom']
228
+ }
229
+ }
230
+ end
231
+ end
232
+
233
+ context 'when value is a hash with symbol operator key combine values with different operator keys' do
234
+ let(:base) do
235
+ query.add_field_expression('foo', {:$in => ['bar']})
236
+ end
237
+
238
+ let(:modified) do
239
+ base.add_field_expression('foo', {:$nin => ['zoom']})
240
+ end
241
+
242
+ it 'combines the conditions using $and' do
243
+ modified.selector.should == {
244
+ 'foo' => {
245
+ :$in => ['bar'],
246
+ :$nin => ['zoom']
247
+ }
248
+ }
249
+ end
250
+ end
251
+
252
+ context 'when value is a hash add values with same operator keys using $and' do
253
+ let(:base) do
254
+ query.add_field_expression('foo', {'$in' => ['bar']})
255
+ end
256
+
257
+ let(:modified) do
258
+ base.add_field_expression('foo', {'$in' => ['zoom']})
259
+ end
260
+
261
+ it 'adds the new condition using $and' do
262
+ modified.selector.should == {
263
+ 'foo' => {'$in' => ['bar']},
264
+ '$and' => ['foo' => {'$in' => ['zoom']}]
265
+ }
266
+ end
267
+ end
268
+
269
+ context 'when value is a hash with symbol operator key add values with same operator keys using $and' do
270
+ let(:base) do
271
+ query.add_field_expression('foo', {:$in => ['bar']})
272
+ end
273
+
274
+ let(:modified) do
275
+ base.add_field_expression('foo', {:$in => ['zoom']})
276
+ end
277
+
278
+ it 'adds the new condition using $and' do
279
+ modified.selector.should == {
280
+ 'foo' => {:$in => ['bar']},
281
+ '$and' => ['foo' => {:$in => ['zoom']}]
282
+ }
283
+ end
213
284
  end
285
+ end
214
286
 
215
287
  describe '#add_operator_expression' do
216
288
  let(:query_method) { :add_operator_expression }
@@ -178,7 +178,7 @@ describe Mongoid::Extensions::Hash do
178
178
 
179
179
  it "moves the non hash values under the provided key" do
180
180
  expect(consolidated).to eq({
181
- "$set" => { name: "Tool", likes: 10 }, "$inc" => { plays: 1 }
181
+ "$set" => { 'name' => "Tool", likes: 10 }, "$inc" => { 'plays' => 1 }
182
182
  })
183
183
  end
184
184
  end
@@ -195,7 +195,7 @@ describe Mongoid::Extensions::Hash do
195
195
 
196
196
  it "moves the non hash values under the provided key" do
197
197
  expect(consolidated).to eq({
198
- "$set" => { likes: 10, name: "Tool" }, "$inc" => { plays: 1 }
198
+ "$set" => { likes: 10, 'name' => "Tool" }, "$inc" => { 'plays' => 1 }
199
199
  })
200
200
  end
201
201
  end
@@ -213,7 +213,7 @@ describe Mongoid::Extensions::Hash do
213
213
 
214
214
  it "moves the non hash values under the provided key" do
215
215
  expect(consolidated).to eq({
216
- "$set" => { likes: 10, name: "Tool" }, "$inc" => { plays: 1 }
216
+ "$set" => { likes: 10, name: "Tool" }, "$inc" => { 'plays' => 1 }
217
217
  })
218
218
  end
219
219
  end
@@ -559,6 +559,49 @@ describe Mongoid::Fields do
559
559
  end
560
560
  end
561
561
  end
562
+
563
+ context 'when the field is declared as BSON::Decimal128' do
564
+ let(:document) { Mop.create!(decimal128_field: BSON::Decimal128.new(Math::PI.to_s)).reload }
565
+
566
+ shared_context 'BSON::Decimal128 is BigDecimal' do
567
+ it 'should return a BigDecimal' do
568
+ expect(document.decimal128_field).to be_a BigDecimal
569
+ end
570
+ end
571
+
572
+ shared_context 'BSON::Decimal128 is BSON::Decimal128' do
573
+ it 'should return a BSON::Decimal128' do
574
+ expect(document.decimal128_field).to be_a BSON::Decimal128
575
+ end
576
+ end
577
+
578
+ it 'is declared as BSON::Decimal128' do
579
+ expect(Mop.fields['decimal128_field'].type).to be == BSON::Decimal128
580
+ end
581
+
582
+ context 'when BSON version <= 4' do
583
+ max_bson_version '4.99.99'
584
+ it_behaves_like 'BSON::Decimal128 is BSON::Decimal128'
585
+ end
586
+
587
+ context 'when BSON version >= 5' do
588
+ min_bson_version '5.0.0'
589
+
590
+ context 'when allow_bson5_decimal128 is false' do
591
+ config_override :allow_bson5_decimal128, false
592
+ it_behaves_like 'BSON::Decimal128 is BigDecimal'
593
+ end
594
+
595
+ context 'when allow_bson5_decimal128 is true' do
596
+ config_override :allow_bson5_decimal128, true
597
+ it_behaves_like 'BSON::Decimal128 is BSON::Decimal128'
598
+ end
599
+
600
+ context 'when allow_bson5_decimal128 is default' do
601
+ it_behaves_like 'BSON::Decimal128 is BigDecimal'
602
+ end
603
+ end
604
+ end
562
605
  end
563
606
 
564
607
  describe "#getter_before_type_cast" do