diff options
Diffstat (limited to 'activerecord/test/cases')
-rwxr-xr-x | activerecord/test/cases/base_test.rb | 5 | ||||
-rw-r--r-- | activerecord/test/cases/callbacks_observers_test.rb | 37 | ||||
-rw-r--r-- | activerecord/test/cases/fixtures_test.rb | 2 | ||||
-rw-r--r-- | activerecord/test/cases/lifecycle_test.rb | 54 | ||||
-rw-r--r-- | activerecord/test/cases/xml_serialization_test.rb | 3 |
5 files changed, 60 insertions, 41 deletions
diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 1c3655b587..2f4243a6aa 100755 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -2054,7 +2054,7 @@ class BasicsTest < ActiveRecord::TestCase end def test_base_subclasses_is_public_method - assert ActiveRecord::Base.public_methods.include?("subclasses") + assert ActiveRecord::Base.public_methods.map(&:to_sym).include?(:subclasses) end def test_find_on_abstract_base_class_doesnt_use_type_condition @@ -2095,7 +2095,8 @@ class BasicsTest < ActiveRecord::TestCase assert_equal written_on_in_current_timezone, xml.elements["//written-on"].text assert_equal "datetime" , xml.elements["//written-on"].attributes['type'] - assert_equal "--- Have a nice day\n" , xml.elements["//content"].text + assert_match(/^--- Have a nice day\n/ , xml.elements["//content"].text) + assert_equal 'Have a nice day' , YAML.load(xml.elements["//content"].text) assert_equal "yaml" , xml.elements["//content"].attributes['type'] assert_equal "david@loudthinking.com", xml.elements["//author-email-address"].text diff --git a/activerecord/test/cases/callbacks_observers_test.rb b/activerecord/test/cases/callbacks_observers_test.rb deleted file mode 100644 index 52ce384844..0000000000 --- a/activerecord/test/cases/callbacks_observers_test.rb +++ /dev/null @@ -1,37 +0,0 @@ -require "cases/helper" - -class Comment < ActiveRecord::Base - attr_accessor :callers - - before_validation :record_callers - - after_validation do - record_callers - end - - def record_callers - callers << self.class if callers - end -end - -class CommentObserver < ActiveRecord::Observer - attr_accessor :callers - - def after_validation(model) - callers << self.class if callers - end -end - -class CallbacksObserversTest < ActiveRecord::TestCase - def test_model_callbacks_fire_before_observers_are_notified - callers = [] - - comment = Comment.new - comment.callers = callers - - CommentObserver.instance.callers = callers - - comment.valid? - assert_equal [Comment, Comment, CommentObserver], callers, "model callbacks did not fire before observers were notified" - end -end diff --git a/activerecord/test/cases/fixtures_test.rb b/activerecord/test/cases/fixtures_test.rb index e78b522b65..d24283fe4e 100644 --- a/activerecord/test/cases/fixtures_test.rb +++ b/activerecord/test/cases/fixtures_test.rb @@ -26,7 +26,7 @@ class FixturesTest < ActiveRecord::TestCase FIXTURES = %w( accounts binaries companies customers developers developers_projects entrants movies projects subscribers topics tasks ) - MATCH_ATTRIBUTE_NAME = /[a-zA-Z][-_\w]*/ + MATCH_ATTRIBUTE_NAME = /[a-zA-Z][-\w]*/ def test_clean_fixtures FIXTURES.each do |name| diff --git a/activerecord/test/cases/lifecycle_test.rb b/activerecord/test/cases/lifecycle_test.rb index fcad3e90d3..233338498f 100644 --- a/activerecord/test/cases/lifecycle_test.rb +++ b/activerecord/test/cases/lifecycle_test.rb @@ -3,9 +3,18 @@ require 'models/topic' require 'models/developer' require 'models/reply' require 'models/minimalistic' +require 'models/comment' class SpecialDeveloper < Developer; end +class SalaryChecker < ActiveRecord::Observer + observe :special_developer + + def before_save(developer) + return developer.salary > 80000 + end +end + class TopicaAuditor < ActiveRecord::Observer observe :topic @@ -57,6 +66,28 @@ class MultiObserver < ActiveRecord::Observer end end +class ValidatedComment < Comment + attr_accessor :callers + + before_validation :record_callers + + after_validation do + record_callers + end + + def record_callers + callers << self.class if callers + end +end + +class ValidatedCommentObserver < ActiveRecord::Observer + attr_accessor :callers + + def after_validation(model) + callers << self.class if callers + end +end + class LifecycleTest < ActiveRecord::TestCase fixtures :topics, :developers, :minimalistics @@ -125,4 +156,27 @@ class LifecycleTest < ActiveRecord::TestCase def test_invalid_observer assert_raise(ArgumentError) { Topic.observers = Object.new; Topic.instantiate_observers } end + + test "model callbacks fire before observers are notified" do + callers = [] + + comment = ValidatedComment.new + comment.callers = ValidatedCommentObserver.instance.callers = callers + + comment.valid? + assert_equal [ValidatedComment, ValidatedComment, ValidatedCommentObserver], callers, + "model callbacks did not fire before observers were notified" + end + + test "able to save developer" do + SalaryChecker.instance # activate + developer = SpecialDeveloper.new :name => 'Roger', :salary => 100000 + assert developer.save, "developer with normal salary failed to save" + end + + test "unable to save developer with low salary" do + SalaryChecker.instance # activate + developer = SpecialDeveloper.new :name => 'Rookie', :salary => 50000 + assert !developer.save, "allowed to save a developer with too low salary" + end end diff --git a/activerecord/test/cases/xml_serialization_test.rb b/activerecord/test/cases/xml_serialization_test.rb index 2849ff11b7..b1c75ec8cd 100644 --- a/activerecord/test/cases/xml_serialization_test.rb +++ b/activerecord/test/cases/xml_serialization_test.rb @@ -4,6 +4,7 @@ require 'models/post' require 'models/author' require 'models/tagging' require 'models/comment' +require 'models/company_in_module' class XmlSerializationTest < ActiveRecord::TestCase def test_should_serialize_default_root @@ -79,7 +80,7 @@ class DefaultXmlSerializationTest < ActiveRecord::TestCase end def test_should_serialize_yaml - assert_match %r{<preferences type=\"yaml\">--- \n:gem: ruby\n</preferences>}, @xml + assert_match %r{<preferences type=\"yaml\">---\s?\n:gem: ruby\n</preferences>}, @xml end end |