aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/test/cases/validations/length_validation_test.rb
diff options
context:
space:
mode:
authorJosh Kalderimis <josh.kalderimis@gmail.com>2010-05-08 23:27:49 +0300
committerCarl Lerche <carllerche@mac.com>2010-05-08 23:51:36 +0300
commit66913a76af9969ddf12021992eeb418e270bebe2 (patch)
tree8ff5d8cf518f086979fd4e4b0633c7f8c340323e /activemodel/test/cases/validations/length_validation_test.rb
parent82485068f8b64a49cbb6529d17dd5de27c28e951 (diff)
downloadrails-66913a76af9969ddf12021992eeb418e270bebe2.tar.gz
rails-66913a76af9969ddf12021992eeb418e270bebe2.tar.bz2
rails-66913a76af9969ddf12021992eeb418e270bebe2.zip
removed use of AR in AMo tests and removed testing of scopes (:on) in individual validation tests and moved them to their own test file
Diffstat (limited to 'activemodel/test/cases/validations/length_validation_test.rb')
-rw-r--r--activemodel/test/cases/validations/length_validation_test.rb193
1 files changed, 53 insertions, 140 deletions
diff --git a/activemodel/test/cases/validations/length_validation_test.rb b/activemodel/test/cases/validations/length_validation_test.rb
index 254e823b7c..da3146f267 100644
--- a/activemodel/test/cases/validations/length_validation_test.rb
+++ b/activemodel/test/cases/validations/length_validation_test.rb
@@ -16,53 +16,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 +72,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 +87,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 +98,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 +120,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 +130,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 +189,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 +252,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 +266,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 +281,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 +294,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 +305,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 +322,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