class PresenceValidationTest

Public Instance Methods

teardown() click to toggle source
# File activemodel/test/cases/validations/format_validation_test.rb, line 9
def teardown
  Topic.clear_validators!
end
test_accepts_array_arguments() click to toggle source
# File activemodel/test/cases/validations/presence_validation_test.rb, line 35
def test_accepts_array_arguments
  Topic.validates_presence_of %w(title content)
  t = Topic.new
  assert t.invalid?
  assert_equal ["can't be blank"], t.errors[:title]
  assert_equal ["can't be blank"], t.errors[:content]
end
test_validate_format() click to toggle source
# File activemodel/test/cases/validations/format_validation_test.rb, line 13
def test_validate_format
  Topic.validates_format_of(:title, :content, with: /\AValidation\smacros \w+!\z/, message: "is bad data")

  t = Topic.new("title" => "i'm incorrect", "content" => "Validation macros rule!")
  assert t.invalid?, "Shouldn't be valid"
  assert_equal ["is bad data"], t.errors[:title]
  assert t.errors[:content].empty?

  t.title = "Validation macros rule!"

  assert t.valid?
  assert t.errors[:title].empty?

  assert_raise(ArgumentError) { Topic.validates_format_of(:title, :content) }
end
test_validate_format_numeric() click to toggle source

testing ticket #3142

# File activemodel/test/cases/validations/format_validation_test.rb, line 38
def test_validate_format_numeric
  Topic.validates_format_of(:title, :content, with: /\A[1-9][0-9]*\z/, message: "is bad data")

  t = Topic.new("title" => "72x", "content" => "6789")
  assert t.invalid?, "Shouldn't be valid"

  assert_equal ["is bad data"], t.errors[:title]
  assert t.errors[:content].empty?

  t.title = "-11"
  assert t.invalid?, "Shouldn't be valid"

  t.title = "03"
  assert t.invalid?, "Shouldn't be valid"

  t.title = "z44"
  assert t.invalid?, "Shouldn't be valid"

  t.title = "5v7"
  assert t.invalid?, "Shouldn't be valid"

  t.title = "1"

  assert t.valid?
  assert t.errors[:title].empty?
end
test_validate_format_of_with_multiline_regexp_and_option() click to toggle source
# File activemodel/test/cases/validations/format_validation_test.rb, line 76
def test_validate_format_of_with_multiline_regexp_and_option
  assert_nothing_raised do
    Topic.validates_format_of(:title, with: /^Valid Title$/, multiline: true)
  end
end
test_validate_format_of_with_multiline_regexp_should_raise_error() click to toggle source
# File activemodel/test/cases/validations/format_validation_test.rb, line 72
def test_validate_format_of_with_multiline_regexp_should_raise_error
  assert_raise(ArgumentError) { Topic.validates_format_of(:title, with: /^Valid Title$/) }
end
test_validate_format_of_without_any_regexp_should_raise_error() click to toggle source
# File activemodel/test/cases/validations/format_validation_test.rb, line 95
def test_validate_format_of_without_any_regexp_should_raise_error
  assert_raise(ArgumentError) { Topic.validates_format_of(:title) }
end
test_validate_format_with_allow_blank() click to toggle source
# File activemodel/test/cases/validations/format_validation_test.rb, line 29
def test_validate_format_with_allow_blank
  Topic.validates_format_of(:title, with: /\AValidation\smacros \w+!\z/, allow_blank: true)
  assert Topic.new("title" => "Shouldn't be valid").invalid?
  assert Topic.new("title" => "").valid?
  assert Topic.new("title" => nil).valid?
  assert Topic.new("title" => "Validation macros rule!").valid?
end
test_validate_format_with_formatted_message() click to toggle source
# File activemodel/test/cases/validations/format_validation_test.rb, line 65
def test_validate_format_with_formatted_message
  Topic.validates_format_of(:title, with: /\AValid Title\z/, message: "can't be %{value}")
  t = Topic.new(title: "Invalid title")
  assert t.invalid?
  assert_equal ["can't be Invalid title"], t.errors[:title]
end
test_validate_format_with_not_option() click to toggle source
# File activemodel/test/cases/validations/format_validation_test.rb, line 82
def test_validate_format_with_not_option
  Topic.validates_format_of(:title, without: /foo/, message: "should not contain foo")
  t = Topic.new

  t.title = "foobar"
  t.valid?
  assert_equal ["should not contain foo"], t.errors[:title]

  t.title = "something else"
  t.valid?
  assert_equal [], t.errors[:title]
end
test_validate_presences() click to toggle source
# File activemodel/test/cases/validations/presence_validation_test.rb, line 16
def test_validate_presences
  Topic.validates_presence_of(:title, :content)

  t = Topic.new
  assert t.invalid?
  assert_equal ["can't be blank"], t.errors[:title]
  assert_equal ["can't be blank"], t.errors[:content]

  t.title = "something"
  t.content = "   "

  assert t.invalid?
  assert_equal ["can't be blank"], t.errors[:content]

  t.content = "like stuff"

  assert t.valid?
end
test_validates_acceptance_of_with_custom_error_using_quotes() click to toggle source
# File activemodel/test/cases/validations/presence_validation_test.rb, line 43
def test_validates_acceptance_of_with_custom_error_using_quotes
  Person.validates_presence_of :karma, message: "This string contains 'single' and \"double\" quotes"
  p = Person.new
  assert p.invalid?
  assert_equal "This string contains 'single' and \"double\" quotes", p.errors[:karma].last
end
test_validates_format_of_for_ruby_class() click to toggle source
# File activemodel/test/cases/validations/format_validation_test.rb, line 135
def test_validates_format_of_for_ruby_class
  Person.validates_format_of :karma, with: /\A\d+\z/

  p = Person.new
  p.karma = "Pixies"
  assert p.invalid?

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

  p.karma = "1234"
  assert p.valid?
ensure
  Person.clear_validators!
end
test_validates_format_of_when_not_isnt_a_regexp_should_raise_error() click to toggle source
# File activemodel/test/cases/validations/format_validation_test.rb, line 107
def test_validates_format_of_when_not_isnt_a_regexp_should_raise_error
  assert_raise(ArgumentError) { Topic.validates_format_of(:title, without: "clearly not a regexp") }
end
test_validates_format_of_when_with_isnt_a_regexp_should_raise_error() click to toggle source
# File activemodel/test/cases/validations/format_validation_test.rb, line 103
def test_validates_format_of_when_with_isnt_a_regexp_should_raise_error
  assert_raise(ArgumentError) { Topic.validates_format_of(:title, with: "clearly not a regexp") }
end
test_validates_format_of_with_both_regexps_should_raise_error() click to toggle source
# File activemodel/test/cases/validations/format_validation_test.rb, line 99
def test_validates_format_of_with_both_regexps_should_raise_error
  assert_raise(ArgumentError) { Topic.validates_format_of(:title, with: /this/, without: /that/) }
end
test_validates_format_of_with_lambda() click to toggle source
# File activemodel/test/cases/validations/format_validation_test.rb, line 111
def test_validates_format_of_with_lambda
  Topic.validates_format_of :content, with: lambda { |topic| topic.title == "digit" ? /\A\d+\z/ : /\A\S+\z/ }

  t = Topic.new
  t.title = "digit"
  t.content = "Pixies"
  assert t.invalid?

  t.content = "1234"
  assert t.valid?
end
test_validates_format_of_without_lambda() click to toggle source
# File activemodel/test/cases/validations/format_validation_test.rb, line 123
def test_validates_format_of_without_lambda
  Topic.validates_format_of :content, without: lambda { |topic| topic.title == "characters" ? /\A\d+\z/ : /\A\S+\z/ }

  t = Topic.new
  t.title = "characters"
  t.content = "1234"
  assert t.invalid?

  t.content = "Pixies"
  assert t.valid?
end
test_validates_presence_doesnt_convert_to_array() click to toggle source
# File activerecord/test/cases/validations/presence_validation_test.rb, line 55
def test_validates_presence_doesnt_convert_to_array
  speedometer = Class.new(Speedometer)
  speedometer.validates_presence_of :dashboard

  dash = Dashboard.new

  # dashboard has to_a method
  def dash.to_a; ["(/)", '(\)']; end

  s = speedometer.new
  s.dashboard = dash

  assert_nothing_raised { s.valid? }
end
test_validates_presence_of_for_ruby_class() click to toggle source
# File activemodel/test/cases/validations/presence_validation_test.rb, line 50
def test_validates_presence_of_for_ruby_class
  Person.validates_presence_of :karma

  p = Person.new
  assert p.invalid?

  assert_equal ["can't be blank"], p.errors[:karma]

  p.karma = "Cold"
  assert p.valid?
end
test_validates_presence_of_for_ruby_class_with_custom_reader() click to toggle source
# File activemodel/test/cases/validations/presence_validation_test.rb, line 62
def test_validates_presence_of_for_ruby_class_with_custom_reader
  CustomReader.validates_presence_of :karma

  p = CustomReader.new
  assert p.invalid?

  assert_equal ["can't be blank"], p.errors[:karma]

  p[:karma] = "Cold"
  assert p.valid?
end
test_validates_presence_of_has_many_marked_for_destruction() click to toggle source
# File activerecord/test/cases/validations/presence_validation_test.rb, line 42
def test_validates_presence_of_has_many_marked_for_destruction
  Boy.validates_presence_of(:interests)
  b = Boy.new
  b.interests << [i1 = Interest.new, i2 = Interest.new]
  assert b.valid?

  i1.mark_for_destruction
  assert b.valid?

  i2.mark_for_destruction
  assert b.invalid?
end
test_validates_presence_of_has_one() click to toggle source
# File activerecord/test/cases/validations/presence_validation_test.rb, line 24
def test_validates_presence_of_has_one
  Boy.validates_presence_of(:face)
  b = Boy.new
  assert b.invalid?, "should not be valid if has_one association missing"
  assert_equal 1, b.errors[:face].size, "validates_presence_of should only add one error"
end
test_validates_presence_of_has_one_marked_for_destruction() click to toggle source
# File activerecord/test/cases/validations/presence_validation_test.rb, line 31
def test_validates_presence_of_has_one_marked_for_destruction
  Boy.validates_presence_of(:face)
  b = Boy.new
  f = Face.new
  b.face = f
  assert b.valid?

  f.mark_for_destruction
  assert b.invalid?
end
test_validates_presence_of_non_association() click to toggle source
# File activerecord/test/cases/validations/presence_validation_test.rb, line 15
def test_validates_presence_of_non_association
  Boy.validates_presence_of(:name)
  b = Boy.new
  assert b.invalid?

  b.name = "Alex"
  assert b.valid?
end
test_validates_presence_of_virtual_attribute_on_model() click to toggle source
# File activerecord/test/cases/validations/presence_validation_test.rb, line 70
def test_validates_presence_of_virtual_attribute_on_model
  repair_validations(Interest) do
    Interest.send(:attr_accessor, :abbreviation)
    Interest.validates_presence_of(:topic)
    Interest.validates_presence_of(:abbreviation)

    interest = Interest.create!(topic: "Thought Leadering", abbreviation: "tl")
    assert interest.valid?

    interest.abbreviation = ""

    assert interest.invalid?
  end
end
test_validates_presence_of_with_allow_blank_option() click to toggle source
# File activemodel/test/cases/validations/presence_validation_test.rb, line 92
def test_validates_presence_of_with_allow_blank_option
  Topic.validates_presence_of(:title, allow_blank: true)

  t = Topic.new(title: "something")
  assert t.valid?, t.errors.full_messages

  t.title = ""
  assert t.valid?, t.errors.full_messages

  t.title = "  "
  assert t.valid?, t.errors.full_messages

  t.title = nil
  assert t.valid?, t.errors.full_messages
end
test_validates_presence_of_with_allow_nil_option() click to toggle source
# File activemodel/test/cases/validations/presence_validation_test.rb, line 74
def test_validates_presence_of_with_allow_nil_option
  Topic.validates_presence_of(:title, allow_nil: true)

  t = Topic.new(title: "something")
  assert t.valid?, t.errors.full_messages

  t.title = ""
  assert t.invalid?
  assert_equal ["can't be blank"], t.errors[:title]

  t.title = "  "
  assert t.invalid?, t.errors.full_messages
  assert_equal ["can't be blank"], t.errors[:title]

  t.title = nil
  assert t.valid?, t.errors.full_messages
end
test_validates_presence_with_on_context() click to toggle source
# File activerecord/test/cases/validations/presence_validation_test.rb, line 97
def test_validates_presence_with_on_context
  repair_validations(Interest) do
    Interest.validates_presence_of(:topic, on: :required_name)
    interest = Interest.new
    interest.save!
    assert_not interest.valid?(:required_name)
  end
end
test_validations_run_on_persisted_record() click to toggle source
# File activerecord/test/cases/validations/presence_validation_test.rb, line 85
def test_validations_run_on_persisted_record
  repair_validations(Interest) do
    interest = Interest.new
    interest.save!
    assert_predicate interest, :valid?

    Interest.validates_presence_of(:topic)

    assert_not_predicate interest, :valid?
  end
end