test/cases/validations_test.rb in ibm_db-1.0.2 vs test/cases/validations_test.rb in ibm_db-1.0.5
- old
+ new
@@ -4,10 +4,13 @@
require 'models/reply'
require 'models/person'
require 'models/developer'
require 'models/warehouse_thing'
require 'models/guid'
+require 'models/owner'
+require 'models/pet'
+require 'models/event'
# The following methods in Topic are used in test_conditional_validation_*
class Topic
has_many :unique_replies, :dependent => :destroy, :foreign_key => "parent_id"
has_many :silly_unique_replies, :dependent => :destroy, :foreign_key => "parent_id"
@@ -29,14 +32,10 @@
class UniqueReply < Reply
validates_uniqueness_of :content, :scope => 'parent_id'
end
-class PlagiarizedReply < Reply
- validates_acceptance_of :author_name
-end
-
class SillyUniqueReply < UniqueReply
end
class Wizard < ActiveRecord::Base
self.abstract_class = true
@@ -56,15 +55,13 @@
class ValidationsTest < ActiveRecord::TestCase
fixtures :topics, :developers, :warehouse_things
- def setup
- Topic.instance_variable_set("@validate_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
- Topic.instance_variable_set("@validate_on_create_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
- Topic.instance_variable_set("@validate_on_update_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
- end
+ # Most of the tests mess with the validations of Topic, so lets repair it all the time.
+ # Other classes we mess with will be dealt with in the specific tests
+ repair_validations(Topic)
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"
@@ -115,12 +112,12 @@
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_invalid_record_exception
- assert_raises(ActiveRecord::RecordInvalid) { Reply.create! }
- assert_raises(ActiveRecord::RecordInvalid) { Reply.new.save! }
+ assert_raise(ActiveRecord::RecordInvalid) { Reply.create! }
+ assert_raise(ActiveRecord::RecordInvalid) { Reply.new.save! }
begin
r = Reply.new
r.save!
flunk
@@ -128,34 +125,34 @@
assert_equal r, invalid.record
end
end
def test_exception_on_create_bang_many
- assert_raises(ActiveRecord::RecordInvalid) do
+ assert_raise(ActiveRecord::RecordInvalid) do
Reply.create!([ { "title" => "OK" }, { "title" => "Wrong Create" }])
end
end
def test_exception_on_create_bang_with_block
- assert_raises(ActiveRecord::RecordInvalid) do
+ assert_raise(ActiveRecord::RecordInvalid) do
Reply.create!({ "title" => "OK" }) do |r|
r.content = nil
end
end
end
def test_exception_on_create_bang_many_with_block
- assert_raises(ActiveRecord::RecordInvalid) do
+ assert_raise(ActiveRecord::RecordInvalid) do
Reply.create!([{ "title" => "OK" }, { "title" => "Wrong Create" }]) do |r|
r.content = nil
end
end
end
def test_scoped_create_without_attributes
Reply.with_scope(:create => {}) do
- assert_raises(ActiveRecord::RecordInvalid) { Reply.create! }
+ assert_raise(ActiveRecord::RecordInvalid) { Reply.create! }
end
end
def test_create_with_exceptions_using_scope_for_protected_attributes
assert_nothing_raised do
@@ -171,11 +168,11 @@
ProtectedPerson.with_scope( :create => { :first_name => "Mary" } ) do
person = ProtectedPerson.create!
assert_equal person.first_name, "Mary", "should be ok when no attributes are passed to create!"
end
end
- end
+ end
def test_single_error_per_attr_iteration
r = Reply.new
r.save
@@ -227,25 +224,20 @@
assert_nothing_raised { Reply.new.save_without_validation! }
assert count+1, Reply.count
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
+ record.errors.add attr, 'gotcha'
+ hits += 1
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_no_title_confirmation
Topic.validates_confirmation_of(:title)
@@ -313,12 +305,16 @@
t.terms_of_service = "I agree."
assert t.save
end
def test_validates_acceptance_of_as_database_column
- reply = PlagiarizedReply.create("author_name" => "Dan Brown")
- assert_equal "Dan Brown", reply["author_name"]
+ repair_validations(Reply) do
+ Reply.validates_acceptance_of(:author_name)
+
+ reply = Reply.create("author_name" => "Dan Brown")
+ assert_equal "Dan Brown", reply["author_name"]
+ end
end
def test_validates_acceptance_of_with_non_existant_table
Object.const_set :IncorporealModel, Class.new(ActiveRecord::Base)
@@ -370,26 +366,28 @@
t = Topic.new("title" => "new\nline")
assert t.save, "Should save t as unique"
end
def test_validate_uniqueness_with_scope
- Reply.validates_uniqueness_of(:content, :scope => "parent_id")
+ repair_validations(Reply) do
+ Reply.validates_uniqueness_of(:content, :scope => "parent_id")
- t = Topic.create("title" => "I'm unique!")
+ t = Topic.create("title" => "I'm unique!")
- r1 = t.replies.create "title" => "r1", "content" => "hello world"
- assert r1.valid?, "Saving r1"
+ r1 = t.replies.create "title" => "r1", "content" => "hello world"
+ assert r1.valid?, "Saving r1"
- r2 = t.replies.create "title" => "r2", "content" => "hello world"
- assert !r2.valid?, "Saving r2 first time"
+ r2 = t.replies.create "title" => "r2", "content" => "hello world"
+ assert !r2.valid?, "Saving r2 first time"
- r2.content = "something else"
- assert r2.save, "Saving r2 second time"
+ r2.content = "something else"
+ assert r2.save, "Saving r2 second time"
- t2 = Topic.create("title" => "I'm unique too!")
- r3 = t2.replies.create "title" => "r3", "content" => "hello world"
- assert r3.valid?, "Saving r3"
+ t2 = Topic.create("title" => "I'm unique too!")
+ r3 = t2.replies.create "title" => "r3", "content" => "hello world"
+ assert r3.valid?, "Saving r3"
+ end
end
def test_validate_uniqueness_scoped_to_defining_class
t = Topic.create("title" => "What, me worry?")
@@ -404,31 +402,33 @@
r3 = t.replies.create "title" => "r2", "content" => "a barrel of fun"
assert r3.valid?, "Saving r3"
end
def test_validate_uniqueness_with_scope_array
- Reply.validates_uniqueness_of(:author_name, :scope => [:author_email_address, :parent_id])
+ repair_validations(Reply) do
+ Reply.validates_uniqueness_of(:author_name, :scope => [:author_email_address, :parent_id])
- t = Topic.create("title" => "The earth is actually flat!")
+ t = Topic.create("title" => "The earth is actually flat!")
- r1 = t.replies.create "author_name" => "jeremy", "author_email_address" => "jeremy@rubyonrails.com", "title" => "You're crazy!", "content" => "Crazy reply"
- assert r1.valid?, "Saving r1"
+ r1 = t.replies.create "author_name" => "jeremy", "author_email_address" => "jeremy@rubyonrails.com", "title" => "You're crazy!", "content" => "Crazy reply"
+ assert r1.valid?, "Saving r1"
- r2 = t.replies.create "author_name" => "jeremy", "author_email_address" => "jeremy@rubyonrails.com", "title" => "You're crazy!", "content" => "Crazy reply again..."
- assert !r2.valid?, "Saving r2. Double reply by same author."
+ r2 = t.replies.create "author_name" => "jeremy", "author_email_address" => "jeremy@rubyonrails.com", "title" => "You're crazy!", "content" => "Crazy reply again..."
+ assert !r2.valid?, "Saving r2. Double reply by same author."
- r2.author_email_address = "jeremy_alt_email@rubyonrails.com"
- assert r2.save, "Saving r2 the second time."
+ r2.author_email_address = "jeremy_alt_email@rubyonrails.com"
+ assert r2.save, "Saving r2 the second time."
- r3 = t.replies.create "author_name" => "jeremy", "author_email_address" => "jeremy_alt_email@rubyonrails.com", "title" => "You're wrong", "content" => "It's cubic"
- assert !r3.valid?, "Saving r3"
+ r3 = t.replies.create "author_name" => "jeremy", "author_email_address" => "jeremy_alt_email@rubyonrails.com", "title" => "You're wrong", "content" => "It's cubic"
+ assert !r3.valid?, "Saving r3"
- r3.author_name = "jj"
- assert r3.save, "Saving r3 the second time."
+ r3.author_name = "jj"
+ assert r3.save, "Saving r3 the second time."
- r3.author_name = "jeremy"
- assert !r3.save, "Saving r3 the third time."
+ r3.author_name = "jeremy"
+ assert !r3.save, "Saving r3 the third time."
+ end
end
def test_validate_case_insensitive_uniqueness
Topic.validates_uniqueness_of(:title, :parent_id, :case_sensitive => false, :allow_nil => true)
@@ -521,16 +521,26 @@
assert !t2.valid?
end
end
def test_validate_uniqueness_with_columns_which_are_sql_keywords
- Guid.validates_uniqueness_of :key
- g = Guid.new
- g.key = "foo"
- assert_nothing_raised { !g.valid? }
+ repair_validations(Guid) do
+ Guid.validates_uniqueness_of :key
+ g = Guid.new
+ g.key = "foo"
+ assert_nothing_raised { !g.valid? }
+ end
end
+ def test_validate_uniqueness_with_limit
+ # Event.title is limited to 5 characters
+ e1 = Event.create(:title => "abcde")
+ assert e1.valid?, "Could not create an event with a unique, 5 character title"
+ e2 = Event.create(:title => "abcdefgh")
+ assert !e2.valid?, "Created an event whose title, with limit taken into account, is not unique"
+ end
+
def test_validate_straight_inheritance_uniqueness
w1 = IneptWizard.create(:name => "Rincewind", :city => "Ankh-Morpork")
assert w1.valid?, "Saving w1"
# Should use validation from base class (which is abstract)
@@ -646,14 +656,16 @@
assert !Topic.create("title" => "", "content" => "abc").valid?
assert Topic.create("title" => nil, "content" => "abc").valid?
end
def test_numericality_with_getter_method
- Developer.validates_numericality_of( :salary )
- developer = Developer.new("name" => "michael", "salary" => nil)
- developer.instance_eval("def salary; read_attribute('salary') ? read_attribute('salary') : 100000; end")
- assert developer.valid?
+ repair_validations(Developer) do
+ Developer.validates_numericality_of( :salary )
+ developer = Developer.new("name" => "michael", "salary" => nil)
+ developer.instance_eval("def salary; read_attribute('salary') ? read_attribute('salary') : 100000; end")
+ assert developer.valid?
+ end
end
def test_validates_length_of_with_allow_nil
Topic.validates_length_of( :title, :is => 5, :allow_nil=>true )
@@ -682,14 +694,16 @@
assert t.errors.on(:title)
assert_equal "option uhoh is not in the list", t.errors["title"]
end
def test_numericality_with_allow_nil_and_getter_method
- Developer.validates_numericality_of( :salary, :allow_nil => true)
- developer = Developer.new("name" => "michael", "salary" => nil)
- developer.instance_eval("def salary; read_attribute('salary') ? read_attribute('salary') : 100000; end")
- assert developer.valid?
+ repair_validations(Developer) do
+ Developer.validates_numericality_of( :salary, :allow_nil => true)
+ developer = Developer.new("name" => "michael", "salary" => nil)
+ developer.instance_eval("def salary; read_attribute('salary') ? read_attribute('salary') : 100000; end")
+ assert developer.valid?
+ end
end
def test_validates_exclusion_of
Topic.validates_exclusion_of( :title, :in => %w( abe monkey ) )
@@ -890,30 +904,34 @@
assert_equal 'tu est trops petit hombre 10', t.errors['title']
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)
- reply = t.replies.build('title' => 'areply', 'content' => 'whateveragain')
- assert t.valid?
+ 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.on(:pets)
+ pet = o.pets.build('name' => 'apet')
+ assert o.valid?
+ end
end
def test_validates_size_of_association_using_within
- assert_nothing_raised { Topic.validates_size_of :replies, :within => 1..2 }
- t = Topic.new('title' => 'noreplies', 'content' => 'whatever')
- assert !t.save
- assert t.errors.on(:replies)
+ 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.on(:pets)
- reply = t.replies.build('title' => 'areply', 'content' => 'whateveragain')
- assert t.valid?
+ pet = o.pets.build('name' => 'apet')
+ assert o.valid?
- 2.times { t.replies.build('title' => 'areply', 'content' => 'whateveragain') }
- assert !t.save
- assert t.errors.on(:replies)
+ 2.times { o.pets.build('name' => 'apet') }
+ assert !o.save
+ assert o.errors.on(:pets)
+ end
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) }
@@ -947,10 +965,23 @@
assert !t.valid?
assert t.errors.on(:title)
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?
+ assert t.errors.on(:title)
+ assert_equal "hoo 10", t.errors["title"]
+
+ t = Topic.create("title" => "uhohuhohuhohuhohuhohuhohuhohuhoh", "content" => "whatever")
+ assert !t.valid?
+ assert t.errors.on(:title)
+ 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?
assert t.errors.on(:title)
@@ -1100,17 +1131,19 @@
assert t.errors.on(:content)
assert_equal "Your essay must be at least 5 words.", t.errors[:content]
end
def test_validates_size_of_association_utf8
- with_kcode('UTF8') do
- assert_nothing_raised { Topic.validates_size_of :replies, :minimum => 1 }
- t = Topic.new('title' => 'あいうえお', 'content' => 'かきくけこ')
- assert !t.save
- assert t.errors.on(:replies)
- t.replies.build('title' => 'あいうえお', 'content' => 'かきくけこ')
- assert t.valid?
+ repair_validations(Owner) do
+ with_kcode('UTF8') do
+ assert_nothing_raised { Owner.validates_size_of :pets, :minimum => 1 }
+ o = Owner.new('name' => 'あいうえおかきくけこ')
+ assert !o.save
+ assert o.errors.on(:pets)
+ o.pets.build('name' => 'あいうえおかきくけこ')
+ assert o.valid?
+ end
end
end
def test_validates_associated_many
Topic.validates_associated( :replies )
@@ -1125,18 +1158,20 @@
r.content = r3.content = "non-empty"
assert t.valid?
end
def test_validates_associated_one
- Reply.validates_associated( :topic )
- Topic.validates_presence_of( :content )
- r = Reply.new("title" => "A reply", "content" => "with content!")
- r.topic = Topic.create("title" => "uhohuhoh")
- assert !r.valid?
- assert r.errors.on(:topic)
- r.topic.content = "non-empty"
- assert r.valid?
+ repair_validations(Reply) do
+ Reply.validates_associated( :topic )
+ Topic.validates_presence_of( :content )
+ r = Reply.new("title" => "A reply", "content" => "with content!")
+ r.topic = Topic.create("title" => "uhohuhoh")
+ assert !r.valid?
+ assert r.errors.on(:topic)
+ r.topic.content = "non-empty"
+ assert r.valid?
+ end
end
def test_validate_block
Topic.validate { |topic| topic.errors.add("title", "will never be valid") }
t = Topic.create("title" => "Title", "content" => "whatever")
@@ -1156,89 +1191,109 @@
assert_equal 100, d.salary
assert_equal "100,000", d.salary_before_type_cast
end
def test_validates_acceptance_of_with_custom_error_using_quotes
- Developer.validates_acceptance_of :salary, :message=> "This string contains 'single' and \"double\" quotes"
- d = Developer.new
- d.salary = "0"
- assert !d.valid?
- assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:salary).last
+ repair_validations(Developer) do
+ Developer.validates_acceptance_of :salary, :message=> "This string contains 'single' and \"double\" quotes"
+ d = Developer.new
+ d.salary = "0"
+ assert !d.valid?
+ assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:salary).last
+ end
end
def test_validates_confirmation_of_with_custom_error_using_quotes
- Developer.validates_confirmation_of :name, :message=> "confirm 'single' and \"double\" quotes"
- d = Developer.new
- d.name = "John"
- d.name_confirmation = "Johnny"
- assert !d.valid?
- assert_equal "confirm 'single' and \"double\" quotes", d.errors.on(:name)
+ repair_validations(Developer) do
+ Developer.validates_confirmation_of :name, :message=> "confirm 'single' and \"double\" quotes"
+ d = Developer.new
+ d.name = "John"
+ d.name_confirmation = "Johnny"
+ assert !d.valid?
+ assert_equal "confirm 'single' and \"double\" quotes", d.errors.on(:name)
+ end
end
def test_validates_format_of_with_custom_error_using_quotes
- Developer.validates_format_of :name, :with => /^(A-Z*)$/, :message=> "format 'single' and \"double\" quotes"
- d = Developer.new
- d.name = d.name_confirmation = "John 32"
- assert !d.valid?
- assert_equal "format 'single' and \"double\" quotes", d.errors.on(:name)
+ repair_validations(Developer) do
+ Developer.validates_format_of :name, :with => /^(A-Z*)$/, :message=> "format 'single' and \"double\" quotes"
+ d = Developer.new
+ d.name = d.name_confirmation = "John 32"
+ assert !d.valid?
+ assert_equal "format 'single' and \"double\" quotes", d.errors.on(:name)
+ end
end
def test_validates_inclusion_of_with_custom_error_using_quotes
- Developer.validates_inclusion_of :salary, :in => 1000..80000, :message=> "This string contains 'single' and \"double\" quotes"
- d = Developer.new
- d.salary = "90,000"
- assert !d.valid?
- assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:salary).last
+ repair_validations(Developer) do
+ Developer.validates_inclusion_of :salary, :in => 1000..80000, :message=> "This string contains 'single' and \"double\" quotes"
+ d = Developer.new
+ d.salary = "90,000"
+ assert !d.valid?
+ assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:salary).last
+ end
end
def test_validates_length_of_with_custom_too_long_using_quotes
- Developer.validates_length_of :name, :maximum => 4, :too_long=> "This string contains 'single' and \"double\" quotes"
- d = Developer.new
- d.name = "Jeffrey"
- assert !d.valid?
- assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:name).last
+ repair_validations(Developer) do
+ Developer.validates_length_of :name, :maximum => 4, :too_long=> "This string contains 'single' and \"double\" quotes"
+ d = Developer.new
+ d.name = "Jeffrey"
+ assert !d.valid?
+ assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:name)
+ end
end
def test_validates_length_of_with_custom_too_short_using_quotes
- Developer.validates_length_of :name, :minimum => 4, :too_short=> "This string contains 'single' and \"double\" quotes"
- d = Developer.new
- d.name = "Joe"
- assert !d.valid?
- assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:name).last
+ repair_validations(Developer) do
+ Developer.validates_length_of :name, :minimum => 4, :too_short=> "This string contains 'single' and \"double\" quotes"
+ d = Developer.new
+ d.name = "Joe"
+ assert !d.valid?
+ assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:name)
+ end
end
def test_validates_length_of_with_custom_message_using_quotes
- Developer.validates_length_of :name, :minimum => 4, :message=> "This string contains 'single' and \"double\" quotes"
- d = Developer.new
- d.name = "Joe"
- assert !d.valid?
- assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:name).last
+ repair_validations(Developer) do
+ Developer.validates_length_of :name, :minimum => 4, :message=> "This string contains 'single' and \"double\" quotes"
+ d = Developer.new
+ d.name = "Joe"
+ assert !d.valid?
+ assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:name)
+ end
end
def test_validates_presence_of_with_custom_message_using_quotes
- Developer.validates_presence_of :non_existent, :message=> "This string contains 'single' and \"double\" quotes"
- d = Developer.new
- d.name = "Joe"
- assert !d.valid?
- assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:non_existent)
+ repair_validations(Developer) do
+ Developer.validates_presence_of :non_existent, :message=> "This string contains 'single' and \"double\" quotes"
+ d = Developer.new
+ d.name = "Joe"
+ assert !d.valid?
+ assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:non_existent)
+ end
end
def test_validates_uniqueness_of_with_custom_message_using_quotes
- Developer.validates_uniqueness_of :name, :message=> "This string contains 'single' and \"double\" quotes"
- d = Developer.new
- d.name = "David"
- assert !d.valid?
- assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:name).last
+ repair_validations(Developer) do
+ Developer.validates_uniqueness_of :name, :message=> "This string contains 'single' and \"double\" quotes"
+ d = Developer.new
+ d.name = "David"
+ assert !d.valid?
+ assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:name)
+ end
end
def test_validates_associated_with_custom_message_using_quotes
- Reply.validates_associated :topic, :message=> "This string contains 'single' and \"double\" quotes"
- Topic.validates_presence_of :content
- r = Reply.create("title" => "A reply", "content" => "with content!")
- r.topic = Topic.create("title" => "uhohuhoh")
- assert !r.valid?
- assert_equal "This string contains 'single' and \"double\" quotes", r.errors.on(:topic).last
+ repair_validations(Reply) do
+ Reply.validates_associated :topic, :message=> "This string contains 'single' and \"double\" quotes"
+ Topic.validates_presence_of :content
+ r = Reply.create("title" => "A reply", "content" => "with content!")
+ r.topic = Topic.create("title" => "uhohuhoh")
+ assert !r.valid?
+ assert_equal "This string contains 'single' and \"double\" quotes", r.errors.on(:topic)
+ end
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 )
@@ -1344,17 +1399,19 @@
assert t.errors.on(:title)
assert_equal "hoo 5", t.errors["title"]
end
def test_validates_associated_missing
- Reply.validates_presence_of(:topic)
- r = Reply.create("title" => "A reply", "content" => "with content!")
- assert !r.valid?
- assert r.errors.on(:topic)
+ repair_validations(Reply) do
+ Reply.validates_presence_of(:topic)
+ r = Reply.create("title" => "A reply", "content" => "with content!")
+ assert !r.valid?
+ assert r.errors.on(:topic)
- r.topic = Topic.find :first
- assert r.valid?
+ r.topic = Topic.find :first
+ assert r.valid?
+ end
end
def test_errors_to_xml
r = Reply.new :title => "Wrong Create"
assert !r.valid?
@@ -1362,19 +1419,30 @@
assert_equal "<errors>", xml.first(8)
assert xml.include?("<error>Title is Wrong Create</error>")
assert xml.include?("<error>Content Empty</error>")
end
- def test_validation_order
+ def test_validation_order
+ 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.on("title").first
+ end
+
+ def test_invalid_should_be_the_opposite_of_valid
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.on("title").first
- end
+ t = Topic.new
+ assert t.invalid?
+ assert t.errors.invalid?(:title)
+ t.title = 'Things are going to change'
+ assert !t.invalid?
+ end
+
# previous implementation of validates_presence_of eval'd the
# string with the wrong binding, this regression test is to
# ensure that it works correctly
def test_validation_with_if_as_string
Topic.validates_presence_of(:title)
@@ -1392,24 +1460,10 @@
assert t.errors.invalid?("author_name"), "A topic with an 'important' title should require an author"
t.author_name = "Hubert J. Farnsworth"
assert t.valid?, "A topic with an important title and author should be valid"
end
-
- private
- def with_kcode(kcode)
- if RUBY_VERSION < '1.9'
- orig_kcode, $KCODE = $KCODE, kcode
- begin
- yield
- ensure
- $KCODE = orig_kcode
- end
- else
- yield
- end
- end
end
class ValidatesNumericalityTest < ActiveRecord::TestCase
NIL = [nil]
@@ -1421,14 +1475,10 @@
INTEGERS = [0, 10, -10] + INTEGER_STRINGS
BIGDECIMAL = BIGDECIMAL_STRINGS.collect! { |bd| BigDecimal.new(bd) }
JUNK = ["not a number", "42 not a number", "0xdeadbeef", "00-1", "--3", "+-3", "+3-1", "-+019.0", "12.12.13.12", "123\nnot a number"]
INFINITY = [1.0/0.0]
- def setup
- Topic.instance_variable_set("@validate_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
- Topic.instance_variable_set("@validate_on_create_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
- Topic.instance_variable_set("@validate_on_update_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
- end
+ repair_validations(Topic)
def test_default_validates_numericality_of
Topic.validates_numericality_of :approved
invalid!(NIL + BLANK + JUNK)