aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/test/cases/validations/exclusion_validation_test.rb
blob: 389195a8addc8f487e458aa46561aaf3d8fe01db (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# frozen_string_literal: true

require "cases/helper"
require "active_support/core_ext/numeric/time"

require "models/topic"
require "models/person"

class ExclusionValidationTest < ActiveSupport::TestCase
  def teardown
    Topic.clear_validators!
  end

  def test_validates_exclusion_of
    Topic.validates_exclusion_of(:title, in: %w( abe monkey ))

    assert_predicate Topic.new("title" => "something", "content" => "abc"), :valid?
    assert_predicate Topic.new("title" => "monkey", "content" => "abc"), :invalid?
  end

  def test_validates_exclusion_of_with_formatted_message
    Topic.validates_exclusion_of(:title, in: %w( abe monkey ), message: "option %{value} is restricted")

    assert Topic.new("title" => "something", "content" => "abc")

    t = Topic.new("title" => "monkey")
    assert_predicate t, :invalid?
    assert_predicate t.errors[:title], :any?
    assert_equal ["option monkey is restricted"], t.errors[:title]
  end

  def test_validates_exclusion_of_with_within_option
    Topic.validates_exclusion_of(:title, within: %w( abe monkey ))

    assert Topic.new("title" => "something", "content" => "abc")

    t = Topic.new("title" => "monkey")
    assert_predicate t, :invalid?
    assert_predicate t.errors[:title], :any?
  end

  def test_validates_exclusion_of_for_ruby_class
    Person.validates_exclusion_of :karma, in: %w( abe monkey )

    p = Person.new
    p.karma = "abe"
    assert_predicate p, :invalid?

    assert_equal ["is reserved"], p.errors[:karma]

    p.karma = "Lifo"
    assert_predicate p, :valid?
  ensure
    Person.clear_validators!
  end

  def test_validates_exclusion_of_with_lambda
    Topic.validates_exclusion_of :title, in: lambda { |topic| topic.author_name == "sikachu" ? %w( monkey elephant ) : %w( abe wasabi ) }

    t = Topic.new
    t.title = "elephant"
    t.author_name = "sikachu"
    assert_predicate t, :invalid?

    t.title = "wasabi"
    assert_predicate t, :valid?
  end

  def test_validates_exclusion_of_with_range
    Topic.validates_exclusion_of :content, in: ("a".."g")

    assert_predicate Topic.new(content: "g"), :invalid?
    assert_predicate Topic.new(content: "h"), :valid?
  end

  def test_validates_exclusion_of_with_time_range
    Topic.validates_exclusion_of :created_at, in: 6.days.ago..2.days.ago

    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?
  end

  def test_validates_inclusion_of_with_symbol
    Person.validates_exclusion_of :karma, in: :reserved_karmas

    p = Person.new
    p.karma = "abe"

    def p.reserved_karmas
      %w(abe)
    end

    assert_predicate p, :invalid?
    assert_equal ["is reserved"], p.errors[:karma]

    p = Person.new
    p.karma = "abe"

    def p.reserved_karmas
      %w()
    end

    assert_predicate p, :valid?
  ensure
    Person.clear_validators!
  end
end