提交 ccb335da 编写于 作者: M Marcin Raczkowski 提交者: Emilio Tagua

IdentityMap - Adjustments to test cases

上级 3ab625f9
......@@ -227,23 +227,23 @@ def test_belongs_to_counter_with_reassigning
end
assert r1.save
assert_equal 0, Topic.find(t1.id).replies.size
assert_equal 1, Topic.find(t2.id).replies.size
assert_equal 0, t1.reload.replies.size
assert_equal 1, t2.reload.replies.size
r1.topic = nil
assert_equal 0, Topic.find(t1.id).replies.size
assert_equal 0, Topic.find(t2.id).replies.size
assert_equal 0, t1.reload.replies.size
assert_equal 0, t2.reload.replies.size
r1.topic = t1
assert_equal 1, Topic.find(t1.id).replies.size
assert_equal 0, Topic.find(t2.id).replies.size
assert_equal 1, t1.reload.replies.size
assert_equal 0, t2.reload.replies.size
r1.destroy
assert_equal 0, Topic.find(t1.id).replies.size
assert_equal 0, Topic.find(t2.id).replies.size
assert_equal 0, t1.reload.replies.size
assert_equal 0, t2.reload.replies.size
end
def test_belongs_to_reassign_with_namespaced_models_and_counters
......@@ -259,8 +259,8 @@ def test_belongs_to_reassign_with_namespaced_models_and_counters
r1.topic = Web::Topic.find(t2.id)
assert r1.save
assert_equal 0, Web::Topic.find(t1.id).replies.size
assert_equal 1, Web::Topic.find(t2.id).replies.size
assert_equal 0, t1.reload.replies.size
assert_equal 1, t2.reload.replies.size
end
def test_belongs_to_counter_after_save
......
......@@ -27,6 +27,8 @@ def test_class_names
post = Namespaced::Post.find_by_title( 'Great stuff', :include => :tagging )
assert_nil post.tagging
ActiveRecord::IdentityMap.clear # we need to clear IM to reload post.
ActiveRecord::Base.store_full_sti_class = true
post = Namespaced::Post.find_by_title( 'Great stuff', :include => :tagging )
assert_instance_of Tagging, post.tagging
......
......@@ -170,6 +170,7 @@ def test_finding_with_includes_on_has_one_assocation_with_same_include_includes_
author = authors(:david)
post = author.post_about_thinking_with_last_comment
last_comment = post.last_comment
ActiveRecord::IdentityMap.clear # We need to clear cache to force reload in next block
author = assert_queries(3) { Author.find(author.id, :include => {:post_about_thinking_with_last_comment => :last_comment})} # find the author, then find the posts, then find the comments
assert_no_queries do
assert_equal post, author.post_about_thinking_with_last_comment
......@@ -181,6 +182,7 @@ def test_finding_with_includes_on_belongs_to_association_with_same_include_inclu
post = posts(:welcome)
author = post.author
author_address = author.author_address
ActiveRecord::IdentityMap.clear # We need to clear cache to force reload in next block
post = assert_queries(3) { Post.find(post.id, :include => {:author_with_address => :author_address}) } # find the post, then find the author, then find the address
assert_no_queries do
assert_equal author, post.author_with_address
......@@ -783,6 +785,7 @@ def test_eager_loading_with_order_on_joined_table_preloads
end
def test_eager_loading_with_conditions_on_joined_table_preloads
ActiveRecord::IdentityMap.without do # IM caches records, so we need to disable it to test this functionality.
posts = assert_queries(2) do
Post.find(:all, :select => 'distinct posts.*', :include => :author, :joins => [:comments], :conditions => "comments.body like 'Thank you%'", :order => 'posts.id')
end
......@@ -804,10 +807,11 @@ def test_eager_loading_with_conditions_on_joined_table_preloads
Post.find(:all, :include => :author, :joins => {:taggings => {:tag => :taggings}}, :conditions => "taggings_tags.super_tag_id=2", :order => 'posts.id')
end
assert_equal posts(:welcome, :thinking), posts
end
end
def test_eager_loading_with_conditions_on_string_joined_table_preloads
ActiveRecord::IdentityMap.without do # IM caches records, so we need to disable it to test this functionality.
posts = assert_queries(2) do
Post.find(:all, :select => 'distinct posts.*', :include => :author, :joins => "INNER JOIN comments on comments.post_id = posts.id", :conditions => "comments.body like 'Thank you%'", :order => 'posts.id')
end
......@@ -819,7 +823,7 @@ def test_eager_loading_with_conditions_on_string_joined_table_preloads
end
assert_equal [posts(:welcome)], posts
assert_equal authors(:david), assert_no_queries { posts[0].author}
end
end
def test_eager_loading_with_select_on_joined_table_preloads
......
......@@ -578,7 +578,7 @@ def test_a_marked_for_destruction_record_should_not_be_be_marked_after_reload
@pirate.ship.mark_for_destruction
assert !@pirate.reload.marked_for_destruction?
assert !@pirate.ship.marked_for_destruction?
assert !@pirate.ship.target.reload.marked_for_destruction?
end
# has_one
......@@ -1194,6 +1194,7 @@ class TestAutosaveAssociationValidationsOnAHasOneAssociation < ActiveRecord::Tes
self.use_transactional_fixtures = false
def setup
ActiveRecord::IdentityMap.enabled = false # This tests use trick with double association, IM prevents that, so we disable it.
@pirate = Pirate.create(:catchphrase => "Don' botharrr talkin' like one, savvy?")
@pirate.create_ship(:name => 'titanic')
end
......@@ -1209,6 +1210,10 @@ def setup
@pirate.non_validated_ship.name = ''
assert @pirate.valid?
end
def teardown
ActiveRecord::IdentityMap.enabled = true
end
end
class TestAutosaveAssociationValidationsOnABelongsToAssociation < ActiveRecord::TestCase
......
......@@ -70,14 +70,22 @@ def test_creation
assert_same(t1, t2)
end
def test_updating_of_pkey
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
# 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
t1 = Topic.create("title" => "t1")
......@@ -176,7 +184,7 @@ def test_eager_loading_with_conditions_on_joined_table_preloads
end
assert_equal posts(:welcome, :thinking), posts
posts = assert_queries(2) do
posts = assert_queries(1) do
Post.find(:all, :include => :author, :joins => {:taggings => {:tag => :taggings}}, :conditions => "taggings_tags.super_tag_id=2", :order => 'posts.id')
end
assert_equal posts(:welcome, :thinking), posts
......@@ -195,4 +203,10 @@ def test_eager_loading_with_conditions_on_string_joined_table_preloads
assert_equal [posts(:welcome)], posts
assert_equal authors(:david), assert_no_queries { posts[0].author}
end
# Second search should not change read only status for collection
def test_find_with_joins_option_implies_readonly
Developer.joins(', projects').each { |d| assert d.readonly? }
Developer.joins(', projects').readonly(false).each { |d| assert d.readonly? }
end
end
......@@ -551,9 +551,14 @@ def test_should_not_load_association_when_updating_existing_records
assert_equal 'Grace OMalley', @child_1.reload.name
end
def test_should_not_overwrite_unsaved_updates_when_loading_association
@pirate.reload
# p(@pirate.send(@association_name))
@pirate.send(association_setter, [{ :id => @child_1.id, :name => 'Grace OMalley' }])
# p(@pirate.send(@association_name))
# p([@pirate.send(@association_name).detect { |r| r.id == @child_1.id }, @child_1.id])
# puts
assert_equal 'Grace OMalley', @pirate.send(@association_name).send(:load_target).find { |r| r.id == @child_1.id }.name
end
......
......@@ -40,6 +40,10 @@ def test_find_with_readonly_option
def test_find_with_joins_option_implies_readonly
# We disable IM, becouse we want to check default settings here
# adding readonly(false) does not update readonly status with IM
# (see corresponding test in IM)
ActiveRecord::IdentityMap.without do
# Blank joins don't count.
Developer.joins(' ').each { |d| assert !d.readonly? }
Developer.joins(' ').readonly(false).each { |d| assert !d.readonly? }
......@@ -47,6 +51,7 @@ def test_find_with_joins_option_implies_readonly
# Others do.
Developer.joins(', projects').each { |d| assert d.readonly? }
Developer.joins(', projects').readonly(false).each { |d| assert !d.readonly? }
end
end
......@@ -72,6 +77,10 @@ def test_has_many_with_through_is_not_implicitly_marked_readonly
end
def test_readonly_scoping
# We disable IM, becouse we want to check default settings here
# adding readonly(false) does not update readonly status with IM
# (see corresponding test in IM)
ActiveRecord::IdentityMap.without do
Post.send(:with_scope, :find => { :conditions => '1=1' }) do
assert !Post.find(1).readonly?
assert Post.readonly(true).find(1).readonly?
......@@ -99,6 +108,7 @@ def test_readonly_scoping
assert Post.readonly.find(1).readonly?
assert !Post.readonly(false).find(1).readonly?
end
end
end
def test_association_collection_method_missing_scoping_not_readonly
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册