diff --git a/lib/mongoid/criteria/queryable/extensions/numeric.rb b/lib/mongoid/criteria/queryable/extensions/numeric.rb index b895b26a27..787022da00 100644 --- a/lib/mongoid/criteria/queryable/extensions/numeric.rb +++ b/lib/mongoid/criteria/queryable/extensions/numeric.rb @@ -71,3 +71,5 @@ def evolve(object) ::Float.__send__(:include, Mongoid::Criteria::Queryable::Extensions::Numeric) ::Float.__send__(:extend, Mongoid::Criteria::Queryable::Extensions::Numeric::ClassMethods) + +::BigDecimal.__send__(:include, Mongoid::Criteria::Queryable::Extensions::Numeric) diff --git a/spec/integration/criteria/raw_value_spec.rb b/spec/integration/criteria/raw_value_spec.rb index e1756d9bf4..50f28c73d9 100644 --- a/spec/integration/criteria/raw_value_spec.rb +++ b/spec/integration/criteria/raw_value_spec.rb @@ -311,8 +311,8 @@ expect(Band.where(founded: Mongoid::RawValue(BigDecimal('1577923200'))).to_a).to eq [band7] end - it 'matches objects without raw value because BigDecimal cannot be evolved to Date' do - expect(Band.where(founded: BigDecimal('1577923200')).to_a).to eq [band7] + it 'matches objects without raw value' do + expect(Band.where(founded: BigDecimal('1577923200')).to_a).to eq [band3, band4] end end @@ -321,8 +321,8 @@ expect(Band.where(updated: Mongoid::RawValue(BigDecimal('1578153600'))).to_a).to eq [band7] end - it 'matches objects without raw value because BigDecimal cannot be evolved to Time' do - expect(Band.where(updated: BigDecimal('1578153600')).to_a).to eq [band7] + it 'matches objects without raw value' do + expect(Band.where(updated: BigDecimal('1578153600')).to_a).to eq [band4, band5] end end end diff --git a/spec/mongoid/criteria/queryable/extensions/big_decimal_spec.rb b/spec/mongoid/criteria/queryable/extensions/big_decimal_spec.rb index 97379077c9..b6c7a45f76 100644 --- a/spec/mongoid/criteria/queryable/extensions/big_decimal_spec.rb +++ b/spec/mongoid/criteria/queryable/extensions/big_decimal_spec.rb @@ -166,4 +166,80 @@ end end end + + describe "#__evolve_time__" do + + context 'UTC time zone' do + let(:time) do + Time.parse("2022-01-01 16:15:01 UTC") + end + + let(:evolved) do + time.to_i.to_d.__evolve_time__ + end + + it 'evolves the correct time' do + expect(evolved).to eq(time) + end + end + + context 'other time zone' do + let(:time) do + Time.parse("2022-01-01 16:15:01 +0900") + end + + let(:evolved) do + time.to_i.to_d.__evolve_time__ + end + + it 'evolves the correct time' do + expect(evolved).to eq(time) + end + end + end + + describe "#__evolve_date__" do + + context 'exact match' do + let(:date) do + Date.parse("2022-01-01") + end + + let(:evolved) do + Time.parse("2022-01-01 0:00 UTC").to_i.to_d.__evolve_date__ + end + + it 'evolves the correct time' do + expect(evolved).to eq(date) + end + end + + context 'one second earlier' do + let(:date) do + Date.parse("2021-12-31") + end + + let(:evolved) do + (Time.parse("2022-01-01 0:00 UTC").to_i.to_d - 1).__evolve_date__ + end + + it 'evolves the correct time' do + expect(evolved).to eq(date) + end + end + + context 'one second later' do + let(:date) do + Date.parse("2022-01-01") + end + + let(:evolved) do + (Time.parse("2022-01-01 0:00 UTC").to_i.to_d + 1).__evolve_date__ + end + + it 'evolves the correct time' do + expect(evolved).to eq(date) + end + end + end end diff --git a/spec/mongoid/criteria/queryable/extensions/float_spec.rb b/spec/mongoid/criteria/queryable/extensions/float_spec.rb index d05d55df84..8c403a02f4 100644 --- a/spec/mongoid/criteria/queryable/extensions/float_spec.rb +++ b/spec/mongoid/criteria/queryable/extensions/float_spec.rb @@ -64,4 +64,80 @@ end end end + + describe "#__evolve_time__" do + + context 'UTC time zone' do + let(:time) do + Time.parse("2022-01-01 16:15:01 UTC") + end + + let(:evolved) do + time.to_f.__evolve_time__ + end + + it 'evolves the correct time' do + expect(evolved).to eq(time) + end + end + + context 'other time zone' do + let(:time) do + Time.parse("2022-01-01 16:15:01 +0900") + end + + let(:evolved) do + time.to_f.__evolve_time__ + end + + it 'evolves the correct time' do + expect(evolved).to eq(time) + end + end + end + + describe "#__evolve_date__" do + + context 'exact match' do + let(:date) do + Date.parse("2022-01-01") + end + + let(:evolved) do + Time.parse("2022-01-01 0:00 UTC").to_f.__evolve_date__ + end + + it 'evolves the correct time' do + expect(evolved).to eq(date) + end + end + + context 'one second earlier' do + let(:date) do + Date.parse("2021-12-31") + end + + let(:evolved) do + (Time.parse("2022-01-01 0:00 UTC").to_f - 1).__evolve_date__ + end + + it 'evolves the correct time' do + expect(evolved).to eq(date) + end + end + + context 'one second later' do + let(:date) do + Date.parse("2022-01-01") + end + + let(:evolved) do + (Time.parse("2022-01-01 0:00 UTC").to_f + 1).__evolve_date__ + end + + it 'evolves the correct time' do + expect(evolved).to eq(date) + end + end + end end diff --git a/spec/mongoid/criteria/queryable/extensions/integer_spec.rb b/spec/mongoid/criteria/queryable/extensions/integer_spec.rb index ecf20700a7..98dd884a90 100644 --- a/spec/mongoid/criteria/queryable/extensions/integer_spec.rb +++ b/spec/mongoid/criteria/queryable/extensions/integer_spec.rb @@ -39,29 +39,105 @@ end end end + + context "when provided a number" do + + context "when the number is an integer" do + + it "returns an integer" do + expect(described_class.evolve(1)).to eq(1) + end + end + + context "when the number is a float" do + + it "returns the float" do + expect(described_class.evolve(2.23)).to eq(2.23) + end + end + end + + context "when provided nil" do + + it "returns nil" do + expect(described_class.evolve(nil)).to be_nil + end + end end - context "when provided a number" do + describe "#__evolve_time__" do + + context 'UTC time zone' do + let(:time) do + Time.parse("2022-01-01 16:15:01 UTC") + end - context "when the number is an integer" do + let(:evolved) do + time.to_i.__evolve_time__ + end - it "returns an integer" do - expect(described_class.evolve(1)).to eq(1) + it 'evolves the correct time' do + expect(evolved).to eq(time) end end - context "when the number is a float" do + context 'other time zone' do + let(:time) do + Time.parse("2022-01-01 16:15:01 +0900") + end + + let(:evolved) do + time.to_i.__evolve_time__ + end - it "returns the float" do - expect(described_class.evolve(2.23)).to eq(2.23) + it 'evolves the correct time' do + expect(evolved).to eq(time) end end end - context "when provided nil" do + describe "#__evolve_date__" do - it "returns nil" do - expect(described_class.evolve(nil)).to be_nil + context 'exact match' do + let(:date) do + Date.parse("2022-01-01") + end + + let(:evolved) do + Time.parse("2022-01-01 0:00 UTC").to_i.__evolve_date__ + end + + it 'evolves the correct time' do + expect(evolved).to eq(date) + end + end + + context 'one second earlier' do + let(:date) do + Date.parse("2021-12-31") + end + + let(:evolved) do + (Time.parse("2022-01-01 0:00 UTC").to_i - 1).__evolve_date__ + end + + it 'evolves the correct time' do + expect(evolved).to eq(date) + end + end + + context 'one second later' do + let(:date) do + Date.parse("2022-01-01") + end + + let(:evolved) do + (Time.parse("2022-01-01 0:00 UTC").to_i + 1).__evolve_date__ + end + + it 'evolves the correct time' do + expect(evolved).to eq(date) + end end end end diff --git a/spec/shared b/spec/shared index 30e2f1bcab..4b895e70b0 160000 --- a/spec/shared +++ b/spec/shared @@ -1 +1 @@ -Subproject commit 30e2f1bcabe957f76dc412dac3e91077b3cb3715 +Subproject commit 4b895e70b02a26368c2fdd40764c3dd7933bad9c