Skip to content

Commit b454253

Browse files
p-mongop
andauthored
MONGOID-5195 MONGOID-5237 fix logic checking for compare_time_by_ms (#5179)
Co-authored-by: Oleg Pudeyev <[email protected]>
1 parent 59fa493 commit b454253

File tree

2 files changed

+56
-26
lines changed

2 files changed

+56
-26
lines changed

lib/mongoid/matcher/eq_impl.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ module EqImpl
2525
else
2626
# When doing a comparison with Time objects, compare using millisecond precision
2727
if Mongoid.compare_time_by_ms
28-
value == condition ||
29-
value.is_a?(Array) && value.include?(condition)
30-
else
3128
if value.kind_of?(Time) && condition.kind_of?(Time)
3229
time_eq?(value, condition)
3330
elsif value.is_a?(Array) && condition.kind_of?(Time)
@@ -42,6 +39,9 @@ module EqImpl
4239
value == condition ||
4340
value.is_a?(Array) && value.include?(condition)
4441
end
42+
else
43+
value == condition ||
44+
value.is_a?(Array) && value.include?(condition)
4545
end
4646
end
4747
end

spec/integration/matcher_spec.rb

Lines changed: 53 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,24 @@
2828
context 'comparing millisecond precision' do
2929
let(:time_millis) {Time.utc(2021, 10, 25, 10, 30, 30, 581774)}
3030

31-
context 'with exact match' do
32-
let(:query) do
33-
{'started_at' => time_millis}
34-
end
35-
36-
it_behaves_like 'is true'
31+
context "when compare_time_by_ms feature flag is set" do
32+
config_override :compare_time_by_ms, true
3733

38-
context 'and query has different timezone' do
39-
let(:time_millis) do
40-
Time.utc(2021, 10, 25, 10, 30, 30, 581345).in_time_zone("Stockholm")
34+
context 'with exact match' do
35+
let(:query) do
36+
{'started_at' => time_millis}
4137
end
4238

4339
it_behaves_like 'is true'
44-
end
45-
end
4640

47-
context "when compare_time_by_ms feature flag is set" do
48-
config_override :compare_time_by_ms, true
41+
context 'and query has different timezone' do
42+
let(:time_millis) do
43+
Time.utc(2021, 10, 25, 10, 30, 30, 581345).in_time_zone("Stockholm")
44+
end
45+
46+
it_behaves_like 'is true'
47+
end
48+
end
4949

5050
context 'with $in' do
5151
let(:query) do
@@ -54,31 +54,61 @@
5454

5555
it_behaves_like 'is true'
5656
end
57+
58+
context 'when matching an element in an array' do
59+
let(:document) do
60+
Mop.new(:array_field => [time])
61+
end
62+
63+
context 'with equals match' do
64+
let(:query) do
65+
{'array_field' => time_millis}
66+
end
67+
68+
it_behaves_like 'is true'
69+
end
70+
end
5771
end
5872

5973
context "when compare_time_by_ms feature flag is not set" do
6074
config_override :compare_time_by_ms, false
6175

62-
context 'with $in' do
76+
context 'with exact match' do
6377
let(:query) do
64-
{'started_at' => {:$in => [time_millis]}}
78+
{'started_at' => time_millis}
6579
end
6680

6781
it_behaves_like 'is false'
68-
end
69-
end
7082

71-
context 'when matching an element in an array' do
72-
let(:document) do
73-
Mop.new(:array_field => [time])
83+
context 'and query has different timezone' do
84+
let(:time_millis) do
85+
Time.utc(2021, 10, 25, 10, 30, 30, 581345).in_time_zone("Stockholm")
86+
end
87+
88+
it_behaves_like 'is true'
89+
end
7490
end
7591

76-
context 'with equals match' do
92+
context 'with $in' do
7793
let(:query) do
78-
{'array_field' => time_millis}
94+
{'started_at' => {:$in => [time_millis]}}
7995
end
8096

81-
it_behaves_like 'is true'
97+
it_behaves_like 'is false'
98+
end
99+
100+
context 'when matching an element in an array' do
101+
let(:document) do
102+
Mop.new(:array_field => [time])
103+
end
104+
105+
context 'with equals match' do
106+
let(:query) do
107+
{'array_field' => time_millis}
108+
end
109+
110+
it_behaves_like 'is false'
111+
end
82112
end
83113
end
84114
end

0 commit comments

Comments
 (0)