Merge pull request #27902 from kirs/yaml-column-error

Indicate action that failed in YamlColumn
This commit is contained in:
Kasper Timm Hansen 2017-02-06 09:27:23 +01:00 committed by GitHub
commit 2bbea8b099
4 changed files with 10 additions and 10 deletions

View File

@ -14,7 +14,7 @@ module ActiveRecord
def dump(obj)
return if obj.nil?
assert_valid_value(obj)
assert_valid_value(obj, action: "dump")
YAML.dump obj
end
@ -23,16 +23,16 @@ module ActiveRecord
return yaml unless yaml.is_a?(String) && /^---/.match?(yaml)
obj = YAML.load(yaml)
assert_valid_value(obj)
assert_valid_value(obj, action: "load")
obj ||= object_class.new if object_class != Object
obj
end
def assert_valid_value(obj)
def assert_valid_value(obj, action:)
unless obj.nil? || obj.is_a?(object_class)
raise SerializationTypeMismatch,
"Attribute `#{@attr_name}` was supposed to be a #{object_class}, but was a #{obj.class}. -- #{obj.inspect}"
"can't #{action} `#{@attr_name}`: was supposed to be a #{object_class}, but was a #{obj.class}. -- #{obj.inspect}"
end
end

View File

@ -43,7 +43,7 @@ module ActiveRecord
def assert_valid_value(value)
if coder.respond_to?(:assert_valid_value)
coder.assert_valid_value(value)
coder.assert_valid_value(value, action: "serialize")
end
end

View File

@ -10,19 +10,19 @@ module ActiveRecord
end
def test_type_mismatch_on_different_classes_on_dump
coder = YAMLColumn.new("attr_name", Array)
coder = YAMLColumn.new("tags", Array)
error = assert_raises(SerializationTypeMismatch) do
coder.dump("a")
end
assert_equal %{Attribute `attr_name` was supposed to be a Array, but was a String. -- "a"}, error.to_s
assert_equal %{can't dump `tags`: was supposed to be a Array, but was a String. -- "a"}, error.to_s
end
def test_type_mismatch_on_different_classes
coder = YAMLColumn.new("attr_name", Array)
coder = YAMLColumn.new("tags", Array)
error = assert_raises(SerializationTypeMismatch) do
coder.load "--- foo"
end
assert_equal %{Attribute `attr_name` was supposed to be a Array, but was a String. -- "foo"}, error.to_s
assert_equal %{can't load `tags`: was supposed to be a Array, but was a String. -- "foo"}, error.to_s
end
def test_nil_is_ok

View File

@ -250,7 +250,7 @@ class SerializedAttributeTest < ActiveRecord::TestCase
error = assert_raise(ActiveRecord::SerializationTypeMismatch) do
topic.content
end
expected = "Attribute `content` was supposed to be a Array, but was a Hash. -- {:zomg=>true}"
expected = "can't load `content`: was supposed to be a Array, but was a Hash. -- {:zomg=>true}"
assert_equal expected, error.to_s
end