1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fix decimal_test module and add new test for object responding to to_d

This commit is contained in:
Mariano Valles 2014-07-16 17:17:31 +02:00
parent 431b4b4d88
commit dd02463a26
2 changed files with 15 additions and 7 deletions

View file

@ -23,7 +23,6 @@ module ActiveRecord
cast_value(value.to_s)
end
end
end
end
end

View file

@ -1,24 +1,33 @@
require "cases/helper"
module ActiveRecord
module ConnectionAdapters
module Type
class DecimalTest < ActiveRecord::TestCase
def test_type_cast_decimal
type = Type::Decimal.new
type = Decimal.new
assert_equal BigDecimal.new("0"), type.type_cast_from_user(BigDecimal.new("0"))
assert_equal BigDecimal.new("123"), type.type_cast_from_user(123.0)
assert_equal BigDecimal.new("1"), type.type_cast_from_user(:"1")
end
def test_type_cast_rational_to_decimal_with_precision
type = Type::Decimal.new(precision: 2)
def test_type_cast_decimal_from_rational_with_precision
type = Decimal.new(precision: 2)
assert_equal BigDecimal("0.33"), type.type_cast_from_user(Rational(1, 3))
end
def test_type_cast_rational_to_decimal_without_precision_defaults_to_18_36
type = Type::Decimal.new
def test_type_cast_decimal_from_rational_without_precision_defaults_to_18_36
type = Decimal.new
assert_equal BigDecimal("0.333333333333333333E0"), type.type_cast_from_user(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.type_cast_from_user(value)
end
end
end
end