mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #27902 from kirs/yaml-column-error
Indicate action that failed in YamlColumn
This commit is contained in:
commit
2bbea8b099
4 changed files with 10 additions and 10 deletions
|
@ -14,7 +14,7 @@ module ActiveRecord
|
||||||
def dump(obj)
|
def dump(obj)
|
||||||
return if obj.nil?
|
return if obj.nil?
|
||||||
|
|
||||||
assert_valid_value(obj)
|
assert_valid_value(obj, action: "dump")
|
||||||
YAML.dump obj
|
YAML.dump obj
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -23,16 +23,16 @@ module ActiveRecord
|
||||||
return yaml unless yaml.is_a?(String) && /^---/.match?(yaml)
|
return yaml unless yaml.is_a?(String) && /^---/.match?(yaml)
|
||||||
obj = YAML.load(yaml)
|
obj = YAML.load(yaml)
|
||||||
|
|
||||||
assert_valid_value(obj)
|
assert_valid_value(obj, action: "load")
|
||||||
obj ||= object_class.new if object_class != Object
|
obj ||= object_class.new if object_class != Object
|
||||||
|
|
||||||
obj
|
obj
|
||||||
end
|
end
|
||||||
|
|
||||||
def assert_valid_value(obj)
|
def assert_valid_value(obj, action:)
|
||||||
unless obj.nil? || obj.is_a?(object_class)
|
unless obj.nil? || obj.is_a?(object_class)
|
||||||
raise SerializationTypeMismatch,
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -43,7 +43,7 @@ module ActiveRecord
|
||||||
|
|
||||||
def assert_valid_value(value)
|
def assert_valid_value(value)
|
||||||
if coder.respond_to?(:assert_valid_value)
|
if coder.respond_to?(:assert_valid_value)
|
||||||
coder.assert_valid_value(value)
|
coder.assert_valid_value(value, action: "serialize")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -10,19 +10,19 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_type_mismatch_on_different_classes_on_dump
|
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
|
error = assert_raises(SerializationTypeMismatch) do
|
||||||
coder.dump("a")
|
coder.dump("a")
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
def test_type_mismatch_on_different_classes
|
def test_type_mismatch_on_different_classes
|
||||||
coder = YAMLColumn.new("attr_name", Array)
|
coder = YAMLColumn.new("tags", Array)
|
||||||
error = assert_raises(SerializationTypeMismatch) do
|
error = assert_raises(SerializationTypeMismatch) do
|
||||||
coder.load "--- foo"
|
coder.load "--- foo"
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
def test_nil_is_ok
|
def test_nil_is_ok
|
||||||
|
|
|
@ -250,7 +250,7 @@ class SerializedAttributeTest < ActiveRecord::TestCase
|
||||||
error = assert_raise(ActiveRecord::SerializationTypeMismatch) do
|
error = assert_raise(ActiveRecord::SerializationTypeMismatch) do
|
||||||
topic.content
|
topic.content
|
||||||
end
|
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
|
assert_equal expected, error.to_s
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue