diff options
Diffstat (limited to 'activerecord/test/validations_test.rb')
-rwxr-xr-x | activerecord/test/validations_test.rb | 169 |
1 files changed, 130 insertions, 39 deletions
diff --git a/activerecord/test/validations_test.rb b/activerecord/test/validations_test.rb index 7ac93b43fb..f03c7dfcae 100755 --- a/activerecord/test/validations_test.rb +++ b/activerecord/test/validations_test.rb @@ -6,9 +6,10 @@ require 'fixtures/developer' class ValidationsTest < Test::Unit::TestCase fixtures :topics, :developers - def teardown - Topic.write_inheritable_attribute("validate", []) - Topic.write_inheritable_attribute("validate_on_create", []) + def setup + Topic.write_inheritable_attribute(:validate, nil) + Topic.write_inheritable_attribute(:validate_on_create, nil) + Topic.write_inheritable_attribute(:validate_on_update, nil) end def test_single_field_validation @@ -19,7 +20,7 @@ class ValidationsTest < Test::Unit::TestCase r.content = "Messa content!" assert r.save, "A reply with content should be saveable" end - + def test_single_attr_validation_and_error_msg r = Reply.new r.title = "There's no content!" @@ -41,7 +42,7 @@ class ValidationsTest < Test::Unit::TestCase assert_equal 2, r.errors.count end - + def test_error_on_create r = Reply.new r.title = "Wrong Create" @@ -50,57 +51,55 @@ class ValidationsTest < Test::Unit::TestCase assert_equal "is Wrong Create", r.errors.on("title"), "A reply with a bad content should contain an error" end - def test_error_on_update r = Reply.new r.title = "Bad" r.content = "Good" - assert r.save, "First save should be successful" - + r.title = "Wrong Update" assert !r.save, "Second save should fail" - + assert r.errors.invalid?("title"), "A reply with a bad title should mark that attribute as invalid" assert_equal "is Wrong Update", r.errors.on("title"), "A reply with a bad content should contain an error" end - + def test_single_error_per_attr_iteration r = Reply.new r.save - + errors = [] r.errors.each { |attr, msg| errors << [attr, msg] } - + assert errors.include?(["title", "Empty"]) assert errors.include?(["content", "Empty"]) end - + def test_multiple_errors_per_attr_iteration_with_full_error_composition r = Reply.new r.title = "Wrong Create" r.content = "Mismatch" r.save - + errors = [] r.errors.each_full { |error| errors << error } - + assert_equal "Title is Wrong Create", errors[0] assert_equal "Title is Content Mismatch", errors[1] assert_equal 2, r.errors.count end - + def test_errors_on_base r = Reply.new r.content = "Mismatch" r.save r.errors.add_to_base "Reply is not dignifying" - + errors = [] r.errors.each_full { |error| errors << error } - + assert_equal "Reply is not dignifying", r.errors.on_base - + assert errors.include?("Title Empty") assert errors.include?("Reply is not dignifying") assert_equal 2, r.errors.count @@ -111,12 +110,30 @@ class ValidationsTest < Test::Unit::TestCase assert !reply.save assert reply.save(false) end - + + def test_validates_each + perform = true + hits = 0 + Topic.validates_each(:title, :content, [:title, :content]) do |record, attr| + if perform + record.errors.add attr, 'gotcha' + hits += 1 + end + end + t = Topic.new("title" => "valid", "content" => "whatever") + assert !t.save + assert_equal 4, hits + assert_equal %w(gotcha gotcha), t.errors.on(:title) + assert_equal %w(gotcha gotcha), t.errors.on(:content) + ensure + perform = false + end + def test_errors_on_boundary_breaking developer = Developer.new("name" => "xs") assert !developer.save assert_equal "is too short (min is 3 characters)", developer.errors.on("name") - + developer.name = "All too very long for this boundary, it really is" assert !developer.save assert_equal "is too long (max is 20 characters)", developer.errors.on("name") @@ -127,11 +144,11 @@ class ValidationsTest < Test::Unit::TestCase def test_title_confirmation_no_confirm Topic.validates_confirmation_of(:title) - + t = Topic.create("title" => "We should not be confirmed") assert t.save end - + def test_title_confirmation Topic.validates_confirmation_of(:title) @@ -171,7 +188,7 @@ class ValidationsTest < Test::Unit::TestCase t.eula = "1" assert t.save end - + def test_validate_presences Topic.validates_presence_of(:title, :content) @@ -179,16 +196,16 @@ class ValidationsTest < Test::Unit::TestCase assert !t.save assert_equal "can't be empty", t.errors.on(:title) assert_equal "can't be empty", t.errors.on(:content) - + t.title = "something" t.content = "another" - + assert t.save end - + def test_validate_uniqueness Topic.validates_uniqueness_of(:title) - + t = Topic.new("title" => "I'm unique!") assert t.save, "Should save t as unique" @@ -199,14 +216,14 @@ class ValidationsTest < Test::Unit::TestCase assert !t2.valid?, "Shouldn't be valid" assert !t2.save, "Shouldn't save t2 as unique" assert_equal "has already been taken", t2.errors.on(:title) - + t2.title = "Now Im really also unique" assert t2.save, "Should now save t2 as unique" end def test_validate_uniqueness_with_scope Reply.validates_uniqueness_of(:content, :scope => "parent_id") - + t = Topic.create("title" => "I'm unique!") r1 = t.replies.create "title" => "r1", "content" => "hello world" @@ -246,8 +263,7 @@ class ValidationsTest < Test::Unit::TestCase assert !Topic.create("title" => "a!", "content" => "abc").valid? assert !Topic.create("title" => "a b", "content" => "abc").valid? assert !Topic.create("title" => nil, "content" => "def").valid? - assert !Topic.create("title" => %w(a b c), "content" => "def").valid? - + t = Topic.create("title" => "a", "content" => "I know you are but what am I?") assert t.valid? t.title = "uhoh" @@ -272,32 +288,61 @@ class ValidationsTest < Test::Unit::TestCase end def test_validates_length_of_using_minimum - Topic.validates_length_of( :title, :minimum=>5 ) + Topic.validates_length_of :title, :minimum => 5 + t = Topic.create("title" => "valid", "content" => "whatever") assert t.valid? + t.title = "not" assert !t.valid? assert t.errors.on(:title) assert_equal "is too short (min is 5 characters)", t.errors["title"] + t.title = "" assert !t.valid? assert t.errors.on(:title) + assert_equal "is too short (min is 5 characters)", t.errors["title"] + t.title = nil assert !t.valid? - assert_equal "is too short (min is 5 characters)", t.errors["title"] assert t.errors.on(:title) + assert_equal "is too short (min is 5 characters)", t.errors["title"] end - + + def test_optionally_validates_length_of_using_minimum + Topic.validates_length_of :title, :minimum => 5, :allow_nil => true + + t = Topic.create("title" => "valid", "content" => "whatever") + assert t.valid? + + t.title = nil + assert t.valid? + end + def test_validates_length_of_using_maximum - Topic.validates_length_of( :title, :maximum=>5 ) + Topic.validates_length_of :title, :maximum => 5 + t = Topic.create("title" => "valid", "content" => "whatever") assert t.valid? + t.title = "notvalid" assert !t.valid? assert t.errors.on(:title) assert_equal "is too long (max is 5 characters)", t.errors["title"] + t.title = "" assert t.valid? + + t.title = nil + assert !t.valid? + end + + def test_optionally_validates_length_of_using_maximum + Topic.validates_length_of :title, :maximum => 5, :allow_nil => true + + t = Topic.create("title" => "valid", "content" => "whatever") + assert t.valid? + t.title = nil assert t.valid? end @@ -307,29 +352,75 @@ class ValidationsTest < Test::Unit::TestCase t = Topic.create("title" => "a!", "content" => "I'm ooooooooh so very long") assert !t.save + assert_equal "is too short (min is 3 characters)", t.errors.on(:title) assert_equal "is too long (max is 5 characters)", t.errors.on(:content) t.title = "abe" t.content = "mad" - assert t.save end + def test_optionally_validates_length_of_using_within + Topic.validates_length_of :title, :content, :within => 3..5, :allow_nil => true + + t = Topic.create('title' => 'abc', 'content' => 'abcd') + assert t.valid? + + t.title = nil + assert t.valid? + end + def test_validates_length_of_using_is - Topic.validates_length_of( :title, :is=>5 ) + Topic.validates_length_of :title, :is => 5 + t = Topic.create("title" => "valid", "content" => "whatever") assert t.valid? + t.title = "notvalid" assert !t.valid? assert t.errors.on(:title) assert_equal "is the wrong length (should be 5 characters)", t.errors["title"] + t.title = "" assert !t.valid? + t.title = nil assert !t.valid? end + def test_optionally_validates_length_of_using_is + Topic.validates_length_of :title, :is => 5, :allow_nil => true + + t = Topic.create("title" => "valid", "content" => "whatever") + assert t.valid? + + t.title = nil + assert t.valid? + end + + 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 + + def test_validates_size_of_association + assert_nothing_raised { Topic.validates_size_of :replies, :minimum => 1 } + t = Topic.new('title' => 'noreplies', 'content' => 'whatever') + assert !t.save + assert t.errors.on(:replies) + t.replies.create('title' => 'areply', 'content' => 'whateveragain') + assert t.valid? + end + def test_validates_length_of_nasty_params assert_raise(ArgumentError) { Topic.validates_length_of(:title, :minimum=>6, :maximum=>9) } assert_raise(ArgumentError) { Topic.validates_length_of(:title, :within=>6, :maximum=>9) } @@ -372,7 +463,7 @@ class ValidationsTest < Test::Unit::TestCase assert t.errors.on(:title) assert_equal "hoo 5", t.errors["title"] end - + def test_validates_length_of_custom_errors_for_is_with_message Topic.validates_length_of( :title, :is=>5, :message=>"boo %d" ) t = Topic.create("title" => "uhohuhoh", "content" => "whatever") |