Skip to content

Commit

Permalink
MONGOID-5559 Fix bigdecimal evolution to time (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyshields authored Apr 11, 2023
1 parent 0862d1d commit a89b714
Show file tree
Hide file tree
Showing 6 changed files with 248 additions and 14 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ Refer to the [Roadmap issue](https://github.com/tablecheck/mongoid-ultra/issues/
-[MONGOID-5556](https://jira.mongodb.org/browse/MONGOID-5556) - Add `Criteria#tally` `:unwind` arg to splat array results.
- More to come soon!

#### Bug Fixes

- 🐞 [MONGOID-5559](https://jira.mongodb.org/browse/MONGOID-5559) - `BigDecimal` should correctly type-cast to `Time`.

#### Best Practices

-[MONGOID-5572](https://jira.mongodb.org/browse/MONGOID-5572) - RSpec: Use expectation syntax, enforced with RSpec config setting.
Expand Down
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)
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

0 comments on commit a89b714

Please sign in to comment.