Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚀 [READY FOR REVIEW] MONGOID-5559 - Bugfix: BigDecimal should include Mongoid::Criteria::Queryable::Extensions::Numeric #5469

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/mongoid/criteria/queryable/extensions/numeric.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor Author

@johnnyshields johnnyshields Feb 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extend ClassMethods not needed here since it mainly exists to define evolve which is done differently for BigDecimal already. (In the future, all this stuff typecasting stuff needs to be refactored.)

8 changes: 4 additions & 4 deletions spec/integration/criteria/raw_value_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
76 changes: 76 additions & 0 deletions spec/mongoid/criteria/queryable/extensions/big_decimal_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
76 changes: 76 additions & 0 deletions spec/mongoid/criteria/queryable/extensions/float_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
96 changes: 86 additions & 10 deletions spec/mongoid/criteria/queryable/extensions/integer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion spec/shared
Submodule shared updated 1 files
+5 −5 shlib/server.sh