aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/associations/has_many_through_associations_test.rb
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2008-04-06 03:01:09 +0000
committerPratik Naik <pratiknaik@gmail.com>2008-04-06 03:01:09 +0000
commit5b822aca4b6524c9147224764ec0f20a728ba2a1 (patch)
tree1834d978c369f899bfa9ffba92c9f35353587eb8 /activerecord/test/cases/associations/has_many_through_associations_test.rb
parent7ddc8f2e1bc9c818b622373a8c85bd679533cefd (diff)
downloadrails-5b822aca4b6524c9147224764ec0f20a728ba2a1.tar.gz
rails-5b822aca4b6524c9147224764ec0f20a728ba2a1.tar.bz2
rails-5b822aca4b6524c9147224764ec0f20a728ba2a1.zip
Split associations_test.rb into multiple files based on association type. [Pratik]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9233 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/test/cases/associations/has_many_through_associations_test.rb')
-rw-r--r--activerecord/test/cases/associations/has_many_through_associations_test.rb177
1 files changed, 177 insertions, 0 deletions
diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb
new file mode 100644
index 0000000000..00bcb05e2d
--- /dev/null
+++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb
@@ -0,0 +1,177 @@
+require "cases/helper"
+require 'models/post'
+require 'models/person'
+require 'models/reader'
+
+class HasManyThroughAssociationsTest < ActiveRecord::TestCase
+ fixtures :posts, :readers, :people
+
+ def test_associate_existing
+ assert_queries(2) { posts(:thinking);people(:david) }
+
+ assert_queries(1) do
+ posts(:thinking).people << people(:david)
+ end
+
+ assert_queries(1) do
+ assert posts(:thinking).people.include?(people(:david))
+ end
+
+ assert posts(:thinking).reload.people(true).include?(people(:david))
+ end
+
+ def test_associating_new
+ assert_queries(1) { posts(:thinking) }
+ new_person = nil # so block binding catches it
+
+ assert_queries(0) do
+ new_person = Person.new
+ end
+
+ # Associating new records always saves them
+ # Thus, 1 query for the new person record, 1 query for the new join table record
+ assert_queries(2) do
+ posts(:thinking).people << new_person
+ end
+
+ assert_queries(1) do
+ assert posts(:thinking).people.include?(new_person)
+ end
+
+ assert posts(:thinking).reload.people(true).include?(new_person)
+ end
+
+ def test_associate_new_by_building
+ assert_queries(1) { posts(:thinking) }
+
+ assert_queries(0) do
+ posts(:thinking).people.build(:first_name=>"Bob")
+ posts(:thinking).people.new(:first_name=>"Ted")
+ end
+
+ # Should only need to load the association once
+ assert_queries(1) do
+ assert posts(:thinking).people.collect(&:first_name).include?("Bob")
+ assert posts(:thinking).people.collect(&:first_name).include?("Ted")
+ end
+
+ # 2 queries for each new record (1 to save the record itself, 1 for the join model)
+ # * 2 new records = 4
+ # + 1 query to save the actual post = 5
+ assert_queries(5) do
+ posts(:thinking).save
+ end
+
+ assert posts(:thinking).reload.people(true).collect(&:first_name).include?("Bob")
+ assert posts(:thinking).reload.people(true).collect(&:first_name).include?("Ted")
+ end
+
+ def test_delete_association
+ assert_queries(2){posts(:welcome);people(:michael); }
+
+ assert_queries(1) do
+ posts(:welcome).people.delete(people(:michael))
+ end
+
+ assert_queries(1) do
+ assert posts(:welcome).people.empty?
+ end
+
+ assert posts(:welcome).reload.people(true).empty?
+ end
+
+ def test_replace_association
+ assert_queries(4){posts(:welcome);people(:david);people(:michael); posts(:welcome).people(true)}
+
+ # 1 query to delete the existing reader (michael)
+ # 1 query to associate the new reader (david)
+ assert_queries(2) do
+ posts(:welcome).people = [people(:david)]
+ end
+
+ assert_queries(0){
+ assert posts(:welcome).people.include?(people(:david))
+ assert !posts(:welcome).people.include?(people(:michael))
+ }
+
+ assert posts(:welcome).reload.people(true).include?(people(:david))
+ assert !posts(:welcome).reload.people(true).include?(people(:michael))
+ end
+
+ def test_associate_with_create
+ assert_queries(1) { posts(:thinking) }
+
+ # 1 query for the new record, 1 for the join table record
+ # No need to update the actual collection yet!
+ assert_queries(2) do
+ posts(:thinking).people.create(:first_name=>"Jeb")
+ end
+
+ # *Now* we actually need the collection so it's loaded
+ assert_queries(1) do
+ assert posts(:thinking).people.collect(&:first_name).include?("Jeb")
+ end
+
+ assert posts(:thinking).reload.people(true).collect(&:first_name).include?("Jeb")
+ end
+
+ def test_clear_associations
+ assert_queries(2) { posts(:welcome);posts(:welcome).people(true) }
+
+ assert_queries(1) do
+ posts(:welcome).people.clear
+ end
+
+ assert_queries(0) do
+ assert posts(:welcome).people.empty?
+ end
+
+ assert posts(:welcome).reload.people(true).empty?
+ end
+
+ def test_association_callback_ordering
+ Post.reset_log
+ log = Post.log
+ post = posts(:thinking)
+
+ post.people_with_callbacks << people(:michael)
+ assert_equal [
+ [:added, :before, "Michael"],
+ [:added, :after, "Michael"]
+ ], log.last(2)
+
+ post.people_with_callbacks.push(people(:david), Person.create!(:first_name => "Bob"), Person.new(:first_name => "Lary"))
+ assert_equal [
+ [:added, :before, "David"],
+ [:added, :after, "David"],
+ [:added, :before, "Bob"],
+ [:added, :after, "Bob"],
+ [:added, :before, "Lary"],
+ [:added, :after, "Lary"]
+ ],log.last(6)
+
+ post.people_with_callbacks.build(:first_name => "Ted")
+ assert_equal [
+ [:added, :before, "Ted"],
+ [:added, :after, "Ted"]
+ ], log.last(2)
+
+ post.people_with_callbacks.create(:first_name => "Sam")
+ assert_equal [
+ [:added, :before, "Sam"],
+ [:added, :after, "Sam"]
+ ], log.last(2)
+
+ post.people_with_callbacks = [people(:michael),people(:david), Person.new(:first_name => "Julian"), Person.create!(:first_name => "Roger")]
+ assert_equal (%w(Ted Bob Sam Lary) * 2).sort, log[-12..-5].collect(&:last).sort
+ assert_equal [
+ [:added, :before, "Julian"],
+ [:added, :after, "Julian"],
+ [:added, :before, "Roger"],
+ [:added, :after, "Roger"]
+ ], log.last(4)
+
+ post.people_with_callbacks.clear
+ assert_equal (%w(Michael David Julian Roger) * 2).sort, log.last(8).collect(&:last).sort
+ end
+end