2017-07-16 13:11:16 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:38:23 -04:00
|
|
|
require "cases/helper"
|
2009-03-20 11:07:49 -04:00
|
|
|
|
2016-08-06 12:38:23 -04:00
|
|
|
require "models/topic"
|
2009-03-20 11:07:49 -04:00
|
|
|
|
2019-08-02 00:25:13 -04:00
|
|
|
class ConditionalValidationTest < 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
|
2010-05-08 16:27:49 -04:00
|
|
|
|
2009-03-20 11:07:49 -04:00
|
|
|
def test_if_validation_using_method_true
|
|
|
|
# When the method returns true
|
2013-05-01 20:10:06 -04:00
|
|
|
Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}", if: :condition_is_true)
|
2010-05-08 16:27:49 -04:00
|
|
|
t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate t, :invalid?
|
|
|
|
assert_predicate t.errors[:title], :any?
|
2009-03-20 11:07:49 -04:00
|
|
|
assert_equal ["hoo 5"], t.errors["title"]
|
|
|
|
end
|
|
|
|
|
2017-11-05 17:25:37 -05:00
|
|
|
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")
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate t, :invalid?
|
|
|
|
assert_predicate t.errors[:title], :any?
|
2017-11-05 17:25:37 -05:00
|
|
|
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")
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate t, :invalid?
|
|
|
|
assert_predicate t.errors[:title], :any?
|
2017-11-05 17:25:37 -05:00
|
|
|
assert_equal ["hoo 5"], t.errors["title"]
|
|
|
|
end
|
|
|
|
|
2009-03-20 11:07:49 -04:00
|
|
|
def test_unless_validation_using_method_true
|
|
|
|
# When the method returns true
|
2013-05-01 20:10:06 -04:00
|
|
|
Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}", unless: :condition_is_true)
|
2010-05-08 16:27:49 -04:00
|
|
|
t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate t, :valid?
|
2013-09-10 07:56:39 -04:00
|
|
|
assert_empty t.errors[:title]
|
2009-03-20 11:07:49 -04:00
|
|
|
end
|
|
|
|
|
2017-11-05 17:25:37 -05:00
|
|
|
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")
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate t, :valid?
|
2017-11-05 17:25:37 -05:00
|
|
|
assert_empty t.errors[:title]
|
|
|
|
end
|
|
|
|
|
2019-02-25 06:46:10 -05:00
|
|
|
def test_unless_validation_using_array_of_true_and_false_methods
|
2017-11-05 17:25:37 -05:00
|
|
|
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")
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate t, :valid?
|
2017-11-05 17:25:37 -05:00
|
|
|
assert_empty t.errors[:title]
|
|
|
|
end
|
|
|
|
|
2009-03-20 11:07:49 -04:00
|
|
|
def test_if_validation_using_method_false
|
|
|
|
# When the method returns false
|
2017-11-05 17:25:37 -05:00
|
|
|
Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}", if: :condition_is_false)
|
2010-05-08 16:27:49 -04:00
|
|
|
t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate t, :valid?
|
2013-09-10 07:56:39 -04:00
|
|
|
assert_empty t.errors[:title]
|
2009-03-20 11:07:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_unless_validation_using_method_false
|
|
|
|
# When the method returns false
|
2017-11-05 17:25:37 -05:00
|
|
|
Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}", unless: :condition_is_false)
|
2010-05-08 16:27:49 -04:00
|
|
|
t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate t, :invalid?
|
|
|
|
assert_predicate t.errors[:title], :any?
|
2009-03-20 11:07:49 -04:00
|
|
|
assert_equal ["hoo 5"], t.errors["title"]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_if_validation_using_block_true
|
|
|
|
# When the block returns true
|
2013-05-01 20:10:06 -04:00
|
|
|
Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}",
|
|
|
|
if: Proc.new { |r| r.content.size > 4 })
|
2010-05-08 16:27:49 -04:00
|
|
|
t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate t, :invalid?
|
|
|
|
assert_predicate t.errors[:title], :any?
|
2009-03-20 11:07:49 -04:00
|
|
|
assert_equal ["hoo 5"], t.errors["title"]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_unless_validation_using_block_true
|
|
|
|
# When the block returns true
|
2013-05-01 20:10:06 -04:00
|
|
|
Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}",
|
|
|
|
unless: Proc.new { |r| r.content.size > 4 })
|
2010-05-08 16:27:49 -04:00
|
|
|
t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate t, :valid?
|
2013-09-10 07:56:39 -04:00
|
|
|
assert_empty t.errors[:title]
|
2009-03-20 11:07:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_if_validation_using_block_false
|
|
|
|
# When the block returns false
|
2013-05-01 20:10:06 -04:00
|
|
|
Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}",
|
2016-08-16 03:30:11 -04:00
|
|
|
if: Proc.new { |r| r.title != "uhohuhoh" })
|
2010-05-08 16:27:49 -04:00
|
|
|
t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate t, :valid?
|
2013-09-10 07:56:39 -04:00
|
|
|
assert_empty t.errors[:title]
|
2009-03-20 11:07:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_unless_validation_using_block_false
|
|
|
|
# When the block returns false
|
2013-05-01 20:10:06 -04:00
|
|
|
Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}",
|
2016-10-28 23:05:58 -04:00
|
|
|
unless: Proc.new { |r| r.title != "uhohuhoh" })
|
2010-05-08 16:27:49 -04:00
|
|
|
t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate t, :invalid?
|
|
|
|
assert_predicate t.errors[:title], :any?
|
2009-03-20 11:07:49 -04:00
|
|
|
assert_equal ["hoo 5"], t.errors["title"]
|
|
|
|
end
|
2017-11-05 17:25:37 -05:00
|
|
|
|
2019-02-25 06:46:10 -05:00
|
|
|
def test_validation_using_combining_if_true_and_unless_true_conditions
|
2017-11-05 17:25:37 -05:00
|
|
|
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")
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate t, :valid?
|
2017-11-05 17:25:37 -05:00
|
|
|
assert_empty t.errors[:title]
|
|
|
|
end
|
|
|
|
|
2019-02-25 06:46:10 -05:00
|
|
|
def test_validation_using_combining_if_true_and_unless_false_conditions
|
2017-11-05 17:25:37 -05:00
|
|
|
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")
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate t, :invalid?
|
|
|
|
assert_predicate t.errors[:title], :any?
|
2017-11-05 17:25:37 -05:00
|
|
|
assert_equal ["hoo 5"], t.errors["title"]
|
|
|
|
end
|
2009-03-20 11:07:49 -04:00
|
|
|
end
|