-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
Fix NumericData.average test on 2.6.0-dev #34601
Fix NumericData.average test on 2.6.0-dev #34601
Conversation
Can you investigate why |
@kamipo Sure, that's what I'm doing right now. |
@kamipo This should be the commit that caused this issue. ruby/ruby@a0e438c#diff-6b866d482baf2bdfd8433893fb1f6d36R144 What's the right action to take right now? |
@@ -58,7 +58,7 @@ def test_should_return_integer_average_if_db_returns_such | |||
end | |||
|
|||
def test_should_return_nil_as_average | |||
assert_nil NumericData.average(:bank_balance) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This assertion actually would be failed without Ruby 2.6.0 if bigdecimal 1.4.0.pre.20181130a is used.
rails/activerecord/lib/active_record/relation/calculations.rb
Lines 400 to 405 in e7717ec
def type_cast_calculated_value(value, type, operation = nil) | |
case operation | |
when "count" then value.to_i | |
when "sum" then type.deserialize(value || 0) | |
when "average" then value.respond_to?(:to_d) ? value.to_d : value | |
else type.deserialize(value) |
How about checking nil.respond_to?(:to_d)
?
--- a/activerecord/test/cases/calculations_test.rb
+++ b/activerecord/test/cases/calculations_test.rb
@@ -57,8 +57,12 @@ def test_should_return_integer_average_if_db_returns_such
assert_equal 3, value
end
- def test_should_return_nil_as_average
- assert_nil NumericData.average(:bank_balance)
+ def test_should_return_nil_to_d_as_average
+ if nil.respond_to?(:to_d)
+ assert_equal BigDecimal(0), NumericData.average(:bank_balance)
+ else
+ assert_nil NumericData.average(:bank_balance)
+ end
end
def test_should_get_maximum_of_field
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense, Thanks for helping out 🙌
664c45a
to
89b4612
Compare
This patch modifies XmlMini::Parsing["decimal"] to handle a string that contains an invalid number. Since [ruby/ruby@a0e438c#diff-6b866d482baf2bdfd8433893fb1f6d36R144](ruby/ruby@a0e438c#diff-6b866d482baf2bdfd8433893fb1f6d36R144) this case raises an `ArgumentError`. `String.to_f` returns 0.0 if there is not a valid number at the start of the argument, so current behavior is conserved. See https://travis-ci.org/rails/rails/jobs/463180341#L6264 Related: rails#34600, rails#34601
…s_average Fix NumericData.average test on 2.6.0-dev
I think this should still return |
I'm on it @rafaelfranca |
Fixes #34600
Summary
NumericData.average was returning nil on ruby versions <= ruby 2.6.0dev (2018-12-03 trunk 66154)
Now it returns 0.0 instead of nil