From 023ceacb9c26f27326b44fa109e200aa39bb8f35 Mon Sep 17 00:00:00 2001 From: shields Date: Sun, 18 Sep 2022 21:57:25 +0900 Subject: [PATCH] Fix BigDecimal evolution to Time --- .../criteria/queryable/extensions/numeric.rb | 2 + .../queryable/extensions/big_decimal_spec.rb | 76 +++++++++++++++ .../queryable/extensions/float_spec.rb | 76 +++++++++++++++ .../queryable/extensions/integer_spec.rb | 96 +++++++++++++++++-- 4 files changed, 240 insertions(+), 10 deletions(-) diff --git a/lib/mongoid/criteria/queryable/extensions/numeric.rb b/lib/mongoid/criteria/queryable/extensions/numeric.rb index 382182a3a5..22f45c0ec4 100644 --- a/lib/mongoid/criteria/queryable/extensions/numeric.rb +++ b/lib/mongoid/criteria/queryable/extensions/numeric.rb @@ -79,3 +79,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/mongoid/criteria/queryable/extensions/big_decimal_spec.rb b/spec/mongoid/criteria/queryable/extensions/big_decimal_spec.rb index f85c3725aa..a2b0807b5d 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