aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/validations/length_validation_test.rb
blob: 952e1681a77a6f5bde3deb4fb1b2f88f1cacaa06 (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
require "cases/helper"
require 'models/owner'
require 'models/pet'
require 'models/person'

class LengthValidationTest < ActiveRecord::TestCase
  fixtures :owners
  repair_validations(Owner)

  def test_validates_size_of_association
    repair_validations Owner do
      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
  end

  def test_validates_size_of_association_using_within
    repair_validations Owner do
      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
  end

  def test_validates_size_of_association_utf8
    repair_validations Owner do
      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
  end

  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

  def test_does_not_validate_length_of_if_parent_record_is_validate_false
    Owner.validates_length_of :name, minimum: 1
    owner = Owner.new
    owner.save!(validate: false)
    assert owner.persisted?

    pet = Pet.new(owner_id: owner.id)
    pet.save!

    assert_equal owner.pets.size, 1
    assert owner.valid?
    assert pet.valid?
  end
end