2016-08-06 12:38:23 -04:00
|
|
|
require "cases/helper"
|
2009-08-09 03:29:34 -04:00
|
|
|
|
2016-08-06 12:38:23 -04:00
|
|
|
require "models/topic"
|
2009-08-09 03:29:34 -04:00
|
|
|
|
2010-05-08 16:27:49 -04:00
|
|
|
class ValidatesWithTest < ActiveModel::TestCase
|
2009-12-23 07:30:58 -05:00
|
|
|
def teardown
|
2014-01-27 05:22:26 -05:00
|
|
|
Topic.clear_validators!
|
2009-12-23 07:30:58 -05:00
|
|
|
end
|
2009-08-09 03:29:34 -04:00
|
|
|
|
|
|
|
ERROR_MESSAGE = "Validation error from validator"
|
|
|
|
OTHER_ERROR_MESSAGE = "Validation error from other validator"
|
|
|
|
|
2009-10-20 20:20:01 -04:00
|
|
|
class ValidatorThatAddsErrors < ActiveModel::Validator
|
2009-12-22 17:12:21 -05:00
|
|
|
def validate(record)
|
2009-08-09 03:29:34 -04:00
|
|
|
record.errors[:base] << ERROR_MESSAGE
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-10-20 20:20:01 -04:00
|
|
|
class OtherValidatorThatAddsErrors < ActiveModel::Validator
|
2009-12-22 17:12:21 -05:00
|
|
|
def validate(record)
|
2009-08-09 03:29:34 -04:00
|
|
|
record.errors[:base] << OTHER_ERROR_MESSAGE
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-10-20 20:20:01 -04:00
|
|
|
class ValidatorThatDoesNotAddErrors < ActiveModel::Validator
|
2009-12-22 17:12:21 -05:00
|
|
|
def validate(record)
|
2009-08-09 03:29:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-10-20 20:20:01 -04:00
|
|
|
class ValidatorThatValidatesOptions < ActiveModel::Validator
|
2009-12-22 17:12:21 -05:00
|
|
|
def validate(record)
|
2009-08-09 03:29:34 -04:00
|
|
|
if options[:field] == :first_name
|
|
|
|
record.errors[:base] << ERROR_MESSAGE
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-03 10:58:33 -05:00
|
|
|
class ValidatorPerEachAttribute < ActiveModel::EachValidator
|
|
|
|
def validate_each(record, attribute, value)
|
|
|
|
record.errors[attribute] << "Value is #{value}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class ValidatorCheckValidity < ActiveModel::EachValidator
|
|
|
|
def check_validity!
|
|
|
|
raise "boom!"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-18 07:50:05 -04:00
|
|
|
test "validation with class that adds errors" do
|
2009-08-09 03:29:34 -04:00
|
|
|
Topic.validates_with(ValidatorThatAddsErrors)
|
|
|
|
topic = Topic.new
|
2010-05-08 16:27:49 -04:00
|
|
|
assert topic.invalid?, "A class that adds errors causes the record to be invalid"
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes topic.errors[:base], ERROR_MESSAGE
|
2009-08-09 03:29:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "with a class that returns valid" do
|
|
|
|
Topic.validates_with(ValidatorThatDoesNotAddErrors)
|
|
|
|
topic = Topic.new
|
|
|
|
assert topic.valid?, "A class that does not add errors does not cause the record to be invalid"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with multiple classes" do
|
|
|
|
Topic.validates_with(ValidatorThatAddsErrors, OtherValidatorThatAddsErrors)
|
|
|
|
topic = Topic.new
|
2010-05-08 16:27:49 -04:00
|
|
|
assert topic.invalid?
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes topic.errors[:base], ERROR_MESSAGE
|
|
|
|
assert_includes topic.errors[:base], OTHER_ERROR_MESSAGE
|
2009-08-09 03:29:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "with if statements that return false" do
|
2013-05-01 20:10:06 -04:00
|
|
|
Topic.validates_with(ValidatorThatAddsErrors, if: "1 == 2")
|
2009-08-09 03:29:34 -04:00
|
|
|
topic = Topic.new
|
|
|
|
assert topic.valid?
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with if statements that return true" do
|
2013-05-01 20:10:06 -04:00
|
|
|
Topic.validates_with(ValidatorThatAddsErrors, if: "1 == 1")
|
2009-08-09 03:29:34 -04:00
|
|
|
topic = Topic.new
|
2010-05-08 16:27:49 -04:00
|
|
|
assert topic.invalid?
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes topic.errors[:base], ERROR_MESSAGE
|
2009-08-09 03:29:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "with unless statements that return true" do
|
2013-05-01 20:10:06 -04:00
|
|
|
Topic.validates_with(ValidatorThatAddsErrors, unless: "1 == 1")
|
2009-08-09 03:29:34 -04:00
|
|
|
topic = Topic.new
|
|
|
|
assert topic.valid?
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with unless statements that returns false" do
|
2013-05-01 20:10:06 -04:00
|
|
|
Topic.validates_with(ValidatorThatAddsErrors, unless: "1 == 2")
|
2009-08-09 03:29:34 -04:00
|
|
|
topic = Topic.new
|
2010-05-08 16:27:49 -04:00
|
|
|
assert topic.invalid?
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes topic.errors[:base], ERROR_MESSAGE
|
2009-08-09 03:29:34 -04:00
|
|
|
end
|
|
|
|
|
2009-12-22 18:36:51 -05:00
|
|
|
test "passes all configuration options to the validator class" do
|
2009-08-09 03:29:34 -04:00
|
|
|
topic = Topic.new
|
2015-07-10 17:42:18 -04:00
|
|
|
validator = Minitest::Mock.new
|
2016-08-16 03:30:11 -04:00
|
|
|
validator.expect(:new, validator, [{ foo: :bar, if: "1 == 1", class: Topic }])
|
2015-05-28 19:33:21 -04:00
|
|
|
validator.expect(:validate, nil, [topic])
|
|
|
|
validator.expect(:is_a?, false, [Symbol])
|
2015-12-15 07:12:16 -05:00
|
|
|
validator.expect(:is_a?, false, [String])
|
2009-08-09 03:29:34 -04:00
|
|
|
|
2013-05-01 20:10:06 -04:00
|
|
|
Topic.validates_with(validator, if: "1 == 1", foo: :bar)
|
2009-08-09 03:29:34 -04:00
|
|
|
assert topic.valid?
|
2015-05-28 19:33:21 -04:00
|
|
|
validator.verify
|
2009-08-09 03:29:34 -04:00
|
|
|
end
|
2010-05-08 16:27:49 -04:00
|
|
|
|
2009-08-09 03:29:34 -04:00
|
|
|
test "validates_with with options" do
|
2013-05-01 20:10:06 -04:00
|
|
|
Topic.validates_with(ValidatorThatValidatesOptions, field: :first_name)
|
2009-08-09 03:29:34 -04:00
|
|
|
topic = Topic.new
|
2010-05-08 16:27:49 -04:00
|
|
|
assert topic.invalid?
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes topic.errors[:base], ERROR_MESSAGE
|
2009-08-09 03:29:34 -04:00
|
|
|
end
|
|
|
|
|
2010-01-03 10:58:33 -05:00
|
|
|
test "validates_with each validator" do
|
2013-05-01 20:10:06 -04:00
|
|
|
Topic.validates_with(ValidatorPerEachAttribute, attributes: [:title, :content])
|
|
|
|
topic = Topic.new title: "Title", content: "Content"
|
2010-05-08 16:27:49 -04:00
|
|
|
assert topic.invalid?
|
2010-01-03 10:58:33 -05:00
|
|
|
assert_equal ["Value is Title"], topic.errors[:title]
|
|
|
|
assert_equal ["Value is Content"], topic.errors[:content]
|
|
|
|
end
|
|
|
|
|
|
|
|
test "each validator checks validity" do
|
|
|
|
assert_raise RuntimeError do
|
2013-05-01 20:10:06 -04:00
|
|
|
Topic.validates_with(ValidatorCheckValidity, attributes: [:title])
|
2010-01-03 10:58:33 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "each validator expects attributes to be given" do
|
2012-11-04 08:41:05 -05:00
|
|
|
assert_raise ArgumentError do
|
2010-01-03 10:58:33 -05:00
|
|
|
Topic.validates_with(ValidatorPerEachAttribute)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "each validator skip nil values if :allow_nil is set to true" do
|
2013-05-01 20:10:06 -04:00
|
|
|
Topic.validates_with(ValidatorPerEachAttribute, attributes: [:title, :content], allow_nil: true)
|
|
|
|
topic = Topic.new content: ""
|
2010-05-08 16:27:49 -04:00
|
|
|
assert topic.invalid?
|
2010-01-03 10:58:33 -05:00
|
|
|
assert topic.errors[:title].empty?
|
|
|
|
assert_equal ["Value is "], topic.errors[:content]
|
|
|
|
end
|
|
|
|
|
|
|
|
test "each validator skip blank values if :allow_blank is set to true" do
|
2013-05-01 20:10:06 -04:00
|
|
|
Topic.validates_with(ValidatorPerEachAttribute, attributes: [:title, :content], allow_blank: true)
|
|
|
|
topic = Topic.new content: ""
|
2010-01-03 10:58:33 -05:00
|
|
|
assert topic.valid?
|
|
|
|
assert topic.errors[:title].empty?
|
|
|
|
assert topic.errors[:content].empty?
|
|
|
|
end
|
2011-02-05 19:33:00 -05:00
|
|
|
|
|
|
|
test "validates_with can validate with an instance method" do
|
2013-05-01 20:10:06 -04:00
|
|
|
Topic.validates :title, with: :my_validation
|
2011-02-05 19:33:00 -05:00
|
|
|
|
2013-05-01 20:10:06 -04:00
|
|
|
topic = Topic.new title: "foo"
|
2011-02-05 19:33:00 -05:00
|
|
|
assert topic.valid?
|
|
|
|
assert topic.errors[:title].empty?
|
|
|
|
|
|
|
|
topic = Topic.new
|
|
|
|
assert !topic.valid?
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal ["is missing"], topic.errors[:title]
|
2011-02-05 19:33:00 -05:00
|
|
|
end
|
2011-02-05 19:44:35 -05:00
|
|
|
|
|
|
|
test "optionally pass in the attribute being validated when validating with an instance method" do
|
2013-05-01 20:10:06 -04:00
|
|
|
Topic.validates :title, :content, with: :my_validation_with_arg
|
2011-02-05 19:44:35 -05:00
|
|
|
|
2013-05-01 20:10:06 -04:00
|
|
|
topic = Topic.new title: "foo"
|
2011-02-05 19:44:35 -05:00
|
|
|
assert !topic.valid?
|
|
|
|
assert topic.errors[:title].empty?
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal ["is missing"], topic.errors[:content]
|
2011-02-05 19:44:35 -05:00
|
|
|
end
|
2009-08-09 03:29:34 -04:00
|
|
|
end
|