mirror of https://github.com/rails/rails
Merge pull request #31061 from bogdanvlviv/test-if-unless-options-for-validations
Add cases to test combining validation conditions
This commit is contained in:
commit
2e585e4040
|
@ -18,6 +18,22 @@ class ConditionalValidationTest < ActiveModel::TestCase
|
|||
assert_equal ["hoo 5"], t.errors["title"]
|
||||
end
|
||||
|
||||
def test_if_validation_using_array_of_true_methods
|
||||
Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}", if: [:condition_is_true, :condition_is_true])
|
||||
t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
|
||||
assert t.invalid?
|
||||
assert t.errors[:title].any?
|
||||
assert_equal ["hoo 5"], t.errors["title"]
|
||||
end
|
||||
|
||||
def test_unless_validation_using_array_of_false_methods
|
||||
Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}", unless: [:condition_is_false, :condition_is_false])
|
||||
t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
|
||||
assert t.invalid?
|
||||
assert t.errors[:title].any?
|
||||
assert_equal ["hoo 5"], t.errors["title"]
|
||||
end
|
||||
|
||||
def test_unless_validation_using_method_true
|
||||
# When the method returns true
|
||||
Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}", unless: :condition_is_true)
|
||||
|
@ -26,9 +42,23 @@ class ConditionalValidationTest < ActiveModel::TestCase
|
|||
assert_empty t.errors[:title]
|
||||
end
|
||||
|
||||
def test_if_validation_using_array_of_true_and_false_methods
|
||||
Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}", if: [:condition_is_true, :condition_is_false])
|
||||
t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
|
||||
assert t.valid?
|
||||
assert_empty t.errors[:title]
|
||||
end
|
||||
|
||||
def test_unless_validation_using_array_of_true_and_felse_methods
|
||||
Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}", unless: [:condition_is_true, :condition_is_false])
|
||||
t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
|
||||
assert t.valid?
|
||||
assert_empty t.errors[:title]
|
||||
end
|
||||
|
||||
def test_if_validation_using_method_false
|
||||
# When the method returns false
|
||||
Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}", if: :condition_is_true_but_its_not)
|
||||
Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}", if: :condition_is_false)
|
||||
t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
|
||||
assert t.valid?
|
||||
assert_empty t.errors[:title]
|
||||
|
@ -36,7 +66,7 @@ class ConditionalValidationTest < ActiveModel::TestCase
|
|||
|
||||
def test_unless_validation_using_method_false
|
||||
# When the method returns false
|
||||
Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}", unless: :condition_is_true_but_its_not)
|
||||
Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}", unless: :condition_is_false)
|
||||
t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
|
||||
assert t.invalid?
|
||||
assert t.errors[:title].any?
|
||||
|
@ -80,4 +110,19 @@ class ConditionalValidationTest < ActiveModel::TestCase
|
|||
assert t.errors[:title].any?
|
||||
assert_equal ["hoo 5"], t.errors["title"]
|
||||
end
|
||||
|
||||
def test_validation_using_conbining_if_true_and_unless_true_conditions
|
||||
Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}", if: :condition_is_true, unless: :condition_is_true)
|
||||
t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
|
||||
assert t.valid?
|
||||
assert_empty t.errors[:title]
|
||||
end
|
||||
|
||||
def test_validation_using_conbining_if_true_and_unless_false_conditions
|
||||
Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}", if: :condition_is_true, unless: :condition_is_false)
|
||||
t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
|
||||
assert t.invalid?
|
||||
assert t.errors[:title].any?
|
||||
assert_equal ["hoo 5"], t.errors["title"]
|
||||
end
|
||||
end
|
||||
|
|
|
@ -59,7 +59,7 @@ class NumericalityValidationTest < ActiveModel::TestCase
|
|||
end
|
||||
|
||||
def test_validates_numericality_of_with_integer_only_and_symbol_as_value
|
||||
Topic.validates_numericality_of :approved, only_integer: :condition_is_true_but_its_not
|
||||
Topic.validates_numericality_of :approved, only_integer: :condition_is_false
|
||||
|
||||
invalid!(NIL + BLANK + JUNK)
|
||||
valid!(FLOATS + INTEGERS + BIGDECIMAL + INFINITY)
|
||||
|
|
|
@ -62,17 +62,23 @@ class ValidatesTest < ActiveModel::TestCase
|
|||
end
|
||||
|
||||
def test_validates_with_if_as_local_conditions
|
||||
Person.validates :karma, presence: true, email: { unless: :condition_is_true }
|
||||
Person.validates :karma, presence: true, email: { if: :condition_is_false }
|
||||
person = Person.new
|
||||
person.valid?
|
||||
assert_equal ["can't be blank"], person.errors[:karma]
|
||||
end
|
||||
|
||||
def test_validates_with_if_as_shared_conditions
|
||||
Person.validates :karma, presence: true, email: true, if: :condition_is_true
|
||||
Person.validates :karma, presence: true, email: true, if: :condition_is_false
|
||||
person = Person.new
|
||||
assert person.valid?
|
||||
end
|
||||
|
||||
def test_validates_with_unless_as_local_conditions
|
||||
Person.validates :karma, presence: true, email: { unless: :condition_is_true }
|
||||
person = Person.new
|
||||
person.valid?
|
||||
assert_equal ["can't be blank", "is not an email"], person.errors[:karma].sort
|
||||
assert_equal ["can't be blank"], person.errors[:karma]
|
||||
end
|
||||
|
||||
def test_validates_with_unless_shared_conditions
|
||||
|
|
|
@ -9,6 +9,10 @@ class Person
|
|||
def condition_is_true
|
||||
true
|
||||
end
|
||||
|
||||
def condition_is_false
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
class Person::Gender
|
||||
|
|
|
@ -23,7 +23,7 @@ class Topic
|
|||
true
|
||||
end
|
||||
|
||||
def condition_is_true_but_its_not
|
||||
def condition_is_false
|
||||
false
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in New Issue