mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Add missing models and fixtures [#2673 state:resolved]
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
This commit is contained in:
parent
07f733c631
commit
20deb677a2
3 changed files with 32 additions and 25 deletions
|
@ -13,7 +13,7 @@ require 'models/pet'
|
||||||
require 'models/toy'
|
require 'models/toy'
|
||||||
|
|
||||||
class HasManyThroughAssociationsTest < ActiveRecord::TestCase
|
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
|
def test_associate_existing
|
||||||
assert_queries(2) { posts(:thinking);people(:david) }
|
assert_queries(2) { posts(:thinking);people(:david) }
|
||||||
|
@ -23,49 +23,49 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase
|
||||||
assert_queries(1) do
|
assert_queries(1) do
|
||||||
posts(:thinking).people << people(:david)
|
posts(:thinking).people << people(:david)
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_queries(1) do
|
assert_queries(1) do
|
||||||
assert posts(:thinking).people.include?(people(:david))
|
assert posts(:thinking).people.include?(people(:david))
|
||||||
end
|
end
|
||||||
|
|
||||||
assert posts(:thinking).reload.people(true).include?(people(:david))
|
assert posts(:thinking).reload.people(true).include?(people(:david))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_associating_new
|
def test_associating_new
|
||||||
assert_queries(1) { posts(:thinking) }
|
assert_queries(1) { posts(:thinking) }
|
||||||
new_person = nil # so block binding catches it
|
new_person = nil # so block binding catches it
|
||||||
|
|
||||||
assert_queries(0) do
|
assert_queries(0) do
|
||||||
new_person = Person.new :first_name => 'bob'
|
new_person = Person.new :first_name => 'bob'
|
||||||
end
|
end
|
||||||
|
|
||||||
# Associating new records always saves them
|
# Associating new records always saves them
|
||||||
# Thus, 1 query for the new person record, 1 query for the new join table record
|
# Thus, 1 query for the new person record, 1 query for the new join table record
|
||||||
assert_queries(2) do
|
assert_queries(2) do
|
||||||
posts(:thinking).people << new_person
|
posts(:thinking).people << new_person
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_queries(1) do
|
assert_queries(1) do
|
||||||
assert posts(:thinking).people.include?(new_person)
|
assert posts(:thinking).people.include?(new_person)
|
||||||
end
|
end
|
||||||
|
|
||||||
assert posts(:thinking).reload.people(true).include?(new_person)
|
assert posts(:thinking).reload.people(true).include?(new_person)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_associate_new_by_building
|
def test_associate_new_by_building
|
||||||
assert_queries(1) { posts(:thinking) }
|
assert_queries(1) { posts(:thinking) }
|
||||||
|
|
||||||
assert_queries(0) do
|
assert_queries(0) do
|
||||||
posts(:thinking).people.build(:first_name=>"Bob")
|
posts(:thinking).people.build(:first_name=>"Bob")
|
||||||
posts(:thinking).people.new(:first_name=>"Ted")
|
posts(:thinking).people.new(:first_name=>"Ted")
|
||||||
end
|
end
|
||||||
|
|
||||||
# Should only need to load the association once
|
# Should only need to load the association once
|
||||||
assert_queries(1) do
|
assert_queries(1) do
|
||||||
assert posts(:thinking).people.collect(&:first_name).include?("Bob")
|
assert posts(:thinking).people.collect(&:first_name).include?("Bob")
|
||||||
assert posts(:thinking).people.collect(&:first_name).include?("Ted")
|
assert posts(:thinking).people.collect(&:first_name).include?("Ted")
|
||||||
end
|
end
|
||||||
|
|
||||||
# 2 queries for each new record (1 to save the record itself, 1 for the join model)
|
# 2 queries for each new record (1 to save the record itself, 1 for the join model)
|
||||||
# * 2 new records = 4
|
# * 2 new records = 4
|
||||||
# + 1 query to save the actual post = 5
|
# + 1 query to save the actual post = 5
|
||||||
|
@ -73,22 +73,22 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase
|
||||||
posts(:thinking).body += '-changed'
|
posts(:thinking).body += '-changed'
|
||||||
posts(:thinking).save
|
posts(:thinking).save
|
||||||
end
|
end
|
||||||
|
|
||||||
assert posts(:thinking).reload.people(true).collect(&:first_name).include?("Bob")
|
assert posts(:thinking).reload.people(true).collect(&:first_name).include?("Bob")
|
||||||
assert posts(:thinking).reload.people(true).collect(&:first_name).include?("Ted")
|
assert posts(:thinking).reload.people(true).collect(&:first_name).include?("Ted")
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_delete_association
|
def test_delete_association
|
||||||
assert_queries(2){posts(:welcome);people(:michael); }
|
assert_queries(2){posts(:welcome);people(:michael); }
|
||||||
|
|
||||||
assert_queries(1) do
|
assert_queries(1) do
|
||||||
posts(:welcome).people.delete(people(:michael))
|
posts(:welcome).people.delete(people(:michael))
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_queries(1) do
|
assert_queries(1) do
|
||||||
assert posts(:welcome).people.empty?
|
assert posts(:welcome).people.empty?
|
||||||
end
|
end
|
||||||
|
|
||||||
assert posts(:welcome).reload.people(true).empty?
|
assert posts(:welcome).reload.people(true).empty?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -118,36 +118,36 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase
|
||||||
|
|
||||||
def test_replace_association
|
def test_replace_association
|
||||||
assert_queries(4){posts(:welcome);people(:david);people(:michael); posts(:welcome).people(true)}
|
assert_queries(4){posts(:welcome);people(:david);people(:michael); posts(:welcome).people(true)}
|
||||||
|
|
||||||
# 1 query to delete the existing reader (michael)
|
# 1 query to delete the existing reader (michael)
|
||||||
# 1 query to associate the new reader (david)
|
# 1 query to associate the new reader (david)
|
||||||
assert_queries(2) do
|
assert_queries(2) do
|
||||||
posts(:welcome).people = [people(:david)]
|
posts(:welcome).people = [people(:david)]
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_queries(0){
|
assert_queries(0){
|
||||||
assert posts(:welcome).people.include?(people(:david))
|
assert posts(:welcome).people.include?(people(:david))
|
||||||
assert !posts(:welcome).people.include?(people(:michael))
|
assert !posts(:welcome).people.include?(people(:michael))
|
||||||
}
|
}
|
||||||
|
|
||||||
assert posts(:welcome).reload.people(true).include?(people(:david))
|
assert posts(:welcome).reload.people(true).include?(people(:david))
|
||||||
assert !posts(:welcome).reload.people(true).include?(people(:michael))
|
assert !posts(:welcome).reload.people(true).include?(people(:michael))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_associate_with_create
|
def test_associate_with_create
|
||||||
assert_queries(1) { posts(:thinking) }
|
assert_queries(1) { posts(:thinking) }
|
||||||
|
|
||||||
# 1 query for the new record, 1 for the join table record
|
# 1 query for the new record, 1 for the join table record
|
||||||
# No need to update the actual collection yet!
|
# No need to update the actual collection yet!
|
||||||
assert_queries(2) do
|
assert_queries(2) do
|
||||||
posts(:thinking).people.create(:first_name=>"Jeb")
|
posts(:thinking).people.create(:first_name=>"Jeb")
|
||||||
end
|
end
|
||||||
|
|
||||||
# *Now* we actually need the collection so it's loaded
|
# *Now* we actually need the collection so it's loaded
|
||||||
assert_queries(1) do
|
assert_queries(1) do
|
||||||
assert posts(:thinking).people.collect(&:first_name).include?("Jeb")
|
assert posts(:thinking).people.collect(&:first_name).include?("Jeb")
|
||||||
end
|
end
|
||||||
|
|
||||||
assert posts(:thinking).reload.people(true).collect(&:first_name).include?("Jeb")
|
assert posts(:thinking).reload.people(true).collect(&:first_name).include?("Jeb")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -165,15 +165,15 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase
|
||||||
|
|
||||||
def test_clear_associations
|
def test_clear_associations
|
||||||
assert_queries(2) { posts(:welcome);posts(:welcome).people(true) }
|
assert_queries(2) { posts(:welcome);posts(:welcome).people(true) }
|
||||||
|
|
||||||
assert_queries(1) do
|
assert_queries(1) do
|
||||||
posts(:welcome).people.clear
|
posts(:welcome).people.clear
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_queries(0) do
|
assert_queries(0) do
|
||||||
assert posts(:welcome).people.empty?
|
assert posts(:welcome).people.empty?
|
||||||
end
|
end
|
||||||
|
|
||||||
assert posts(:welcome).reload.people(true).empty?
|
assert posts(:welcome).reload.people(true).empty?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,7 @@ require 'models/reader'
|
||||||
require 'models/ship'
|
require 'models/ship'
|
||||||
require 'models/ship_part'
|
require 'models/ship_part'
|
||||||
require 'models/treasure'
|
require 'models/treasure'
|
||||||
|
require 'models/company'
|
||||||
|
|
||||||
class TestAutosaveAssociationsInGeneral < ActiveRecord::TestCase
|
class TestAutosaveAssociationsInGeneral < ActiveRecord::TestCase
|
||||||
def test_autosave_should_be_a_valid_option_for_has_one
|
def test_autosave_should_be_a_valid_option_for_has_one
|
||||||
|
@ -38,6 +39,8 @@ class TestAutosaveAssociationsInGeneral < ActiveRecord::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
class TestDefaultAutosaveAssociationOnAHasOneAssociation < ActiveRecord::TestCase
|
class TestDefaultAutosaveAssociationOnAHasOneAssociation < ActiveRecord::TestCase
|
||||||
|
fixtures :companies, :accounts
|
||||||
|
|
||||||
def test_should_save_parent_but_not_invalid_child
|
def test_should_save_parent_but_not_invalid_child
|
||||||
firm = Firm.new(:name => 'GlobalMegaCorp')
|
firm = Firm.new(:name => 'GlobalMegaCorp')
|
||||||
assert firm.valid?
|
assert firm.valid?
|
||||||
|
@ -137,6 +140,8 @@ class TestDefaultAutosaveAssociationOnAHasOneAssociation < ActiveRecord::TestCas
|
||||||
end
|
end
|
||||||
|
|
||||||
class TestDefaultAutosaveAssociationOnABelongsToAssociation < ActiveRecord::TestCase
|
class TestDefaultAutosaveAssociationOnABelongsToAssociation < ActiveRecord::TestCase
|
||||||
|
fixtures :companies
|
||||||
|
|
||||||
def test_should_save_parent_but_not_invalid_child
|
def test_should_save_parent_but_not_invalid_child
|
||||||
client = Client.new(:name => 'Joe (the Plumber)')
|
client = Client.new(:name => 'Joe (the Plumber)')
|
||||||
assert client.valid?
|
assert client.valid?
|
||||||
|
@ -920,4 +925,4 @@ class TestAutosaveAssociationOnAHasAndBelongsToManyAssociation < ActiveRecord::T
|
||||||
end
|
end
|
||||||
|
|
||||||
include AutosaveAssociationOnACollectionAssociationTests
|
include AutosaveAssociationOnACollectionAssociationTests
|
||||||
end
|
end
|
||||||
|
|
|
@ -373,7 +373,7 @@ class NamedScopeTest < ActiveRecord::TestCase
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class DynamicScopeMatchTest < ActiveRecord::TestCase
|
class DynamicScopeMatchTest < ActiveRecord::TestCase
|
||||||
def test_scoped_by_no_match
|
def test_scoped_by_no_match
|
||||||
assert_nil ActiveRecord::DynamicScopeMatch.match("not_scoped_at_all")
|
assert_nil ActiveRecord::DynamicScopeMatch.match("not_scoped_at_all")
|
||||||
end
|
end
|
||||||
|
@ -387,6 +387,8 @@ class DynamicScopeMatchTest < ActiveRecord::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
class DynamicScopeTest < ActiveRecord::TestCase
|
class DynamicScopeTest < ActiveRecord::TestCase
|
||||||
|
fixtures :posts
|
||||||
|
|
||||||
def test_dynamic_scope
|
def test_dynamic_scope
|
||||||
assert_equal Post.scoped_by_author_id(1).find(1), Post.find(1)
|
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"})
|
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"})
|
||||||
|
|
Loading…
Reference in a new issue