diff options
Diffstat (limited to 'activemodel/test/cases/validations/length_validation_test.rb')
-rw-r--r-- | activemodel/test/cases/validations/length_validation_test.rb | 196 |
1 files changed, 53 insertions, 143 deletions
diff --git a/activemodel/test/cases/validations/length_validation_test.rb b/activemodel/test/cases/validations/length_validation_test.rb index 254e823b7c..012c5a2f37 100644 --- a/activemodel/test/cases/validations/length_validation_test.rb +++ b/activemodel/test/cases/validations/length_validation_test.rb @@ -1,13 +1,10 @@ # encoding: utf-8 require 'cases/helper' -require 'cases/tests_database' require 'models/topic' -require 'models/developer' require 'models/person' class LengthValidationTest < ActiveModel::TestCase - include ActiveModel::TestsDatabase def teardown Topic.reset_callbacks(:validate) @@ -16,53 +13,53 @@ class LengthValidationTest < ActiveModel::TestCase def test_validates_length_of_with_allow_nil Topic.validates_length_of( :title, :is => 5, :allow_nil=>true ) - assert !Topic.create("title" => "ab").valid? - assert !Topic.create("title" => "").valid? - assert Topic.create("title" => nil).valid? - assert Topic.create("title" => "abcde").valid? + assert Topic.new("title" => "ab").invalid? + assert Topic.new("title" => "").invalid? + assert Topic.new("title" => nil).valid? + assert Topic.new("title" => "abcde").valid? end def test_validates_length_of_with_allow_blank Topic.validates_length_of( :title, :is => 5, :allow_blank=>true ) - assert !Topic.create("title" => "ab").valid? - assert Topic.create("title" => "").valid? - assert Topic.create("title" => nil).valid? - assert Topic.create("title" => "abcde").valid? + assert Topic.new("title" => "ab").invalid? + assert Topic.new("title" => "").valid? + assert Topic.new("title" => nil).valid? + assert Topic.new("title" => "abcde").valid? end def test_validates_length_of_using_minimum Topic.validates_length_of :title, :minimum => 5 - t = Topic.create("title" => "valid", "content" => "whatever") + t = Topic.new("title" => "valid", "content" => "whatever") assert t.valid? t.title = "not" - assert !t.valid? + assert t.invalid? assert t.errors[:title].any? assert_equal ["is too short (minimum is 5 characters)"], t.errors[:title] t.title = "" - assert !t.valid? + 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.valid? + assert t.invalid? assert t.errors[:title].any? assert_equal ["is too short (minimum is 5 characters)"], t.errors["title"] end def test_validates_length_of_using_maximum_should_allow_nil Topic.validates_length_of :title, :maximum => 10 - t = Topic.create + t = Topic.new assert t.valid? 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") + t = Topic.new("title" => "valid", "content" => "whatever") assert t.valid? t.title = nil @@ -72,11 +69,11 @@ class LengthValidationTest < ActiveModel::TestCase def test_validates_length_of_using_maximum Topic.validates_length_of :title, :maximum => 5 - t = Topic.create("title" => "valid", "content" => "whatever") + t = Topic.new("title" => "valid", "content" => "whatever") assert t.valid? t.title = "notvalid" - assert !t.valid? + assert t.invalid? assert t.errors[:title].any? assert_equal ["is too long (maximum is 5 characters)"], t.errors[:title] @@ -87,7 +84,7 @@ class LengthValidationTest < ActiveModel::TestCase 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") + t = Topic.new("title" => "valid", "content" => "whatever") assert t.valid? t.title = nil @@ -98,13 +95,13 @@ class LengthValidationTest < ActiveModel::TestCase Topic.validates_length_of(:title, :content, :within => 3..5) t = Topic.new("title" => "a!", "content" => "I'm ooooooooh so very long") - assert !t.valid? + 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.valid? + 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] @@ -120,7 +117,7 @@ class LengthValidationTest < ActiveModel::TestCase assert t.valid? t.title = "Now I'm 10" - assert !t.valid? + assert t.invalid? assert_equal ["is too long (maximum is 9 characters)"], t.errors[:title] t.title = "Four" @@ -130,77 +127,35 @@ class LengthValidationTest < ActiveModel::TestCase 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') + t = Topic.new('title' => 'abc', 'content' => 'abcd') assert t.valid? t.title = nil assert t.valid? end - def test_optionally_validates_length_of_using_within_on_create - Topic.validates_length_of :title, :content, :within => 5..10, :on => :create, :too_long => "my string is too long: %{count}" - - t = Topic.create("title" => "thisisnotvalid", "content" => "whatever") - assert !t.save - assert t.errors[:title].any? - assert_equal ["my string is too long: 10"], t.errors[:title] - - t.title = "butthisis" - assert t.save - - t.title = "few" - assert t.save - - t.content = "andthisislong" - assert t.save - - t.content = t.title = "iamfine" - assert t.save - end - - def test_optionally_validates_length_of_using_within_on_update - Topic.validates_length_of :title, :content, :within => 5..10, :on => :update, :too_short => "my string is too short: %{count}" - - t = Topic.create("title" => "vali", "content" => "whatever") - assert !t.save - assert t.errors[:title].any? - - t.title = "not" - assert !t.save - assert t.errors[:title].any? - assert_equal ["my string is too short: 5"], t.errors[:title] - - t.title = "valid" - t.content = "andthisistoolong" - assert !t.save - assert t.errors[:content].any? - - t.content = "iamfine" - assert t.save - end - def test_validates_length_of_using_is Topic.validates_length_of :title, :is => 5 - t = Topic.create("title" => "valid", "content" => "whatever") + t = Topic.new("title" => "valid", "content" => "whatever") assert t.valid? t.title = "notvalid" - assert !t.valid? + 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.valid? + assert t.invalid? t.title = nil - assert !t.valid? + assert t.invalid? 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") + t = Topic.new("title" => "valid", "content" => "whatever") assert t.valid? t.title = nil @@ -231,61 +186,61 @@ class LengthValidationTest < ActiveModel::TestCase def test_validates_length_of_custom_errors_for_minimum_with_message Topic.validates_length_of( :title, :minimum=>5, :message=>"boo %{count}" ) - t = Topic.create("title" => "uhoh", "content" => "whatever") - assert !t.valid? + t = Topic.new("title" => "uhoh", "content" => "whatever") + assert t.invalid? assert t.errors[:title].any? assert_equal ["boo 5"], t.errors[:title] end 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.create("title" => "uhoh", "content" => "whatever") - assert !t.valid? + t = Topic.new("title" => "uhoh", "content" => "whatever") + assert t.invalid? assert t.errors[:title].any? assert_equal ["hoo 5"], t.errors[:title] end def test_validates_length_of_custom_errors_for_maximum_with_message Topic.validates_length_of( :title, :maximum=>5, :message=>"boo %{count}" ) - t = Topic.create("title" => "uhohuhoh", "content" => "whatever") - assert !t.valid? + t = Topic.new("title" => "uhohuhoh", "content" => "whatever") + assert t.invalid? assert t.errors[:title].any? assert_equal ["boo 5"], t.errors[:title] end def test_validates_length_of_custom_errors_for_in Topic.validates_length_of(:title, :in => 10..20, :message => "hoo %{count}") - t = Topic.create("title" => "uhohuhoh", "content" => "whatever") - assert !t.valid? + t = Topic.new("title" => "uhohuhoh", "content" => "whatever") + assert t.invalid? assert t.errors[:title].any? assert_equal ["hoo 10"], t.errors["title"] - t = Topic.create("title" => "uhohuhohuhohuhohuhohuhohuhohuhoh", "content" => "whatever") - assert !t.valid? + t = Topic.new("title" => "uhohuhohuhohuhohuhohuhohuhohuhoh", "content" => "whatever") + assert t.invalid? assert t.errors[:title].any? assert_equal ["hoo 20"], t.errors["title"] end 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.create("title" => "uhohuhoh", "content" => "whatever") - assert !t.valid? + t = Topic.new("title" => "uhohuhoh", "content" => "whatever") + assert t.invalid? assert t.errors[:title].any? 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 %{count}" ) - t = Topic.create("title" => "uhohuhoh", "content" => "whatever") - assert !t.valid? + t = Topic.new("title" => "uhohuhoh", "content" => "whatever") + assert t.invalid? assert t.errors[:title].any? assert_equal ["boo 5"], t.errors["title"] end 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.create("title" => "uhohuhoh", "content" => "whatever") - assert !t.valid? + t = Topic.new("title" => "uhohuhoh", "content" => "whatever") + assert t.invalid? assert t.errors[:title].any? assert_equal ["hoo 5"], t.errors["title"] end @@ -294,11 +249,11 @@ class LengthValidationTest < ActiveModel::TestCase with_kcode('UTF8') do Topic.validates_length_of :title, :minimum => 5 - t = Topic.create("title" => "一二三四五", "content" => "whatever") + t = Topic.new("title" => "一二三四五", "content" => "whatever") assert t.valid? t.title = "一二三四" - assert !t.valid? + assert t.invalid? assert t.errors[:title].any? assert_equal ["is too short (minimum is 5 characters)"], t.errors["title"] end @@ -308,11 +263,11 @@ class LengthValidationTest < ActiveModel::TestCase with_kcode('UTF8') do Topic.validates_length_of :title, :maximum => 5 - t = Topic.create("title" => "一二三四五", "content" => "whatever") + t = Topic.new("title" => "一二三四五", "content" => "whatever") assert t.valid? t.title = "一二34五六" - assert !t.valid? + assert t.invalid? assert t.errors[:title].any? assert_equal ["is too long (maximum is 5 characters)"], t.errors["title"] end @@ -323,7 +278,7 @@ class LengthValidationTest < ActiveModel::TestCase Topic.validates_length_of(:title, :content, :within => 3..5) t = Topic.new("title" => "一二", "content" => "12三四五六七") - assert !t.valid? + 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 = "一二三" @@ -336,10 +291,10 @@ class LengthValidationTest < ActiveModel::TestCase with_kcode('UTF8') do Topic.validates_length_of :title, :within => 3..5, :allow_nil => true - t = Topic.create(:title => "一二三四五") + t = Topic.new(:title => "一二三四五") assert t.valid?, t.errors.inspect - t = Topic.create(:title => "一二三") + t = Topic.new(:title => "一二三") assert t.valid?, t.errors.inspect t.title = nil @@ -347,60 +302,15 @@ class LengthValidationTest < ActiveModel::TestCase end end - def test_optionally_validates_length_of_using_within_on_create_utf8 - with_kcode('UTF8') do - Topic.validates_length_of :title, :within => 5..10, :on => :create, :too_long => "長すぎます: %{count}" - - t = Topic.create("title" => "一二三四五六七八九十A", "content" => "whatever") - assert !t.save - assert t.errors[:title].any? - assert_equal "長すぎます: 10", t.errors[:title].first - - t.title = "一二三四五六七八九" - assert t.save - - t.title = "一二3" - assert t.save - - t.content = "一二三四五六七八九十" - assert t.save - - t.content = t.title = "一二三四五六" - assert t.save - end - end - - def test_optionally_validates_length_of_using_within_on_update_utf8 - with_kcode('UTF8') do - Topic.validates_length_of :title, :within => 5..10, :on => :update, :too_short => "短すぎます: %{count}" - - t = Topic.create("title" => "一二三4", "content" => "whatever") - assert !t.save - assert t.errors[:title].any? - - t.title = "1二三4" - assert !t.save - assert t.errors[:title].any? - assert_equal ["短すぎます: 5"], t.errors[:title] - - t.title = "一二三四五六七八九十A" - assert !t.save - assert t.errors[:title].any? - - t.title = "一二345" - assert t.save - end - end - def test_validates_length_of_using_is_utf8 with_kcode('UTF8') do Topic.validates_length_of :title, :is => 5 - t = Topic.create("title" => "一二345", "content" => "whatever") + t = Topic.new("title" => "一二345", "content" => "whatever") assert t.valid? t.title = "一二345六" - assert !t.valid? + assert t.invalid? assert t.errors[:title].any? assert_equal ["is the wrong length (should be 5 characters)"], t.errors["title"] end @@ -409,11 +319,11 @@ class LengthValidationTest < ActiveModel::TestCase def test_validates_length_of_with_block Topic.validates_length_of :content, :minimum => 5, :too_short=>"Your essay must be at least %{count} words.", :tokenizer => lambda {|str| str.scan(/\w+/) } - t = Topic.create!(:content => "this content should be long enough") + t = Topic.new(:content => "this content should be long enough") assert t.valid? t.content = "not long enough" - assert !t.valid? + assert t.invalid? assert t.errors[:content].any? assert_equal ["Your essay must be at least 5 words."], t.errors[:content] end |