Skip to content

Commit ad3c600

Browse files
committed
MONGOID-5222 remove InvalidValue from tests
1 parent 67e7d8b commit ad3c600

File tree

11 files changed

+88
-97
lines changed

11 files changed

+88
-97
lines changed

lib/mongoid/criteria/queryable/extensions/boolean.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,8 @@ module ClassMethods
2020
# @return [ true, false ] The boolean value.
2121
def evolve(object)
2222
__evolve__(object) do |obj|
23-
begin
24-
mongoize(object)
25-
rescue InvalidValue
26-
object
27-
end
23+
res = mongoize(object)
24+
res.nil? ? object : res
2825
end
2926
end
3027
end

spec/mongoid/attributes_spec.rb

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,16 +1583,14 @@
15831583
context "when attribute is a Hash" do
15841584
let(:person) { Person.new map: { somekey: "somevalue" } }
15851585

1586-
it "raises an error when trying to set a value of invalid type - array" do
1587-
expect do
1588-
person.map = []
1589-
end.to raise_error(Mongoid::Errors::InvalidValue, /The value \[\] cannot be written to a field of type Hash/)
1586+
it "writes nil when trying to set a value of invalid type - array" do
1587+
person.map = []
1588+
expect(person.map).to be_nil
15901589
end
15911590

1592-
it "raises an error when trying to set a value of invalid type - boolean" do
1593-
expect do
1594-
person.map = false
1595-
end.to raise_error(Mongoid::Errors::InvalidValue, /The value false cannot be written to a field of type Hash/)
1591+
it "writes nil when trying to set a value of invalid type - boolean" do
1592+
person.map = false
1593+
expect(person.map).to be_nil
15961594
end
15971595

15981596
it "can set a Hash value" do
@@ -1607,16 +1605,14 @@
16071605
expect(person.aliases).to eq([ :alias_1 ])
16081606
end
16091607

1610-
it "raises an error when trying to set a value of invalid type - hash" do
1611-
expect do
1612-
person.aliases = {}
1613-
end.to raise_error(Mongoid::Errors::InvalidValue, /The value {} cannot be written to a field of type Array/)
1608+
it "writes nil when trying to set a value of invalid type - hash" do
1609+
person.aliases = {}
1610+
expect(person.aliases).to be_nil
16141611
end
16151612

1616-
it "raises an error when trying to set a value of invalid type - boolean" do
1617-
expect do
1618-
person.aliases = false
1619-
end.to raise_error(Mongoid::Errors::InvalidValue, /The value false cannot be written to a field of type Array/)
1613+
it "writes nil when trying to set a value of invalid type - boolean" do
1614+
person.aliases = false
1615+
expect(person.aliases).to be_nil
16201616
end
16211617
end
16221618

spec/mongoid/extensions/big_decimal_spec.rb

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,8 @@
268268
"1a2"
269269
end
270270

271-
it "raises an error" do
272-
expect do
273-
mongoized
274-
end.to raise_error(Mongoid::Errors::InvalidValue)
271+
it "returns nil" do
272+
expect(mongoized).to be_nil
275273
end
276274
end
277275

@@ -292,10 +290,8 @@
292290
true
293291
end
294292

295-
it "raises an error" do
296-
expect do
297-
mongoized
298-
end.to raise_error(Mongoid::Errors::InvalidValue)
293+
it "returns nil" do
294+
expect(mongoized).to be_nil
299295
end
300296
end
301297

@@ -305,10 +301,8 @@
305301
false
306302
end
307303

308-
it "raises an error" do
309-
expect do
310-
mongoized
311-
end.to raise_error(Mongoid::Errors::InvalidValue)
304+
it "returns nil" do
305+
expect(mongoized).to be_nil
312306
end
313307
end
314308

@@ -712,10 +706,8 @@
712706
"1a2"
713707
end
714708

715-
it "returns a decimal128" do
716-
expect do
717-
mongoized
718-
end.to raise_error(Mongoid::Errors::InvalidValue)
709+
it "returns nil" do
710+
expect(mongoized).to be_nil
719711
end
720712
end
721713

@@ -736,10 +728,8 @@
736728
true
737729
end
738730

739-
it "raises an error" do
740-
expect do
741-
mongoized
742-
end.to raise_error(Mongoid::Errors::InvalidValue)
731+
it "returns nil" do
732+
expect(mongoized).to be_nil
743733
end
744734
end
745735

@@ -749,10 +739,8 @@
749739
false
750740
end
751741

752-
it "raises an error" do
753-
expect do
754-
mongoized
755-
end.to raise_error(Mongoid::Errors::InvalidValue)
742+
it "returns nil" do
743+
expect(mongoized).to be_nil
756744
end
757745
end
758746

spec/mongoid/extensions/binary_spec.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,8 @@
5656

5757
let(:value) { true }
5858

59-
it "raises an error" do
60-
expect do
61-
mongoized
62-
end.to raise_error(Mongoid::Errors::InvalidValue)
59+
it "returns nil" do
60+
expect(mongoized).to be_nil
6361
end
6462
end
6563
end

spec/mongoid/extensions/date_class_mongoize_spec.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,19 +102,19 @@
102102
Date.mongoize("")
103103
end
104104

105-
it "raises an error" do
106-
expect do
107-
evolved
108-
end.to raise_error(Mongoid::Errors::InvalidValue)
105+
it "returns nil" do
106+
expect(evolved).to be_nil
109107
end
110108
end
111109

112110
context "when the string is an invalid time" do
113111

114-
it "raises an error" do
115-
expect do
116-
Date.mongoize("time")
117-
end.to raise_error(Mongoid::Errors::InvalidValue)
112+
let(:evolved) do
113+
Date.mongoize("time")
114+
end
115+
116+
it "returns nil" do
117+
expect(evolved).to be_nil
118118
end
119119
end
120120
end

spec/mongoid/extensions/date_time_spec.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,12 @@
7979

8080
context "when the string is an invalid time" do
8181

82-
it "raises an error" do
83-
expect do
84-
DateTime.mongoize("time")
85-
end.to raise_error(Mongoid::Errors::InvalidValue)
82+
let(:mongoized) do
83+
DateTime.mongoize("time")
84+
end
85+
86+
it "returns nil" do
87+
expect(mongoized).to be_nil
8688
end
8789
end
8890
end

spec/mongoid/extensions/float_spec.rb

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,12 @@
6060

6161
context "when the value is not a float string" do
6262

63-
it "return 0" do
64-
expect do
65-
Float.demongoize('asdf')
66-
end.to raise_error(Mongoid::Errors::InvalidValue)
63+
let(:demongoized) do
64+
Float.demongoize('asdf')
65+
end
66+
67+
it "returns nil" do
68+
expect(demongoized).to be_nil
6769
end
6870
end
6971
end
@@ -126,10 +128,12 @@
126128

127129
context "when the string is non numerical" do
128130

129-
it "return 0" do
130-
expect do
131-
Float.mongoize("foo")
132-
end.to raise_error(Mongoid::Errors::InvalidValue)
131+
let(:mongoized) do
132+
Float.mongoize("foo")
133+
end
134+
135+
it "returns nil" do
136+
expect(mongoized).to be_nil
133137
end
134138
end
135139

spec/mongoid/extensions/integer_spec.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,12 @@
111111

112112
context "when the string is non numerical" do
113113

114-
it "returns 0" do
115-
expect do
116-
Integer.mongoize("foo")
117-
end.to raise_error(Mongoid::Errors::InvalidValue)
114+
let(:mongoized) do
115+
Integer.mongoize("foo")
116+
end
117+
118+
it "returns nil" do
119+
expect(mongoized).to be_nil
118120
end
119121
end
120122

spec/mongoid/extensions/range_spec.rb

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -312,10 +312,8 @@
312312
context 'given a String' do
313313
let(:range) { '3' }
314314

315-
it 'raises an error' do
316-
expect do
317-
subject
318-
end.to raise_error(Mongoid::Errors::InvalidValue)
315+
it "returns nil" do
316+
is_expected.to be_nil
319317
end
320318
end
321319

@@ -330,10 +328,8 @@
330328
context "given a hash with no correct fields" do
331329
let(:range) { { 'min^' => 1, 'max^' => 5, 'exclude_end^' => true} }
332330

333-
it "raises an error" do
334-
expect do
335-
subject
336-
end.to raise_error(Mongoid::Errors::InvalidValue)
331+
it "returns nil" do
332+
is_expected.to be_nil
337333
end
338334
end
339335

spec/mongoid/extensions/time_spec.rb

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,12 @@
163163

164164
context "when string is empty" do
165165

166-
it "raises an error" do
167-
expect do
168-
Time.mongoize("")
169-
end.to raise_error(Mongoid::Errors::InvalidValue)
166+
let(:mongoized) do
167+
Time.mongoize("")
168+
end
169+
170+
it "returns nil" do
171+
expect(mongoized).to be_nil
170172
end
171173
end
172174

@@ -191,10 +193,12 @@
191193

192194
context "when the string is an invalid time" do
193195

194-
it "raises an error" do
195-
expect do
196-
Time.mongoize("time")
197-
end.to raise_error(Mongoid::Errors::InvalidValue)
196+
let(:mongoized) do
197+
Time.mongoize("time")
198+
end
199+
200+
it "returns nil" do
201+
expect(mongoized).to be_nil
198202
end
199203
end
200204

0 commit comments

Comments
 (0)