diff options
Diffstat (limited to 'activerecord/test/cases/attribute_test.rb')
-rw-r--r-- | activerecord/test/cases/attribute_test.rb | 67 |
1 files changed, 37 insertions, 30 deletions
diff --git a/activerecord/test/cases/attribute_test.rb b/activerecord/test/cases/attribute_test.rb index a24a4fc6a4..7cf6b498c9 100644 --- a/activerecord/test/cases/attribute_test.rb +++ b/activerecord/test/cases/attribute_test.rb @@ -1,4 +1,4 @@ -require 'cases/helper' +require "cases/helper" module ActiveRecord class AttributeTest < ActiveRecord::TestCase @@ -11,91 +11,91 @@ module ActiveRecord end test "from_database + read type casts from database" do - @type.expect(:deserialize, 'type cast from database', ['a value']) - attribute = Attribute.from_database(nil, 'a value', @type) + @type.expect(:deserialize, "type cast from database", ["a value"]) + attribute = Attribute.from_database(nil, "a value", @type) type_cast_value = attribute.value - assert_equal 'type cast from database', type_cast_value + assert_equal "type cast from database", type_cast_value end test "from_user + read type casts from user" do - @type.expect(:cast, 'type cast from user', ['a value']) - attribute = Attribute.from_user(nil, 'a value', @type) + @type.expect(:cast, "type cast from user", ["a value"]) + attribute = Attribute.from_user(nil, "a value", @type) type_cast_value = attribute.value - assert_equal 'type cast from user', type_cast_value + assert_equal "type cast from user", type_cast_value end test "reading memoizes the value" do - @type.expect(:deserialize, 'from the database', ['whatever']) - attribute = Attribute.from_database(nil, 'whatever', @type) + @type.expect(:deserialize, "from the database", ["whatever"]) + attribute = Attribute.from_database(nil, "whatever", @type) type_cast_value = attribute.value second_read = attribute.value - assert_equal 'from the database', type_cast_value + assert_equal "from the database", type_cast_value assert_same type_cast_value, second_read end test "reading memoizes falsy values" do - @type.expect(:deserialize, false, ['whatever']) - attribute = Attribute.from_database(nil, 'whatever', @type) + @type.expect(:deserialize, false, ["whatever"]) + attribute = Attribute.from_database(nil, "whatever", @type) attribute.value attribute.value end test "read_before_typecast returns the given value" do - attribute = Attribute.from_database(nil, 'raw value', @type) + attribute = Attribute.from_database(nil, "raw value", @type) raw_value = attribute.value_before_type_cast - assert_equal 'raw value', raw_value + assert_equal "raw value", raw_value end test "from_database + read_for_database type casts to and from database" do - @type.expect(:deserialize, 'read from database', ['whatever']) - @type.expect(:serialize, 'ready for database', ['read from database']) - attribute = Attribute.from_database(nil, 'whatever', @type) + @type.expect(:deserialize, "read from database", ["whatever"]) + @type.expect(:serialize, "ready for database", ["read from database"]) + attribute = Attribute.from_database(nil, "whatever", @type) serialize = attribute.value_for_database - assert_equal 'ready for database', serialize + assert_equal "ready for database", serialize end test "from_user + read_for_database type casts from the user to the database" do - @type.expect(:cast, 'read from user', ['whatever']) - @type.expect(:serialize, 'ready for database', ['read from user']) - attribute = Attribute.from_user(nil, 'whatever', @type) + @type.expect(:cast, "read from user", ["whatever"]) + @type.expect(:serialize, "ready for database", ["read from user"]) + attribute = Attribute.from_user(nil, "whatever", @type) serialize = attribute.value_for_database - assert_equal 'ready for database', serialize + assert_equal "ready for database", serialize end test "duping dups the value" do - @type.expect(:deserialize, 'type cast', ['a value']) - attribute = Attribute.from_database(nil, 'a value', @type) + @type.expect(:deserialize, "type cast", ["a value"]) + attribute = Attribute.from_database(nil, "a value", @type) value_from_orig = attribute.value value_from_clone = attribute.dup.value - value_from_orig << ' foo' + value_from_orig << " foo" - assert_equal 'type cast foo', value_from_orig - assert_equal 'type cast', value_from_clone + assert_equal "type cast foo", value_from_orig + assert_equal "type cast", value_from_clone end test "duping does not dup the value if it is not dupable" do - @type.expect(:deserialize, false, ['a value']) - attribute = Attribute.from_database(nil, 'a value', @type) + @type.expect(:deserialize, false, ["a value"]) + attribute = Attribute.from_database(nil, "a value", @type) assert_same attribute.value, attribute.dup.value end test "duping does not eagerly type cast if we have not yet type cast" do - attribute = Attribute.from_database(nil, 'a value', @type) + attribute = Attribute.from_database(nil, "a value", @type) attribute.dup end @@ -242,5 +242,12 @@ module ActiveRecord attribute.with_value_from_user(1) end end + + test "with_type preserves mutations" do + attribute = Attribute.from_database(:foo, "", Type::Value.new) + attribute.value << "1" + + assert_equal 1, attribute.with_type(Type::Integer.new).value + end end end |