aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/test/cases/type/decimal_test.rb
diff options
context:
space:
mode:
authorSean Griffin <sean@seantheprogrammer.com>2015-09-21 10:12:44 -0600
committerSean Griffin <sean@seantheprogrammer.com>2015-09-21 10:12:44 -0600
commit56a8b40b400169fd9d96856721d0bbb01dbfaa64 (patch)
tree173a80a90c6f8a46010943421e874724427a62f6 /activemodel/test/cases/type/decimal_test.rb
parentb223d729d823dcf3c6915daa2bf48c028718c465 (diff)
parent858a7b04291b447edc541c6e17eec81f0cce34e5 (diff)
downloadrails-56a8b40b400169fd9d96856721d0bbb01dbfaa64.tar.gz
rails-56a8b40b400169fd9d96856721d0bbb01dbfaa64.tar.bz2
rails-56a8b40b400169fd9d96856721d0bbb01dbfaa64.zip
Merge pull request #21533
Move ActiveRecord::Type to ActiveModel The intention has always been to move the attributes API off of Active Record and onto Active Model. This is the first step in that transition. This merges the pull request (which has been squashed and rebased), with various changes to the code to get the quality to a level which is acceptable to merge. Close #21533
Diffstat (limited to 'activemodel/test/cases/type/decimal_test.rb')
-rw-r--r--activemodel/test/cases/type/decimal_test.rb57
1 files changed, 57 insertions, 0 deletions
diff --git a/activemodel/test/cases/type/decimal_test.rb b/activemodel/test/cases/type/decimal_test.rb
new file mode 100644
index 0000000000..353dbf84ad
--- /dev/null
+++ b/activemodel/test/cases/type/decimal_test.rb
@@ -0,0 +1,57 @@
+require "cases/helper"
+require "active_model/type"
+
+module ActiveModel
+ module Type
+ class DecimalTest < ActiveModel::TestCase
+ def test_type_cast_decimal
+ type = Decimal.new
+ assert_equal BigDecimal.new("0"), type.cast(BigDecimal.new("0"))
+ assert_equal BigDecimal.new("123"), type.cast(123.0)
+ assert_equal BigDecimal.new("1"), type.cast(:"1")
+ end
+
+ def test_type_cast_decimal_from_float_with_large_precision
+ type = Decimal.new(precision: ::Float::DIG + 2)
+ assert_equal BigDecimal.new("123.0"), type.cast(123.0)
+ end
+
+ def test_type_cast_from_float_with_unspecified_precision
+ type = Decimal.new
+ assert_equal 22.68.to_d, type.cast(22.68)
+ end
+
+ def test_type_cast_decimal_from_rational_with_precision
+ type = Decimal.new(precision: 2)
+ 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))
+ end
+
+ def test_type_cast_decimal_from_object_responding_to_d
+ value = Object.new
+ def value.to_d
+ BigDecimal.new("1")
+ end
+ type = Decimal.new
+ assert_equal BigDecimal("1"), type.cast(value)
+ end
+
+ def test_changed?
+ type = Decimal.new
+
+ assert type.changed?(5.0, 5.0, '5.0wibble')
+ assert_not type.changed?(5.0, 5.0, '5.0')
+ assert_not type.changed?(-5.0, -5.0, '-5.0')
+ end
+ end
+ end
+end