2017-07-16 13:11:16 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2010-08-03 04:32:48 -04:00
|
|
|
require "cases/helper"
|
2016-12-12 08:32:04 -05:00
|
|
|
require "yaml"
|
2010-08-03 04:32:48 -04:00
|
|
|
|
2019-08-02 00:25:13 -04:00
|
|
|
class ErrorsTest < ActiveModel::TestCase
|
2010-08-03 04:32:48 -04:00
|
|
|
class Person
|
|
|
|
extend ActiveModel::Naming
|
|
|
|
def initialize
|
|
|
|
@errors = ActiveModel::Errors.new(self)
|
|
|
|
end
|
|
|
|
|
2018-03-20 10:39:44 -04:00
|
|
|
attr_accessor :name, :age, :gender, :city
|
2010-08-03 04:32:48 -04:00
|
|
|
attr_reader :errors
|
|
|
|
|
|
|
|
def validate!
|
2016-05-01 17:09:07 -04:00
|
|
|
errors.add(:name, :blank, message: "cannot be nil") if name == nil
|
2010-08-03 04:32:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def read_attribute_for_validation(attr)
|
|
|
|
send(attr)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.human_attribute_name(attr, options = {})
|
|
|
|
attr
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.lookup_ancestors
|
|
|
|
[self]
|
|
|
|
end
|
2011-01-05 06:56:25 -05:00
|
|
|
end
|
2010-08-03 04:32:48 -04:00
|
|
|
|
2012-01-16 11:22:44 -05:00
|
|
|
def test_delete
|
2018-03-20 10:39:44 -04:00
|
|
|
errors = ActiveModel::Errors.new(Person.new)
|
|
|
|
errors.add(:name, :blank)
|
|
|
|
errors.delete("name")
|
|
|
|
assert_empty errors[:name]
|
2012-01-16 11:22:44 -05:00
|
|
|
end
|
|
|
|
|
2011-02-09 12:19:15 -05:00
|
|
|
def test_include?
|
2018-03-20 10:39:44 -04:00
|
|
|
errors = ActiveModel::Errors.new(Person.new)
|
2018-03-31 10:42:40 -04:00
|
|
|
assert_deprecated { errors[:foo] << "omg" }
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes errors, :foo, "errors should include :foo"
|
2016-02-14 11:41:59 -05:00
|
|
|
assert_includes errors, "foo", "errors should include 'foo' as :foo"
|
2011-02-09 12:19:15 -05:00
|
|
|
end
|
|
|
|
|
2019-07-20 15:26:43 -04:00
|
|
|
def test_each_when_arity_is_negative
|
|
|
|
errors = ActiveModel::Errors.new(Person.new)
|
|
|
|
errors.add(:name, :blank)
|
|
|
|
errors.add(:gender, :blank)
|
|
|
|
|
|
|
|
assert_equal([:name, :gender], errors.map(&:attribute))
|
|
|
|
end
|
|
|
|
|
2019-04-30 17:54:40 -04:00
|
|
|
def test_any?
|
|
|
|
errors = ActiveModel::Errors.new(Person.new)
|
|
|
|
errors.add(:name)
|
|
|
|
assert_not_deprecated {
|
|
|
|
assert errors.any?, "any? should return true"
|
|
|
|
}
|
|
|
|
assert_not_deprecated {
|
|
|
|
assert errors.any? { |_| true }, "any? should return true"
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-04-13 19:07:59 -04:00
|
|
|
def test_first
|
|
|
|
errors = ActiveModel::Errors.new(Person.new)
|
|
|
|
errors.add(:name, :blank)
|
|
|
|
|
|
|
|
error = errors.first
|
|
|
|
assert_kind_of ActiveModel::Error, error
|
|
|
|
end
|
|
|
|
|
2012-01-17 02:07:15 -05:00
|
|
|
def test_dup
|
2018-03-20 10:39:44 -04:00
|
|
|
errors = ActiveModel::Errors.new(Person.new)
|
|
|
|
errors.add(:name)
|
2012-01-17 02:07:15 -05:00
|
|
|
errors_dup = errors.dup
|
2018-03-20 10:39:44 -04:00
|
|
|
assert_not_same errors_dup.errors, errors.errors
|
2012-01-17 02:07:15 -05:00
|
|
|
end
|
|
|
|
|
2011-09-06 12:35:12 -04:00
|
|
|
def test_has_key?
|
2018-03-20 10:39:44 -04:00
|
|
|
errors = ActiveModel::Errors.new(Person.new)
|
|
|
|
errors.add(:foo, "omg")
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal true, errors.has_key?(:foo), "errors should have key :foo"
|
2016-02-14 11:41:59 -05:00
|
|
|
assert_equal true, errors.has_key?("foo"), "errors should have key 'foo' as :foo"
|
2011-09-06 12:35:12 -04:00
|
|
|
end
|
|
|
|
|
2014-01-22 09:22:35 -05:00
|
|
|
def test_has_no_key
|
2018-03-20 10:39:44 -04:00
|
|
|
errors = ActiveModel::Errors.new(Person.new)
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal false, errors.has_key?(:name), "errors should not have key :name"
|
2014-01-22 09:22:35 -05:00
|
|
|
end
|
|
|
|
|
2014-10-14 04:53:26 -04:00
|
|
|
def test_key?
|
2018-03-20 10:39:44 -04:00
|
|
|
errors = ActiveModel::Errors.new(Person.new)
|
|
|
|
errors.add(:foo, "omg")
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal true, errors.key?(:foo), "errors should have key :foo"
|
2016-02-14 11:41:59 -05:00
|
|
|
assert_equal true, errors.key?("foo"), "errors should have key 'foo' as :foo"
|
2014-10-14 04:53:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_no_key
|
2018-03-20 10:39:44 -04:00
|
|
|
errors = ActiveModel::Errors.new(Person.new)
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal false, errors.key?(:name), "errors should not have key :name"
|
2014-10-14 04:53:26 -04:00
|
|
|
end
|
|
|
|
|
2013-03-30 14:04:08 -04:00
|
|
|
test "clear errors" do
|
2013-01-10 14:49:47 -05:00
|
|
|
person = Person.new
|
|
|
|
person.validate!
|
|
|
|
|
|
|
|
assert_equal 1, person.errors.count
|
|
|
|
person.errors.clear
|
2018-01-25 18:16:57 -05:00
|
|
|
assert_empty person.errors
|
2013-01-10 14:49:47 -05:00
|
|
|
end
|
|
|
|
|
2019-08-29 22:41:39 -04:00
|
|
|
test "clear errors by key" do
|
|
|
|
person = Person.new
|
|
|
|
person.validate!
|
|
|
|
|
|
|
|
assert_equal 1, person.errors.count
|
|
|
|
assert_deprecated { person.errors[:name].clear }
|
|
|
|
assert_empty person.errors
|
|
|
|
end
|
|
|
|
|
2013-08-19 03:52:31 -04:00
|
|
|
test "error access is indifferent" do
|
2018-03-20 10:39:44 -04:00
|
|
|
errors = ActiveModel::Errors.new(Person.new)
|
|
|
|
errors.add(:name, "omg")
|
2013-08-19 03:52:31 -04:00
|
|
|
|
2018-03-20 10:39:44 -04:00
|
|
|
assert_equal ["omg"], errors["name"]
|
2013-08-19 03:52:31 -04:00
|
|
|
end
|
|
|
|
|
2013-01-10 14:49:47 -05:00
|
|
|
test "values returns an array of messages" do
|
2018-03-20 10:39:44 -04:00
|
|
|
errors = ActiveModel::Errors.new(Person.new)
|
2018-03-31 10:42:40 -04:00
|
|
|
assert_deprecated { errors.messages[:foo] = "omg" }
|
|
|
|
assert_deprecated { errors.messages[:baz] = "zomg" }
|
2013-01-10 14:49:47 -05:00
|
|
|
|
2018-03-20 10:39:44 -04:00
|
|
|
assert_deprecated do
|
|
|
|
assert_equal ["omg", "zomg"], errors.values
|
|
|
|
end
|
2013-01-10 14:49:47 -05:00
|
|
|
end
|
|
|
|
|
2019-03-16 22:25:08 -04:00
|
|
|
test "[]= overrides values" do
|
|
|
|
errors = ActiveModel::Errors.new(self)
|
|
|
|
assert_deprecated { errors.messages[:foo] = "omg" }
|
|
|
|
assert_deprecated { errors.messages[:foo] = "zomg" }
|
|
|
|
|
|
|
|
assert_equal ["zomg"], errors[:foo]
|
|
|
|
end
|
|
|
|
|
2017-03-28 05:05:14 -04:00
|
|
|
test "values returns an empty array after try to get a message only" do
|
2018-03-20 10:39:44 -04:00
|
|
|
errors = ActiveModel::Errors.new(Person.new)
|
2017-03-28 05:05:14 -04:00
|
|
|
errors.messages[:foo]
|
|
|
|
errors.messages[:baz]
|
|
|
|
|
2018-03-20 10:39:44 -04:00
|
|
|
assert_deprecated do
|
|
|
|
assert_equal [], errors.values
|
|
|
|
end
|
2017-03-28 05:05:14 -04:00
|
|
|
end
|
|
|
|
|
2013-01-10 14:49:47 -05:00
|
|
|
test "keys returns the error keys" do
|
2018-03-20 10:39:44 -04:00
|
|
|
errors = ActiveModel::Errors.new(Person.new)
|
2018-03-31 10:42:40 -04:00
|
|
|
assert_deprecated { errors.messages[:foo] << "omg" }
|
|
|
|
assert_deprecated { errors.messages[:baz] << "zomg" }
|
2013-01-10 14:49:47 -05:00
|
|
|
|
2018-03-20 10:39:44 -04:00
|
|
|
assert_deprecated do
|
2018-03-31 10:42:40 -04:00
|
|
|
assert_equal [:foo, :baz], errors.keys
|
2018-03-20 10:39:44 -04:00
|
|
|
end
|
2013-01-10 14:49:47 -05:00
|
|
|
end
|
|
|
|
|
2017-03-28 05:05:14 -04:00
|
|
|
test "keys returns an empty array after try to get a message only" do
|
2018-03-20 10:39:44 -04:00
|
|
|
errors = ActiveModel::Errors.new(Person.new)
|
2017-03-28 05:05:14 -04:00
|
|
|
errors.messages[:foo]
|
|
|
|
errors.messages[:baz]
|
|
|
|
|
2018-03-20 10:39:44 -04:00
|
|
|
assert_deprecated do
|
|
|
|
assert_equal [], errors.keys
|
|
|
|
end
|
2017-03-28 05:05:14 -04:00
|
|
|
end
|
|
|
|
|
2020-04-08 18:29:08 -04:00
|
|
|
test "attribute_names returns the error attributes" do
|
|
|
|
errors = ActiveModel::Errors.new(Person.new)
|
|
|
|
errors.add(:foo, "omg")
|
|
|
|
errors.add(:baz, "zomg")
|
|
|
|
|
|
|
|
assert_equal [:foo, :baz], errors.attribute_names
|
|
|
|
end
|
|
|
|
|
|
|
|
test "attribute_names only returns unique attribute names" do
|
|
|
|
errors = ActiveModel::Errors.new(Person.new)
|
|
|
|
errors.add(:foo, "omg")
|
|
|
|
errors.add(:foo, "zomg")
|
|
|
|
|
|
|
|
assert_equal [:foo], errors.attribute_names
|
|
|
|
end
|
|
|
|
|
|
|
|
test "attribute_names returns an empty array after try to get a message only" do
|
|
|
|
errors = ActiveModel::Errors.new(Person.new)
|
|
|
|
errors.messages[:foo]
|
|
|
|
errors.messages[:baz]
|
|
|
|
|
|
|
|
assert_equal [], errors.attribute_names
|
|
|
|
end
|
|
|
|
|
2013-03-30 14:04:08 -04:00
|
|
|
test "detecting whether there are errors with empty?, blank?, include?" do
|
2011-01-05 06:56:25 -05:00
|
|
|
person = Person.new
|
|
|
|
person.errors[:foo]
|
2018-01-25 18:16:57 -05:00
|
|
|
assert_empty person.errors
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate person.errors, :blank?
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_not_includes person.errors, :foo
|
2010-08-03 04:32:48 -04:00
|
|
|
end
|
|
|
|
|
2016-03-24 10:27:31 -04:00
|
|
|
test "include? does not add a key to messages hash" do
|
|
|
|
person = Person.new
|
|
|
|
person.errors.include?(:foo)
|
|
|
|
|
|
|
|
assert_not person.errors.messages.key?(:foo)
|
|
|
|
end
|
|
|
|
|
2013-03-30 14:04:08 -04:00
|
|
|
test "adding errors using conditionals with Person#validate!" do
|
2010-08-03 04:32:48 -04:00
|
|
|
person = Person.new
|
|
|
|
person.validate!
|
2014-01-03 17:02:31 -05:00
|
|
|
assert_equal ["name cannot be nil"], person.errors.full_messages
|
|
|
|
assert_equal ["cannot be nil"], person.errors[:name]
|
2010-08-03 04:32:48 -04:00
|
|
|
end
|
|
|
|
|
2018-03-31 10:42:40 -04:00
|
|
|
test "add an error message on a specific attribute (deprecated)" do
|
|
|
|
person = Person.new
|
|
|
|
person.errors.add(:name, "cannot be blank")
|
|
|
|
assert_equal ["cannot be blank"], person.errors[:name]
|
|
|
|
end
|
|
|
|
|
|
|
|
test "add an error message on a specific attribute with a defined type (deprecated)" do
|
|
|
|
person = Person.new
|
|
|
|
person.errors.add(:name, :blank, message: "cannot be blank")
|
|
|
|
assert_equal ["cannot be blank"], person.errors[:name]
|
|
|
|
end
|
|
|
|
|
|
|
|
test "add an error with a symbol (deprecated)" do
|
|
|
|
person = Person.new
|
|
|
|
person.errors.add(:name, :blank)
|
|
|
|
message = person.errors.generate_message(:name, :blank)
|
|
|
|
assert_equal [message], person.errors[:name]
|
|
|
|
end
|
|
|
|
|
|
|
|
test "add an error with a proc (deprecated)" do
|
|
|
|
person = Person.new
|
|
|
|
message = Proc.new { "cannot be blank" }
|
|
|
|
person.errors.add(:name, message)
|
|
|
|
assert_equal ["cannot be blank"], person.errors[:name]
|
|
|
|
end
|
|
|
|
|
2018-03-20 10:39:44 -04:00
|
|
|
test "add creates an error object and returns it" do
|
2010-08-03 04:32:48 -04:00
|
|
|
person = Person.new
|
2018-03-20 10:39:44 -04:00
|
|
|
error = person.errors.add(:name, :blank)
|
|
|
|
|
|
|
|
assert_equal :name, error.attribute
|
|
|
|
assert_equal :blank, error.type
|
2018-04-02 12:17:45 -04:00
|
|
|
assert_equal error, person.errors.objects.first
|
2010-08-03 04:32:48 -04:00
|
|
|
end
|
|
|
|
|
2018-03-20 10:39:44 -04:00
|
|
|
test "add, with type as symbol" do
|
2015-05-04 08:36:26 -04:00
|
|
|
person = Person.new
|
2018-03-20 10:39:44 -04:00
|
|
|
person.errors.add(:name, :blank)
|
|
|
|
|
2018-04-02 12:17:45 -04:00
|
|
|
assert_equal :blank, person.errors.objects.first.type
|
2018-03-20 10:39:44 -04:00
|
|
|
assert_equal ["can't be blank"], person.errors[:name]
|
2015-05-04 08:36:26 -04:00
|
|
|
end
|
|
|
|
|
2018-03-20 10:39:44 -04:00
|
|
|
test "add, with type as String" do
|
|
|
|
msg = "custom msg"
|
|
|
|
|
2011-10-11 15:44:22 -04:00
|
|
|
person = Person.new
|
2018-03-20 10:39:44 -04:00
|
|
|
person.errors.add(:name, msg)
|
|
|
|
|
|
|
|
assert_equal [msg], person.errors[:name]
|
2011-10-11 15:44:22 -04:00
|
|
|
end
|
|
|
|
|
2018-03-20 10:39:44 -04:00
|
|
|
test "add, with type as nil" do
|
2011-10-11 15:44:22 -04:00
|
|
|
person = Person.new
|
2018-03-20 10:39:44 -04:00
|
|
|
person.errors.add(:name)
|
|
|
|
|
2018-04-02 12:17:45 -04:00
|
|
|
assert_equal :invalid, person.errors.objects.first.type
|
2018-03-20 10:39:44 -04:00
|
|
|
assert_equal ["is invalid"], person.errors[:name]
|
|
|
|
end
|
|
|
|
|
|
|
|
test "add, with type as Proc, which evaluates to String" do
|
|
|
|
msg = "custom msg"
|
|
|
|
type = Proc.new { msg }
|
|
|
|
|
|
|
|
person = Person.new
|
|
|
|
person.errors.add(:name, type)
|
|
|
|
|
|
|
|
assert_equal [msg], person.errors[:name]
|
|
|
|
end
|
|
|
|
|
|
|
|
test "add, type being Proc, which evaluates to Symbol" do
|
|
|
|
type = Proc.new { :blank }
|
|
|
|
|
|
|
|
person = Person.new
|
|
|
|
person.errors.add(:name, type)
|
|
|
|
|
2018-04-02 12:17:45 -04:00
|
|
|
assert_equal :blank, person.errors.objects.first.type
|
2018-03-20 10:39:44 -04:00
|
|
|
assert_equal ["can't be blank"], person.errors[:name]
|
|
|
|
end
|
|
|
|
|
|
|
|
test "initialize options[:message] as Proc, which evaluates to String" do
|
|
|
|
msg = "custom msg"
|
|
|
|
type = Proc.new { msg }
|
|
|
|
|
|
|
|
person = Person.new
|
|
|
|
person.errors.add(:name, :blank, message: type)
|
|
|
|
|
2018-04-02 12:17:45 -04:00
|
|
|
assert_equal :blank, person.errors.objects.first.type
|
2018-03-20 10:39:44 -04:00
|
|
|
assert_equal [msg], person.errors[:name]
|
|
|
|
end
|
|
|
|
|
|
|
|
test "add, with options[:message] as Proc, which evaluates to String, where type is nil" do
|
|
|
|
msg = "custom msg"
|
|
|
|
type = Proc.new { msg }
|
|
|
|
|
|
|
|
person = Person.new
|
|
|
|
person.errors.add(:name, message: type)
|
|
|
|
|
2018-04-02 12:17:45 -04:00
|
|
|
assert_equal :invalid, person.errors.objects.first.type
|
2018-03-20 10:39:44 -04:00
|
|
|
assert_equal [msg], person.errors[:name]
|
2011-10-11 15:44:22 -04:00
|
|
|
end
|
|
|
|
|
2019-07-10 11:52:43 -04:00
|
|
|
test "added? when attribute was added through a collection" do
|
|
|
|
person = Person.new
|
|
|
|
person.errors.add(:"family_members.name", :too_long, count: 25)
|
|
|
|
assert person.errors.added?(:"family_members.name", :too_long, count: 25)
|
|
|
|
assert_not person.errors.added?(:"family_members.name", :too_long)
|
|
|
|
assert_not person.errors.added?(:"family_members.name", :too_long, name: "hello")
|
|
|
|
end
|
|
|
|
|
|
|
|
test "added? ignores callback option" do
|
|
|
|
person = Person.new
|
|
|
|
|
|
|
|
person.errors.add(:name, :too_long, if: -> { true })
|
|
|
|
assert person.errors.added?(:name, :too_long)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "added? ignores message option" do
|
|
|
|
person = Person.new
|
|
|
|
|
|
|
|
person.errors.add(:name, :too_long, message: proc { "foo" })
|
|
|
|
assert person.errors.added?(:name, :too_long)
|
|
|
|
end
|
|
|
|
|
2016-02-14 11:41:59 -05:00
|
|
|
test "added? detects indifferent if a specific error was added to the object" do
|
2011-10-19 12:47:28 -04:00
|
|
|
person = Person.new
|
2014-01-03 17:02:31 -05:00
|
|
|
person.errors.add(:name, "cannot be blank")
|
|
|
|
assert person.errors.added?(:name, "cannot be blank")
|
2016-02-14 11:41:59 -05:00
|
|
|
assert person.errors.added?("name", "cannot be blank")
|
2011-10-19 12:47:28 -04:00
|
|
|
end
|
|
|
|
|
2013-03-30 14:04:08 -04:00
|
|
|
test "added? handles symbol message" do
|
2011-10-19 12:47:28 -04:00
|
|
|
person = Person.new
|
|
|
|
person.errors.add(:name, :blank)
|
|
|
|
assert person.errors.added?(:name, :blank)
|
|
|
|
end
|
|
|
|
|
2018-07-14 01:57:43 -04:00
|
|
|
test "added? returns true when string attribute is used with a symbol message" do
|
|
|
|
person = Person.new
|
|
|
|
person.errors.add(:name, :blank)
|
|
|
|
assert person.errors.added?("name", :blank)
|
|
|
|
end
|
|
|
|
|
2013-03-30 14:04:08 -04:00
|
|
|
test "added? handles proc messages" do
|
2011-10-19 12:47:28 -04:00
|
|
|
person = Person.new
|
2014-01-03 17:02:31 -05:00
|
|
|
message = Proc.new { "cannot be blank" }
|
2011-10-19 12:47:28 -04:00
|
|
|
person.errors.add(:name, message)
|
|
|
|
assert person.errors.added?(:name, message)
|
|
|
|
end
|
|
|
|
|
2013-03-30 14:04:08 -04:00
|
|
|
test "added? defaults message to :invalid" do
|
2011-10-19 12:47:28 -04:00
|
|
|
person = Person.new
|
2012-12-01 19:27:55 -05:00
|
|
|
person.errors.add(:name)
|
2011-10-19 12:47:28 -04:00
|
|
|
assert person.errors.added?(:name)
|
|
|
|
end
|
|
|
|
|
2013-03-30 14:04:08 -04:00
|
|
|
test "added? matches the given message when several errors are present for the same attribute" do
|
2011-10-19 12:47:28 -04:00
|
|
|
person = Person.new
|
2014-01-03 17:02:31 -05:00
|
|
|
person.errors.add(:name, "cannot be blank")
|
2011-10-19 12:47:28 -04:00
|
|
|
person.errors.add(:name, "is invalid")
|
2014-01-03 17:02:31 -05:00
|
|
|
assert person.errors.added?(:name, "cannot be blank")
|
2019-01-04 13:35:17 -05:00
|
|
|
assert person.errors.added?(:name, "is invalid")
|
|
|
|
assert_not person.errors.added?(:name, "incorrect")
|
2011-10-19 12:47:28 -04:00
|
|
|
end
|
|
|
|
|
2013-03-30 14:04:08 -04:00
|
|
|
test "added? returns false when no errors are present" do
|
2011-10-19 12:47:28 -04:00
|
|
|
person = Person.new
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not person.errors.added?(:name)
|
2011-10-19 12:47:28 -04:00
|
|
|
end
|
|
|
|
|
2013-03-30 14:04:08 -04:00
|
|
|
test "added? returns false when checking a nonexisting error and other errors are present for the given attribute" do
|
2011-10-19 12:47:28 -04:00
|
|
|
person = Person.new
|
|
|
|
person.errors.add(:name, "is invalid")
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not person.errors.added?(:name, "cannot be blank")
|
2011-10-19 12:47:28 -04:00
|
|
|
end
|
|
|
|
|
2019-01-04 13:35:17 -05:00
|
|
|
test "added? returns false when checking for an error, but not providing message argument" do
|
2016-06-21 11:18:02 -04:00
|
|
|
person = Person.new
|
|
|
|
person.errors.add(:name, "cannot be blank")
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not person.errors.added?(:name)
|
2016-06-21 11:18:02 -04:00
|
|
|
end
|
|
|
|
|
2018-11-09 10:45:18 -05:00
|
|
|
test "added? returns false when checking for an error with an incorrect or missing option" do
|
|
|
|
person = Person.new
|
|
|
|
person.errors.add :name, :too_long, count: 25
|
|
|
|
|
|
|
|
assert person.errors.added? :name, :too_long, count: 25
|
2019-01-04 13:35:17 -05:00
|
|
|
assert person.errors.added? :name, "is too long (maximum is 25 characters)"
|
2018-11-09 10:45:18 -05:00
|
|
|
assert_not person.errors.added? :name, :too_long, count: 24
|
|
|
|
assert_not person.errors.added? :name, :too_long
|
|
|
|
assert_not person.errors.added? :name, "is too long"
|
|
|
|
end
|
|
|
|
|
2017-11-10 03:38:05 -05:00
|
|
|
test "added? returns false when checking for an error by symbol and a different error with same message is present" do
|
|
|
|
I18n.backend.store_translations("en", errors: { attributes: { name: { wrong: "is wrong", used: "is wrong" } } })
|
|
|
|
person = Person.new
|
|
|
|
person.errors.add(:name, :wrong)
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not person.errors.added?(:name, :used)
|
2019-01-04 13:35:17 -05:00
|
|
|
assert person.errors.added?(:name, :wrong)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "of_kind? returns false when checking for an error, but not providing message argument" do
|
|
|
|
person = Person.new
|
|
|
|
person.errors.add(:name, "cannot be blank")
|
|
|
|
assert_not person.errors.of_kind?(:name)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "of_kind? returns false when checking a nonexisting error and other errors are present for the given attribute" do
|
|
|
|
person = Person.new
|
|
|
|
person.errors.add(:name, "is invalid")
|
|
|
|
assert_not person.errors.of_kind?(:name, "cannot be blank")
|
|
|
|
end
|
|
|
|
|
|
|
|
test "of_kind? returns false when no errors are present" do
|
|
|
|
person = Person.new
|
|
|
|
assert_not person.errors.of_kind?(:name)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "of_kind? matches the given message when several errors are present for the same attribute" do
|
|
|
|
person = Person.new
|
|
|
|
person.errors.add(:name, "cannot be blank")
|
|
|
|
person.errors.add(:name, "is invalid")
|
|
|
|
assert person.errors.of_kind?(:name, "cannot be blank")
|
|
|
|
assert person.errors.of_kind?(:name, "is invalid")
|
|
|
|
assert_not person.errors.of_kind?(:name, "incorrect")
|
|
|
|
end
|
|
|
|
|
|
|
|
test "of_kind? defaults message to :invalid" do
|
|
|
|
person = Person.new
|
|
|
|
person.errors.add(:name)
|
|
|
|
assert person.errors.of_kind?(:name)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "of_kind? handles proc messages" do
|
|
|
|
person = Person.new
|
|
|
|
message = Proc.new { "cannot be blank" }
|
|
|
|
person.errors.add(:name, message)
|
|
|
|
assert person.errors.of_kind?(:name, message)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "of_kind? returns true when string attribute is used with a symbol message" do
|
|
|
|
person = Person.new
|
|
|
|
person.errors.add(:name, :blank)
|
|
|
|
assert person.errors.of_kind?("name", :blank)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "of_kind? handles symbol message" do
|
|
|
|
person = Person.new
|
|
|
|
person.errors.add(:name, :blank)
|
|
|
|
assert person.errors.of_kind?(:name, :blank)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "of_kind? detects indifferent if a specific error was added to the object" do
|
|
|
|
person = Person.new
|
|
|
|
person.errors.add(:name, "cannot be blank")
|
|
|
|
assert person.errors.of_kind?(:name, "cannot be blank")
|
|
|
|
assert person.errors.of_kind?("name", "cannot be blank")
|
|
|
|
end
|
|
|
|
|
|
|
|
test "of_kind? ignores options" do
|
|
|
|
person = Person.new
|
|
|
|
person.errors.add :name, :too_long, count: 25
|
|
|
|
|
|
|
|
assert person.errors.of_kind? :name, :too_long
|
|
|
|
assert person.errors.of_kind? :name, "is too long (maximum is 25 characters)"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "of_kind? returns false when checking for an error by symbol and a different error with same message is present" do
|
|
|
|
I18n.backend.store_translations("en", errors: { attributes: { name: { wrong: "is wrong", used: "is wrong" } } })
|
|
|
|
person = Person.new
|
|
|
|
person.errors.add(:name, :wrong)
|
|
|
|
assert_not person.errors.of_kind?(:name, :used)
|
|
|
|
assert person.errors.of_kind?(:name, :wrong)
|
2017-11-10 03:38:05 -05:00
|
|
|
end
|
|
|
|
|
2013-03-30 14:04:08 -04:00
|
|
|
test "size calculates the number of error messages" do
|
2010-08-03 04:32:48 -04:00
|
|
|
person = Person.new
|
2014-01-03 17:02:31 -05:00
|
|
|
person.errors.add(:name, "cannot be blank")
|
2010-08-03 04:32:48 -04:00
|
|
|
assert_equal 1, person.errors.size
|
|
|
|
end
|
|
|
|
|
2015-02-20 15:31:24 -05:00
|
|
|
test "count calculates the number of error messages" do
|
|
|
|
person = Person.new
|
|
|
|
person.errors.add(:name, "cannot be blank")
|
|
|
|
assert_equal 1, person.errors.count
|
|
|
|
end
|
|
|
|
|
2013-03-30 14:04:08 -04:00
|
|
|
test "to_a returns the list of errors with complete messages containing the attribute names" do
|
2010-08-03 04:32:48 -04:00
|
|
|
person = Person.new
|
2014-01-03 17:02:31 -05:00
|
|
|
person.errors.add(:name, "cannot be blank")
|
|
|
|
person.errors.add(:name, "cannot be nil")
|
|
|
|
assert_equal ["name cannot be blank", "name cannot be nil"], person.errors.to_a
|
2010-08-03 04:32:48 -04:00
|
|
|
end
|
|
|
|
|
2019-07-22 08:02:15 -04:00
|
|
|
test "to_h is deprecated" do
|
|
|
|
person = Person.new
|
|
|
|
person.errors.add(:name, "cannot be blank")
|
2019-07-23 11:58:59 -04:00
|
|
|
person.errors.add(:name, "too long")
|
2019-07-22 08:02:15 -04:00
|
|
|
|
2019-07-23 11:58:59 -04:00
|
|
|
expected_deprecation = "ActiveModel::Errors#to_h is deprecated"
|
2019-07-22 08:02:15 -04:00
|
|
|
assert_deprecated(expected_deprecation) do
|
2019-07-23 11:58:59 -04:00
|
|
|
assert_equal({ name: "too long" }, person.errors.to_h)
|
2019-07-22 08:02:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-30 14:04:08 -04:00
|
|
|
test "to_hash returns the error messages hash" do
|
2010-11-28 08:36:40 -05:00
|
|
|
person = Person.new
|
2014-01-03 17:02:31 -05:00
|
|
|
person.errors.add(:name, "cannot be blank")
|
|
|
|
assert_equal({ name: ["cannot be blank"] }, person.errors.to_hash)
|
2010-11-28 08:36:40 -05:00
|
|
|
end
|
2011-09-09 04:28:25 -04:00
|
|
|
|
2016-09-27 08:33:16 -04:00
|
|
|
test "to_hash returns a hash without default proc" do
|
|
|
|
person = Person.new
|
|
|
|
assert_nil person.errors.to_hash.default_proc
|
|
|
|
end
|
|
|
|
|
|
|
|
test "as_json returns a hash without default proc" do
|
|
|
|
person = Person.new
|
|
|
|
assert_nil person.errors.as_json.default_proc
|
|
|
|
end
|
|
|
|
|
2019-07-09 08:40:57 -04:00
|
|
|
test "full_messages doesn't require the base object to respond to `:errors" do
|
|
|
|
model = Class.new do
|
|
|
|
def initialize
|
|
|
|
@errors = ActiveModel::Errors.new(self)
|
|
|
|
@errors.add(:name, "bar")
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.human_attribute_name(attr, options = {})
|
|
|
|
"foo"
|
|
|
|
end
|
|
|
|
|
|
|
|
def call
|
|
|
|
error_wrapper = Struct.new(:model_errors)
|
|
|
|
|
|
|
|
error_wrapper.new(@errors)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal(["foo bar"], model.new.call.model_errors.full_messages)
|
|
|
|
end
|
|
|
|
|
2013-03-30 14:04:08 -04:00
|
|
|
test "full_messages creates a list of error messages with the attribute name included" do
|
2011-09-09 04:28:25 -04:00
|
|
|
person = Person.new
|
2014-01-03 17:02:31 -05:00
|
|
|
person.errors.add(:name, "cannot be blank")
|
|
|
|
person.errors.add(:name, "cannot be nil")
|
|
|
|
assert_equal ["name cannot be blank", "name cannot be nil"], person.errors.full_messages
|
2011-09-09 04:28:25 -04:00
|
|
|
end
|
2013-03-30 14:04:08 -04:00
|
|
|
|
2016-02-14 11:41:59 -05:00
|
|
|
test "full_messages_for contains all the error messages for the given attribute indifferent" do
|
2013-03-24 03:34:27 -04:00
|
|
|
person = Person.new
|
2014-01-03 17:02:31 -05:00
|
|
|
person.errors.add(:name, "cannot be blank")
|
|
|
|
person.errors.add(:name, "cannot be nil")
|
|
|
|
assert_equal ["name cannot be blank", "name cannot be nil"], person.errors.full_messages_for(:name)
|
2013-03-24 03:34:27 -04:00
|
|
|
end
|
|
|
|
|
2013-03-30 14:04:08 -04:00
|
|
|
test "full_messages_for does not contain error messages from other attributes" do
|
2013-03-24 03:34:27 -04:00
|
|
|
person = Person.new
|
2014-01-03 17:02:31 -05:00
|
|
|
person.errors.add(:name, "cannot be blank")
|
|
|
|
person.errors.add(:email, "cannot be blank")
|
|
|
|
assert_equal ["name cannot be blank"], person.errors.full_messages_for(:name)
|
2016-02-14 11:41:59 -05:00
|
|
|
assert_equal ["name cannot be blank"], person.errors.full_messages_for("name")
|
2013-03-24 03:34:27 -04:00
|
|
|
end
|
|
|
|
|
2013-03-30 14:04:08 -04:00
|
|
|
test "full_messages_for returns an empty list in case there are no errors for the given attribute" do
|
2013-03-24 03:34:27 -04:00
|
|
|
person = Person.new
|
2014-01-03 17:02:31 -05:00
|
|
|
person.errors.add(:name, "cannot be blank")
|
2013-03-24 03:34:27 -04:00
|
|
|
assert_equal [], person.errors.full_messages_for(:email)
|
|
|
|
end
|
2011-09-09 04:28:25 -04:00
|
|
|
|
2013-03-30 14:04:08 -04:00
|
|
|
test "full_message returns the given message when attribute is :base" do
|
2011-09-09 04:28:25 -04:00
|
|
|
person = Person.new
|
|
|
|
assert_equal "press the button", person.errors.full_message(:base, "press the button")
|
|
|
|
end
|
|
|
|
|
2013-03-30 14:04:08 -04:00
|
|
|
test "full_message returns the given message with the attribute name included" do
|
2011-09-09 04:28:25 -04:00
|
|
|
person = Person.new
|
2014-01-03 17:02:31 -05:00
|
|
|
assert_equal "name cannot be blank", person.errors.full_message(:name, "cannot be blank")
|
|
|
|
assert_equal "name_test cannot be blank", person.errors.full_message(:name_test, "cannot be blank")
|
2011-09-09 04:28:25 -04:00
|
|
|
end
|
|
|
|
|
2013-03-30 14:04:08 -04:00
|
|
|
test "as_json creates a json formatted representation of the errors hash" do
|
2011-09-09 04:34:13 -04:00
|
|
|
person = Person.new
|
2013-03-30 14:04:08 -04:00
|
|
|
person.validate!
|
|
|
|
|
2014-01-03 17:02:31 -05:00
|
|
|
assert_equal({ name: ["cannot be nil"] }, person.errors.as_json)
|
2011-09-09 04:34:13 -04:00
|
|
|
end
|
|
|
|
|
2013-03-30 14:04:08 -04:00
|
|
|
test "as_json with :full_messages option creates a json formatted representation of the errors containing complete messages" do
|
2012-02-17 05:12:10 -05:00
|
|
|
person = Person.new
|
2013-03-30 14:04:08 -04:00
|
|
|
person.validate!
|
|
|
|
|
2014-01-03 17:02:31 -05:00
|
|
|
assert_equal({ name: ["name cannot be nil"] }, person.errors.as_json(full_messages: true))
|
2012-02-17 05:12:10 -05:00
|
|
|
end
|
|
|
|
|
2013-03-30 14:04:08 -04:00
|
|
|
test "generate_message works without i18n_scope" do
|
2011-10-11 15:44:22 -04:00
|
|
|
person = Person.new
|
2018-01-24 21:14:10 -05:00
|
|
|
assert_not_respond_to Person, :i18n_scope
|
2011-10-11 15:44:22 -04:00
|
|
|
assert_nothing_raised {
|
|
|
|
person.errors.generate_message(:name, :blank)
|
|
|
|
}
|
|
|
|
end
|
2012-11-05 10:29:31 -05:00
|
|
|
|
2015-01-04 03:18:03 -05:00
|
|
|
test "details returns added error detail" do
|
|
|
|
person = Person.new
|
|
|
|
person.errors.add(:name, :invalid)
|
|
|
|
assert_equal({ name: [{ error: :invalid }] }, person.errors.details)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "details returns added error detail with custom option" do
|
|
|
|
person = Person.new
|
|
|
|
person.errors.add(:name, :greater_than, count: 5)
|
|
|
|
assert_equal({ name: [{ error: :greater_than, count: 5 }] }, person.errors.details)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "details do not include message option" do
|
|
|
|
person = Person.new
|
|
|
|
person.errors.add(:name, :invalid, message: "is bad")
|
|
|
|
assert_equal({ name: [{ error: :invalid }] }, person.errors.details)
|
|
|
|
end
|
|
|
|
|
2019-03-17 06:07:47 -04:00
|
|
|
test "details retains original type as error" do
|
|
|
|
errors = ActiveModel::Errors.new(Person.new)
|
|
|
|
errors.add(:name, "cannot be nil")
|
|
|
|
errors.add("foo", "bar")
|
|
|
|
errors.add(:baz, nil)
|
|
|
|
errors.add(:age, :invalid, count: 3, message: "%{count} is too low")
|
|
|
|
|
|
|
|
assert_equal(
|
|
|
|
{
|
|
|
|
name: [{ error: "cannot be nil" }],
|
|
|
|
foo: [{ error: "bar" }],
|
|
|
|
baz: [{ error: nil }],
|
|
|
|
age: [{ error: :invalid, count: 3 }]
|
|
|
|
},
|
|
|
|
errors.details
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-08-29 22:41:39 -04:00
|
|
|
test "messages delete (deprecated)" do
|
|
|
|
person = Person.new
|
|
|
|
person.validate!
|
|
|
|
|
|
|
|
assert_equal 1, person.errors.count
|
|
|
|
assert_deprecated { person.errors.messages.delete(:name) }
|
|
|
|
assert_empty person.errors
|
|
|
|
end
|
|
|
|
|
2018-03-26 01:09:59 -04:00
|
|
|
test "group_by_attribute" do
|
|
|
|
person = Person.new
|
|
|
|
error = person.errors.add(:name, :invalid, message: "is bad")
|
|
|
|
hash = person.errors.group_by_attribute
|
|
|
|
|
|
|
|
assert_equal({ name: [error] }, hash)
|
|
|
|
end
|
|
|
|
|
2015-01-04 03:18:03 -05:00
|
|
|
test "dup duplicates details" do
|
|
|
|
errors = ActiveModel::Errors.new(Person.new)
|
|
|
|
errors.add(:name, :invalid)
|
|
|
|
errors_dup = errors.dup
|
|
|
|
errors_dup.add(:name, :taken)
|
2015-01-24 05:58:52 -05:00
|
|
|
assert_not_equal errors_dup.details, errors.details
|
2015-01-04 03:18:03 -05:00
|
|
|
end
|
|
|
|
|
2019-07-10 19:41:14 -04:00
|
|
|
test "delete returns nil when no errors were deleted" do
|
|
|
|
errors = ActiveModel::Errors.new(Person.new)
|
|
|
|
|
|
|
|
assert_nil(errors.delete(:name))
|
|
|
|
end
|
|
|
|
|
2015-01-04 03:18:03 -05:00
|
|
|
test "delete removes details on given attribute" do
|
|
|
|
errors = ActiveModel::Errors.new(Person.new)
|
|
|
|
errors.add(:name, :invalid)
|
|
|
|
errors.delete(:name)
|
2018-03-20 10:39:44 -04:00
|
|
|
assert_not errors.added?(:name)
|
2015-01-04 03:18:03 -05:00
|
|
|
end
|
|
|
|
|
2015-03-22 18:10:28 -04:00
|
|
|
test "delete returns the deleted messages" do
|
|
|
|
errors = ActiveModel::Errors.new(Person.new)
|
|
|
|
errors.add(:name, :invalid)
|
2015-03-22 19:25:07 -04:00
|
|
|
assert_equal ["is invalid"], errors.delete(:name)
|
2015-03-22 18:10:28 -04:00
|
|
|
end
|
|
|
|
|
2015-01-04 03:18:03 -05:00
|
|
|
test "clear removes details" do
|
|
|
|
person = Person.new
|
|
|
|
person.errors.add(:name, :invalid)
|
|
|
|
|
|
|
|
assert_equal 1, person.errors.details.count
|
|
|
|
person.errors.clear
|
2018-01-25 18:16:57 -05:00
|
|
|
assert_empty person.errors.details
|
2015-01-04 03:18:03 -05:00
|
|
|
end
|
2015-12-03 13:18:57 -05:00
|
|
|
|
2018-03-31 10:42:40 -04:00
|
|
|
test "copy errors (deprecated)" do
|
|
|
|
errors = ActiveModel::Errors.new(Person.new)
|
|
|
|
errors.add(:name, :invalid)
|
|
|
|
person = Person.new
|
|
|
|
person.errors.copy!(errors)
|
|
|
|
|
|
|
|
assert_equal [:name], person.errors.messages.keys
|
|
|
|
assert_equal [:name], person.errors.details.keys
|
|
|
|
end
|
|
|
|
|
2019-03-17 03:57:00 -04:00
|
|
|
test "details returns empty array when accessed with non-existent attribute" do
|
|
|
|
errors = ActiveModel::Errors.new(Person.new)
|
|
|
|
|
|
|
|
assert_equal [], errors.details[:foo]
|
|
|
|
end
|
|
|
|
|
2015-12-03 13:18:57 -05:00
|
|
|
test "copy errors" do
|
|
|
|
errors = ActiveModel::Errors.new(Person.new)
|
|
|
|
errors.add(:name, :invalid)
|
|
|
|
person = Person.new
|
|
|
|
person.errors.copy!(errors)
|
|
|
|
|
2018-03-20 10:39:44 -04:00
|
|
|
assert person.errors.added?(:name, :invalid)
|
|
|
|
person.errors.each do |error|
|
|
|
|
assert_same person, error.base
|
|
|
|
end
|
2015-12-03 13:18:57 -05:00
|
|
|
end
|
2016-05-30 14:00:14 -04:00
|
|
|
|
2018-03-31 10:42:40 -04:00
|
|
|
test "merge errors (deprecated)" do
|
|
|
|
errors = ActiveModel::Errors.new(Person.new)
|
|
|
|
errors.add(:name, :invalid)
|
|
|
|
|
|
|
|
person = Person.new
|
|
|
|
person.errors.add(:name, :blank)
|
|
|
|
person.errors.merge!(errors)
|
|
|
|
|
|
|
|
assert_equal({ name: ["can't be blank", "is invalid"] }, person.errors.messages)
|
|
|
|
assert_equal({ name: [{ error: :blank }, { error: :invalid }] }, person.errors.details)
|
|
|
|
end
|
|
|
|
|
2017-07-07 13:16:06 -04:00
|
|
|
test "merge errors" do
|
|
|
|
errors = ActiveModel::Errors.new(Person.new)
|
|
|
|
errors.add(:name, :invalid)
|
|
|
|
|
|
|
|
person = Person.new
|
|
|
|
person.errors.add(:name, :blank)
|
|
|
|
person.errors.merge!(errors)
|
|
|
|
|
2018-03-20 10:39:44 -04:00
|
|
|
assert(person.errors.added?(:name, :invalid))
|
|
|
|
assert(person.errors.added?(:name, :blank))
|
2017-07-07 13:16:06 -04:00
|
|
|
end
|
|
|
|
|
2018-11-20 18:54:22 -05:00
|
|
|
test "slice! removes all errors except the given keys" do
|
|
|
|
person = Person.new
|
|
|
|
person.errors.add(:name, "cannot be nil")
|
|
|
|
person.errors.add(:age, "cannot be nil")
|
|
|
|
person.errors.add(:gender, "cannot be nil")
|
|
|
|
person.errors.add(:city, "cannot be nil")
|
|
|
|
|
2018-12-22 07:25:02 -05:00
|
|
|
assert_deprecated { person.errors.slice!(:age, "gender") }
|
2018-11-20 18:54:22 -05:00
|
|
|
|
2018-12-22 07:25:02 -05:00
|
|
|
assert_equal [:age, :gender], assert_deprecated { person.errors.keys }
|
2018-11-20 18:54:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "slice! returns the deleted errors" do
|
|
|
|
person = Person.new
|
|
|
|
person.errors.add(:name, "cannot be nil")
|
|
|
|
person.errors.add(:age, "cannot be nil")
|
|
|
|
person.errors.add(:gender, "cannot be nil")
|
|
|
|
person.errors.add(:city, "cannot be nil")
|
|
|
|
|
2018-12-22 07:25:02 -05:00
|
|
|
removed_errors = assert_deprecated { person.errors.slice!(:age, "gender") }
|
2018-11-20 18:54:22 -05:00
|
|
|
|
|
|
|
assert_equal({ name: ["cannot be nil"], city: ["cannot be nil"] }, removed_errors)
|
|
|
|
end
|
|
|
|
|
2016-05-30 14:00:14 -04:00
|
|
|
test "errors are marshalable" do
|
|
|
|
errors = ActiveModel::Errors.new(Person.new)
|
|
|
|
errors.add(:name, :invalid)
|
|
|
|
serialized = Marshal.load(Marshal.dump(errors))
|
|
|
|
|
2018-03-20 10:39:44 -04:00
|
|
|
assert_equal Person, serialized.instance_variable_get(:@base).class
|
2016-05-30 14:00:14 -04:00
|
|
|
assert_equal errors.messages, serialized.messages
|
|
|
|
assert_equal errors.details, serialized.details
|
|
|
|
end
|
2016-12-08 16:20:07 -05:00
|
|
|
|
2018-03-20 10:39:44 -04:00
|
|
|
test "errors are compatible with marshal dumped from Rails 5.x" do
|
|
|
|
# Derived from
|
|
|
|
# errors = ActiveModel::Errors.new(Person.new)
|
|
|
|
# errors.add(:name, :invalid)
|
|
|
|
dump = "\x04\bU:\x18ActiveModel::Errors[\bo:\x17ErrorsTest::Person\x06:\f@errorsU;\x00[\b@\a{\x00{\x00{\x06:\tname[\x06I\"\x0Fis invalid\x06:\x06ET{\x06;\b[\x06{\x06:\nerror:\finvalid"
|
|
|
|
serialized = Marshal.load(dump)
|
|
|
|
|
|
|
|
assert_equal Person, serialized.instance_variable_get(:@base).class
|
|
|
|
assert_equal({ name: ["is invalid"] }, serialized.messages)
|
|
|
|
assert_equal({ name: [{ error: :invalid }] }, serialized.details)
|
|
|
|
end
|
|
|
|
|
2016-12-08 16:20:07 -05:00
|
|
|
test "errors are backward compatible with the Rails 4.2 format" do
|
2018-02-16 19:28:30 -05:00
|
|
|
yaml = <<~CODE
|
2016-12-08 16:20:07 -05:00
|
|
|
--- !ruby/object:ActiveModel::Errors
|
|
|
|
base: &1 !ruby/object:ErrorsTest::Person
|
|
|
|
errors: !ruby/object:ActiveModel::Errors
|
|
|
|
base: *1
|
|
|
|
messages: {}
|
|
|
|
messages: {}
|
|
|
|
CODE
|
|
|
|
|
|
|
|
errors = YAML.load(yaml)
|
|
|
|
errors.add(:name, :invalid)
|
|
|
|
assert_equal({ name: ["is invalid"] }, errors.messages)
|
|
|
|
assert_equal({ name: [{ error: :invalid }] }, errors.details)
|
|
|
|
|
|
|
|
errors.clear
|
|
|
|
assert_equal({}, errors.messages)
|
|
|
|
assert_equal({}, errors.details)
|
|
|
|
end
|
2018-03-20 10:39:44 -04:00
|
|
|
|
|
|
|
test "errors are compatible with YAML dumped from Rails 5.x" do
|
|
|
|
yaml = <<~CODE
|
|
|
|
--- !ruby/object:ActiveModel::Errors
|
|
|
|
base: &1 !ruby/object:ErrorsTest::Person
|
|
|
|
errors: !ruby/object:ActiveModel::Errors
|
|
|
|
base: *1
|
|
|
|
messages: {}
|
|
|
|
details: {}
|
|
|
|
messages:
|
|
|
|
:name:
|
|
|
|
- is invalid
|
|
|
|
details:
|
|
|
|
:name:
|
|
|
|
- :error: :invalid
|
|
|
|
CODE
|
|
|
|
|
|
|
|
errors = YAML.load(yaml)
|
|
|
|
assert_equal({ name: ["is invalid"] }, errors.messages)
|
|
|
|
assert_equal({ name: [{ error: :invalid }] }, errors.details)
|
|
|
|
|
|
|
|
errors.clear
|
|
|
|
assert_equal({}, errors.messages)
|
|
|
|
assert_equal({}, errors.details)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "errors are compatible with YAML dumped from Rails 6.x" do
|
|
|
|
yaml = <<~CODE
|
|
|
|
--- !ruby/object:ActiveModel::Errors
|
|
|
|
base: &1 !ruby/object:ErrorsTest::Person
|
|
|
|
errors: !ruby/object:ActiveModel::Errors
|
|
|
|
base: *1
|
|
|
|
errors: []
|
|
|
|
errors:
|
|
|
|
- !ruby/object:ActiveModel::Error
|
|
|
|
base: *1
|
|
|
|
attribute: :name
|
|
|
|
type: :invalid
|
|
|
|
raw_type: :invalid
|
|
|
|
options: {}
|
|
|
|
CODE
|
|
|
|
|
|
|
|
errors = YAML.load(yaml)
|
|
|
|
assert_equal({ name: ["is invalid"] }, errors.messages)
|
|
|
|
assert_equal({ name: [{ error: :invalid }] }, errors.details)
|
|
|
|
|
|
|
|
errors.clear
|
|
|
|
assert_equal({}, errors.messages)
|
|
|
|
assert_equal({}, errors.details)
|
|
|
|
end
|
2010-08-03 04:32:48 -04:00
|
|
|
end
|