2008-01-21 12:20:51 -05:00
|
|
|
require "cases/helper"
|
2008-01-18 02:31:37 -05:00
|
|
|
require 'models/developer'
|
|
|
|
require 'models/project'
|
|
|
|
require 'models/company'
|
|
|
|
require 'models/topic'
|
|
|
|
require 'models/reply'
|
|
|
|
require 'models/computer'
|
|
|
|
require 'models/customer'
|
|
|
|
require 'models/order'
|
|
|
|
require 'models/categorization'
|
|
|
|
require 'models/category'
|
|
|
|
require 'models/post'
|
|
|
|
require 'models/author'
|
|
|
|
require 'models/comment'
|
|
|
|
require 'models/tag'
|
|
|
|
require 'models/tagging'
|
|
|
|
require 'models/person'
|
|
|
|
require 'models/reader'
|
2008-02-01 22:27:31 -05:00
|
|
|
require 'models/parrot'
|
|
|
|
require 'models/pirate'
|
|
|
|
require 'models/treasure'
|
|
|
|
require 'models/price_estimate'
|
2008-03-21 14:09:03 -04:00
|
|
|
require 'models/club'
|
|
|
|
require 'models/member'
|
|
|
|
require 'models/membership'
|
|
|
|
require 'models/sponsor'
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class AssociationsTest < ActiveRecord::TestCase
|
2005-06-10 09:54:58 -04:00
|
|
|
fixtures :accounts, :companies, :developers, :projects, :developers_projects,
|
2008-08-26 05:38:48 -04:00
|
|
|
:computers, :people, :readers
|
2005-09-27 23:52:57 -04:00
|
|
|
|
2008-01-18 23:19:53 -05:00
|
|
|
def test_include_with_order_works
|
|
|
|
assert_nothing_raised {Account.find(:first, :order => 'id', :include => :firm)}
|
|
|
|
assert_nothing_raised {Account.find(:first, :order => :id, :include => :firm)}
|
|
|
|
end
|
|
|
|
|
2006-09-25 01:58:38 -04:00
|
|
|
def test_bad_collection_keys
|
|
|
|
assert_raise(ArgumentError, 'ActiveRecord should have barked on bad collection keys') do
|
|
|
|
Class.new(ActiveRecord::Base).has_many(:wheels, :name => 'wheels')
|
|
|
|
end
|
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2007-10-16 03:24:23 -04:00
|
|
|
def test_should_construct_new_finder_sql_after_create
|
2008-04-21 06:00:01 -04:00
|
|
|
person = Person.new :first_name => 'clark'
|
2007-10-16 03:24:23 -04:00
|
|
|
assert_equal [], person.readers.find(:all)
|
|
|
|
person.save!
|
|
|
|
reader = Reader.create! :person => person, :post => Post.new(:title => "foo", :body => "bar")
|
2008-08-26 05:38:48 -04:00
|
|
|
assert person.readers.find(reader.id)
|
2007-10-16 03:24:23 -04:00
|
|
|
end
|
2006-09-25 01:58:38 -04:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def test_force_reload
|
2005-01-15 12:45:16 -05:00
|
|
|
firm = Firm.new("name" => "A New Firm, Inc")
|
2004-11-23 20:04:44 -05:00
|
|
|
firm.save
|
|
|
|
firm.clients.each {|c|} # forcing to load all clients
|
|
|
|
assert firm.clients.empty?, "New firm shouldn't have client objects"
|
|
|
|
assert_equal 0, firm.clients.size, "New firm should have 0 clients"
|
|
|
|
|
2005-01-15 12:45:16 -05:00
|
|
|
client = Client.new("name" => "TheClient.com", "firm_id" => firm.id)
|
2004-11-23 20:04:44 -05:00
|
|
|
client.save
|
|
|
|
|
|
|
|
assert firm.clients.empty?, "New firm should have cached no client objects"
|
|
|
|
assert_equal 0, firm.clients.size, "New firm should have cached 0 clients count"
|
|
|
|
|
|
|
|
assert !firm.clients(true).empty?, "New firm should have reloaded client objects"
|
|
|
|
assert_equal 1, firm.clients(true).size, "New firm should have reloaded clients count"
|
|
|
|
end
|
2009-12-16 11:49:06 -05:00
|
|
|
|
|
|
|
def test_force_reload_is_uncached
|
|
|
|
firm = Firm.create!("name" => "A New Firm, Inc")
|
|
|
|
client = Client.create!("name" => "TheClient.com", :firm => firm)
|
|
|
|
ActiveRecord::Base.cache do
|
|
|
|
firm.clients.each {}
|
|
|
|
assert_queries(0) { assert_not_nil firm.clients.each {} }
|
|
|
|
assert_queries(1) { assert_not_nil firm.clients(true).each {} }
|
|
|
|
end
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class AssociationProxyTest < ActiveRecord::TestCase
|
2007-06-21 16:47:26 -04:00
|
|
|
fixtures :authors, :posts, :categorizations, :categories, :developers, :projects, :developers_projects
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2006-05-28 17:33:34 -04:00
|
|
|
def test_proxy_accessors
|
|
|
|
welcome = posts(:welcome)
|
|
|
|
assert_equal welcome, welcome.author.proxy_owner
|
|
|
|
assert_equal welcome.class.reflect_on_association(:author), welcome.author.proxy_reflection
|
|
|
|
welcome.author.class # force load target
|
|
|
|
assert_equal welcome.author, welcome.author.proxy_target
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2006-05-28 17:33:34 -04:00
|
|
|
david = authors(:david)
|
|
|
|
assert_equal david, david.posts.proxy_owner
|
|
|
|
assert_equal david.class.reflect_on_association(:posts), david.posts.proxy_reflection
|
2008-05-20 07:11:25 -04:00
|
|
|
david.posts.class # force load target
|
2006-05-28 17:33:34 -04:00
|
|
|
assert_equal david.posts, david.posts.proxy_target
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2006-05-28 17:33:34 -04:00
|
|
|
assert_equal david, david.posts_with_extension.testing_proxy_owner
|
|
|
|
assert_equal david.class.reflect_on_association(:posts_with_extension), david.posts_with_extension.testing_proxy_reflection
|
2008-05-20 07:11:25 -04:00
|
|
|
david.posts_with_extension.class # force load target
|
2006-05-28 17:33:34 -04:00
|
|
|
assert_equal david.posts_with_extension, david.posts_with_extension.testing_proxy_target
|
|
|
|
end
|
2006-11-09 21:00:14 -05:00
|
|
|
|
2007-04-25 20:18:38 -04:00
|
|
|
def test_push_does_not_load_target
|
|
|
|
david = authors(:david)
|
2007-06-11 03:15:59 -04:00
|
|
|
|
2007-12-28 13:01:22 -05:00
|
|
|
david.posts << (post = Post.new(:title => "New on Edge", :body => "More cool stuff!"))
|
|
|
|
assert !david.posts.loaded?
|
|
|
|
assert david.posts.include?(post)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_push_has_many_through_does_not_load_target
|
|
|
|
david = authors(:david)
|
|
|
|
|
2007-04-25 20:18:38 -04:00
|
|
|
david.categories << categories(:technology)
|
2007-05-17 22:11:43 -04:00
|
|
|
assert !david.categories.loaded?
|
2007-04-25 20:18:38 -04:00
|
|
|
assert david.categories.include?(categories(:technology))
|
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2007-12-28 13:01:22 -05:00
|
|
|
def test_push_followed_by_save_does_not_load_target
|
|
|
|
david = authors(:david)
|
|
|
|
|
|
|
|
david.posts << (post = Post.new(:title => "New on Edge", :body => "More cool stuff!"))
|
|
|
|
assert !david.posts.loaded?
|
|
|
|
david.save
|
|
|
|
assert !david.posts.loaded?
|
|
|
|
assert david.posts.include?(post)
|
|
|
|
end
|
2007-06-11 03:15:59 -04:00
|
|
|
|
2007-10-27 23:39:18 -04:00
|
|
|
def test_push_does_not_lose_additions_to_new_record
|
|
|
|
josh = Author.new(:name => "Josh")
|
|
|
|
josh.posts << Post.new(:title => "New on Edge", :body => "More cool stuff!")
|
|
|
|
assert josh.posts.loaded?
|
|
|
|
assert_equal 1, josh.posts.size
|
|
|
|
end
|
|
|
|
|
2007-06-21 16:47:26 -04:00
|
|
|
def test_save_on_parent_does_not_load_target
|
|
|
|
david = developers(:david)
|
|
|
|
|
|
|
|
assert !david.projects.loaded?
|
|
|
|
david.update_attribute(:created_at, Time.now)
|
|
|
|
assert !david.projects.loaded?
|
|
|
|
end
|
|
|
|
|
2008-05-08 01:04:53 -04:00
|
|
|
def test_inspect_does_not_reload_a_not_yet_loaded_target
|
|
|
|
andreas = Developer.new :name => 'Andreas', :log => 'new developer added'
|
|
|
|
assert !andreas.audit_logs.loaded?
|
|
|
|
assert_match(/message: "new developer added"/, andreas.audit_logs.inspect)
|
|
|
|
end
|
|
|
|
|
2007-10-10 02:45:13 -04:00
|
|
|
def test_save_on_parent_saves_children
|
|
|
|
developer = Developer.create :name => "Bryan", :salary => 50_000
|
|
|
|
assert_equal 1, developer.reload.audit_logs.size
|
|
|
|
end
|
2007-11-28 21:19:10 -05:00
|
|
|
|
2008-05-23 16:51:56 -04:00
|
|
|
def test_create_via_association_with_block
|
2008-05-23 17:57:11 -04:00
|
|
|
post = authors(:david).posts.create(:title => "New on Edge") {|p| p.body = "More cool stuff!"}
|
|
|
|
assert_equal post.title, "New on Edge"
|
|
|
|
assert_equal post.body, "More cool stuff!"
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_create_with_bang_via_association_with_block
|
|
|
|
post = authors(:david).posts.create!(:title => "New on Edge") {|p| p.body = "More cool stuff!"}
|
|
|
|
assert_equal post.title, "New on Edge"
|
|
|
|
assert_equal post.body, "More cool stuff!"
|
2008-05-23 16:51:56 -04:00
|
|
|
end
|
|
|
|
|
2007-11-28 21:19:10 -05:00
|
|
|
def test_failed_reload_returns_nil
|
|
|
|
p = setup_dangling_association
|
|
|
|
assert_nil p.author.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_failed_reset_returns_nil
|
|
|
|
p = setup_dangling_association
|
|
|
|
assert_nil p.author.reset
|
|
|
|
end
|
|
|
|
|
2007-12-09 20:07:04 -05:00
|
|
|
def test_reload_returns_assocition
|
|
|
|
david = developers(:david)
|
|
|
|
assert_nothing_raised do
|
|
|
|
assert_equal david.projects, david.projects.reload.reload
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-11-28 21:19:10 -05:00
|
|
|
def setup_dangling_association
|
|
|
|
josh = Author.create(:name => "Josh")
|
|
|
|
p = Post.create(:title => "New on Edge", :body => "More cool stuff!", :author => josh)
|
|
|
|
josh.destroy
|
|
|
|
p
|
|
|
|
end
|
2006-05-28 17:33:34 -04:00
|
|
|
end
|
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class OverridingAssociationsTest < ActiveRecord::TestCase
|
2007-10-27 16:31:09 -04:00
|
|
|
class Person < ActiveRecord::Base; end
|
|
|
|
class DifferentPerson < ActiveRecord::Base; end
|
|
|
|
|
|
|
|
class PeopleList < ActiveRecord::Base
|
|
|
|
has_and_belongs_to_many :has_and_belongs_to_many, :before_add => :enlist
|
|
|
|
has_many :has_many, :before_add => :enlist
|
|
|
|
belongs_to :belongs_to
|
|
|
|
has_one :has_one
|
|
|
|
end
|
|
|
|
|
|
|
|
class DifferentPeopleList < PeopleList
|
|
|
|
# Different association with the same name, callbacks should be omitted here.
|
|
|
|
has_and_belongs_to_many :has_and_belongs_to_many, :class_name => 'DifferentPerson'
|
|
|
|
has_many :has_many, :class_name => 'DifferentPerson'
|
|
|
|
belongs_to :belongs_to, :class_name => 'DifferentPerson'
|
|
|
|
has_one :has_one, :class_name => 'DifferentPerson'
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_habtm_association_redefinition_callbacks_should_differ_and_not_inherited
|
|
|
|
# redeclared association on AR descendant should not inherit callbacks from superclass
|
|
|
|
callbacks = PeopleList.read_inheritable_attribute(:before_add_for_has_and_belongs_to_many)
|
|
|
|
assert_equal([:enlist], callbacks)
|
|
|
|
callbacks = DifferentPeopleList.read_inheritable_attribute(:before_add_for_has_and_belongs_to_many)
|
|
|
|
assert_equal([], callbacks)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_has_many_association_redefinition_callbacks_should_differ_and_not_inherited
|
|
|
|
# redeclared association on AR descendant should not inherit callbacks from superclass
|
|
|
|
callbacks = PeopleList.read_inheritable_attribute(:before_add_for_has_many)
|
|
|
|
assert_equal([:enlist], callbacks)
|
|
|
|
callbacks = DifferentPeopleList.read_inheritable_attribute(:before_add_for_has_many)
|
|
|
|
assert_equal([], callbacks)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_habtm_association_redefinition_reflections_should_differ_and_not_inherited
|
|
|
|
assert_not_equal(
|
|
|
|
PeopleList.reflect_on_association(:has_and_belongs_to_many),
|
|
|
|
DifferentPeopleList.reflect_on_association(:has_and_belongs_to_many)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_has_many_association_redefinition_reflections_should_differ_and_not_inherited
|
|
|
|
assert_not_equal(
|
|
|
|
PeopleList.reflect_on_association(:has_many),
|
|
|
|
DifferentPeopleList.reflect_on_association(:has_many)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_belongs_to_association_redefinition_reflections_should_differ_and_not_inherited
|
|
|
|
assert_not_equal(
|
|
|
|
PeopleList.reflect_on_association(:belongs_to),
|
|
|
|
DifferentPeopleList.reflect_on_association(:belongs_to)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_has_one_association_redefinition_reflections_should_differ_and_not_inherited
|
|
|
|
assert_not_equal(
|
|
|
|
PeopleList.reflect_on_association(:has_one),
|
|
|
|
DifferentPeopleList.reflect_on_association(:has_one)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|