2008-02-28 15:41:41 -05:00
|
|
|
# encoding: utf-8
|
2011-06-06 14:17:44 -04:00
|
|
|
require "cases/helper"
|
2008-01-18 02:31:37 -05:00
|
|
|
require 'models/topic'
|
|
|
|
require 'models/reply'
|
|
|
|
require 'models/person'
|
|
|
|
require 'models/developer'
|
2010-06-09 23:07:54 -04:00
|
|
|
require 'models/parrot'
|
|
|
|
require 'models/company'
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class ValidationsTest < ActiveRecord::TestCase
|
2009-03-20 11:07:49 -04:00
|
|
|
fixtures :topics, :developers
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2008-12-19 08:27:34 -05:00
|
|
|
# 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)
|
2004-12-10 08:11:13 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def test_error_on_create
|
2009-12-31 21:20:38 -05:00
|
|
|
r = WrongReply.new
|
2004-11-23 20:04:44 -05:00
|
|
|
r.title = "Wrong Create"
|
2010-05-08 16:29:20 -04:00
|
|
|
assert !r.save
|
2009-03-21 14:29:15 -04:00
|
|
|
assert r.errors[:title].any?, "A reply with a bad title should mark that attribute as invalid"
|
2009-03-20 13:36:22 -04:00
|
|
|
assert_equal ["is Wrong Create"], r.errors[:title], "A reply with a bad content should contain an error"
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_error_on_update
|
2009-12-31 21:20:38 -05:00
|
|
|
r = WrongReply.new
|
2004-11-23 20:04:44 -05:00
|
|
|
r.title = "Bad"
|
|
|
|
r.content = "Good"
|
|
|
|
assert r.save, "First save should be successful"
|
2005-02-18 12:27:26 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
r.title = "Wrong Update"
|
|
|
|
assert !r.save, "Second save should fail"
|
2005-02-18 12:27:26 -05:00
|
|
|
|
2009-03-21 14:29:15 -04:00
|
|
|
assert r.errors[:title].any?, "A reply with a bad title should mark that attribute as invalid"
|
2009-03-20 13:36:22 -04:00
|
|
|
assert_equal ["is Wrong Update"], r.errors[:title], "A reply with a bad content should contain an error"
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2005-02-18 12:27:26 -05:00
|
|
|
|
2010-05-10 05:28:38 -04:00
|
|
|
def test_error_on_given_context
|
2010-05-21 10:20:56 -04:00
|
|
|
r = WrongReply.new(:title => "Valid title")
|
2010-05-10 05:28:38 -04:00
|
|
|
assert !r.valid?(:special_case)
|
2010-05-21 10:20:56 -04:00
|
|
|
assert_equal "Invalid", r.errors[:author_name].join
|
2010-05-10 05:28:38 -04:00
|
|
|
|
2010-05-21 10:20:56 -04:00
|
|
|
r.author_name = "secret"
|
2010-05-10 05:28:38 -04:00
|
|
|
r.content = "Good"
|
|
|
|
assert r.valid?(:special_case)
|
|
|
|
|
2010-05-21 10:20:56 -04:00
|
|
|
r.author_name = nil
|
2010-05-10 05:28:38 -04:00
|
|
|
assert !r.save(:context => :special_case)
|
2010-05-21 10:20:56 -04:00
|
|
|
assert_equal "Invalid", r.errors[:author_name].join
|
2010-05-10 05:28:38 -04:00
|
|
|
|
2010-05-21 10:20:56 -04:00
|
|
|
r.author_name = "secret"
|
2010-05-10 05:28:38 -04:00
|
|
|
assert r.save(:context => :special_case)
|
|
|
|
end
|
|
|
|
|
2005-04-18 15:19:23 -04:00
|
|
|
def test_invalid_record_exception
|
2009-12-31 21:20:38 -05:00
|
|
|
assert_raise(ActiveRecord::RecordInvalid) { WrongReply.create! }
|
|
|
|
assert_raise(ActiveRecord::RecordInvalid) { WrongReply.new.save! }
|
2005-11-03 13:54:16 -05:00
|
|
|
|
|
|
|
begin
|
2009-12-31 21:20:38 -05:00
|
|
|
r = WrongReply.new
|
2005-11-03 13:54:16 -05:00
|
|
|
r.save!
|
|
|
|
flunk
|
|
|
|
rescue ActiveRecord::RecordInvalid => invalid
|
|
|
|
assert_equal r, invalid.record
|
|
|
|
end
|
2006-05-18 21:05:20 -04:00
|
|
|
end
|
|
|
|
|
2007-08-28 16:44:51 -04:00
|
|
|
def test_exception_on_create_bang_many
|
2009-03-08 16:11:58 -04:00
|
|
|
assert_raise(ActiveRecord::RecordInvalid) do
|
2009-12-31 21:20:38 -05:00
|
|
|
WrongReply.create!([ { "title" => "OK" }, { "title" => "Wrong Create" }])
|
2007-08-28 16:44:51 -04:00
|
|
|
end
|
|
|
|
end
|
2008-12-19 08:27:34 -05:00
|
|
|
|
2008-05-01 00:14:32 -04:00
|
|
|
def test_exception_on_create_bang_with_block
|
2009-03-08 16:11:58 -04:00
|
|
|
assert_raise(ActiveRecord::RecordInvalid) do
|
2009-12-31 21:20:38 -05:00
|
|
|
WrongReply.create!({ "title" => "OK" }) do |r|
|
2008-05-01 00:14:32 -04:00
|
|
|
r.content = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2008-12-19 08:27:34 -05:00
|
|
|
|
2008-05-01 00:14:32 -04:00
|
|
|
def test_exception_on_create_bang_many_with_block
|
2009-03-08 16:11:58 -04:00
|
|
|
assert_raise(ActiveRecord::RecordInvalid) do
|
2009-12-31 21:20:38 -05:00
|
|
|
WrongReply.create!([{ "title" => "OK" }, { "title" => "Wrong Create" }]) do |r|
|
2008-05-01 00:14:32 -04:00
|
|
|
r.content = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2006-12-05 19:13:31 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def test_create_without_validation
|
2009-12-31 21:20:38 -05:00
|
|
|
reply = WrongReply.new
|
2004-11-23 20:04:44 -05:00
|
|
|
assert !reply.save
|
2010-01-17 08:22:27 -05:00
|
|
|
assert reply.save(:validate => false)
|
|
|
|
end
|
|
|
|
|
2007-12-27 06:16:51 -05:00
|
|
|
def test_validates_acceptance_of_with_non_existant_table
|
|
|
|
Object.const_set :IncorporealModel, Class.new(ActiveRecord::Base)
|
|
|
|
|
|
|
|
assert_nothing_raised ActiveRecord::StatementInvalid do
|
|
|
|
IncorporealModel.validates_acceptance_of(:incorporeal_column)
|
|
|
|
end
|
|
|
|
end
|
2007-12-12 18:55:14 -05:00
|
|
|
|
2004-12-19 06:25:55 -05:00
|
|
|
def test_throw_away_typing
|
2006-09-03 13:54:48 -04:00
|
|
|
d = Developer.new("name" => "David", "salary" => "100,000")
|
2004-12-19 06:25:55 -05:00
|
|
|
assert !d.valid?
|
2004-12-19 06:44:59 -05:00
|
|
|
assert_equal 100, d.salary
|
2004-12-19 06:25:55 -05:00
|
|
|
assert_equal "100,000", d.salary_before_type_cast
|
|
|
|
end
|
2009-03-20 17:40:37 -04:00
|
|
|
|
2009-03-20 18:21:27 -04:00
|
|
|
def test_validates_acceptance_of_as_database_column
|
2010-01-31 22:18:55 -05:00
|
|
|
Topic.validates_acceptance_of(:approved)
|
|
|
|
topic = Topic.create("approved" => true)
|
|
|
|
assert topic["approved"]
|
2009-03-20 18:21:27 -04:00
|
|
|
end
|
2010-01-18 03:56:36 -05:00
|
|
|
|
2010-06-09 23:07:54 -04:00
|
|
|
def test_validators
|
|
|
|
assert_equal 1, Parrot.validators.size
|
|
|
|
assert_equal 1, Company.validators.size
|
|
|
|
assert_equal 1, Parrot.validators_on(:name).size
|
|
|
|
assert_equal 1, Company.validators_on(:name).size
|
|
|
|
end
|
|
|
|
|
2005-09-28 22:35:41 -04:00
|
|
|
end
|