class LengthValidationTest

Public Class Methods

name() click to toggle source
# File activerecord/test/cases/validations/length_validation_test.rb, line 13
def self.name; "Owner"; end

Public Instance Methods

teardown() click to toggle source
# File activemodel/test/cases/validations/length_validation_test.rb, line 9
def teardown
  Topic.clear_validators!
end
test_optionally_validates_length_of_using_is() click to toggle source
# File activemodel/test/cases/validations/length_validation_test.rb, line 155
def test_optionally_validates_length_of_using_is
  Topic.validates_length_of :title, is: 5, allow_nil: true

  t = Topic.new("title" => "valid", "content" => "whatever")
  assert t.valid?

  t.title = nil
  assert t.valid?
end
test_optionally_validates_length_of_using_maximum() click to toggle source
# File activemodel/test/cases/validations/length_validation_test.rb, line 84
def test_optionally_validates_length_of_using_maximum
  Topic.validates_length_of :title, maximum: 5, allow_nil: true

  t = Topic.new("title" => "valid", "content" => "whatever")
  assert t.valid?

  t.title = nil
  assert t.valid?
end
test_optionally_validates_length_of_using_minimum() click to toggle source
# File activemodel/test/cases/validations/length_validation_test.rb, line 59
def test_optionally_validates_length_of_using_minimum
  Topic.validates_length_of :title, minimum: 5, allow_nil: true

  t = Topic.new("title" => "valid", "content" => "whatever")
  assert t.valid?

  t.title = nil
  assert t.valid?
end
test_optionally_validates_length_of_using_within() click to toggle source
# File activemodel/test/cases/validations/length_validation_test.rb, line 127
def test_optionally_validates_length_of_using_within
  Topic.validates_length_of :title, :content, within: 3..5, allow_nil: true

  t = Topic.new("title" => "abc", "content" => "abcd")
  assert t.valid?

  t.title = nil
  assert t.valid?
end
test_optionally_validates_length_of_using_within_utf8() click to toggle source
# File activemodel/test/cases/validations/length_validation_test.rb, line 298
def test_optionally_validates_length_of_using_within_utf8
  Topic.validates_length_of :title, within: 3..5, allow_nil: true

  t = Topic.new(title: "一二三四五")
  assert t.valid?, t.errors.inspect

  t = Topic.new(title: "一二三")
  assert t.valid?, t.errors.inspect

  t.title = nil
  assert t.valid?, t.errors.inspect
end
test_validates_length_of_custom_errors_for_both_too_short_and_too_long() click to toggle source
# File activemodel/test/cases/validations/length_validation_test.rb, line 232
def test_validates_length_of_custom_errors_for_both_too_short_and_too_long
  Topic.validates_length_of :title, minimum: 3, maximum: 5, too_short: "too short", too_long: "too long"

  t = Topic.new(title: "a")
  assert t.invalid?
  assert t.errors[:title].any?
  assert_equal ["too short"], t.errors["title"]

  t = Topic.new(title: "aaaaaa")
  assert t.invalid?
  assert t.errors[:title].any?
  assert_equal ["too long"], t.errors["title"]
end
test_validates_length_of_custom_errors_for_in() click to toggle source
# File activemodel/test/cases/validations/length_validation_test.rb, line 211
def test_validates_length_of_custom_errors_for_in
  Topic.validates_length_of(:title, in: 10..20, message: "hoo %{count}")
  t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
  assert t.invalid?
  assert t.errors[:title].any?
  assert_equal ["hoo 10"], t.errors["title"]

  t = Topic.new("title" => "uhohuhohuhohuhohuhohuhohuhohuhoh", "content" => "whatever")
  assert t.invalid?
  assert t.errors[:title].any?
  assert_equal ["hoo 20"], t.errors["title"]
end
test_validates_length_of_custom_errors_for_is_with_message() click to toggle source
# File activemodel/test/cases/validations/length_validation_test.rb, line 246
def test_validates_length_of_custom_errors_for_is_with_message
  Topic.validates_length_of(:title, is: 5, message: "boo %{count}")
  t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
  assert t.invalid?
  assert t.errors[:title].any?
  assert_equal ["boo 5"], t.errors["title"]
end
test_validates_length_of_custom_errors_for_is_with_wrong_length() click to toggle source
# File activemodel/test/cases/validations/length_validation_test.rb, line 254
def test_validates_length_of_custom_errors_for_is_with_wrong_length
  Topic.validates_length_of(:title, is: 5, wrong_length: "hoo %{count}")
  t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
  assert t.invalid?
  assert t.errors[:title].any?
  assert_equal ["hoo 5"], t.errors["title"]
end
test_validates_length_of_custom_errors_for_maximum_with_message() click to toggle source
# File activemodel/test/cases/validations/length_validation_test.rb, line 203
def test_validates_length_of_custom_errors_for_maximum_with_message
  Topic.validates_length_of(:title, maximum: 5, message: "boo %{count}")
  t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
  assert t.invalid?
  assert t.errors[:title].any?
  assert_equal ["boo 5"], t.errors[:title]
end
test_validates_length_of_custom_errors_for_maximum_with_too_long() click to toggle source
# File activemodel/test/cases/validations/length_validation_test.rb, line 224
def test_validates_length_of_custom_errors_for_maximum_with_too_long
  Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}")
  t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
  assert t.invalid?
  assert t.errors[:title].any?
  assert_equal ["hoo 5"], t.errors["title"]
end
test_validates_length_of_custom_errors_for_minimum_with_message() click to toggle source
# File activemodel/test/cases/validations/length_validation_test.rb, line 187
def test_validates_length_of_custom_errors_for_minimum_with_message
  Topic.validates_length_of(:title, minimum: 5, message: "boo %{count}")
  t = Topic.new("title" => "uhoh", "content" => "whatever")
  assert t.invalid?
  assert t.errors[:title].any?
  assert_equal ["boo 5"], t.errors[:title]
end
test_validates_length_of_custom_errors_for_minimum_with_too_short() click to toggle source
# File activemodel/test/cases/validations/length_validation_test.rb, line 195
def test_validates_length_of_custom_errors_for_minimum_with_too_short
  Topic.validates_length_of(:title, minimum: 5, too_short: "hoo %{count}")
  t = Topic.new("title" => "uhoh", "content" => "whatever")
  assert t.invalid?
  assert t.errors[:title].any?
  assert_equal ["hoo 5"], t.errors[:title]
end
test_validates_length_of_for_infinite_maxima() click to toggle source
# File activemodel/test/cases/validations/length_validation_test.rb, line 349
def test_validates_length_of_for_infinite_maxima
  Topic.validates_length_of(:title, within: 5..Float::INFINITY)

  t = Topic.new("title" => "1234")
  assert t.invalid?
  assert t.errors[:title].any?

  t.title = "12345"
  assert t.valid?

  Topic.validates_length_of(:author_name, maximum: Float::INFINITY)

  assert t.valid?

  t.author_name = "A very long author name that should still be valid." * 100
  assert t.valid?
end
test_validates_length_of_for_integer() click to toggle source
# File activemodel/test/cases/validations/length_validation_test.rb, line 323
def test_validates_length_of_for_integer
  Topic.validates_length_of(:approved, is: 4)

  t = Topic.new("title" => "uhohuhoh", "content" => "whatever", approved: 1)
  assert t.invalid?
  assert t.errors[:approved].any?

  t = Topic.new("title" => "uhohuhoh", "content" => "whatever", approved: 1234)
  assert t.valid?
end
test_validates_length_of_for_ruby_class() click to toggle source
# File activemodel/test/cases/validations/length_validation_test.rb, line 334
def test_validates_length_of_for_ruby_class
  Person.validates_length_of :karma, minimum: 5

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

  assert_equal ["is too short (minimum is 5 characters)"], p.errors[:karma]

  p.karma = "The Smiths"
  assert p.valid?
ensure
  Person.clear_validators!
end
test_validates_length_of_nasty_params() click to toggle source
# File activemodel/test/cases/validations/length_validation_test.rb, line 178
def test_validates_length_of_nasty_params
  assert_raise(ArgumentError) { Topic.validates_length_of(:title, is: -6) }
  assert_raise(ArgumentError) { Topic.validates_length_of(:title, within: 6) }
  assert_raise(ArgumentError) { Topic.validates_length_of(:title, minimum: "a") }
  assert_raise(ArgumentError) { Topic.validates_length_of(:title, maximum: "a") }
  assert_raise(ArgumentError) { Topic.validates_length_of(:title, within: "a") }
  assert_raise(ArgumentError) { Topic.validates_length_of(:title, is: "a") }
end
test_validates_length_of_using_bignum() click to toggle source
# File activemodel/test/cases/validations/length_validation_test.rb, line 165
def test_validates_length_of_using_bignum
  bigmin = 2**30
  bigmax = 2**32
  bigrange = bigmin...bigmax
  assert_nothing_raised do
    Topic.validates_length_of :title, is: bigmin + 5
    Topic.validates_length_of :title, within: bigrange
    Topic.validates_length_of :title, in: bigrange
    Topic.validates_length_of :title, minimum: bigmin
    Topic.validates_length_of :title, maximum: bigmax
  end
end
test_validates_length_of_using_both_minimum_and_maximum_should_not_allow_nil() click to toggle source
# File activemodel/test/cases/validations/length_validation_test.rb, line 382
def test_validates_length_of_using_both_minimum_and_maximum_should_not_allow_nil
  Topic.validates_length_of :title, minimum: 5, maximum: 10
  t = Topic.new
  assert t.invalid?
end
test_validates_length_of_using_is() click to toggle source
# File activemodel/test/cases/validations/length_validation_test.rb, line 137
def test_validates_length_of_using_is
  Topic.validates_length_of :title, is: 5

  t = Topic.new("title" => "valid", "content" => "whatever")
  assert t.valid?

  t.title = "notvalid"
  assert t.invalid?
  assert t.errors[:title].any?
  assert_equal ["is the wrong length (should be 5 characters)"], t.errors[:title]

  t.title = ""
  assert t.invalid?

  t.title = nil
  assert t.invalid?
end
test_validates_length_of_using_is_0_should_not_allow_nil() click to toggle source
# File activemodel/test/cases/validations/length_validation_test.rb, line 397
def test_validates_length_of_using_is_0_should_not_allow_nil
  Topic.validates_length_of :title, is: 0
  t = Topic.new
  assert t.invalid?

  t.title = ""
  assert t.valid?
end
test_validates_length_of_using_is_utf8() click to toggle source
# File activemodel/test/cases/validations/length_validation_test.rb, line 311
def test_validates_length_of_using_is_utf8
  Topic.validates_length_of :title, is: 5

  t = Topic.new("title" => "一二345", "content" => "whatever")
  assert t.valid?

  t.title = "一二345六"
  assert t.invalid?
  assert t.errors[:title].any?
  assert_equal ["is the wrong length (should be 5 characters)"], t.errors["title"]
end
test_validates_length_of_using_maximum() click to toggle source
# File activemodel/test/cases/validations/length_validation_test.rb, line 69
def test_validates_length_of_using_maximum
  Topic.validates_length_of :title, maximum: 5

  t = Topic.new("title" => "valid", "content" => "whatever")
  assert t.valid?

  t.title = "notvalid"
  assert t.invalid?
  assert t.errors[:title].any?
  assert_equal ["is too long (maximum is 5 characters)"], t.errors[:title]

  t.title = ""
  assert t.valid?
end
test_validates_length_of_using_maximum_should_allow_nil() click to toggle source
# File activemodel/test/cases/validations/length_validation_test.rb, line 53
def test_validates_length_of_using_maximum_should_allow_nil
  Topic.validates_length_of :title, maximum: 10
  t = Topic.new
  assert t.valid?
end
test_validates_length_of_using_maximum_should_not_allow_nil_and_empty_string_when_blank_not_allowed() click to toggle source
# File activemodel/test/cases/validations/length_validation_test.rb, line 373
def test_validates_length_of_using_maximum_should_not_allow_nil_and_empty_string_when_blank_not_allowed
  Topic.validates_length_of :title, maximum: 10, allow_blank: false
  t = Topic.new
  assert t.invalid?

  t.title = ""
  assert t.invalid?
end
test_validates_length_of_using_maximum_should_not_allow_nil_when_nil_not_allowed() click to toggle source
# File activemodel/test/cases/validations/length_validation_test.rb, line 367
def test_validates_length_of_using_maximum_should_not_allow_nil_when_nil_not_allowed
  Topic.validates_length_of :title, maximum: 10, allow_nil: false
  t = Topic.new
  assert t.invalid?
end
test_validates_length_of_using_maximum_utf8() click to toggle source
# File activemodel/test/cases/validations/length_validation_test.rb, line 274
def test_validates_length_of_using_maximum_utf8
  Topic.validates_length_of :title, maximum: 5

  t = Topic.new("title" => "一二三四五", "content" => "whatever")
  assert t.valid?

  t.title = "一二34五六"
  assert t.invalid?
  assert t.errors[:title].any?
  assert_equal ["is too long (maximum is 5 characters)"], t.errors["title"]
end
test_validates_length_of_using_minimum() click to toggle source
# File activemodel/test/cases/validations/length_validation_test.rb, line 31
def test_validates_length_of_using_minimum
  Topic.validates_length_of :title, minimum: 5

  t = Topic.new("title" => "valid", "content" => "whatever")
  assert t.valid?

  t.title = "not"
  assert t.invalid?
  assert t.errors[:title].any?
  assert_equal ["is too short (minimum is 5 characters)"], t.errors[:title]

  t.title = ""
  assert t.invalid?
  assert t.errors[:title].any?
  assert_equal ["is too short (minimum is 5 characters)"], t.errors[:title]

  t.title = nil
  assert t.invalid?
  assert t.errors[:title].any?
  assert_equal ["is too short (minimum is 5 characters)"], t.errors["title"]
end
test_validates_length_of_using_minimum_0_should_not_allow_nil() click to toggle source
# File activemodel/test/cases/validations/length_validation_test.rb, line 388
def test_validates_length_of_using_minimum_0_should_not_allow_nil
  Topic.validates_length_of :title, minimum: 0
  t = Topic.new
  assert t.invalid?

  t.title = ""
  assert t.valid?
end
test_validates_length_of_using_minimum_utf8() click to toggle source
# File activemodel/test/cases/validations/length_validation_test.rb, line 262
def test_validates_length_of_using_minimum_utf8
  Topic.validates_length_of :title, minimum: 5

  t = Topic.new("title" => "一二三四五", "content" => "whatever")
  assert t.valid?

  t.title = "一二三四"
  assert t.invalid?
  assert t.errors[:title].any?
  assert_equal ["is too short (minimum is 5 characters)"], t.errors["title"]
end
test_validates_length_of_using_within() click to toggle source
# File activemodel/test/cases/validations/length_validation_test.rb, line 94
def test_validates_length_of_using_within
  Topic.validates_length_of(:title, :content, within: 3..5)

  t = Topic.new("title" => "a!", "content" => "I'm ooooooooh so very long")
  assert t.invalid?
  assert_equal ["is too short (minimum is 3 characters)"], t.errors[:title]
  assert_equal ["is too long (maximum is 5 characters)"], t.errors[:content]

  t.title = nil
  t.content = nil
  assert t.invalid?
  assert_equal ["is too short (minimum is 3 characters)"], t.errors[:title]
  assert_equal ["is too short (minimum is 3 characters)"], t.errors[:content]

  t.title = "abe"
  t.content = "mad"
  assert t.valid?
end
test_validates_length_of_using_within_utf8() click to toggle source
# File activemodel/test/cases/validations/length_validation_test.rb, line 286
def test_validates_length_of_using_within_utf8
  Topic.validates_length_of(:title, :content, within: 3..5)

  t = Topic.new("title" => "一二", "content" => "12三四五六七")
  assert t.invalid?
  assert_equal ["is too short (minimum is 3 characters)"], t.errors[:title]
  assert_equal ["is too long (maximum is 5 characters)"], t.errors[:content]
  t.title = "一二三"
  t.content = "12三"
  assert t.valid?
end
test_validates_length_of_using_within_with_exclusive_range() click to toggle source
# File activemodel/test/cases/validations/length_validation_test.rb, line 113
def test_validates_length_of_using_within_with_exclusive_range
  Topic.validates_length_of(:title, within: 4...10)

  t = Topic.new("title" => "9 chars!!")
  assert t.valid?

  t.title = "Now I'm 10"
  assert t.invalid?
  assert_equal ["is too long (maximum is 9 characters)"], t.errors[:title]

  t.title = "Four"
  assert t.valid?
end
test_validates_length_of_virtual_attribute_on_model() click to toggle source
# File activerecord/test/cases/validations/length_validation_test.rb, line 65
def test_validates_length_of_virtual_attribute_on_model
  repair_validations(Pet) do
    Pet.send(:attr_accessor, :nickname)
    Pet.validates_length_of(:name, minimum: 1)
    Pet.validates_length_of(:nickname, minimum: 1)

    pet = Pet.create!(name: "Fancy Pants", nickname: "Fancy")

    assert pet.valid?

    pet.nickname = ""

    assert pet.invalid?
  end
end
test_validates_length_of_with_allow_blank() click to toggle source
# File activemodel/test/cases/validations/length_validation_test.rb, line 22
def test_validates_length_of_with_allow_blank
  Topic.validates_length_of(:title, is: 5, allow_blank: true)

  assert Topic.new("title" => "ab").invalid?
  assert Topic.new("title" => "").valid?
  assert Topic.new("title" => nil).valid?
  assert Topic.new("title" => "abcde").valid?
end
test_validates_length_of_with_allow_nil() click to toggle source
# File activemodel/test/cases/validations/length_validation_test.rb, line 13
def test_validates_length_of_with_allow_nil
  Topic.validates_length_of(:title, is: 5, allow_nil: true)

  assert Topic.new("title" => "ab").invalid?
  assert Topic.new("title" => "").invalid?
  assert Topic.new("title" => nil).valid?
  assert Topic.new("title" => "abcde").valid?
end
test_validates_size_of_association() click to toggle source
# File activerecord/test/cases/validations/length_validation_test.rb, line 17
def test_validates_size_of_association
  assert_nothing_raised { @owner.validates_size_of :pets, minimum: 1 }
  o = @owner.new("name" => "nopets")
  assert !o.save
  assert o.errors[:pets].any?
  o.pets.build("name" => "apet")
  assert o.valid?
end
test_validates_size_of_association_using_within() click to toggle source
# File activerecord/test/cases/validations/length_validation_test.rb, line 26
def test_validates_size_of_association_using_within
  assert_nothing_raised { @owner.validates_size_of :pets, within: 1..2 }
  o = @owner.new("name" => "nopets")
  assert !o.save
  assert o.errors[:pets].any?

  o.pets.build("name" => "apet")
  assert o.valid?

  2.times { o.pets.build("name" => "apet") }
  assert !o.save
  assert o.errors[:pets].any?
end
test_validates_size_of_association_utf8() click to toggle source
# File activerecord/test/cases/validations/length_validation_test.rb, line 40
def test_validates_size_of_association_utf8
  @owner.validates_size_of :pets, minimum: 1
  o = @owner.new("name" => "あいうえおかきくけこ")
  assert !o.save
  assert o.errors[:pets].any?
  o.pets.build("name" => "あいうえおかきくけこ")
  assert o.valid?
end
test_validates_size_of_respects_records_marked_for_destruction() click to toggle source
# File activerecord/test/cases/validations/length_validation_test.rb, line 49
def test_validates_size_of_respects_records_marked_for_destruction
  @owner.validates_size_of :pets, minimum: 1
  owner = @owner.new
  assert_not owner.save
  assert owner.errors[:pets].any?
  pet = owner.pets.build
  assert owner.valid?
  assert owner.save

  pet_count = Pet.count
  assert_not owner.update_attributes pets_attributes: [ { _destroy: 1, id: pet.id } ]
  assert_not owner.valid?
  assert owner.errors[:pets].any?
  assert_equal pet_count, Pet.count
end
test_validates_with_diff_in_option() click to toggle source
# File activemodel/test/cases/validations/length_validation_test.rb, line 406
def test_validates_with_diff_in_option
  Topic.validates_length_of(:title, is: 5)
  Topic.validates_length_of(:title, is: 5, if: Proc.new { false })

  assert Topic.new("title" => "david").valid?
  assert Topic.new("title" => "david2").invalid?
end