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

Fix NumericData.average test on 2.6.0-dev #34601

Conversation

abdallahalsamman
Copy link
Contributor

@abdallahalsamman abdallahalsamman commented Dec 3, 2018

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

@kamipo
Copy link
Member

kamipo commented Dec 3, 2018

Can you investigate why NumericData.average with Ruby 2.6 returns 0.0?

@abdallahalsamman
Copy link
Contributor Author

@kamipo Sure, that's what I'm doing right now.

#34600 (comment)

@abdallahalsamman
Copy link
Contributor Author

@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)
Copy link
Member

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.

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

Copy link
Contributor Author

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 🙌

@abdallahalsamman abdallahalsamman force-pushed the 34600-test_should_return_nil_as_average branch from 664c45a to 89b4612 Compare December 3, 2018 11:00
@kamipo kamipo merged commit 41e468a into rails:master Dec 3, 2018
utilum added a commit to utilum/rails that referenced this pull request Dec 4, 2018
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
rafaelfranca pushed a commit that referenced this pull request Dec 4, 2018
…s_average

Fix NumericData.average test on 2.6.0-dev
@rafaelfranca
Copy link
Member

I think this should still return nil. Can someone fix it?

@albertoalmagro
Copy link
Contributor

I'm on it @rafaelfranca

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants