Skip to content

Commit

Permalink
Add missing models and fixtures [#2673 state:resolved]
Browse files Browse the repository at this point in the history
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information
miloops authored and lifo committed May 19, 2009
1 parent 07f733c commit 20deb67
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 25 deletions.
Expand Up @@ -13,7 +13,7 @@
require 'models/toy'

class HasManyThroughAssociationsTest < ActiveRecord::TestCase
fixtures :posts, :readers, :people, :comments, :authors, :owners, :pets, :toys
fixtures :posts, :readers, :people, :comments, :authors, :owners, :pets, :toys, :jobs, :references

def test_associate_existing
assert_queries(2) { posts(:thinking);people(:david) }
Expand All @@ -23,72 +23,72 @@ def test_associate_existing
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 :first_name => 'bob'
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).body += '-changed'
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

Expand Down Expand Up @@ -118,36 +118,36 @@ def test_should_raise_exception_for_destroying_mismatching_records

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

Expand All @@ -165,15 +165,15 @@ def test_associate_with_create_exclamation_and_no_options

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

Expand Down
7 changes: 6 additions & 1 deletion activerecord/test/cases/autosave_association_test.rb
Expand Up @@ -12,6 +12,7 @@
require 'models/ship'
require 'models/ship_part'
require 'models/treasure'
require 'models/company'

class TestAutosaveAssociationsInGeneral < ActiveRecord::TestCase
def test_autosave_should_be_a_valid_option_for_has_one
Expand All @@ -38,6 +39,8 @@ def base
end

class TestDefaultAutosaveAssociationOnAHasOneAssociation < ActiveRecord::TestCase
fixtures :companies, :accounts

def test_should_save_parent_but_not_invalid_child
firm = Firm.new(:name => 'GlobalMegaCorp')
assert firm.valid?
Expand Down Expand Up @@ -137,6 +140,8 @@ def test_not_resaved_when_unchanged
end

class TestDefaultAutosaveAssociationOnABelongsToAssociation < ActiveRecord::TestCase
fixtures :companies

def test_should_save_parent_but_not_invalid_child
client = Client.new(:name => 'Joe (the Plumber)')
assert client.valid?
Expand Down Expand Up @@ -920,4 +925,4 @@ def setup
end

include AutosaveAssociationOnACollectionAssociationTests
end
end
4 changes: 3 additions & 1 deletion activerecord/test/cases/named_scope_test.rb
Expand Up @@ -373,7 +373,7 @@ def test_named_scopes_batch_finders
end
end

class DynamicScopeMatchTest < ActiveRecord::TestCase
class DynamicScopeMatchTest < ActiveRecord::TestCase
def test_scoped_by_no_match
assert_nil ActiveRecord::DynamicScopeMatch.match("not_scoped_at_all")
end
Expand All @@ -387,6 +387,8 @@ def test_scoped_by
end

class DynamicScopeTest < ActiveRecord::TestCase
fixtures :posts

def test_dynamic_scope
assert_equal Post.scoped_by_author_id(1).find(1), Post.find(1)
assert_equal Post.scoped_by_author_id_and_title(1, "Welcome to the weblog").first, Post.find(:first, :conditions => { :author_id => 1, :title => "Welcome to the weblog"})
Expand Down

0 comments on commit 20deb67

Please sign in to comment.