Skip to content

Commit a89b714

Browse files
MONGOID-5559 Fix bigdecimal evolution to time (#27)
1 parent 0862d1d commit a89b714

File tree

6 files changed

+248
-14
lines changed

6 files changed

+248
-14
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ Refer to the [Roadmap issue](https://github.com/tablecheck/mongoid-ultra/issues/
7979
-[MONGOID-5556](https://jira.mongodb.org/browse/MONGOID-5556) - Add `Criteria#tally` `:unwind` arg to splat array results.
8080
- More to come soon!
8181

82+
#### Bug Fixes
83+
84+
- 🐞 [MONGOID-5559](https://jira.mongodb.org/browse/MONGOID-5559) - `BigDecimal` should correctly type-cast to `Time`.
85+
8286
#### Best Practices
8387

8488
-[MONGOID-5572](https://jira.mongodb.org/browse/MONGOID-5572) - RSpec: Use expectation syntax, enforced with RSpec config setting.

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,5 @@ def evolve(object)
7171

7272
::Float.__send__(:include, Mongoid::Criteria::Queryable::Extensions::Numeric)
7373
::Float.__send__(:extend, Mongoid::Criteria::Queryable::Extensions::Numeric::ClassMethods)
74+
75+
::BigDecimal.__send__(:include, Mongoid::Criteria::Queryable::Extensions::Numeric)

spec/integration/criteria/raw_value_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,8 @@
311311
expect(Band.where(founded: Mongoid::RawValue(BigDecimal('1577923200'))).to_a).to eq [band7]
312312
end
313313

314-
it 'matches objects without raw value because BigDecimal cannot be evolved to Date' do
315-
expect(Band.where(founded: BigDecimal('1577923200')).to_a).to eq [band7]
314+
it 'matches objects without raw value' do
315+
expect(Band.where(founded: BigDecimal('1577923200')).to_a).to eq [band3, band4]
316316
end
317317
end
318318

@@ -321,8 +321,8 @@
321321
expect(Band.where(updated: Mongoid::RawValue(BigDecimal('1578153600'))).to_a).to eq [band7]
322322
end
323323

324-
it 'matches objects without raw value because BigDecimal cannot be evolved to Time' do
325-
expect(Band.where(updated: BigDecimal('1578153600')).to_a).to eq [band7]
324+
it 'matches objects without raw value' do
325+
expect(Band.where(updated: BigDecimal('1578153600')).to_a).to eq [band4, band5]
326326
end
327327
end
328328
end

spec/mongoid/criteria/queryable/extensions/big_decimal_spec.rb

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,80 @@
166166
end
167167
end
168168
end
169+
170+
describe "#__evolve_time__" do
171+
172+
context 'UTC time zone' do
173+
let(:time) do
174+
Time.parse("2022-01-01 16:15:01 UTC")
175+
end
176+
177+
let(:evolved) do
178+
time.to_i.to_d.__evolve_time__
179+
end
180+
181+
it 'evolves the correct time' do
182+
expect(evolved).to eq(time)
183+
end
184+
end
185+
186+
context 'other time zone' do
187+
let(:time) do
188+
Time.parse("2022-01-01 16:15:01 +0900")
189+
end
190+
191+
let(:evolved) do
192+
time.to_i.to_d.__evolve_time__
193+
end
194+
195+
it 'evolves the correct time' do
196+
expect(evolved).to eq(time)
197+
end
198+
end
199+
end
200+
201+
describe "#__evolve_date__" do
202+
203+
context 'exact match' do
204+
let(:date) do
205+
Date.parse("2022-01-01")
206+
end
207+
208+
let(:evolved) do
209+
Time.parse("2022-01-01 0:00 UTC").to_i.to_d.__evolve_date__
210+
end
211+
212+
it 'evolves the correct time' do
213+
expect(evolved).to eq(date)
214+
end
215+
end
216+
217+
context 'one second earlier' do
218+
let(:date) do
219+
Date.parse("2021-12-31")
220+
end
221+
222+
let(:evolved) do
223+
(Time.parse("2022-01-01 0:00 UTC").to_i.to_d - 1).__evolve_date__
224+
end
225+
226+
it 'evolves the correct time' do
227+
expect(evolved).to eq(date)
228+
end
229+
end
230+
231+
context 'one second later' do
232+
let(:date) do
233+
Date.parse("2022-01-01")
234+
end
235+
236+
let(:evolved) do
237+
(Time.parse("2022-01-01 0:00 UTC").to_i.to_d + 1).__evolve_date__
238+
end
239+
240+
it 'evolves the correct time' do
241+
expect(evolved).to eq(date)
242+
end
243+
end
244+
end
169245
end

spec/mongoid/criteria/queryable/extensions/float_spec.rb

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,80 @@
6464
end
6565
end
6666
end
67+
68+
describe "#__evolve_time__" do
69+
70+
context 'UTC time zone' do
71+
let(:time) do
72+
Time.parse("2022-01-01 16:15:01 UTC")
73+
end
74+
75+
let(:evolved) do
76+
time.to_f.__evolve_time__
77+
end
78+
79+
it 'evolves the correct time' do
80+
expect(evolved).to eq(time)
81+
end
82+
end
83+
84+
context 'other time zone' do
85+
let(:time) do
86+
Time.parse("2022-01-01 16:15:01 +0900")
87+
end
88+
89+
let(:evolved) do
90+
time.to_f.__evolve_time__
91+
end
92+
93+
it 'evolves the correct time' do
94+
expect(evolved).to eq(time)
95+
end
96+
end
97+
end
98+
99+
describe "#__evolve_date__" do
100+
101+
context 'exact match' do
102+
let(:date) do
103+
Date.parse("2022-01-01")
104+
end
105+
106+
let(:evolved) do
107+
Time.parse("2022-01-01 0:00 UTC").to_f.__evolve_date__
108+
end
109+
110+
it 'evolves the correct time' do
111+
expect(evolved).to eq(date)
112+
end
113+
end
114+
115+
context 'one second earlier' do
116+
let(:date) do
117+
Date.parse("2021-12-31")
118+
end
119+
120+
let(:evolved) do
121+
(Time.parse("2022-01-01 0:00 UTC").to_f - 1).__evolve_date__
122+
end
123+
124+
it 'evolves the correct time' do
125+
expect(evolved).to eq(date)
126+
end
127+
end
128+
129+
context 'one second later' do
130+
let(:date) do
131+
Date.parse("2022-01-01")
132+
end
133+
134+
let(:evolved) do
135+
(Time.parse("2022-01-01 0:00 UTC").to_f + 1).__evolve_date__
136+
end
137+
138+
it 'evolves the correct time' do
139+
expect(evolved).to eq(date)
140+
end
141+
end
142+
end
67143
end

spec/mongoid/criteria/queryable/extensions/integer_spec.rb

Lines changed: 86 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,29 +39,105 @@
3939
end
4040
end
4141
end
42+
43+
context "when provided a number" do
44+
45+
context "when the number is an integer" do
46+
47+
it "returns an integer" do
48+
expect(described_class.evolve(1)).to eq(1)
49+
end
50+
end
51+
52+
context "when the number is a float" do
53+
54+
it "returns the float" do
55+
expect(described_class.evolve(2.23)).to eq(2.23)
56+
end
57+
end
58+
end
59+
60+
context "when provided nil" do
61+
62+
it "returns nil" do
63+
expect(described_class.evolve(nil)).to be_nil
64+
end
65+
end
4266
end
4367

44-
context "when provided a number" do
68+
describe "#__evolve_time__" do
69+
70+
context 'UTC time zone' do
71+
let(:time) do
72+
Time.parse("2022-01-01 16:15:01 UTC")
73+
end
4574

46-
context "when the number is an integer" do
75+
let(:evolved) do
76+
time.to_i.__evolve_time__
77+
end
4778

48-
it "returns an integer" do
49-
expect(described_class.evolve(1)).to eq(1)
79+
it 'evolves the correct time' do
80+
expect(evolved).to eq(time)
5081
end
5182
end
5283

53-
context "when the number is a float" do
84+
context 'other time zone' do
85+
let(:time) do
86+
Time.parse("2022-01-01 16:15:01 +0900")
87+
end
88+
89+
let(:evolved) do
90+
time.to_i.__evolve_time__
91+
end
5492

55-
it "returns the float" do
56-
expect(described_class.evolve(2.23)).to eq(2.23)
93+
it 'evolves the correct time' do
94+
expect(evolved).to eq(time)
5795
end
5896
end
5997
end
6098

61-
context "when provided nil" do
99+
describe "#__evolve_date__" do
62100

63-
it "returns nil" do
64-
expect(described_class.evolve(nil)).to be_nil
101+
context 'exact match' do
102+
let(:date) do
103+
Date.parse("2022-01-01")
104+
end
105+
106+
let(:evolved) do
107+
Time.parse("2022-01-01 0:00 UTC").to_i.__evolve_date__
108+
end
109+
110+
it 'evolves the correct time' do
111+
expect(evolved).to eq(date)
112+
end
113+
end
114+
115+
context 'one second earlier' do
116+
let(:date) do
117+
Date.parse("2021-12-31")
118+
end
119+
120+
let(:evolved) do
121+
(Time.parse("2022-01-01 0:00 UTC").to_i - 1).__evolve_date__
122+
end
123+
124+
it 'evolves the correct time' do
125+
expect(evolved).to eq(date)
126+
end
127+
end
128+
129+
context 'one second later' do
130+
let(:date) do
131+
Date.parse("2022-01-01")
132+
end
133+
134+
let(:evolved) do
135+
(Time.parse("2022-01-01 0:00 UTC").to_i + 1).__evolve_date__
136+
end
137+
138+
it 'evolves the correct time' do
139+
expect(evolved).to eq(date)
140+
end
65141
end
66142
end
67143
end

0 commit comments

Comments
 (0)