From 8c5fe60ec82986ab4aa337a69c2ab753f518976d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Fri, 1 Jan 2010 03:20:38 +0100 Subject: Simplify repair_validations on AR and make it work with new callbacks. --- activerecord/test/cases/base_test.rb | 5 +++- .../validations/association_validation_test.rb | 5 ++-- .../test/cases/validations/i18n_validation_test.rb | 4 ++-- .../validations/uniqueness_validation_test.rb | 11 --------- .../test/cases/validations_repair_helper.rb | 20 ++++------------ activerecord/test/cases/validations_test.rb | 28 +++++++++++----------- activerecord/test/models/reply.rb | 6 +++-- 7 files changed, 31 insertions(+), 48 deletions(-) (limited to 'activerecord') diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index ebb717812d..730d9d8df7 100755 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -201,7 +201,7 @@ class BasicsTest < ActiveRecord::TestCase topic = Topic.new(:title => "New Topic") assert topic.save! - reply = Reply.new + reply = WrongReply.new assert_raise(ActiveRecord::RecordInvalid) { reply.save! } end @@ -959,6 +959,7 @@ class BasicsTest < ActiveRecord::TestCase end def test_update_attributes! + Reply.validates_presence_of(:title) reply = Reply.find(2) assert_equal "The Second Topic of the day", reply.title assert_equal "Have a nice day", reply.content @@ -974,6 +975,8 @@ class BasicsTest < ActiveRecord::TestCase assert_equal "Have a nice day", reply.content assert_raise(ActiveRecord::RecordInvalid) { reply.update_attributes!(:title => nil, :content => "Have a nice evening") } + ensure + Reply.reset_callbacks(:validate) end def test_mass_assignment_should_raise_exception_if_accessible_and_protected_attribute_writers_are_both_used diff --git a/activerecord/test/cases/validations/association_validation_test.rb b/activerecord/test/cases/validations/association_validation_test.rb index 278a7a6a06..5ed997356b 100644 --- a/activerecord/test/cases/validations/association_validation_test.rb +++ b/activerecord/test/cases/validations/association_validation_test.rb @@ -10,7 +10,7 @@ require 'models/interest' class AssociationValidationTest < ActiveRecord::TestCase fixtures :topics, :owners - repair_validations(Topic) + repair_validations(Topic, Reply) def test_validates_size_of_association repair_validations(Owner) do @@ -40,7 +40,8 @@ class AssociationValidationTest < ActiveRecord::TestCase end def test_validates_associated_many - Topic.validates_associated( :replies ) + Topic.validates_associated(:replies) + Reply.validates_presence_of(:content) t = Topic.create("title" => "uhohuhoh", "content" => "whatever") t.replies << [r = Reply.new("title" => "A reply"), r2 = Reply.new("title" => "Another reply", "content" => "non-empty"), r3 = Reply.new("title" => "Yet another reply"), r4 = Reply.new("title" => "The last reply", "content" => "non-empty")] assert !t.valid? diff --git a/activerecord/test/cases/validations/i18n_validation_test.rb b/activerecord/test/cases/validations/i18n_validation_test.rb index 532de67d99..f017f24048 100644 --- a/activerecord/test/cases/validations/i18n_validation_test.rb +++ b/activerecord/test/cases/validations/i18n_validation_test.rb @@ -3,8 +3,9 @@ require 'models/topic' require 'models/reply' class I18nValidationTest < ActiveRecord::TestCase + repair_validations(Topic, Reply) def setup - Topic.reset_callbacks(:validate) + Reply.validates_presence_of(:title) @topic = Topic.new @old_load_path, @old_backend = I18n.load_path, I18n.backend I18n.load_path.clear @@ -13,7 +14,6 @@ class I18nValidationTest < ActiveRecord::TestCase end def teardown - Topic.reset_callbacks(:validate) I18n.load_path.replace @old_load_path I18n.backend = @old_backend end diff --git a/activerecord/test/cases/validations/uniqueness_validation_test.rb b/activerecord/test/cases/validations/uniqueness_validation_test.rb index db633339f3..8f84841fe6 100644 --- a/activerecord/test/cases/validations/uniqueness_validation_test.rb +++ b/activerecord/test/cases/validations/uniqueness_validation_test.rb @@ -5,7 +5,6 @@ require 'models/reply' require 'models/warehouse_thing' require 'models/guid' require 'models/event' -require 'models/developer' # The following methods in Topic are used in test_conditional_validation_* class Topic @@ -276,14 +275,4 @@ class UniquenessValidationTest < ActiveRecord::TestCase assert w6.errors[:city].any?, "Should have errors for city" assert_equal ["has already been taken"], w6.errors[:city], "Should have uniqueness message for city" end - - def test_validates_uniqueness_of_with_custom_message_using_quotes - 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[:name] - end - end end diff --git a/activerecord/test/cases/validations_repair_helper.rb b/activerecord/test/cases/validations_repair_helper.rb index e04738d209..11912ca1cc 100644 --- a/activerecord/test/cases/validations_repair_helper.rb +++ b/activerecord/test/cases/validations_repair_helper.rb @@ -4,31 +4,19 @@ module ActiveRecord module ClassMethods def repair_validations(*model_classes) - setup do - @_stored_callbacks = {} - model_classes.each do |k| - @_stored_callbacks[k] = k._validate_callbacks.dup - end - end teardown do model_classes.each do |k| - k._validate_callbacks = @_stored_callbacks[k] - k.__update_callbacks(:validate) + k.reset_callbacks(:validate) end end end end - def repair_validations(*model_classes, &block) - @__stored_callbacks = {} - model_classes.each do |k| - @__stored_callbacks[k] = k._validate_callbacks.dup - end - return block.call + def repair_validations(*model_classes) + yield ensure model_classes.each do |k| - k._validate_callbacks = @__stored_callbacks[k] - k.__update_callbacks(:validate) + k.reset_callbacks(:validate) end end end diff --git a/activerecord/test/cases/validations_test.rb b/activerecord/test/cases/validations_test.rb index 7462d944e0..3a1d5ae212 100644 --- a/activerecord/test/cases/validations_test.rb +++ b/activerecord/test/cases/validations_test.rb @@ -42,7 +42,7 @@ class ValidationsTest < ActiveRecord::TestCase repair_validations(Topic) def test_error_on_create - r = Reply.new + r = WrongReply.new r.title = "Wrong Create" assert !r.valid? assert r.errors[:title].any?, "A reply with a bad title should mark that attribute as invalid" @@ -50,7 +50,7 @@ class ValidationsTest < ActiveRecord::TestCase end def test_error_on_update - r = Reply.new + r = WrongReply.new r.title = "Bad" r.content = "Good" assert r.save, "First save should be successful" @@ -63,11 +63,11 @@ class ValidationsTest < ActiveRecord::TestCase end def test_invalid_record_exception - assert_raise(ActiveRecord::RecordInvalid) { Reply.create! } - assert_raise(ActiveRecord::RecordInvalid) { Reply.new.save! } + assert_raise(ActiveRecord::RecordInvalid) { WrongReply.create! } + assert_raise(ActiveRecord::RecordInvalid) { WrongReply.new.save! } begin - r = Reply.new + r = WrongReply.new r.save! flunk rescue ActiveRecord::RecordInvalid => invalid @@ -77,13 +77,13 @@ class ValidationsTest < ActiveRecord::TestCase def test_exception_on_create_bang_many assert_raise(ActiveRecord::RecordInvalid) do - Reply.create!([ { "title" => "OK" }, { "title" => "Wrong Create" }]) + WrongReply.create!([ { "title" => "OK" }, { "title" => "Wrong Create" }]) end end def test_exception_on_create_bang_with_block assert_raise(ActiveRecord::RecordInvalid) do - Reply.create!({ "title" => "OK" }) do |r| + WrongReply.create!({ "title" => "OK" }) do |r| r.content = nil end end @@ -91,15 +91,15 @@ class ValidationsTest < ActiveRecord::TestCase def test_exception_on_create_bang_many_with_block assert_raise(ActiveRecord::RecordInvalid) do - Reply.create!([{ "title" => "OK" }, { "title" => "Wrong Create" }]) do |r| + WrongReply.create!([{ "title" => "OK" }, { "title" => "Wrong Create" }]) do |r| r.content = nil end end end def test_scoped_create_without_attributes - Reply.send(:with_scope, :create => {}) do - assert_raise(ActiveRecord::RecordInvalid) { Reply.create! } + WrongReply.send(:with_scope, :create => {}) do + assert_raise(ActiveRecord::RecordInvalid) { WrongReply.create! } end end @@ -122,15 +122,15 @@ class ValidationsTest < ActiveRecord::TestCase end def test_create_without_validation - reply = Reply.new + reply = WrongReply.new assert !reply.save assert reply.save(false) end def test_create_without_validation_bang - count = Reply.count - assert_nothing_raised { Reply.new.save_without_validation! } - assert count+1, Reply.count + count = WrongReply.count + assert_nothing_raised { WrongReply.new.save_without_validation! } + assert count+1, WrongReply.count end def test_validates_acceptance_of_with_non_existant_table diff --git a/activerecord/test/models/reply.rb b/activerecord/test/models/reply.rb index ba5a1d1d01..f1ba45b528 100644 --- a/activerecord/test/models/reply.rb +++ b/activerecord/test/models/reply.rb @@ -7,11 +7,13 @@ class Reply < Topic belongs_to :topic_with_primary_key, :class_name => "Topic", :primary_key => "title", :foreign_key => "parent_title", :counter_cache => "replies_count" has_many :replies, :class_name => "SillyReply", :dependent => :destroy, :foreign_key => "parent_id" + attr_accessible :title, :author_name, :author_email_address, :written_on, :content, :last_read, :parent_title +end + +class WrongReply < Reply validate :errors_on_empty_content validate :title_is_wrong_create, :on => :create - attr_accessible :title, :author_name, :author_email_address, :written_on, :content, :last_read, :parent_title - validate :check_empty_title validate :check_content_mismatch, :on => :create validate :check_wrong_update, :on => :update -- cgit v1.2.3