2017-07-16 13:11:16 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:38:23 -04:00
|
|
|
require "cases/helper"
|
|
|
|
require "active_support/core_ext/numeric/time"
|
2009-03-20 11:07:49 -04:00
|
|
|
|
2016-08-06 12:38:23 -04:00
|
|
|
require "models/topic"
|
|
|
|
require "models/person"
|
2009-03-20 11:07:49 -04:00
|
|
|
|
2019-08-02 00:25:13 -04:00
|
|
|
class ExclusionValidationTest < 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-03-20 11:07:49 -04:00
|
|
|
|
|
|
|
def test_validates_exclusion_of
|
2013-05-01 20:10:06 -04:00
|
|
|
Topic.validates_exclusion_of(:title, in: %w( abe monkey ))
|
2009-03-20 11:07:49 -04:00
|
|
|
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate Topic.new("title" => "something", "content" => "abc"), :valid?
|
|
|
|
assert_predicate Topic.new("title" => "monkey", "content" => "abc"), :invalid?
|
2009-03-20 11:07:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_validates_exclusion_of_with_formatted_message
|
2013-05-01 20:10:06 -04:00
|
|
|
Topic.validates_exclusion_of(:title, in: %w( abe monkey ), message: "option %{value} is restricted")
|
2009-03-20 11:07:49 -04:00
|
|
|
|
2010-05-08 16:27:49 -04:00
|
|
|
assert Topic.new("title" => "something", "content" => "abc")
|
2009-03-20 11:07:49 -04:00
|
|
|
|
2010-05-08 16:27:49 -04:00
|
|
|
t = Topic.new("title" => "monkey")
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate t, :invalid?
|
|
|
|
assert_predicate t.errors[:title], :any?
|
2009-03-20 13:36:22 -04:00
|
|
|
assert_equal ["option monkey is restricted"], t.errors[:title]
|
2009-03-20 11:07:49 -04:00
|
|
|
end
|
2009-03-20 18:21:27 -04:00
|
|
|
|
2012-07-20 12:53:31 -04:00
|
|
|
def test_validates_exclusion_of_with_within_option
|
2013-05-01 20:10:06 -04:00
|
|
|
Topic.validates_exclusion_of(:title, within: %w( abe monkey ))
|
2012-07-20 12:53:31 -04:00
|
|
|
|
|
|
|
assert Topic.new("title" => "something", "content" => "abc")
|
|
|
|
|
|
|
|
t = Topic.new("title" => "monkey")
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate t, :invalid?
|
|
|
|
assert_predicate t.errors[:title], :any?
|
2012-07-20 12:53:31 -04:00
|
|
|
end
|
|
|
|
|
2009-03-20 18:21:27 -04:00
|
|
|
def test_validates_exclusion_of_for_ruby_class
|
2013-05-01 20:10:06 -04:00
|
|
|
Person.validates_exclusion_of :karma, in: %w( abe monkey )
|
2009-03-20 18:21:27 -04:00
|
|
|
|
2009-12-23 07:30:58 -05:00
|
|
|
p = Person.new
|
|
|
|
p.karma = "abe"
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate p, :invalid?
|
2009-03-20 18:21:27 -04:00
|
|
|
|
2009-12-23 07:30:58 -05:00
|
|
|
assert_equal ["is reserved"], p.errors[:karma]
|
2009-03-20 18:21:27 -04:00
|
|
|
|
2009-12-23 07:30:58 -05:00
|
|
|
p.karma = "Lifo"
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate p, :valid?
|
2009-12-23 07:30:58 -05:00
|
|
|
ensure
|
2014-01-27 05:22:26 -05:00
|
|
|
Person.clear_validators!
|
2009-03-20 18:21:27 -04:00
|
|
|
end
|
2011-03-11 00:35:23 -05:00
|
|
|
|
|
|
|
def test_validates_exclusion_of_with_lambda
|
2013-05-01 20:10:06 -04:00
|
|
|
Topic.validates_exclusion_of :title, in: lambda { |topic| topic.author_name == "sikachu" ? %w( monkey elephant ) : %w( abe wasabi ) }
|
2011-03-11 00:35:23 -05:00
|
|
|
|
2011-11-18 11:51:05 -05:00
|
|
|
t = Topic.new
|
|
|
|
t.title = "elephant"
|
|
|
|
t.author_name = "sikachu"
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate t, :invalid?
|
2011-03-11 00:35:23 -05:00
|
|
|
|
2011-11-18 11:51:05 -05:00
|
|
|
t.title = "wasabi"
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate t, :valid?
|
2011-03-11 00:35:23 -05:00
|
|
|
end
|
2012-08-12 16:38:22 -04:00
|
|
|
|
2015-06-14 05:51:08 -04:00
|
|
|
def test_validates_exclusion_of_with_range
|
|
|
|
Topic.validates_exclusion_of :content, in: ("a".."g")
|
|
|
|
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate Topic.new(content: "g"), :invalid?
|
|
|
|
assert_predicate Topic.new(content: "h"), :valid?
|
2015-06-14 05:51:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_validates_exclusion_of_with_time_range
|
|
|
|
Topic.validates_exclusion_of :created_at, in: 6.days.ago..2.days.ago
|
|
|
|
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate Topic.new(created_at: 5.days.ago), :invalid?
|
|
|
|
assert_predicate Topic.new(created_at: 3.days.ago), :invalid?
|
|
|
|
assert_predicate Topic.new(created_at: 7.days.ago), :valid?
|
|
|
|
assert_predicate Topic.new(created_at: 1.day.ago), :valid?
|
2015-06-14 05:51:08 -04:00
|
|
|
end
|
|
|
|
|
2012-08-12 16:38:22 -04:00
|
|
|
def test_validates_inclusion_of_with_symbol
|
2013-05-01 20:10:06 -04:00
|
|
|
Person.validates_exclusion_of :karma, in: :reserved_karmas
|
2012-08-12 16:38:22 -04:00
|
|
|
|
|
|
|
p = Person.new
|
|
|
|
p.karma = "abe"
|
|
|
|
|
|
|
|
def p.reserved_karmas
|
|
|
|
%w(abe)
|
|
|
|
end
|
|
|
|
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate p, :invalid?
|
2012-08-12 16:38:22 -04:00
|
|
|
assert_equal ["is reserved"], p.errors[:karma]
|
|
|
|
|
2012-08-28 13:21:39 -04:00
|
|
|
p = Person.new
|
|
|
|
p.karma = "abe"
|
|
|
|
|
2012-08-12 16:38:22 -04:00
|
|
|
def p.reserved_karmas
|
|
|
|
%w()
|
|
|
|
end
|
|
|
|
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate p, :valid?
|
2012-08-12 16:38:22 -04:00
|
|
|
ensure
|
2014-01-27 05:22:26 -05:00
|
|
|
Person.clear_validators!
|
2012-08-12 16:38:22 -04:00
|
|
|
end
|
2009-03-20 11:07:49 -04:00
|
|
|
end
|