Skip to content

Commit e29b4f6

Browse files
committed
Merge remote-tracking branch 'remotes/johnnyshields/bigdecimal-evolve-to-time' into evolve-rework
2 parents c1d3382 + 023ceac commit e29b4f6

File tree

4 files changed

+240
-10
lines changed

4 files changed

+240
-10
lines changed

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

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

8080
::Float.__send__(:include, Mongoid::Criteria::Queryable::Extensions::Numeric)
8181
::Float.__send__(:extend, Mongoid::Criteria::Queryable::Extensions::Numeric::ClassMethods)
82+
83+
::BigDecimal.__send__(:include, Mongoid::Criteria::Queryable::Extensions::Numeric)

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)