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

This commit is contained in:
Josh Kalderimis 2010-05-08 23:27:49 +03:00 committed by Carl Lerche
parent 82485068f8
commit 66913a76af
14 changed files with 240 additions and 289 deletions

View File

@ -15,43 +15,43 @@ class AcceptanceValidationTest < ActiveModel::TestCase
end
def test_terms_of_service_agreement_no_acceptance
Topic.validates_acceptance_of(:terms_of_service, :on => :create)
Topic.validates_acceptance_of(:terms_of_service)
t = Topic.create("title" => "We should not be confirmed")
assert t.save
t = Topic.new("title" => "We should not be confirmed")
assert t.valid?
end
def test_terms_of_service_agreement
Topic.validates_acceptance_of(:terms_of_service, :on => :create)
Topic.validates_acceptance_of(:terms_of_service)
t = Topic.create("title" => "We should be confirmed","terms_of_service" => "")
assert !t.save
t = Topic.new("title" => "We should be confirmed","terms_of_service" => "")
assert t.invalid?
assert_equal ["must be accepted"], t.errors[:terms_of_service]
t.terms_of_service = "1"
assert t.save
assert t.valid?
end
def test_eula
Topic.validates_acceptance_of(:eula, :message => "must be abided", :on => :create)
Topic.validates_acceptance_of(:eula, :message => "must be abided")
t = Topic.create("title" => "We should be confirmed","eula" => "")
assert !t.save
t = Topic.new("title" => "We should be confirmed","eula" => "")
assert t.invalid?
assert_equal ["must be abided"], t.errors[:eula]
t.eula = "1"
assert t.save
assert t.valid?
end
def test_terms_of_service_agreement_with_accept_value
Topic.validates_acceptance_of(:terms_of_service, :on => :create, :accept => "I agree.")
Topic.validates_acceptance_of(:terms_of_service, :accept => "I agree.")
t = Topic.create("title" => "We should be confirmed", "terms_of_service" => "")
assert !t.save
t = Topic.new("title" => "We should be confirmed", "terms_of_service" => "")
assert t.invalid?
assert_equal ["must be accepted"], t.errors[:terms_of_service]
t.terms_of_service = "I agree."
assert t.save
assert t.valid?
end
def test_validates_acceptance_of_for_ruby_class

View File

@ -10,12 +10,12 @@ class ConditionalValidationTest < ActiveModel::TestCase
def teardown
Topic.reset_callbacks(:validate)
end
def test_if_validation_using_method_true
# When the method returns true
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %{count}", :if => :condition_is_true )
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
@ -23,15 +23,15 @@ class ConditionalValidationTest < ActiveModel::TestCase
def test_unless_validation_using_method_true
# When the method returns true
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %{count}", :unless => :condition_is_true )
t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
assert t.valid?
assert !t.errors[:title].any?
assert t.errors[:title].empty?
end
def test_if_validation_using_method_false
# When the method returns false
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %{count}", :if => :condition_is_true_but_its_not )
t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
assert t.valid?
assert t.errors[:title].empty?
end
@ -39,8 +39,8 @@ class ConditionalValidationTest < ActiveModel::TestCase
def test_unless_validation_using_method_false
# When the method returns false
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %{count}", :unless => :condition_is_true_but_its_not )
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
@ -48,8 +48,8 @@ class ConditionalValidationTest < ActiveModel::TestCase
def test_if_validation_using_string_true
# When the evaluated string returns true
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %{count}", :if => "a = 1; a == 1" )
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
@ -57,7 +57,7 @@ class ConditionalValidationTest < ActiveModel::TestCase
def test_unless_validation_using_string_true
# When the evaluated string returns true
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %{count}", :unless => "a = 1; a == 1" )
t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
assert t.valid?
assert t.errors[:title].empty?
end
@ -65,7 +65,7 @@ class ConditionalValidationTest < ActiveModel::TestCase
def test_if_validation_using_string_false
# When the evaluated string returns false
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %{count}", :if => "false")
t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
assert t.valid?
assert t.errors[:title].empty?
end
@ -73,8 +73,8 @@ class ConditionalValidationTest < ActiveModel::TestCase
def test_unless_validation_using_string_false
# When the evaluated string returns false
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %{count}", :unless => "false")
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
@ -83,8 +83,8 @@ class ConditionalValidationTest < ActiveModel::TestCase
# When the block returns true
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %{count}",
:if => Proc.new { |r| r.content.size > 4 } )
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
@ -93,7 +93,7 @@ class ConditionalValidationTest < ActiveModel::TestCase
# When the block returns true
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %{count}",
:unless => Proc.new { |r| r.content.size > 4 } )
t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
assert t.valid?
assert t.errors[:title].empty?
end
@ -102,7 +102,7 @@ class ConditionalValidationTest < ActiveModel::TestCase
# When the block returns false
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %{count}",
:if => Proc.new { |r| r.title != "uhohuhoh"} )
t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
assert t.valid?
assert t.errors[:title].empty?
end
@ -111,8 +111,8 @@ class ConditionalValidationTest < ActiveModel::TestCase
# When the block returns false
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %{count}",
:unless => Proc.new { |r| r.title != "uhohuhoh"} )
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
@ -132,7 +132,7 @@ class ConditionalValidationTest < ActiveModel::TestCase
assert t.valid?, "A topic with a basic title should be valid"
t.title = "A very important title"
assert !t.valid?, "A topic with an important title, but without an author, should not be valid"
assert t.invalid?, "A topic with an important title, but without an author, should not be valid"
assert t.errors[:author_name].any?, "A topic with an 'important' title should require an author"
t.author_name = "Hubert J. Farnsworth"

View File

@ -20,7 +20,7 @@ class ConfirmationValidationTest < ActiveModel::TestCase
assert t.valid?
t.title_confirmation = "Parallel Lives"
assert !t.valid?
assert t.invalid?
t.title_confirmation = nil
t.title = "Parallel Lives"
@ -33,11 +33,11 @@ class ConfirmationValidationTest < ActiveModel::TestCase
def test_title_confirmation
Topic.validates_confirmation_of(:title)
t = Topic.create("title" => "We should be confirmed","title_confirmation" => "")
assert !t.save
t = Topic.new("title" => "We should be confirmed","title_confirmation" => "")
assert t.invalid?
t.title_confirmation = "We should be confirmed"
assert t.save
assert t.valid?
end
def test_validates_confirmation_of_for_ruby_class

View File

@ -15,17 +15,17 @@ class ExclusionValidationTest < ActiveModel::TestCase
def test_validates_exclusion_of
Topic.validates_exclusion_of( :title, :in => %w( abe monkey ) )
assert Topic.create("title" => "something", "content" => "abc").valid?
assert !Topic.create("title" => "monkey", "content" => "abc").valid?
assert Topic.new("title" => "something", "content" => "abc").valid?
assert Topic.new("title" => "monkey", "content" => "abc").invalid?
end
def test_validates_exclusion_of_with_formatted_message
Topic.validates_exclusion_of( :title, :in => %w( abe monkey ), :message => "option %{value} is restricted" )
assert Topic.create("title" => "something", "content" => "abc")
assert Topic.new("title" => "something", "content" => "abc")
t = Topic.create("title" => "monkey")
assert !t.valid?
t = Topic.new("title" => "monkey")
assert t.invalid?
assert t.errors[:title].any?
assert_equal ["option monkey is restricted"], t.errors[:title]
end

View File

@ -16,15 +16,14 @@ class PresenceValidationTest < ActiveModel::TestCase
def test_validate_format
Topic.validates_format_of(:title, :content, :with => /^Validation\smacros \w+!$/, :message => "is bad data")
t = Topic.create("title" => "i'm incorrect", "content" => "Validation macros rule!")
assert !t.valid?, "Shouldn't be valid"
assert !t.save, "Shouldn't save because it's invalid"
t = Topic.new("title" => "i'm incorrect", "content" => "Validation macros rule!")
assert t.invalid?, "Shouldn't be valid"
assert_equal ["is bad data"], t.errors[:title]
assert t.errors[:content].empty?
t.title = "Validation macros rule!"
assert t.save
assert t.valid?
assert t.errors[:title].empty?
assert_raise(ArgumentError) { Topic.validates_format_of(:title, :content) }
@ -32,43 +31,44 @@ class PresenceValidationTest < ActiveModel::TestCase
def test_validate_format_with_allow_blank
Topic.validates_format_of(:title, :with => /^Validation\smacros \w+!$/, :allow_blank=>true)
assert !Topic.create("title" => "Shouldn't be valid").valid?
assert Topic.create("title" => "").valid?
assert Topic.create("title" => nil).valid?
assert Topic.create("title" => "Validation macros rule!").valid?
assert Topic.new("title" => "Shouldn't be valid").invalid?
assert Topic.new("title" => "").valid?
assert Topic.new("title" => nil).valid?
assert Topic.new("title" => "Validation macros rule!").valid?
end
# testing ticket #3142
def test_validate_format_numeric
Topic.validates_format_of(:title, :content, :with => /^[1-9][0-9]*$/, :message => "is bad data")
t = Topic.create("title" => "72x", "content" => "6789")
assert !t.valid?, "Shouldn't be valid"
assert !t.save, "Shouldn't save because it's invalid"
t = Topic.new("title" => "72x", "content" => "6789")
assert t.invalid?, "Shouldn't be valid"
assert_equal ["is bad data"], t.errors[:title]
assert t.errors[:content].empty?
t.title = "-11"
assert !t.valid?, "Shouldn't be valid"
assert t.invalid?, "Shouldn't be valid"
t.title = "03"
assert !t.valid?, "Shouldn't be valid"
assert t.invalid?, "Shouldn't be valid"
t.title = "z44"
assert !t.valid?, "Shouldn't be valid"
assert t.invalid?, "Shouldn't be valid"
t.title = "5v7"
assert !t.valid?, "Shouldn't be valid"
assert t.invalid?, "Shouldn't be valid"
t.title = "1"
assert t.save
assert t.valid?
assert t.errors[:title].empty?
end
def test_validate_format_with_formatted_message
Topic.validates_format_of(:title, :with => /^Valid Title$/, :message => "can't be %{value}")
t = Topic.create(:title => 'Invalid title')
t = Topic.new(:title => 'Invalid title')
assert t.invalid?
assert_equal ["can't be Invalid title"], t.errors[:title]
end

View File

@ -16,14 +16,14 @@ class InclusionValidationTest < ActiveModel::TestCase
def test_validates_inclusion_of
Topic.validates_inclusion_of( :title, :in => %w( a b c d e f g ) )
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.new("title" => "a!", "content" => "abc").invalid?
assert Topic.new("title" => "a b", "content" => "abc").invalid?
assert Topic.new("title" => nil, "content" => "def").invalid?
t = Topic.create("title" => "a", "content" => "I know you are but what am I?")
t = Topic.new("title" => "a", "content" => "I know you are but what am I?")
assert t.valid?
t.title = "uhoh"
assert !t.valid?
assert t.invalid?
assert t.errors[:title].any?
assert_equal ["is not included in the list"], t.errors[:title]
@ -38,18 +38,18 @@ class InclusionValidationTest < ActiveModel::TestCase
def test_validates_inclusion_of_with_allow_nil
Topic.validates_inclusion_of( :title, :in => %w( a b c d e f g ), :allow_nil=>true )
assert !Topic.create("title" => "a!", "content" => "abc").valid?
assert !Topic.create("title" => "", "content" => "abc").valid?
assert Topic.create("title" => nil, "content" => "abc").valid?
assert Topic.new("title" => "a!", "content" => "abc").invalid?
assert Topic.new("title" => "", "content" => "abc").invalid?
assert Topic.new("title" => nil, "content" => "abc").valid?
end
def test_validates_inclusion_of_with_formatted_message
Topic.validates_inclusion_of( :title, :in => %w( a b c d e f g ), :message => "option %{value} is not in the list" )
assert Topic.create("title" => "a", "content" => "abc").valid?
assert Topic.new("title" => "a", "content" => "abc").valid?
t = Topic.create("title" => "uhoh", "content" => "abc")
assert !t.valid?
t = Topic.new("title" => "uhoh", "content" => "abc")
assert t.invalid?
assert t.errors[:title].any?
assert_equal ["option uhoh is not in the list"], t.errors[:title]
end

View File

@ -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

View File

@ -33,8 +33,8 @@ class NumericalityValidationTest < ActiveModel::TestCase
def test_validates_numericality_of_with_nil_allowed
Topic.validates_numericality_of :approved, :allow_nil => true
invalid!(JUNK)
valid!(NIL + BLANK + FLOATS + INTEGERS + BIGDECIMAL + INFINITY)
invalid!(JUNK + BLANK)
valid!(NIL + FLOATS + INTEGERS + BIGDECIMAL + INFINITY)
end
def test_validates_numericality_of_with_integer_only
@ -47,8 +47,8 @@ class NumericalityValidationTest < ActiveModel::TestCase
def test_validates_numericality_of_with_integer_only_and_nil_allowed
Topic.validates_numericality_of :approved, :only_integer => true, :allow_nil => true
invalid!(JUNK + FLOATS + BIGDECIMAL + INFINITY)
valid!(NIL + BLANK + INTEGERS)
invalid!(JUNK + BLANK + FLOATS + BIGDECIMAL + INFINITY)
valid!(NIL + INTEGERS)
end
def test_validates_numericality_with_greater_than
@ -166,7 +166,7 @@ class NumericalityValidationTest < ActiveModel::TestCase
def invalid!(values, error = nil)
with_each_topic_approved_value(values) do |topic, value|
assert !topic.valid?, "#{value.inspect} not rejected as a number"
assert topic.invalid?, "#{value.inspect} not rejected as a number"
assert topic.errors[:approved].any?, "FAILED for #{value.inspect}"
assert_equal error, topic.errors[:approved].first if error
end

View File

@ -19,26 +19,26 @@ class PresenceValidationTest < ActiveModel::TestCase
def test_validate_presences
Topic.validates_presence_of(:title, :content)
t = Topic.create
assert !t.save
t = Topic.new
assert t.invalid?
assert_equal ["can't be blank"], t.errors[:title]
assert_equal ["can't be blank"], t.errors[:content]
t.title = "something"
t.content = " "
assert !t.save
assert t.invalid?
assert_equal ["can't be blank"], t.errors[:content]
t.content = "like stuff"
assert t.save
assert t.valid?
end
test 'accepts array arguments' do
Topic.validates_presence_of %w(title content)
t = Topic.new
assert !t.valid?
assert t.invalid?
assert_equal ["can't be blank"], t.errors[:title]
assert_equal ["can't be blank"], t.errors[:content]
end
@ -46,7 +46,7 @@ class PresenceValidationTest < ActiveModel::TestCase
def test_validates_acceptance_of_with_custom_error_using_quotes
Person.validates_presence_of :karma, :message => "This string contains 'single' and \"double\" quotes"
p = Person.new
assert !p.valid?
assert p.invalid?
assert_equal "This string contains 'single' and \"double\" quotes", p.errors[:karma].last
end

View File

@ -0,0 +1,41 @@
# encoding: utf-8
require 'cases/helper'
require 'cases/tests_database'
require 'models/topic'
class ValidationsContextTest < ActiveRecord::TestCase
include ActiveModel::TestsDatabase
def teardown
Topic.reset_callbacks(:validate)
Topic._validators.clear
end
ERROR_MESSAGE = "Validation error from validator"
class ValidatorThatAddsErrors < ActiveModel::Validator
def validate(record)
record.errors[:base] << ERROR_MESSAGE
end
end
test "with a class that adds errors on update and validating a new model with no arguments" do
Topic.validates_with(ValidatorThatAddsErrors, :on => :create)
topic = Topic.new
assert topic.valid?, "Validation doesn't run on create if 'on' is set to update"
end
test "with a class that adds errors on update and validating a new model" do
Topic.validates_with(ValidatorThatAddsErrors, :on => :update)
topic = Topic.new
assert topic.valid?(:create), "Validation doesn't run on create if 'on' is set to update"
end
test "with a class that adds errors on create and validating a new model" do
Topic.validates_with(ValidatorThatAddsErrors, :on => :create)
topic = Topic.new
assert topic.invalid?(:create), "Validation does run on create if 'on' is set to create"
assert topic.errors[:base].include?(ERROR_MESSAGE)
end
end

View File

@ -4,7 +4,7 @@ require 'cases/tests_database'
require 'models/topic'
class ValidatesWithTest < ActiveRecord::TestCase
class ValidatesWithTest < ActiveModel::TestCase
include ActiveModel::TestsDatabase
def teardown
@ -55,7 +55,7 @@ class ValidatesWithTest < ActiveRecord::TestCase
test "vaidation with class that adds errors" do
Topic.validates_with(ValidatorThatAddsErrors)
topic = Topic.new
assert !topic.valid?, "A class that adds errors causes the record to be invalid"
assert topic.invalid?, "A class that adds errors causes the record to be invalid"
assert topic.errors[:base].include?(ERROR_MESSAGE)
end
@ -65,23 +65,10 @@ class ValidatesWithTest < ActiveRecord::TestCase
assert topic.valid?, "A class that does not add errors does not cause the record to be invalid"
end
test "with a class that adds errors on update and a new record" do
Topic.validates_with(ValidatorThatAddsErrors, :on => :update)
topic = Topic.new
assert topic.valid?, "Validation doesn't run on create if 'on' is set to update"
end
test "with a class that adds errors on create and a new record" do
Topic.validates_with(ValidatorThatAddsErrors, :on => :create)
topic = Topic.new
assert !topic.valid?, "Validation does run on create if 'on' is set to create"
assert topic.errors[:base].include?(ERROR_MESSAGE)
end
test "with multiple classes" do
Topic.validates_with(ValidatorThatAddsErrors, OtherValidatorThatAddsErrors)
topic = Topic.new
assert !topic.valid?
assert topic.invalid?
assert topic.errors[:base].include?(ERROR_MESSAGE)
assert topic.errors[:base].include?(OTHER_ERROR_MESSAGE)
end
@ -95,7 +82,7 @@ class ValidatesWithTest < ActiveRecord::TestCase
test "with if statements that return true" do
Topic.validates_with(ValidatorThatAddsErrors, :if => "1 == 1")
topic = Topic.new
assert !topic.valid?
assert topic.invalid?
assert topic.errors[:base].include?(ERROR_MESSAGE)
end
@ -108,7 +95,7 @@ class ValidatesWithTest < ActiveRecord::TestCase
test "with unless statements that returns false" do
Topic.validates_with(ValidatorThatAddsErrors, :unless => "1 == 2")
topic = Topic.new
assert !topic.valid?
assert topic.invalid?
assert topic.errors[:base].include?(ERROR_MESSAGE)
end
@ -121,7 +108,7 @@ class ValidatesWithTest < ActiveRecord::TestCase
Topic.validates_with(validator, :if => "1 == 1", :foo => :bar)
assert topic.valid?
end
test "calls setup method of validator passing in self when validator has setup method" do
topic = Topic.new
validator = stub_everything
@ -132,7 +119,7 @@ class ValidatesWithTest < ActiveRecord::TestCase
Topic.validates_with(validator)
assert topic.valid?
end
test "doesn't call setup method of validator when validator has no setup method" do
topic = Topic.new
validator = stub_everything
@ -147,14 +134,14 @@ class ValidatesWithTest < ActiveRecord::TestCase
test "validates_with with options" do
Topic.validates_with(ValidatorThatValidatesOptions, :field => :first_name)
topic = Topic.new
assert !topic.valid?
assert topic.invalid?
assert topic.errors[:base].include?(ERROR_MESSAGE)
end
test "validates_with each validator" do
Topic.validates_with(ValidatorPerEachAttribute, :attributes => [:title, :content])
topic = Topic.new :title => "Title", :content => "Content"
assert !topic.valid?
assert topic.invalid?
assert_equal ["Value is Title"], topic.errors[:title]
assert_equal ["Value is Content"], topic.errors[:content]
end
@ -174,7 +161,7 @@ class ValidatesWithTest < ActiveRecord::TestCase
test "each validator skip nil values if :allow_nil is set to true" do
Topic.validates_with(ValidatorPerEachAttribute, :attributes => [:title, :content], :allow_nil => true)
topic = Topic.new :content => ""
assert !topic.valid?
assert topic.invalid?
assert topic.errors[:title].empty?
assert_equal ["Value is "], topic.errors[:content]
end

View File

@ -23,7 +23,7 @@ class ValidationsTest < ActiveModel::TestCase
def test_single_field_validation
r = Reply.new
r.title = "There's no content!"
assert !r.valid?, "A reply without content shouldn't be saveable"
assert r.invalid?, "A reply without content shouldn't be saveable"
r.content = "Messa content!"
assert r.valid?, "A reply with content should be saveable"
@ -32,46 +32,46 @@ class ValidationsTest < ActiveModel::TestCase
def test_single_attr_validation_and_error_msg
r = Reply.new
r.title = "There's no content!"
assert !r.valid?
assert r.invalid?
assert r.errors[:content].any?, "A reply without content should mark that attribute as invalid"
assert_equal ["Empty"], r.errors["content"], "A reply without content should contain an error"
assert_equal ["is Empty"], r.errors["content"], "A reply without content should contain an error"
assert_equal 1, r.errors.count
end
def test_double_attr_validation_and_error_msg
r = Reply.new
assert !r.valid?
assert r.invalid?
assert r.errors[:title].any?, "A reply without title should mark that attribute as invalid"
assert_equal ["Empty"], r.errors["title"], "A reply without title should contain an error"
assert_equal ["is Empty"], r.errors["title"], "A reply without title should contain an error"
assert r.errors[:content].any?, "A reply without content should mark that attribute as invalid"
assert_equal ["Empty"], r.errors["content"], "A reply without content should contain an error"
assert_equal ["is Empty"], r.errors["content"], "A reply without content should contain an error"
assert_equal 2, r.errors.count
end
def test_single_error_per_attr_iteration
r = Reply.new
r.save
r.valid?
errors = []
r.errors.each {|attr, messages| errors << [attr.to_s, messages] }
assert errors.include?(["title", "Empty"])
assert errors.include?(["content", "Empty"])
assert errors.include?(["title", "is Empty"])
assert errors.include?(["content", "is 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
r.title = ""
r.content = ""
r.valid?
errors = r.errors.to_a
assert_equal "Title is Wrong Create", errors[0]
assert_equal "Title is Content Mismatch", errors[1]
assert_equal "Content is Empty", errors[0]
assert_equal "Title is Empty", errors[1]
assert_equal 2, r.errors.count
end
@ -84,7 +84,7 @@ class ValidationsTest < ActiveModel::TestCase
def test_errors_on_base
r = Reply.new
r.content = "Mismatch"
r.save
r.valid?
r.errors[:base] << "Reply is not dignifying"
errors = []
@ -92,7 +92,7 @@ class ValidationsTest < ActiveModel::TestCase
assert_equal ["Reply is not dignifying"], r.errors[:base]
assert errors.include?("Title Empty")
assert errors.include?("Title is Empty")
assert errors.include?("Reply is not dignifying")
assert_equal 2, r.errors.count
end
@ -110,12 +110,12 @@ class ValidationsTest < ActiveModel::TestCase
hits += 1
end
t = Topic.new("title" => "valid", "content" => "whatever")
assert !t.save
assert t.invalid?
assert_equal 4, hits
assert_equal %w(gotcha gotcha), t.errors[:title]
assert_equal %w(gotcha gotcha), t.errors[:content]
end
def test_validates_each_custom_reader
hits = 0
CustomReader.validates_each(:title, :content, [:title, :content]) do |record, attr|
@ -123,7 +123,7 @@ class ValidationsTest < ActiveModel::TestCase
hits += 1
end
t = CustomReader.new("title" => "valid", "content" => "whatever")
assert !t.valid?
assert t.invalid?
assert_equal 4, hits
assert_equal %w(gotcha gotcha), t.errors[:title]
assert_equal %w(gotcha gotcha), t.errors[:content]
@ -131,39 +131,41 @@ class ValidationsTest < ActiveModel::TestCase
def test_validate_block
Topic.validate { |topic| topic.errors.add("title", "will never be valid") }
t = Topic.create("title" => "Title", "content" => "whatever")
assert !t.valid?
t = Topic.new("title" => "Title", "content" => "whatever")
assert t.invalid?
assert t.errors[:title].any?
assert_equal ["will never be valid"], t.errors["title"]
end
def test_invalid_validator
Topic.validate :i_dont_exist
assert_raise(NameError) { t = Topic.create }
assert_raise(NameError) do
t = Topic.new
t.valid?
end
end
def test_errors_to_xml
r = Reply.new :title => "Wrong Create"
assert !r.valid?
assert r.invalid?
xml = r.errors.to_xml(:skip_instruct => true)
assert_equal "<errors>", xml.first(8)
assert xml.include?("<error>Title is Wrong Create</error>")
assert xml.include?("<error>Content Empty</error>")
assert xml.include?("<error>Content is Empty</error>")
end
def test_validation_order
Topic.validates_presence_of :title
Topic.validates_length_of :title, :minimum => 2
Topic.validates_presence_of :title
Topic.validates_length_of :title, :minimum => 2
t = Topic.new("title" => "")
assert !t.valid?
assert_equal "can't be blank", t.errors["title"].first
t = Topic.new("title" => "")
assert t.invalid?
assert_equal "can't be blank", t.errors["title"].first
Topic.validates_presence_of :title, :author_name
Topic.validate {|topic| topic.errors.add('author_email_address', 'will never be valid')}
Topic.validates_length_of :title, :content, :minimum => 2
t = Topic.new :title => ''
assert !t.valid?
assert t.invalid?
assert_equal :title, key = t.errors.keys.first
assert_equal "can't be blank", t.errors[key].first
@ -227,7 +229,7 @@ class ValidationsTest < ActiveModel::TestCase
Topic.validates_presence_of(:title, :message => proc { "no blanks here".upcase })
t = Topic.new
assert !t.valid?
assert t.invalid?
assert ["NO BLANKS HERE"], t.errors[:title]
end

View File

@ -2,33 +2,31 @@ require 'models/topic'
class Reply < Topic
validate :errors_on_empty_content
validate :title_is_wrong_create, :on => :create
validate :title_is_wrong_create, :on => :create
validate :check_empty_title
validate :check_content_mismatch, :on => :create
validate :check_wrong_update, :on => :update
attr_accessible :title, :author_name, :author_email_address, :written_on, :content, :last_read
validate :check_wrong_update, :on => :update
def check_empty_title
errors[:title] << "Empty" unless attribute_present?("title")
errors[:title] << "is Empty" unless title && title.size > 0
end
def errors_on_empty_content
errors[:content] << "Empty" unless attribute_present?("content")
errors[:content] << "is Empty" unless content && content.size > 0
end
def check_content_mismatch
if attribute_present?("title") && attribute_present?("content") && content == "Mismatch"
if title && content && content == "Mismatch"
errors[:title] << "is Content Mismatch"
end
end
def title_is_wrong_create
errors[:title] << "is Wrong Create" if attribute_present?("title") && title == "Wrong Create"
errors[:title] << "is Wrong Create" if title && title == "Wrong Create"
end
def check_wrong_update
errors[:title] << "is Wrong Update" if attribute_present?("title") && title == "Wrong Update"
errors[:title] << "is Wrong Update" if title && title == "Wrong Update"
end
end

View File

@ -1,4 +1,14 @@
class Topic < ActiveRecord::Base
class Topic
include ActiveModel::Validations
attr_accessor :title, :author_name, :content, :approved
def initialize(attributes = {})
attributes.each do |key, value|
send "#{key}=", value
end
end
def condition_is_true
true
end