aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2015-09-01 16:00:30 -0300
committerRafael Mendonça França <rafaelmfranca@gmail.com>2015-09-01 16:02:04 -0300
commit4727a94c50e8bb371e9c9e7798a5ba0510320657 (patch)
tree84b79d9c689de09dc6e318384510c944b3ecc276 /activerecord/test
parent9f28cb9dc778b46a3e455f9d64c719a8a95078b8 (diff)
downloadrails-4727a94c50e8bb371e9c9e7798a5ba0510320657.tar.gz
rails-4727a94c50e8bb371e9c9e7798a5ba0510320657.tar.bz2
rails-4727a94c50e8bb371e9c9e7798a5ba0510320657.zip
Respect scale of the column in the Decimal type
[Rafael Mendonça França + Jean Boussier]
Diffstat (limited to 'activerecord/test')
-rw-r--r--activerecord/test/cases/base_test.rb28
-rw-r--r--activerecord/test/cases/type/decimal_test.rb5
2 files changed, 33 insertions, 0 deletions
diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb
index 382adbbdc7..0edb8a8901 100644
--- a/activerecord/test/cases/base_test.rb
+++ b/activerecord/test/cases/base_test.rb
@@ -946,6 +946,34 @@ class BasicsTest < ActiveRecord::TestCase
assert_equal BigDecimal("1000234000567.95"), m1.big_bank_balance
end
+ def test_numeric_fields_with_scale
+ m = NumericData.new(
+ :bank_balance => 1586.43122334,
+ :big_bank_balance => BigDecimal("234000567.952344"),
+ :world_population => 6000000000,
+ :my_house_population => 3
+ )
+ assert m.save
+
+ m1 = NumericData.find(m.id)
+ assert_not_nil m1
+
+ # As with migration_test.rb, we should make world_population >= 2**62
+ # to cover 64-bit platforms and test it is a Bignum, but the main thing
+ # is that it's an Integer.
+ assert_kind_of Integer, m1.world_population
+ assert_equal 6000000000, m1.world_population
+
+ assert_kind_of Fixnum, m1.my_house_population
+ assert_equal 3, m1.my_house_population
+
+ assert_kind_of BigDecimal, m1.bank_balance
+ assert_equal BigDecimal("1586.43"), m1.bank_balance
+
+ assert_kind_of BigDecimal, m1.big_bank_balance
+ assert_equal BigDecimal("234000567.95"), m1.big_bank_balance
+ end
+
def test_auto_id
auto = AutoId.new
auto.save
diff --git a/activerecord/test/cases/type/decimal_test.rb b/activerecord/test/cases/type/decimal_test.rb
index fe49d0e79a..01ee36b892 100644
--- a/activerecord/test/cases/type/decimal_test.rb
+++ b/activerecord/test/cases/type/decimal_test.rb
@@ -25,6 +25,11 @@ module ActiveRecord
assert_equal BigDecimal("0.33"), type.cast(Rational(1, 3))
end
+ def test_type_cast_decimal_from_rational_with_precision_and_scale
+ type = Decimal.new(precision: 4, scale: 2)
+ assert_equal BigDecimal("0.33"), type.cast(Rational(1, 3))
+ end
+
def test_type_cast_decimal_from_rational_without_precision_defaults_to_18_36
type = Decimal.new
assert_equal BigDecimal("0.333333333333333333E0"), type.cast(Rational(1, 3))