2008-02-28 20:41:41 +00:00
|
|
|
# encoding: utf-8
|
2008-01-21 17:20:51 +00:00
|
|
|
require "cases/helper"
|
2008-01-18 07:31:37 +00:00
|
|
|
require 'models/topic'
|
|
|
|
require 'models/reply'
|
|
|
|
require 'models/person'
|
|
|
|
require 'models/developer'
|
|
|
|
require 'models/warehouse_thing'
|
2008-05-04 11:28:08 +00:00
|
|
|
require 'models/guid'
|
2008-12-19 13:27:34 +00:00
|
|
|
require 'models/owner'
|
|
|
|
require 'models/pet'
|
2009-03-07 13:52:17 +00:00
|
|
|
require 'models/event'
|
2004-11-24 01:04:44 +00:00
|
|
|
|
2006-12-06 00:13:31 +00:00
|
|
|
class ProtectedPerson < ActiveRecord::Base
|
|
|
|
set_table_name 'people'
|
|
|
|
attr_accessor :addon
|
|
|
|
attr_protected :first_name
|
|
|
|
end
|
|
|
|
|
2009-03-21 19:05:09 +00:00
|
|
|
class DeprecatedPerson < ActiveRecord::Base
|
|
|
|
set_table_name 'people'
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def validate
|
|
|
|
errors[:name] << "always invalid"
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_on_create
|
|
|
|
errors[:name] << "invalid on create"
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_on_update
|
|
|
|
errors[:name] << "invalid on update"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-01-21 17:20:51 +00:00
|
|
|
class ValidationsTest < ActiveRecord::TestCase
|
2009-03-20 15:07:49 +00:00
|
|
|
fixtures :topics, :developers
|
2004-11-24 01:04:44 +00:00
|
|
|
|
2008-12-19 13:27:34 +00: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 13:11:13 +00:00
|
|
|
|
2004-11-24 01:04:44 +00:00
|
|
|
def test_error_on_create
|
|
|
|
r = Reply.new
|
|
|
|
r.title = "Wrong Create"
|
2006-09-03 17:54:48 +00:00
|
|
|
assert !r.valid?
|
2009-03-21 18:29:15 +00:00
|
|
|
assert r.errors[:title].any?, "A reply with a bad title should mark that attribute as invalid"
|
2009-03-20 17:36:22 +00:00
|
|
|
assert_equal ["is Wrong Create"], r.errors[:title], "A reply with a bad content should contain an error"
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_error_on_update
|
|
|
|
r = Reply.new
|
|
|
|
r.title = "Bad"
|
|
|
|
r.content = "Good"
|
|
|
|
assert r.save, "First save should be successful"
|
2005-02-18 17:27:26 +00:00
|
|
|
|
2004-11-24 01:04:44 +00:00
|
|
|
r.title = "Wrong Update"
|
|
|
|
assert !r.save, "Second save should fail"
|
2005-02-18 17:27:26 +00:00
|
|
|
|
2009-03-21 18:29:15 +00:00
|
|
|
assert r.errors[:title].any?, "A reply with a bad title should mark that attribute as invalid"
|
2009-03-20 17:36:22 +00:00
|
|
|
assert_equal ["is Wrong Update"], r.errors[:title], "A reply with a bad content should contain an error"
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
2005-02-18 17:27:26 +00:00
|
|
|
|
2005-04-18 19:19:23 +00:00
|
|
|
def test_invalid_record_exception
|
2009-03-08 20:11:58 +00:00
|
|
|
assert_raise(ActiveRecord::RecordInvalid) { Reply.create! }
|
|
|
|
assert_raise(ActiveRecord::RecordInvalid) { Reply.new.save! }
|
2005-11-03 18:54:16 +00:00
|
|
|
|
|
|
|
begin
|
|
|
|
r = Reply.new
|
|
|
|
r.save!
|
|
|
|
flunk
|
|
|
|
rescue ActiveRecord::RecordInvalid => invalid
|
|
|
|
assert_equal r, invalid.record
|
|
|
|
end
|
2006-05-19 01:05:20 +00:00
|
|
|
end
|
|
|
|
|
2007-08-28 20:44:51 +00:00
|
|
|
def test_exception_on_create_bang_many
|
2009-03-08 20:11:58 +00:00
|
|
|
assert_raise(ActiveRecord::RecordInvalid) do
|
2007-08-28 20:44:51 +00:00
|
|
|
Reply.create!([ { "title" => "OK" }, { "title" => "Wrong Create" }])
|
|
|
|
end
|
|
|
|
end
|
2008-12-19 13:27:34 +00:00
|
|
|
|
2008-05-01 04:14:32 +00:00
|
|
|
def test_exception_on_create_bang_with_block
|
2009-03-08 20:11:58 +00:00
|
|
|
assert_raise(ActiveRecord::RecordInvalid) do
|
2008-05-01 04:14:32 +00:00
|
|
|
Reply.create!({ "title" => "OK" }) do |r|
|
|
|
|
r.content = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2008-12-19 13:27:34 +00:00
|
|
|
|
2008-05-01 04:14:32 +00:00
|
|
|
def test_exception_on_create_bang_many_with_block
|
2009-03-08 20:11:58 +00:00
|
|
|
assert_raise(ActiveRecord::RecordInvalid) do
|
2008-05-01 04:14:32 +00:00
|
|
|
Reply.create!([{ "title" => "OK" }, { "title" => "Wrong Create" }]) do |r|
|
|
|
|
r.content = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2007-08-28 20:44:51 +00:00
|
|
|
|
2006-05-19 01:05:20 +00:00
|
|
|
def test_scoped_create_without_attributes
|
2009-12-28 22:07:23 +00:00
|
|
|
Reply.send(:with_scope, :create => {}) do
|
2009-03-08 20:11:58 +00:00
|
|
|
assert_raise(ActiveRecord::RecordInvalid) { Reply.create! }
|
2006-05-19 01:05:20 +00:00
|
|
|
end
|
2005-04-18 19:19:23 +00:00
|
|
|
end
|
|
|
|
|
2006-12-06 00:13:31 +00:00
|
|
|
def test_create_with_exceptions_using_scope_for_protected_attributes
|
|
|
|
assert_nothing_raised do
|
2009-12-28 22:07:23 +00:00
|
|
|
ProtectedPerson.send(:with_scope, :create => { :first_name => "Mary" } ) do
|
2006-12-06 00:13:31 +00:00
|
|
|
person = ProtectedPerson.create! :addon => "Addon"
|
|
|
|
assert_equal person.first_name, "Mary", "scope should ignore attr_protected"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_create_with_exceptions_using_scope_and_empty_attributes
|
|
|
|
assert_nothing_raised do
|
2009-12-28 22:07:23 +00:00
|
|
|
ProtectedPerson.send(:with_scope, :create => { :first_name => "Mary" } ) do
|
2006-12-06 00:13:31 +00:00
|
|
|
person = ProtectedPerson.create!
|
|
|
|
assert_equal person.first_name, "Mary", "should be ok when no attributes are passed to create!"
|
|
|
|
end
|
|
|
|
end
|
2009-03-06 22:43:00 +00:00
|
|
|
end
|
2006-12-06 00:13:31 +00:00
|
|
|
|
2004-11-24 01:04:44 +00:00
|
|
|
def test_create_without_validation
|
|
|
|
reply = Reply.new
|
|
|
|
assert !reply.save
|
|
|
|
assert reply.save(false)
|
|
|
|
end
|
2005-02-18 17:27:26 +00:00
|
|
|
|
2006-10-02 03:21:32 +00:00
|
|
|
def test_create_without_validation_bang
|
|
|
|
count = Reply.count
|
|
|
|
assert_nothing_raised { Reply.new.save_without_validation! }
|
|
|
|
assert count+1, Reply.count
|
|
|
|
end
|
|
|
|
|
2007-12-27 11:16:51 +00: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 23:55:14 +00:00
|
|
|
|
2004-12-19 11:25:55 +00:00
|
|
|
def test_throw_away_typing
|
2006-09-03 17:54:48 +00:00
|
|
|
d = Developer.new("name" => "David", "salary" => "100,000")
|
2004-12-19 11:25:55 +00:00
|
|
|
assert !d.valid?
|
2004-12-19 11:44:59 +00:00
|
|
|
assert_equal 100, d.salary
|
2004-12-19 11:25:55 +00:00
|
|
|
assert_equal "100,000", d.salary_before_type_cast
|
|
|
|
end
|
2009-03-20 21:40:37 +00:00
|
|
|
|
2009-03-20 22:21:27 +00:00
|
|
|
def test_validates_acceptance_of_as_database_column
|
2009-09-08 15:10:14 +00:00
|
|
|
Topic.validates_acceptance_of(:author_name)
|
|
|
|
topic = Topic.create("author_name" => "Dan Brown")
|
|
|
|
assert_equal "Dan Brown", topic["author_name"]
|
2009-03-20 22:21:27 +00:00
|
|
|
end
|
2005-09-29 02:35:41 +00:00
|
|
|
end
|