Permalink
Browse files

Test reorganization

  • Loading branch information...
1 parent 24485b9 commit 740450514ce86d56926d8749b1e230012b368567 @swistak swistak committed with miloops Oct 3, 2010
Showing with 88 additions and 39 deletions.
  1. +5 −3 activerecord/lib/active_record/identity_map.rb
  2. +83 −36 activerecord/test/cases/identity_map_test.rb
@@ -56,9 +56,11 @@ module InstanceMethods
# +coder+ must contain the attributes necessary for initializing an empty
# model object.
def reinit_with(coder)
- @previously_changed = changes
- @attributes_cache, @changed_attributes = {}, {}
- @attributes.update(coder['attributes'])
+ @attributes_cache = {}
+ dirty = @changed_attributes.keys
+ @attributes.update(coder['attributes'].except(*dirty))
+ @changed_attributes.update(coder['attributes'].slice(*dirty))
+ @changed_attributes.delete_if{|k,v| v.eql? @attributes[k]}
_run_find_callbacks
@@ -25,6 +25,10 @@ class IdentityMapTest < ActiveRecord::TestCase
:developers_projects, :computers, :authors, :author_addresses,
:posts, :tags, :taggings, :comments, :subscribers
+ ##############################################################################
+ # Basic tests checking if IM is functioning properly on basic find operations#
+ ##############################################################################
+
def test_find_id
assert_same(Client.find(3), Client.find(3))
end
@@ -70,29 +74,77 @@ def test_find_first_pkey
)
end
+ ##############################################################################
+ # Tests checking if IM is functioning properly on more advanced finds #
+ # and associations #
+ ##############################################################################
+
+ def test_owner_object_is_associated_from_identity_map
+ post = Post.find(1)
+ comment = post.comments.first
+
+ assert_no_queries do
+ comment.post
+ end
+ assert_same post, comment.post.target
+ end
+
+ def test_associated_object_are_assigned_from_identity_map
+ post = Post.find(1)
+
+ post.comments.each do |comment|
+ assert_same post, comment.post.target
+ assert_equal post.object_id, comment.post.target.object_id
+ end
+ end
+
def test_creation
t1 = Topic.create("title" => "t1")
t2 = Topic.find(t1.id)
assert_same(t1, t2)
end
+ ##############################################################################
+ # Tests checking dirty attribute behaviour with IM #
+ ##############################################################################
+
def test_loading_new_instance_should_not_update_dirty_attributes
swistak = Subscriber.find(:first, :conditions => {:nick => 'swistak'})
swistak.name = "Swistak Sreberkowiec"
assert_equal(["name"], swistak.changed)
+ assert_equal({"name" => ["Marcin Raczkowski", "Swistak Sreberkowiec"]}, swistak.changes)
s = Subscriber.find('swistak')
assert swistak.name_changed?
assert_equal("Swistak Sreberkowiec", swistak.name)
end
+ def test_loading_new_instance_should_change_dirty_attribute original_value
+ swistak = Subscriber.find(:first, :conditions => {:nick => 'swistak'})
+ swistak.name = "Swistak Sreberkowiec"
+
+ Susbscriber.update_all({:name => "Raczkowski Marcin"}, {:name => "Marcin Raczkowski"})
+
+ s = Subscriber.find('swistak')
+
+ assert_equal({'name' => ["Raczkowski Marcin", "Swistak Sreberkowiec"]}, swistak.changes)
+ assert_equal("Swistak Sreberkowiec", swistak.name)
+ end
+
def test_loading_new_instance_should_remove_dirt
swistak = Subscriber.find(:first, :conditions => {:nick => 'swistak'})
swistak.name = "Swistak Sreberkowiec"
- assert_equal({'name' => ["Marcin Raczkowski", "Swistak Sreberkowiec"]}, swistak.changes)
+ assert_equal({"name" => ["Marcin Raczkowski", "Swistak Sreberkowiec"]}, swistak.changes)
+
+ Subscriber.update_all({:name => "Swistak Sreberkowiec"}, {:name => "Marcin Raczkowski"})
+
+ s = Subscriber.find('swistak')
+
assert_equal("Swistak Sreberkowiec", swistak.name)
+ assert_equal({}, swistak.changes)
+ assert !swistak.name_changed?
end
def test_has_many_associations
@@ -108,23 +160,6 @@ def test_has_many_associations
assert_equal 'Grace OMalley', pirate.birds.send(:load_target).find { |r| r.id == posideons.id }.name
end
-# Currently AR is not allowing changing primary key (see Persistence#update)
-# So we ignore it. If this changes, this test needs to be uncommented.
-# def test_updating_of_pkey
-# assert client = Client.find(3),
-# client.update_attribute(:id, 666)
-#
-# assert Client.find(666)
-# assert_same(client, Client.find(666))
-#
-# s = Subscriber.find_by_nick('swistak')
-# assert s.update_attribute(:nick, 'swistakTheJester')
-# assert_equal('swistakTheJester', s.nick)
-#
-# assert stj = Subscriber.find_by_nick('swistakTheJester')
-# assert_same(s, stj)
-# end
-
def test_changing_associations
post1 = Post.create("title" => "One post", "body" => "Posting...")
post2 = Post.create("title" => "Another post", "body" => "Posting... Again...")
@@ -149,6 +184,11 @@ def test_im_with_polymorphic_has_many_going_through_join_model_with_custom_selec
assert_nothing_raised(NoMethodError, "Joins/select was not loaded") { tag.author_id }
end
+ ##############################################################################
+ # Tests checking Identity Map behaviour with preloaded associations, joins, #
+ # includes etc. #
+ ##############################################################################
+
def test_find_with_preloaded_associations
assert_queries(2) do
posts = Post.preload(:comments)
@@ -238,6 +278,10 @@ def test_eager_loading_with_conditions_on_string_joined_table_preloads
assert_equal authors(:david), assert_no_queries { posts[0].author}
end
+ ##############################################################################
+ # Behaviour releated to saving failures
+ ##############################################################################
+
def test_reload_object_if_save_failed
developer = Developer.first
developer.salary = 0
@@ -277,24 +321,9 @@ def test_reload_object_if_update_attributes_fails
assert_not_equal developer.salary, same_developer.salary
end
- def test_owner_object_is_associated_from_identity_map
- post = Post.find(1)
- comment = post.comments.first
-
- assert_no_queries do
- comment.post
- end
- assert_same post, comment.post.target
- end
-
- def test_associated_object_are_assigned_from_identity_map
- post = Post.find(1)
-
- post.comments.each do |comment|
- assert_same post, comment.post.target
- assert_equal post.object_id, comment.post.target.object_id
- end
- end
+ ##############################################################################
+ # Behaviour of readonly, forzen, destroyed
+ ##############################################################################
def test_find_using_identity_map_respects_readonly_when_loading_associated_object_first
author = Author.first
@@ -321,4 +350,22 @@ def test_find_using_identity_map_respects_readonly
assert_raise(ActiveRecord::ReadOnlyRecord) {readonly_comment.save}
assert comment.save
end
+
+# Currently AR is not allowing changing primary key (see Persistence#update)
+# So we ignore it. If this changes, this test needs to be uncommented.
+# def test_updating_of_pkey
+# assert client = Client.find(3),
+# client.update_attribute(:id, 666)
+#
+# assert Client.find(666)
+# assert_same(client, Client.find(666))
+#
+# s = Subscriber.find_by_nick('swistak')
+# assert s.update_attribute(:nick, 'swistakTheJester')
+# assert_equal('swistakTheJester', s.nick)
+#
+# assert stj = Subscriber.find_by_nick('swistakTheJester')
+# assert_same(s, stj)
+# end
+
end

0 comments on commit 7404505

Please sign in to comment.