2004-11-23 20:04:44 -05:00
require 'abstract_unit'
require 'fixtures/developer'
require 'fixtures/project'
require 'fixtures/company'
require 'fixtures/topic'
require 'fixtures/reply'
2005-01-10 19:24:19 -05:00
require 'fixtures/computer'
2005-08-23 07:05:04 -04:00
require 'fixtures/customer'
require 'fixtures/order'
2006-04-01 15:03:10 -05:00
require 'fixtures/category'
2005-08-24 12:45:46 -04:00
require 'fixtures/post'
require 'fixtures/author'
2004-11-23 20:04:44 -05:00
# Can't declare new classes in test case methods, so tests before that
bad_collection_keys = false
begin
class Car < ActiveRecord :: Base ; has_many :wheels , :name = > " wheels " ; end
2005-09-09 04:32:47 -04:00
rescue ArgumentError
2004-11-23 20:04:44 -05:00
bad_collection_keys = true
end
raise " ActiveRecord should have barked on bad collection keys " unless bad_collection_keys
class AssociationsTest < Test :: Unit :: TestCase
2005-06-10 09:54:58 -04:00
fixtures :accounts , :companies , :developers , :projects , :developers_projects ,
:computers
2005-09-27 23:52:57 -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 ! firm . has_clients? , " New firm shouldn't have clients "
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 ! firm . has_clients? , " New firm should have cached a no-clients response "
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
def test_storing_in_pstore
require " tmpdir "
store_filename = File . join ( Dir . tmpdir , " ar-pstore-association-test " )
File . delete ( store_filename ) if File . exists? ( store_filename )
require " pstore "
apple = Firm . create ( " name " = > " Apple " )
natural = Client . new ( " name " = > " Natural Company " )
apple . clients << natural
db = PStore . new ( store_filename )
db . transaction do
db [ " apple " ] = apple
end
db = PStore . new ( store_filename )
db . transaction do
assert_equal " Natural Company " , db [ " apple " ] . clients . first . name
end
end
end
2006-05-28 17:33:34 -04:00
class AssociationProxyTest < Test :: Unit :: TestCase
fixtures :authors , :posts
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
david = authors ( :david )
assert_equal david , david . posts . proxy_owner
assert_equal david . class . reflect_on_association ( :posts ) , david . posts . proxy_reflection
david . posts . first # force load target
assert_equal david . posts , david . posts . proxy_target
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
david . posts_with_extension . first # force load target
assert_equal david . posts_with_extension , david . posts_with_extension . testing_proxy_target
end
end
2004-11-23 20:04:44 -05:00
class HasOneAssociationsTest < Test :: Unit :: TestCase
2005-06-10 09:54:58 -04:00
fixtures :accounts , :companies , :developers , :projects , :developers_projects
2004-11-23 20:04:44 -05:00
def test_has_one
2005-06-10 09:54:58 -04:00
assert_equal companies ( :first_firm ) . account , Account . find ( 1 )
assert_equal Account . find ( 1 ) . credit_limit , companies ( :first_firm ) . account . credit_limit
2004-11-23 20:04:44 -05:00
end
2005-10-13 15:21:45 -04:00
def test_proxy_assignment
company = companies ( :first_firm )
assert_nothing_raised { company . account = company . account }
end
2005-01-23 10:19:33 -05:00
def test_triple_equality
2005-06-10 09:54:58 -04:00
assert Account === companies ( :first_firm ) . account
2005-11-15 05:48:32 -05:00
assert companies ( :first_firm ) . account === Account
2005-01-23 10:19:33 -05:00
end
2004-11-23 20:04:44 -05:00
def test_type_mismatch
2005-06-10 09:54:58 -04:00
assert_raises ( ActiveRecord :: AssociationTypeMismatch ) { companies ( :first_firm ) . account = 1 }
assert_raises ( ActiveRecord :: AssociationTypeMismatch ) { companies ( :first_firm ) . account = Project . find ( 1 ) }
2004-11-23 20:04:44 -05:00
end
def test_natural_assignment
apple = Firm . create ( " name " = > " Apple " )
citibank = Account . create ( " credit_limit " = > 10 )
apple . account = citibank
assert_equal apple . id , citibank . firm_id
end
def test_natural_assignment_to_nil
2005-06-10 09:54:58 -04:00
old_account_id = companies ( :first_firm ) . account . id
companies ( :first_firm ) . account = nil
companies ( :first_firm ) . save
assert_nil companies ( :first_firm ) . account
2005-03-06 08:40:21 -05:00
# account is dependent, therefore is destroyed when reference to owner is lost
assert_raises ( ActiveRecord :: RecordNotFound ) { Account . find ( old_account_id ) }
2004-11-23 20:04:44 -05:00
end
2005-06-06 17:10:59 -04:00
def test_assignment_without_replacement
apple = Firm . create ( " name " = > " Apple " )
citibank = Account . create ( " credit_limit " = > 10 )
apple . account = citibank
assert_equal apple . id , citibank . firm_id
hsbc = apple . build_account ( { :credit_limit = > 20 } , false )
assert_equal apple . id , hsbc . firm_id
hsbc . save
assert_equal apple . id , citibank . firm_id
nykredit = apple . create_account ( { :credit_limit = > 30 } , false )
assert_equal apple . id , nykredit . firm_id
assert_equal apple . id , citibank . firm_id
assert_equal apple . id , hsbc . firm_id
end
def test_assignment_without_replacement_on_create
apple = Firm . create ( " name " = > " Apple " )
citibank = Account . create ( " credit_limit " = > 10 )
apple . account = citibank
assert_equal apple . id , citibank . firm_id
2005-06-10 09:54:58 -04:00
hsbc = apple . create_account ( { :credit_limit = > 10 } , false )
2005-06-06 17:10:59 -04:00
assert_equal apple . id , hsbc . firm_id
hsbc . save
assert_equal apple . id , citibank . firm_id
end
2004-11-23 20:04:44 -05:00
2005-01-15 12:45:16 -05:00
def test_dependence
2005-10-14 20:46:55 -04:00
num_accounts = Account . count
2005-01-15 12:45:16 -05:00
firm = Firm . find ( 1 )
assert ! firm . account . nil?
2005-10-14 20:46:55 -04:00
firm . destroy
assert_equal num_accounts - 1 , Account . count
2005-01-15 12:45:16 -05:00
end
2005-04-12 01:34:10 -04:00
def test_succesful_build_association
firm = Firm . new ( " name " = > " GlobalMegaCorp " )
firm . save
account = firm . build_account ( " credit_limit " = > 1000 )
assert account . save
assert_equal account , firm . account
end
def test_failing_build_association
firm = Firm . new ( " name " = > " GlobalMegaCorp " )
firm . save
account = firm . build_account
assert ! account . save
assert_equal " can't be empty " , account . errors . on ( " credit_limit " )
end
2006-03-03 01:25:39 -05:00
def test_build_association_twice_without_saving_affects_nothing
count_of_account = Account . count
firm = Firm . find ( :first )
account1 = firm . build_account ( " credit_limit " = > 1000 )
account2 = firm . build_account ( " credit_limit " = > 2000 )
assert_equal count_of_account , Account . count
end
2005-04-12 01:34:10 -04:00
def test_create_association
firm = Firm . new ( " name " = > " GlobalMegaCorp " )
firm . save
assert_equal firm . create_account ( " credit_limit " = > 1000 ) , firm . account
end
2004-11-23 20:04:44 -05:00
def test_build
firm = Firm . new ( " name " = > " GlobalMegaCorp " )
firm . save
2005-01-15 12:45:16 -05:00
2005-01-18 06:07:03 -05:00
firm . account = account = Account . new ( " credit_limit " = > 1000 )
2005-01-15 12:45:16 -05:00
assert_equal account , firm . account
2004-11-23 20:04:44 -05:00
assert account . save
assert_equal account , firm . account
end
2005-01-15 12:45:16 -05:00
def test_build_before_child_saved
firm = Firm . find ( 1 )
account = firm . account . build ( " credit_limit " = > 1000 )
assert_equal account , firm . account
assert account . new_record?
assert firm . save
assert_equal account , firm . account
assert ! account . new_record?
end
def test_build_before_either_saved
firm = Firm . new ( " name " = > " GlobalMegaCorp " )
2005-01-18 06:07:03 -05:00
firm . account = account = Account . new ( " credit_limit " = > 1000 )
2005-01-15 12:45:16 -05:00
assert_equal account , firm . account
assert account . new_record?
assert firm . save
assert_equal account , firm . account
assert ! account . new_record?
end
2004-11-23 20:04:44 -05:00
def test_failing_build_association
firm = Firm . new ( " name " = > " GlobalMegaCorp " )
firm . save
2005-01-18 06:07:03 -05:00
firm . account = account = Account . new
2005-01-15 12:45:16 -05:00
assert_equal account , firm . account
2004-11-23 20:04:44 -05:00
assert ! account . save
2005-01-15 12:45:16 -05:00
assert_equal account , firm . account
2004-11-23 20:04:44 -05:00
assert_equal " can't be empty " , account . errors . on ( " credit_limit " )
end
def test_create
firm = Firm . new ( " name " = > " GlobalMegaCorp " )
firm . save
2005-01-18 06:07:03 -05:00
firm . account = account = Account . create ( " credit_limit " = > 1000 )
assert_equal account , firm . account
2004-11-23 20:04:44 -05:00
end
2005-01-15 12:45:16 -05:00
def test_create_before_save
firm = Firm . new ( " name " = > " GlobalMegaCorp " )
2005-01-18 06:07:03 -05:00
firm . account = account = Account . create ( " credit_limit " = > 1000 )
assert_equal account , firm . account
2004-11-23 20:04:44 -05:00
end
def test_dependence_with_missing_association
Account . destroy_all
2005-01-15 12:45:16 -05:00
firm = Firm . find ( 1 )
assert firm . account . nil?
2004-11-23 20:04:44 -05:00
firm . destroy
end
2005-01-15 12:45:16 -05:00
def test_assignment_before_parent_saved
firm = Firm . new ( " name " = > " GlobalMegaCorp " )
firm . account = a = Account . find ( 1 )
assert firm . new_record?
assert_equal a , firm . account
assert firm . save
assert_equal a , firm . account
assert_equal a , firm . account ( true )
end
def test_assignment_before_child_saved
firm = Firm . find ( 1 )
firm . account = a = Account . new ( " credit_limit " = > 1000 )
assert ! a . new_record?
assert_equal a , firm . account
assert_equal a , firm . account
assert_equal a , firm . account ( true )
end
def test_assignment_before_either_saved
firm = Firm . new ( " name " = > " GlobalMegaCorp " )
firm . account = a = Account . new ( " credit_limit " = > 1000 )
assert firm . new_record?
assert a . new_record?
assert_equal a , firm . account
assert firm . save
assert ! firm . new_record?
assert ! a . new_record?
assert_equal a , firm . account
assert_equal a , firm . account ( true )
end
2006-08-06 15:23:40 -04:00
def test_not_resaved_when_unchanged
firm = Firm . find ( :first , :include = > :account )
assert_queries ( 1 ) { firm . save! }
firm = Firm . find ( :first )
firm . account = Account . find ( :first )
assert_queries ( 1 ) { firm . save! }
firm = Firm . find ( :first ) . clone
firm . account = Account . find ( :first )
assert_queries ( 2 ) { firm . save! }
firm = Firm . find ( :first ) . clone
firm . account = Account . find ( :first ) . clone
assert_queries ( 2 ) { firm . save! }
end
2004-11-23 20:04:44 -05:00
end
class HasManyAssociationsTest < Test :: Unit :: TestCase
2005-06-10 09:54:58 -04:00
fixtures :accounts , :companies , :developers , :projects ,
:developers_projects , :topics
2005-09-27 23:52:57 -04:00
def setup
Client . destroyed_client_ids . clear
end
2004-11-23 20:04:44 -05:00
def force_signal37_to_load_all_clients_of_firm
2005-06-10 09:54:58 -04:00
companies ( :first_firm ) . clients_of_firm . each { | f | }
2004-11-23 20:04:44 -05:00
end
2005-09-27 23:52:57 -04:00
2006-04-19 17:37:54 -04:00
def test_counting_with_counter_sql
2005-06-26 07:25:32 -04:00
assert_equal 2 , Firm . find ( :first ) . clients . count
2005-01-02 13:44:55 -05:00
end
2006-04-19 17:37:54 -04:00
def test_counting
assert_equal 2 , Firm . find ( :first ) . plain_clients . count
end
def test_counting_with_single_conditions
assert_equal 2 , Firm . find ( :first ) . plain_clients . count ( '1=1' )
end
def test_counting_with_single_hash
assert_equal 2 , Firm . find ( :first ) . plain_clients . count ( :conditions = > '1=1' )
end
def test_counting_with_column_name_and_hash
assert_equal 2 , Firm . find ( :first ) . plain_clients . count ( :all , :conditions = > '1=1' )
end
2004-11-23 20:04:44 -05:00
def test_finding
2005-06-26 07:25:32 -04:00
assert_equal 2 , Firm . find ( :first ) . clients . length
2004-11-23 20:04:44 -05:00
end
2005-12-12 19:39:51 -05:00
def test_find_many_with_merged_options
assert_equal 1 , companies ( :first_firm ) . limited_clients . size
assert_equal 1 , companies ( :first_firm ) . limited_clients . find ( :all ) . size
assert_equal 2 , companies ( :first_firm ) . limited_clients . find ( :all , :limit = > nil ) . size
end
2005-11-15 05:48:32 -05:00
def test_triple_equality
assert ! ( Array === Firm . find ( :first ) . clients )
assert Firm . find ( :first ) . clients === Array
end
2004-11-23 20:04:44 -05:00
def test_finding_default_orders
2005-06-26 07:25:32 -04:00
assert_equal " Summit " , Firm . find ( :first ) . clients . first . name
2004-11-23 20:04:44 -05:00
end
def test_finding_with_different_class_name_and_order
2005-06-26 07:25:32 -04:00
assert_equal " Microsoft " , Firm . find ( :first ) . clients_sorted_desc . first . name
2004-11-23 20:04:44 -05:00
end
def test_finding_with_foreign_key
2005-06-26 07:25:32 -04:00
assert_equal " Microsoft " , Firm . find ( :first ) . clients_of_firm . first . name
2004-11-23 20:04:44 -05:00
end
def test_finding_with_condition
2005-06-26 07:25:32 -04:00
assert_equal " Microsoft " , Firm . find ( :first ) . clients_like_ms . first . name
2004-11-23 20:04:44 -05:00
end
def test_finding_using_sql
2005-06-26 07:25:32 -04:00
firm = Firm . find ( :first )
2004-11-23 20:04:44 -05:00
first_client = firm . clients_using_sql . first
assert_not_nil first_client
assert_equal " Microsoft " , first_client . name
assert_equal 1 , firm . clients_using_sql . size
2005-06-26 07:25:32 -04:00
assert_equal 1 , Firm . find ( :first ) . clients_using_sql . size
2004-11-23 20:04:44 -05:00
end
2004-12-07 07:25:01 -05:00
def test_counting_using_sql
2005-06-26 07:25:32 -04:00
assert_equal 1 , Firm . find ( :first ) . clients_using_counter_sql . size
assert_equal 0 , Firm . find ( :first ) . clients_using_zero_counter_sql . size
2004-12-07 07:25:01 -05:00
end
2005-04-07 02:29:31 -04:00
def test_counting_non_existant_items_using_sql
2005-06-26 07:25:32 -04:00
assert_equal 0 , Firm . find ( :first ) . no_clients_using_counter_sql . size
2005-04-07 02:29:31 -04:00
end
2005-01-18 06:07:03 -05:00
def test_belongs_to_sanity
c = Client . new
assert_nil c . firm
if c . firm
assert false , " belongs_to failed if check "
end
unless c . firm
else
assert false , " belongs_to failed unless check "
end
end
2004-12-07 07:25:01 -05:00
2005-01-01 14:50:23 -05:00
def test_find_ids
2005-06-24 16:23:38 -04:00
firm = Firm . find ( :first )
2005-01-01 14:50:23 -05:00
assert_raises ( ActiveRecord :: RecordNotFound ) { firm . clients . find }
client = firm . clients . find ( 2 )
assert_kind_of Client , client
client_ary = firm . clients . find ( [ 2 ] )
assert_kind_of Array , client_ary
assert_equal client , client_ary . first
client_ary = firm . clients . find ( 2 , 3 )
assert_kind_of Array , client_ary
assert_equal 2 , client_ary . size
assert_equal client , client_ary . first
assert_raises ( ActiveRecord :: RecordNotFound ) { firm . clients . find ( 2 , 99 ) }
end
2004-11-23 20:04:44 -05:00
def test_find_all
2005-01-01 14:50:23 -05:00
firm = Firm . find_first
assert_equal firm . clients , firm . clients . find_all
2005-11-16 03:16:54 -05:00
assert_equal 2 , firm . clients . find ( :all , :conditions = > " #{ QUOTED_TYPE } = 'Client' " ) . length
2005-04-18 11:31:20 -04:00
assert_equal 1 , firm . clients . find ( :all , :conditions = > " name = 'Summit' " ) . length
2004-11-23 20:04:44 -05:00
end
def test_find_all_sanitized
firm = Firm . find_first
assert_equal firm . clients . find_all ( " name = 'Summit' " ) , firm . clients . find_all ( [ " name = '%s' " , " Summit " ] )
2005-06-13 00:42:36 -04:00
summit = firm . clients . find ( :all , :conditions = > " name = 'Summit' " )
assert_equal summit , firm . clients . find ( :all , :conditions = > [ " name = ? " , " Summit " ] )
assert_equal summit , firm . clients . find ( :all , :conditions = > [ " name = :name " , { :name = > " Summit " } ] )
2004-11-23 20:04:44 -05:00
end
2005-01-01 14:50:23 -05:00
def test_find_first
firm = Firm . find_first
2005-06-13 00:42:36 -04:00
client2 = Client . find ( 2 )
2005-01-01 14:50:23 -05:00
assert_equal firm . clients . first , firm . clients . find_first
2005-11-16 03:16:54 -05:00
assert_equal client2 , firm . clients . find_first ( " #{ QUOTED_TYPE } = 'Client' " )
assert_equal client2 , firm . clients . find ( :first , :conditions = > " #{ QUOTED_TYPE } = 'Client' " )
2005-01-01 14:50:23 -05:00
end
def test_find_first_sanitized
2005-06-13 00:42:36 -04:00
firm = Firm . find_first
client2 = Client . find ( 2 )
2005-11-16 03:16:54 -05:00
assert_equal client2 , firm . clients . find_first ( [ " #{ QUOTED_TYPE } = ? " , " Client " ] )
assert_equal client2 , firm . clients . find ( :first , :conditions = > [ " #{ QUOTED_TYPE } = ? " , 'Client' ] )
assert_equal client2 , firm . clients . find ( :first , :conditions = > [ " #{ QUOTED_TYPE } = :type " , { :type = > 'Client' } ] )
2005-01-01 14:50:23 -05:00
end
2004-11-23 20:04:44 -05:00
def test_find_in_collection
2005-06-10 09:54:58 -04:00
assert_equal Client . find ( 2 ) . name , companies ( :first_firm ) . clients . find ( 2 ) . name
assert_raises ( ActiveRecord :: RecordNotFound ) { companies ( :first_firm ) . clients . find ( 6 ) }
2004-11-23 20:04:44 -05:00
end
2005-11-10 11:36:01 -05:00
def test_find_grouped
all_clients_of_firm1 = Client . find ( :all , :conditions = > " firm_id = 1 " )
grouped_clients_of_firm1 = Client . find ( :all , :conditions = > " firm_id = 1 " , :group = > " firm_id " , :select = > 'firm_id, count(id) as clients_count' )
assert_equal 2 , all_clients_of_firm1 . size
assert_equal 1 , grouped_clients_of_firm1 . size
end
2004-11-23 20:04:44 -05:00
def test_adding
force_signal37_to_load_all_clients_of_firm
natural = Client . new ( " name " = > " Natural Company " )
2005-06-10 09:54:58 -04:00
companies ( :first_firm ) . clients_of_firm << natural
assert_equal 2 , companies ( :first_firm ) . clients_of_firm . size # checking via the collection
assert_equal 2 , companies ( :first_firm ) . clients_of_firm ( true ) . size # checking using the db
assert_equal natural , companies ( :first_firm ) . clients_of_firm . last
2004-11-23 20:04:44 -05:00
end
2006-06-03 17:16:50 -04:00
def test_adding_using_create
first_firm = companies ( :first_firm )
assert_equal 2 , first_firm . plain_clients . size
natural = first_firm . plain_clients . create ( :name = > " Natural Company " )
assert_equal 3 , first_firm . plain_clients . length
assert_equal 3 , first_firm . plain_clients . size
end
2004-11-23 20:04:44 -05:00
def test_adding_a_mismatch_class
2005-06-10 09:54:58 -04:00
assert_raises ( ActiveRecord :: AssociationTypeMismatch ) { companies ( :first_firm ) . clients_of_firm << nil }
assert_raises ( ActiveRecord :: AssociationTypeMismatch ) { companies ( :first_firm ) . clients_of_firm << 1 }
assert_raises ( ActiveRecord :: AssociationTypeMismatch ) { companies ( :first_firm ) . clients_of_firm << Topic . find ( 1 ) }
2004-11-23 20:04:44 -05:00
end
def test_adding_a_collection
force_signal37_to_load_all_clients_of_firm
2005-06-10 09:54:58 -04:00
companies ( :first_firm ) . clients_of_firm . concat ( [ Client . new ( " name " = > " Natural Company " ) , Client . new ( " name " = > " Apple " ) ] )
assert_equal 3 , companies ( :first_firm ) . clients_of_firm . size
assert_equal 3 , companies ( :first_firm ) . clients_of_firm ( true ) . size
2004-11-23 20:04:44 -05:00
end
2005-01-15 12:45:16 -05:00
def test_adding_before_save
no_of_firms = Firm . count
no_of_clients = Client . count
new_firm = Firm . new ( " name " = > " A New Firm, Inc " )
new_firm . clients_of_firm . push Client . new ( " name " = > " Natural Company " )
new_firm . clients_of_firm << ( c = Client . new ( " name " = > " Apple " ) )
assert new_firm . new_record?
assert c . new_record?
assert_equal 2 , new_firm . clients_of_firm . size
assert_equal no_of_firms , Firm . count # Firm was not saved to database.
assert_equal no_of_clients , Client . count # Clients were not saved to database.
assert new_firm . save
assert ! new_firm . new_record?
assert ! c . new_record?
assert_equal new_firm , c . firm
assert_equal no_of_firms + 1 , Firm . count # Firm was saved to database.
assert_equal no_of_clients + 2 , Client . count # Clients were saved to database.
assert_equal 2 , new_firm . clients_of_firm . size
assert_equal 2 , new_firm . clients_of_firm ( true ) . size
end
def test_invalid_adding
firm = Firm . find ( 1 )
assert ! ( firm . clients_of_firm << c = Client . new )
assert c . new_record?
2005-10-01 22:00:50 -04:00
assert ! firm . valid?
2005-01-15 12:45:16 -05:00
assert ! firm . save
assert c . new_record?
end
def test_invalid_adding_before_save
no_of_firms = Firm . count
no_of_clients = Client . count
new_firm = Firm . new ( " name " = > " A New Firm, Inc " )
new_firm . clients_of_firm . concat ( [ c = Client . new , Client . new ( " name " = > " Apple " ) ] )
assert c . new_record?
assert ! c . valid?
2005-10-01 22:00:50 -04:00
assert ! new_firm . valid?
2005-01-15 12:45:16 -05:00
assert ! new_firm . save
assert c . new_record?
assert new_firm . new_record?
end
2004-11-23 20:04:44 -05:00
def test_build
2005-06-10 09:54:58 -04:00
new_client = companies ( :first_firm ) . clients_of_firm . build ( " name " = > " Another Client " )
2004-11-23 20:04:44 -05:00
assert_equal " Another Client " , new_client . name
2005-01-15 12:45:16 -05:00
assert new_client . new_record?
2005-06-10 09:54:58 -04:00
assert_equal new_client , companies ( :first_firm ) . clients_of_firm . last
assert companies ( :first_firm ) . save
2005-01-15 12:45:16 -05:00
assert ! new_client . new_record?
2005-06-10 09:54:58 -04:00
assert_equal 2 , companies ( :first_firm ) . clients_of_firm ( true ) . size
2004-11-23 20:04:44 -05:00
end
2005-01-15 12:45:16 -05:00
2005-01-25 07:45:01 -05:00
def test_build_many
2005-06-10 09:54:58 -04:00
new_clients = companies ( :first_firm ) . clients_of_firm . build ( [ { " name " = > " Another Client " } , { " name " = > " Another Client II " } ] )
2005-01-25 07:45:01 -05:00
assert_equal 2 , new_clients . size
2005-06-10 09:54:58 -04:00
assert companies ( :first_firm ) . save
assert_equal 3 , companies ( :first_firm ) . clients_of_firm ( true ) . size
2005-01-25 07:45:01 -05:00
end
2006-05-21 15:17:37 -04:00
def test_build_without_loading_association
2006-05-21 18:17:56 -04:00
first_topic = topics ( :first )
2006-05-21 15:17:37 -04:00
Reply . column_names
assert_equal 1 , first_topic . replies . length
assert_no_queries do
first_topic . replies . build ( :title = > " Not saved " , :content = > " Superstars " )
assert_equal 2 , first_topic . replies . size
end
assert_equal 2 , first_topic . replies . to_ary . size
end
def test_create_without_loading_association
first_firm = companies ( :first_firm )
Firm . column_names
Client . column_names
assert_equal 1 , first_firm . clients_of_firm . size
first_firm . clients_of_firm . reset
assert_queries ( 1 ) do
first_firm . clients_of_firm . create ( :name = > " Superstars " )
end
assert_equal 2 , first_firm . clients_of_firm . size
end
2005-01-15 12:45:16 -05:00
def test_invalid_build
2005-06-10 09:54:58 -04:00
new_client = companies ( :first_firm ) . clients_of_firm . build
2005-01-15 12:45:16 -05:00
assert new_client . new_record?
assert ! new_client . valid?
2005-06-10 09:54:58 -04:00
assert_equal new_client , companies ( :first_firm ) . clients_of_firm . last
assert ! companies ( :first_firm ) . save
2005-01-15 12:45:16 -05:00
assert new_client . new_record?
2005-06-10 09:54:58 -04:00
assert_equal 1 , companies ( :first_firm ) . clients_of_firm ( true ) . size
2005-01-15 12:45:16 -05:00
end
2004-11-23 20:04:44 -05:00
def test_create
force_signal37_to_load_all_clients_of_firm
2005-06-10 09:54:58 -04:00
new_client = companies ( :first_firm ) . clients_of_firm . create ( " name " = > " Another Client " )
2005-01-15 12:45:16 -05:00
assert ! new_client . new_record?
2005-06-10 09:54:58 -04:00
assert_equal new_client , companies ( :first_firm ) . clients_of_firm . last
assert_equal new_client , companies ( :first_firm ) . clients_of_firm ( true ) . last
2004-11-23 20:04:44 -05:00
end
2005-01-25 07:45:01 -05:00
def test_create_many
2005-06-10 09:54:58 -04:00
companies ( :first_firm ) . clients_of_firm . create ( [ { " name " = > " Another Client " } , { " name " = > " Another Client II " } ] )
assert_equal 3 , companies ( :first_firm ) . clients_of_firm ( true ) . size
2005-01-25 07:45:01 -05:00
end
2004-11-23 20:04:44 -05:00
2005-11-04 14:39:50 -05:00
def test_find_or_create
number_of_clients = companies ( :first_firm ) . clients . size
the_client = companies ( :first_firm ) . clients . find_or_create_by_name ( " Yet another client " )
assert_equal number_of_clients + 1 , companies ( :first_firm , :refresh ) . clients . size
assert_equal the_client , companies ( :first_firm ) . clients . find_or_create_by_name ( " Yet another client " )
assert_equal number_of_clients + 1 , companies ( :first_firm , :refresh ) . clients . size
end
2004-11-23 20:04:44 -05:00
def test_deleting
force_signal37_to_load_all_clients_of_firm
2005-06-10 09:54:58 -04:00
companies ( :first_firm ) . clients_of_firm . delete ( companies ( :first_firm ) . clients_of_firm . first )
assert_equal 0 , companies ( :first_firm ) . clients_of_firm . size
assert_equal 0 , companies ( :first_firm ) . clients_of_firm ( true ) . size
2004-11-23 20:04:44 -05:00
end
2005-01-15 12:45:16 -05:00
def test_deleting_before_save
new_firm = Firm . new ( " name " = > " A New Firm, Inc. " )
new_client = new_firm . clients_of_firm . build ( " name " = > " Another Client " )
assert_equal 1 , new_firm . clients_of_firm . size
new_firm . clients_of_firm . delete ( new_client )
assert_equal 0 , new_firm . clients_of_firm . size
end
2004-11-23 20:04:44 -05:00
def test_deleting_a_collection
force_signal37_to_load_all_clients_of_firm
2005-06-10 09:54:58 -04:00
companies ( :first_firm ) . clients_of_firm . create ( " name " = > " Another Client " )
assert_equal 2 , companies ( :first_firm ) . clients_of_firm . size
companies ( :first_firm ) . clients_of_firm . delete ( [ companies ( :first_firm ) . clients_of_firm [ 0 ] , companies ( :first_firm ) . clients_of_firm [ 1 ] ] )
assert_equal 0 , companies ( :first_firm ) . clients_of_firm . size
assert_equal 0 , companies ( :first_firm ) . clients_of_firm ( true ) . size
2004-11-23 20:04:44 -05:00
end
2006-04-03 18:37:56 -04:00
def test_delete_all
force_signal37_to_load_all_clients_of_firm
companies ( :first_firm ) . clients_of_firm . create ( " name " = > " Another Client " )
assert_equal 2 , companies ( :first_firm ) . clients_of_firm . size
companies ( :first_firm ) . clients_of_firm . delete_all
assert_equal 0 , companies ( :first_firm ) . clients_of_firm . size
assert_equal 0 , companies ( :first_firm ) . clients_of_firm ( true ) . size
end
def test_delete_all_with_not_yet_loaded_association_collection
force_signal37_to_load_all_clients_of_firm
companies ( :first_firm ) . clients_of_firm . create ( " name " = > " Another Client " )
assert_equal 2 , companies ( :first_firm ) . clients_of_firm . size
companies ( :first_firm ) . clients_of_firm . reset
companies ( :first_firm ) . clients_of_firm . delete_all
assert_equal 0 , companies ( :first_firm ) . clients_of_firm . size
assert_equal 0 , companies ( :first_firm ) . clients_of_firm ( true ) . size
end
2004-11-23 20:04:44 -05:00
2005-09-27 23:52:57 -04:00
def test_clearing_an_association_collection
firm = companies ( :first_firm )
client_id = firm . clients_of_firm . first . id
assert_equal 1 , firm . clients_of_firm . size
firm . clients_of_firm . clear
assert_equal 0 , firm . clients_of_firm . size
assert_equal 0 , firm . clients_of_firm ( true ) . size
assert_equal [ ] , Client . destroyed_client_ids [ firm . id ]
# Should not be destroyed since the association is not dependent.
2005-09-28 00:00:59 -04:00
assert_nothing_raised do
assert Client . find ( client_id ) . firm . nil?
end
2005-09-27 23:52:57 -04:00
end
def test_clearing_a_dependent_association_collection
firm = companies ( :first_firm )
client_id = firm . dependent_clients_of_firm . first . id
assert_equal 1 , firm . dependent_clients_of_firm . size
# :dependent means destroy is called on each client
firm . dependent_clients_of_firm . clear
assert_equal 0 , firm . dependent_clients_of_firm . size
assert_equal 0 , firm . dependent_clients_of_firm ( true ) . size
assert_equal [ client_id ] , Client . destroyed_client_ids [ firm . id ]
# Should be destroyed since the association is dependent.
assert Client . find_by_id ( client_id ) . nil?
end
def test_clearing_an_exclusively_dependent_association_collection
firm = companies ( :first_firm )
client_id = firm . exclusively_dependent_clients_of_firm . first . id
assert_equal 1 , firm . exclusively_dependent_clients_of_firm . size
2005-10-14 08:53:39 -04:00
assert_equal [ ] , Client . destroyed_client_ids [ firm . id ]
2005-09-27 23:52:57 -04:00
# :exclusively_dependent means each client is deleted directly from
# the database without looping through them calling destroy.
firm . exclusively_dependent_clients_of_firm . clear
assert_equal 0 , firm . exclusively_dependent_clients_of_firm . size
assert_equal 0 , firm . exclusively_dependent_clients_of_firm ( true ) . size
2005-10-14 08:53:39 -04:00
assert_equal [ 3 ] , Client . destroyed_client_ids [ firm . id ]
2005-09-27 23:52:57 -04:00
# Should be destroyed since the association is exclusively dependent.
assert Client . find_by_id ( client_id ) . nil?
2005-10-14 20:46:55 -04:00
end
2004-11-23 20:04:44 -05:00
2005-10-20 11:05:48 -04:00
def test_clearing_without_initial_access
firm = companies ( :first_firm )
firm . clients_of_firm . clear
assert_equal 0 , firm . clients_of_firm . size
assert_equal 0 , firm . clients_of_firm ( true ) . size
end
2004-11-23 20:04:44 -05:00
def test_deleting_a_item_which_is_not_in_the_collection
force_signal37_to_load_all_clients_of_firm
summit = Client . find_first ( " name = 'Summit' " )
2005-06-10 09:54:58 -04:00
companies ( :first_firm ) . clients_of_firm . delete ( summit )
assert_equal 1 , companies ( :first_firm ) . clients_of_firm . size
assert_equal 1 , companies ( :first_firm ) . clients_of_firm ( true ) . size
2004-11-23 20:04:44 -05:00
assert_equal 2 , summit . client_of
end
def test_deleting_type_mismatch
david = Developer . find ( 1 )
2005-02-07 08:47:14 -05:00
david . projects . reload
2004-11-23 20:04:44 -05:00
assert_raises ( ActiveRecord :: AssociationTypeMismatch ) { david . projects . delete ( 1 ) }
end
def test_deleting_self_type_mismatch
david = Developer . find ( 1 )
2005-02-07 08:47:14 -05:00
david . projects . reload
2004-11-23 20:04:44 -05:00
assert_raises ( ActiveRecord :: AssociationTypeMismatch ) { david . projects . delete ( Project . find ( 1 ) . developers ) }
end
def test_destroy_all
force_signal37_to_load_all_clients_of_firm
2005-06-10 09:54:58 -04:00
assert ! companies ( :first_firm ) . clients_of_firm . empty? , " 37signals has clients after load "
companies ( :first_firm ) . clients_of_firm . destroy_all
assert companies ( :first_firm ) . clients_of_firm . empty? , " 37signals has no clients after destroy all "
assert companies ( :first_firm ) . clients_of_firm ( true ) . empty? , " 37signals has no clients after destroy all and refresh "
2004-11-23 20:04:44 -05:00
end
def test_dependence
2005-06-13 08:03:33 -04:00
firm = companies ( :first_firm )
assert_equal 2 , firm . clients . size
firm . destroy
assert Client . find ( :all , :conditions = > " firm_id= #{ firm . id } " ) . empty?
2005-04-02 03:52:51 -05:00
end
def test_destroy_dependent_when_deleted_from_association
2005-06-26 07:25:32 -04:00
firm = Firm . find ( :first )
2005-04-02 03:52:51 -05:00
assert_equal 2 , firm . clients . size
client = firm . clients . first
firm . clients . delete ( client )
assert_raise ( ActiveRecord :: RecordNotFound ) { Client . find ( client . id ) }
assert_raise ( ActiveRecord :: RecordNotFound ) { firm . clients . find ( client . id ) }
assert_equal 1 , firm . clients . size
2004-11-23 20:04:44 -05:00
end
2004-12-12 13:12:57 -05:00
def test_three_levels_of_dependence
topic = Topic . create " title " = > " neat and simple "
reply = topic . replies . create " title " = > " neat and simple " , " content " = > " still digging it "
2006-03-09 13:11:41 -05:00
silly_reply = reply . replies . create " title " = > " neat and simple " , " content " = > " ain't complaining "
2004-12-12 13:12:57 -05:00
assert_nothing_raised { topic . destroy }
end
2005-06-10 10:58:02 -04:00
uses_transaction :test_dependence_with_transaction_support_on_failure
2004-11-23 20:04:44 -05:00
def test_dependence_with_transaction_support_on_failure
2005-06-13 08:03:33 -04:00
firm = companies ( :first_firm )
2004-11-23 20:04:44 -05:00
clients = firm . clients
2005-06-13 08:03:33 -04:00
assert_equal 2 , clients . length
2004-11-23 20:04:44 -05:00
clients . last . instance_eval { def before_destroy ( ) raise " Trigger rollback " end }
firm . destroy rescue " do nothing "
2005-06-13 08:03:33 -04:00
assert_equal 2 , Client . find ( :all , :conditions = > " firm_id= #{ firm . id } " ) . size
2004-11-23 20:04:44 -05:00
end
def test_dependence_on_account
2005-10-14 20:46:55 -04:00
num_accounts = Account . count
2005-06-10 09:54:58 -04:00
companies ( :first_firm ) . destroy
2005-10-14 20:46:55 -04:00
assert_equal num_accounts - 1 , Account . count
end
def test_depends_and_nullify
num_accounts = Account . count
num_companies = Company . count
core = companies ( :rails_core )
assert_equal accounts ( :rails_core_account ) , core . account
assert_equal [ companies ( :leetsoft ) , companies ( :jadedpixel ) ] , core . companies
core . destroy
assert_nil accounts ( :rails_core_account ) . reload . firm_id
assert_nil companies ( :leetsoft ) . reload . client_of
assert_nil companies ( :jadedpixel ) . reload . client_of
assert_equal num_accounts , Account . count
2004-11-23 20:04:44 -05:00
end
def test_included_in_collection
2005-06-10 09:54:58 -04:00
assert companies ( :first_firm ) . clients . include? ( Client . find ( 2 ) )
2004-11-23 20:04:44 -05:00
end
def test_adding_array_and_collection
2005-06-26 07:25:32 -04:00
assert_nothing_raised { Firm . find ( :first ) . clients + Firm . find ( :all ) . last . clients }
2004-11-23 20:04:44 -05:00
end
2005-06-13 08:03:33 -04:00
def test_find_all_without_conditions
firm = companies ( :first_firm )
assert_equal 2 , firm . clients . find ( :all ) . length
end
2005-06-16 01:35:10 -04:00
def test_replace_with_less
2005-06-26 07:25:32 -04:00
firm = Firm . find ( :first )
2005-06-16 01:35:10 -04:00
firm . clients = [ companies ( :first_client ) ]
assert firm . save , " Could not save firm "
firm . reload
assert_equal 1 , firm . clients . length
2005-10-14 20:46:55 -04:00
end
2005-06-16 01:35:10 -04:00
def test_replace_with_new
2005-06-26 07:25:32 -04:00
firm = Firm . find ( :first )
2005-06-16 01:35:10 -04:00
new_client = Client . new ( " name " = > " New Client " )
firm . clients = [ companies ( :second_client ) , new_client ]
firm . save
firm . reload
assert_equal 2 , firm . clients . length
assert ! firm . clients . include? ( :first_client )
end
def test_replace_on_new_object
firm = Firm . new ( " name " = > " New Firm " )
firm . clients = [ companies ( :second_client ) , Client . new ( " name " = > " New Client " ) ]
assert firm . save
firm . reload
assert_equal 2 , firm . clients . length
assert firm . clients . include? ( Client . find_by_name ( " New Client " ) )
end
def test_assign_ids
firm = Firm . new ( " name " = > " Apple " )
firm . client_ids = [ companies ( :first_client ) . id , companies ( :second_client ) . id ]
firm . save
firm . reload
assert_equal 2 , firm . clients . length
assert firm . clients . include? ( companies ( :second_client ) )
end
2004-11-23 20:04:44 -05:00
end
class BelongsToAssociationsTest < Test :: Unit :: TestCase
2005-06-10 09:54:58 -04:00
fixtures :accounts , :companies , :developers , :projects , :topics ,
2005-10-15 23:45:39 -04:00
:developers_projects , :computers , :authors , :posts
2005-01-16 19:32:56 -05:00
2004-11-23 20:04:44 -05:00
def test_belongs_to
Client . find ( 3 ) . firm . name
2005-06-10 09:54:58 -04:00
assert_equal companies ( :first_firm ) . name , Client . find ( 3 ) . firm . name
2004-11-23 20:04:44 -05:00
assert ! Client . find ( 3 ) . firm . nil? , " Microsoft should have a firm "
end
2005-10-13 15:21:45 -04:00
def test_proxy_assignment
account = Account . find ( 1 )
assert_nothing_raised { account . firm = account . firm }
end
2005-11-15 05:48:32 -05:00
def test_triple_equality
assert Client . find ( 3 ) . firm === Firm
assert Firm === Client . find ( 3 ) . firm
end
2004-11-23 20:04:44 -05:00
def test_type_mismatch
assert_raise ( ActiveRecord :: AssociationTypeMismatch ) { Account . find ( 1 ) . firm = 1 }
assert_raise ( ActiveRecord :: AssociationTypeMismatch ) { Account . find ( 1 ) . firm = Project . find ( 1 ) }
end
def test_natural_assignment
apple = Firm . create ( " name " = > " Apple " )
citibank = Account . create ( " credit_limit " = > 10 )
citibank . firm = apple
assert_equal apple . id , citibank . firm_id
end
2005-04-12 01:34:10 -04:00
def test_creating_the_belonging_object
citibank = Account . create ( " credit_limit " = > 10 )
apple = citibank . create_firm ( " name " = > " Apple " )
assert_equal apple , citibank . firm
2005-09-20 14:36:14 -04:00
citibank . save
citibank . reload
assert_equal apple , citibank . firm
2005-04-12 01:34:10 -04:00
end
def test_building_the_belonging_object
citibank = Account . create ( " credit_limit " = > 10 )
apple = citibank . build_firm ( " name " = > " Apple " )
citibank . save
assert_equal apple . id , citibank . firm_id
end
2004-11-23 20:04:44 -05:00
def test_natural_assignment_to_nil
client = Client . find ( 3 )
client . firm = nil
client . save
assert_nil client . firm ( true )
assert_nil client . client_of
end
def test_with_different_class_name
assert_equal Company . find ( 1 ) . name , Company . find ( 3 ) . firm_with_other_name . name
assert_not_nil Company . find ( 3 ) . firm_with_other_name , " Microsoft should have a firm "
end
def test_with_condition
assert_equal Company . find ( 1 ) . name , Company . find ( 3 ) . firm_with_condition . name
assert_not_nil Company . find ( 3 ) . firm_with_condition , " Microsoft should have a firm "
end
def test_belongs_to_counter
debate = Topic . create ( " title " = > " debate " )
assert_equal 0 , debate . send ( :read_attribute , " replies_count " ) , " No replies yet "
trash = debate . replies . create ( " title " = > " blah! " , " content " = > " world around! " )
assert_equal 1 , Topic . find ( debate . id ) . send ( :read_attribute , " replies_count " ) , " First reply created "
trash . destroy
assert_equal 0 , Topic . find ( debate . id ) . send ( :read_attribute , " replies_count " ) , " First reply deleted "
end
2006-03-04 14:42:41 -05:00
def test_belongs_to_counter_with_reassigning
t1 = Topic . create ( " title " = > " t1 " )
t2 = Topic . create ( " title " = > " t2 " )
r1 = Reply . new ( " title " = > " r1 " , " content " = > " r1 " )
r1 . topic = t1
assert r1 . save
assert_equal 1 , Topic . find ( t1 . id ) . replies . size
assert_equal 0 , Topic . find ( t2 . id ) . replies . size
r1 . topic = Topic . find ( t2 . id )
assert r1 . save
assert_equal 0 , Topic . find ( t1 . id ) . replies . size
assert_equal 1 , Topic . find ( t2 . id ) . replies . size
r1 . topic = nil
assert_equal 0 , Topic . find ( t1 . id ) . replies . size
assert_equal 0 , Topic . find ( t2 . id ) . replies . size
r1 . topic = t1
assert_equal 1 , Topic . find ( t1 . id ) . replies . size
assert_equal 0 , Topic . find ( t2 . id ) . replies . size
r1 . destroy
assert_equal 0 , Topic . find ( t1 . id ) . replies . size
assert_equal 0 , Topic . find ( t2 . id ) . replies . size
end
2005-01-15 12:45:16 -05:00
def test_assignment_before_parent_saved
2005-06-26 07:25:32 -04:00
client = Client . find ( :first )
2005-01-15 12:45:16 -05:00
apple = Firm . new ( " name " = > " Apple " )
client . firm = apple
assert_equal apple , client . firm
assert apple . new_record?
assert client . save
assert apple . save
assert ! apple . new_record?
assert_equal apple , client . firm
assert_equal apple , client . firm ( true )
end
def test_assignment_before_child_saved
final_cut = Client . new ( " name " = > " Final Cut " )
firm = Firm . find ( 1 )
final_cut . firm = firm
assert final_cut . new_record?
assert final_cut . save
assert ! final_cut . new_record?
assert ! firm . new_record?
assert_equal firm , final_cut . firm
assert_equal firm , final_cut . firm ( true )
end
def test_assignment_before_either_saved
final_cut = Client . new ( " name " = > " Final Cut " )
apple = Firm . new ( " name " = > " Apple " )
final_cut . firm = apple
assert final_cut . new_record?
assert apple . new_record?
assert final_cut . save
assert ! final_cut . new_record?
assert ! apple . new_record?
assert_equal apple , final_cut . firm
assert_equal apple , final_cut . firm ( true )
end
2005-01-16 19:32:56 -05:00
def test_new_record_with_foreign_key_but_no_object
c = Client . new ( " firm_id " = > 1 )
2005-06-26 07:25:32 -04:00
assert_equal Firm . find ( :first ) , c . firm_with_basic_id
2005-01-16 19:32:56 -05:00
end
2005-01-16 19:44:55 -05:00
def test_forgetting_the_load_when_foreign_key_enters_late
c = Client . new
assert_nil c . firm_with_basic_id
c . firm_id = 1
2005-06-26 07:25:32 -04:00
assert_equal Firm . find ( :first ) , c . firm_with_basic_id
2005-01-16 19:44:55 -05:00
end
2005-01-10 19:24:19 -05:00
def test_field_name_same_as_foreign_key
2005-10-15 23:45:39 -04:00
computer = Computer . find ( 1 )
2005-04-18 11:31:20 -04:00
assert_not_nil computer . developer , " :foreign key == attribute didn't lock up " # '
2005-01-10 19:24:19 -05:00
end
2006-03-09 12:23:57 -05:00
def test_counter_cache
topic = Topic . create :title = > " Zoom-zoom-zoom "
assert_equal 0 , topic [ :replies_count ]
reply = Reply . create ( :title = > " re: zoom " , :content = > " speedy quick! " )
reply . topic = topic
assert_equal 1 , topic . reload [ :replies_count ]
2006-03-09 13:11:41 -05:00
assert_equal 1 , topic . replies . size
topic [ :replies_count ] = 15
assert_equal 15 , topic . replies . size
2006-03-09 12:23:57 -05:00
end
def test_custom_counter_cache
reply = Reply . create ( :title = > " re: zoom " , :content = > " speedy quick! " )
assert_equal 0 , reply [ :replies_count ]
silly = SillyReply . create ( :title = > " gaga " , :content = > " boo-boo " )
silly . reply = reply
assert_equal 1 , reply . reload [ :replies_count ]
2006-03-09 13:11:41 -05:00
assert_equal 1 , reply . replies . size
2004-11-23 20:04:44 -05:00
2006-03-09 13:11:41 -05:00
reply [ :replies_count ] = 17
assert_equal 17 , reply . replies . size
2004-11-23 20:04:44 -05:00
end
2005-08-23 07:05:04 -04:00
def test_store_two_association_with_one_save
num_orders = Order . count
num_customers = Customer . count
order = Order . new
customer1 = order . billing = Customer . new
customer2 = order . shipping = Customer . new
assert order . save
assert_equal customer1 , order . billing
assert_equal customer2 , order . shipping
order . reload
assert_equal customer1 , order . billing
assert_equal customer2 , order . shipping
assert_equal num_orders + 1 , Order . count
assert_equal num_customers + 2 , Customer . count
end
2005-09-20 12:12:13 -04:00
2005-08-23 07:05:04 -04:00
def test_store_association_in_two_relations_with_one_save
num_orders = Order . count
num_customers = Customer . count
order = Order . new
customer = order . billing = order . shipping = Customer . new
assert order . save
assert_equal customer , order . billing
assert_equal customer , order . shipping
order . reload
assert_equal customer , order . billing
assert_equal customer , order . shipping
assert_equal num_orders + 1 , Order . count
assert_equal num_customers + 1 , Customer . count
end
2005-08-24 12:25:56 -04:00
2005-09-20 12:12:13 -04:00
def test_store_association_in_two_relations_with_one_save_in_existing_object
num_orders = Order . count
num_customers = Customer . count
order = Order . create
customer = order . billing = order . shipping = Customer . new
assert order . save
assert_equal customer , order . billing
assert_equal customer , order . shipping
order . reload
assert_equal customer , order . billing
assert_equal customer , order . shipping
assert_equal num_orders + 1 , Order . count
assert_equal num_customers + 1 , Customer . count
end
def test_store_association_in_two_relations_with_one_save_in_existing_object_with_values
num_orders = Order . count
num_customers = Customer . count
order = Order . create
customer = order . billing = order . shipping = Customer . new
assert order . save
assert_equal customer , order . billing
assert_equal customer , order . shipping
order . reload
customer = order . billing = order . shipping = Customer . new
assert order . save
order . reload
assert_equal customer , order . billing
assert_equal customer , order . shipping
assert_equal num_orders + 1 , Order . count
assert_equal num_customers + 2 , Customer . count
end
2005-08-24 12:25:56 -04:00
def test_association_assignment_sticks
2005-08-24 12:45:46 -04:00
post = Post . find ( :first )
2005-10-15 23:45:39 -04:00
2005-08-24 12:45:46 -04:00
author1 , author2 = Author . find ( :all , :limit = > 2 )
2005-10-15 23:45:39 -04:00
assert_not_nil author1
assert_not_nil author2
2005-08-24 12:45:46 -04:00
# make sure the association is loaded
post . author
# set the association by id, directly
post . author_id = author2 . id
# save and reload
post . save!
post . reload
# the author id of the post should be the id we set
assert_equal post . author_id , author2 . id
2005-08-24 12:25:56 -04:00
end
2005-06-16 01:35:10 -04:00
2004-11-23 20:04:44 -05:00
end
2005-06-21 08:58:27 -04:00
class ProjectWithAfterCreateHook < ActiveRecord :: Base
set_table_name 'projects'
has_and_belongs_to_many :developers ,
:class_name = > " DeveloperForProjectWithAfterCreateHook " ,
:join_table = > " developers_projects " ,
:foreign_key = > " project_id " ,
:association_foreign_key = > " developer_id "
after_create :add_david
def add_david
david = DeveloperForProjectWithAfterCreateHook . find_by_name ( 'David' )
david . projects << self
end
end
class DeveloperForProjectWithAfterCreateHook < ActiveRecord :: Base
set_table_name 'developers'
has_and_belongs_to_many :projects ,
:class_name = > " ProjectWithAfterCreateHook " ,
:join_table = > " developers_projects " ,
:association_foreign_key = > " project_id " ,
:foreign_key = > " developer_id "
end
2004-11-23 20:04:44 -05:00
class HasAndBelongsToManyAssociationsTest < Test :: Unit :: TestCase
2006-04-01 15:03:10 -05:00
fixtures :accounts , :companies , :categories , :posts , :categories_posts , :developers , :projects , :developers_projects
2004-11-23 20:04:44 -05:00
def test_has_and_belongs_to_many
david = Developer . find ( 1 )
assert ! david . projects . empty?
assert_equal 2 , david . projects . size
active_record = Project . find ( 1 )
assert ! active_record . developers . empty?
2006-03-15 22:24:40 -05:00
assert_equal 3 , active_record . developers . size
2005-06-11 22:27:19 -04:00
assert active_record . developers . include? ( david )
2004-11-23 20:04:44 -05:00
end
2005-06-11 22:27:19 -04:00
2005-11-15 05:48:32 -05:00
def test_triple_equality
assert ! ( Array === Developer . find ( 1 ) . projects )
assert Developer . find ( 1 ) . projects === Array
end
2004-11-23 20:04:44 -05:00
def test_adding_single
jamis = Developer . find ( 2 )
2005-02-07 08:47:14 -05:00
jamis . projects . reload # causing the collection to load
2004-11-23 20:04:44 -05:00
action_controller = Project . find ( 2 )
assert_equal 1 , jamis . projects . size
assert_equal 1 , action_controller . developers . size
jamis . projects << action_controller
assert_equal 2 , jamis . projects . size
assert_equal 2 , jamis . projects ( true ) . size
assert_equal 2 , action_controller . developers ( true ) . size
end
def test_adding_type_mismatch
jamis = Developer . find ( 2 )
assert_raise ( ActiveRecord :: AssociationTypeMismatch ) { jamis . projects << nil }
assert_raise ( ActiveRecord :: AssociationTypeMismatch ) { jamis . projects << 1 }
end
def test_adding_from_the_project
jamis = Developer . find ( 2 )
action_controller = Project . find ( 2 )
2005-02-07 08:47:14 -05:00
action_controller . developers . reload
2004-11-23 20:04:44 -05:00
assert_equal 1 , jamis . projects . size
assert_equal 1 , action_controller . developers . size
action_controller . developers << jamis
assert_equal 2 , jamis . projects ( true ) . size
assert_equal 2 , action_controller . developers . size
assert_equal 2 , action_controller . developers ( true ) . size
end
2005-06-18 01:28:59 -04:00
def test_adding_from_the_project_fixed_timestamp
jamis = Developer . find ( 2 )
action_controller = Project . find ( 2 )
action_controller . developers . reload
assert_equal 1 , jamis . projects . size
assert_equal 1 , action_controller . developers . size
updated_at = jamis . updated_at
action_controller . developers << jamis
assert_equal updated_at , jamis . updated_at
assert_equal 2 , jamis . projects ( true ) . size
assert_equal 2 , action_controller . developers . size
assert_equal 2 , action_controller . developers ( true ) . size
end
2004-11-23 20:04:44 -05:00
def test_adding_multiple
2005-06-13 17:51:43 -04:00
aredridel = Developer . new ( " name " = > " Aredridel " )
2005-02-07 09:15:53 -05:00
aredridel . save
aredridel . projects . reload
aredridel . projects . push ( Project . find ( 1 ) , Project . find ( 2 ) )
assert_equal 2 , aredridel . projects . size
assert_equal 2 , aredridel . projects ( true ) . size
2004-11-23 20:04:44 -05:00
end
def test_adding_a_collection
2005-06-13 17:51:43 -04:00
aredridel = Developer . new ( " name " = > " Aredridel " )
2005-02-07 09:15:53 -05:00
aredridel . save
aredridel . projects . reload
aredridel . projects . concat ( [ Project . find ( 1 ) , Project . find ( 2 ) ] )
assert_equal 2 , aredridel . projects . size
assert_equal 2 , aredridel . projects ( true ) . size
2004-11-23 20:04:44 -05:00
end
2005-01-15 12:45:16 -05:00
2005-07-18 07:06:41 -04:00
def test_adding_uses_default_values_on_join_table
ac = projects ( :action_controller )
assert ! developers ( :jamis ) . projects . include? ( ac )
developers ( :jamis ) . projects << ac
assert developers ( :jamis , :reload ) . projects . include? ( ac )
project = developers ( :jamis ) . projects . detect { | p | p == ac }
assert_equal 1 , project . access_level . to_i
end
def test_adding_uses_explicit_values_on_join_table
ac = projects ( :action_controller )
assert ! developers ( :jamis ) . projects . include? ( ac )
developers ( :jamis ) . projects . push_with_attributes ( ac , :access_level = > 3 )
assert developers ( :jamis , :reload ) . projects . include? ( ac )
project = developers ( :jamis ) . projects . detect { | p | p == ac }
assert_equal 3 , project . access_level . to_i
end
2005-10-22 12:43:39 -04:00
def test_hatbm_attribute_access_and_respond_to
project = developers ( :jamis ) . projects [ 0 ]
assert project . has_attribute? ( " name " )
assert project . has_attribute? ( " joined_on " )
assert project . has_attribute? ( " access_level " )
assert project . respond_to? ( " name " )
assert project . respond_to? ( " name= " )
assert project . respond_to? ( " name? " )
assert project . respond_to? ( " joined_on " )
assert project . respond_to? ( " joined_on= " )
assert project . respond_to? ( " joined_on? " )
assert project . respond_to? ( " access_level " )
assert project . respond_to? ( " access_level= " )
assert project . respond_to? ( " access_level? " )
end
2005-01-15 12:45:16 -05:00
def test_habtm_adding_before_save
no_of_devels = Developer . count
no_of_projects = Project . count
2005-06-13 17:51:43 -04:00
aredridel = Developer . new ( " name " = > " Aredridel " )
2005-02-07 09:15:53 -05:00
aredridel . projects . concat ( [ Project . find ( 1 ) , p = Project . new ( " name " = > " Projekt " ) ] )
assert aredridel . new_record?
2005-01-15 12:45:16 -05:00
assert p . new_record?
2005-02-07 09:15:53 -05:00
assert aredridel . save
assert ! aredridel . new_record?
2005-01-15 12:45:16 -05:00
assert_equal no_of_devels + 1 , Developer . count
assert_equal no_of_projects + 1 , Project . count
2005-02-07 09:15:53 -05:00
assert_equal 2 , aredridel . projects . size
assert_equal 2 , aredridel . projects ( true ) . size
2005-01-15 12:45:16 -05:00
end
2005-01-24 06:39:23 -05:00
def test_habtm_adding_before_save_with_join_attributes
no_of_devels = Developer . count
no_of_projects = Project . count
now = Date . today
ken = Developer . new ( " name " = > " Ken " )
ken . projects . push_with_attributes ( Project . find ( 1 ) , :joined_on = > now )
p = Project . new ( " name " = > " Foomatic " )
ken . projects . push_with_attributes ( p , :joined_on = > now )
assert ken . new_record?
assert p . new_record?
assert ken . save
assert ! ken . new_record?
assert_equal no_of_devels + 1 , Developer . count
assert_equal no_of_projects + 1 , Project . count
assert_equal 2 , ken . projects . size
assert_equal 2 , ken . projects ( true ) . size
kenReloaded = Developer . find_by_name 'Ken'
2006-01-21 18:20:00 -05:00
kenReloaded . projects . each { | prj | assert_date_from_db ( now , prj . joined_on ) }
2005-01-24 06:39:23 -05:00
end
2005-12-21 10:50:31 -05:00
def test_habtm_saving_multiple_relationships
new_project = Project . new ( " name " = > " Grimetime " )
amount_of_developers = 4
2006-05-06 22:03:25 -04:00
developers = ( 0 ... amount_of_developers ) . collect { | i | Developer . create ( :name = > " JME #{ i } " ) } . reverse
2005-12-21 10:50:31 -05:00
new_project . developer_ids = [ developers [ 0 ] . id , developers [ 1 ] . id ]
new_project . developers_with_callback_ids = [ developers [ 2 ] . id , developers [ 3 ] . id ]
assert new_project . save
2006-05-06 22:03:25 -04:00
2005-12-21 10:50:31 -05:00
new_project . reload
assert_equal amount_of_developers , new_project . developers . size
2006-05-06 22:03:25 -04:00
assert_equal developers , new_project . developers
end
def test_habtm_unique_order_preserved
assert_equal [ developers ( :poor_jamis ) , developers ( :jamis ) , developers ( :david ) ] , projects ( :active_record ) . non_unique_developers
assert_equal [ developers ( :poor_jamis ) , developers ( :jamis ) , developers ( :david ) ] , projects ( :active_record ) . developers
2005-12-21 10:50:31 -05:00
end
2005-01-15 12:45:16 -05:00
def test_build
devel = Developer . find ( 1 )
proj = devel . projects . build ( " name " = > " Projekt " )
assert_equal devel . projects . last , proj
assert proj . new_record?
devel . save
assert ! proj . new_record?
assert_equal devel . projects . last , proj
2006-05-29 00:22:49 -04:00
assert_equal Developer . find ( 1 ) . projects . sort_by ( & :id ) . last , proj # prove join table is updated
2006-05-28 23:48:17 -04:00
end
def test_build_by_new_record
devel = Developer . new ( :name = > " Marcel " , :salary = > 75000 )
proj1 = devel . projects . build ( :name = > " Make bed " )
proj2 = devel . projects . build ( :name = > " Lie in it " )
assert_equal devel . projects . last , proj2
assert proj2 . new_record?
devel . save
assert ! devel . new_record?
assert ! proj2 . new_record?
assert_equal devel . projects . last , proj2
assert_equal Developer . find_by_name ( " Marcel " ) . projects . last , proj2 # prove join table is updated
2005-01-15 12:45:16 -05:00
end
def test_create
devel = Developer . find ( 1 )
proj = devel . projects . create ( " name " = > " Projekt " )
assert_equal devel . projects . last , proj
assert ! proj . new_record?
2006-05-29 00:22:49 -04:00
assert_equal Developer . find ( 1 ) . projects . sort_by ( & :id ) . last , proj # prove join table is updated
2006-05-28 23:48:17 -04:00
end
def test_create_by_new_record
devel = Developer . new ( :name = > " Marcel " , :salary = > 75000 )
proj1 = devel . projects . create ( :name = > " Make bed " )
proj2 = devel . projects . create ( :name = > " Lie in it " )
assert_equal devel . projects . last , proj2
assert proj2 . new_record?
devel . save
assert ! devel . new_record?
assert ! proj2 . new_record?
assert_equal devel . projects . last , proj2
assert_equal Developer . find_by_name ( " Marcel " ) . projects . last , proj2 # prove join table is updated
2005-01-15 12:45:16 -05:00
end
2004-11-23 20:04:44 -05:00
def test_uniq_after_the_fact
2005-06-10 09:54:58 -04:00
developers ( :jamis ) . projects << projects ( :active_record )
developers ( :jamis ) . projects << projects ( :active_record )
assert_equal 3 , developers ( :jamis ) . projects . size
assert_equal 1 , developers ( :jamis ) . projects . uniq . size
2004-11-23 20:04:44 -05:00
end
def test_uniq_before_the_fact
2005-06-10 09:54:58 -04:00
projects ( :active_record ) . developers << developers ( :jamis )
projects ( :active_record ) . developers << developers ( :david )
2006-03-15 22:24:40 -05:00
assert_equal 3 , projects ( :active_record , :reload ) . developers . size
2004-11-23 20:04:44 -05:00
end
def test_deleting
david = Developer . find ( 1 )
active_record = Project . find ( 1 )
2005-02-07 08:47:14 -05:00
david . projects . reload
2004-11-23 20:04:44 -05:00
assert_equal 2 , david . projects . size
2006-03-15 22:24:40 -05:00
assert_equal 3 , active_record . developers . size
2004-11-23 20:04:44 -05:00
david . projects . delete ( active_record )
assert_equal 1 , david . projects . size
assert_equal 1 , david . projects ( true ) . size
2006-03-15 22:24:40 -05:00
assert_equal 2 , active_record . developers ( true ) . size
2004-11-23 20:04:44 -05:00
end
def test_deleting_array
david = Developer . find ( 1 )
2005-02-07 08:47:14 -05:00
david . projects . reload
2005-06-26 07:25:32 -04:00
david . projects . delete ( Project . find ( :all ) )
2004-11-23 20:04:44 -05:00
assert_equal 0 , david . projects . size
assert_equal 0 , david . projects ( true ) . size
end
2005-05-19 13:07:56 -04:00
def test_deleting_with_sql
david = Developer . find ( 1 )
active_record = Project . find ( 1 )
active_record . developers . reload
2006-03-15 22:24:40 -05:00
assert_equal 3 , active_record . developers_by_sql . size
2005-05-19 13:07:56 -04:00
active_record . developers_by_sql . delete ( david )
2006-03-15 22:24:40 -05:00
assert_equal 2 , active_record . developers_by_sql ( true ) . size
2005-05-19 13:07:56 -04:00
end
def test_deleting_array_with_sql
active_record = Project . find ( 1 )
active_record . developers . reload
2006-03-15 22:24:40 -05:00
assert_equal 3 , active_record . developers_by_sql . size
2005-05-19 13:07:56 -04:00
2005-06-26 07:25:32 -04:00
active_record . developers_by_sql . delete ( Developer . find ( :all ) )
2005-05-19 13:07:56 -04:00
assert_equal 0 , active_record . developers_by_sql ( true ) . size
end
2004-11-23 20:04:44 -05:00
def test_deleting_all
david = Developer . find ( 1 )
2005-02-07 08:47:14 -05:00
david . projects . reload
2004-11-23 20:04:44 -05:00
david . projects . clear
assert_equal 0 , david . projects . size
assert_equal 0 , david . projects ( true ) . size
end
def test_removing_associations_on_destroy
2005-11-08 05:19:09 -05:00
david = DeveloperWithBeforeDestroyRaise . find ( 1 )
assert ! david . projects . empty?
assert_nothing_raised { david . destroy }
assert david . projects . empty?
assert DeveloperWithBeforeDestroyRaise . connection . select_all ( " SELECT * FROM developers_projects WHERE developer_id = 1 " ) . empty?
2004-11-23 20:04:44 -05:00
end
2005-11-08 05:19:09 -05:00
2004-11-23 20:04:44 -05:00
def test_additional_columns_from_join_table
2006-01-21 18:20:00 -05:00
assert_date_from_db Date . new ( 2004 , 10 , 10 ) , Developer . find ( 1 ) . projects . first . joined_on
2004-11-23 20:04:44 -05:00
end
def test_destroy_all
david = Developer . find ( 1 )
2005-02-07 08:47:14 -05:00
david . projects . reload
2004-11-23 20:04:44 -05:00
assert ! david . projects . empty?
david . projects . destroy_all
assert david . projects . empty?
assert david . projects ( true ) . empty?
end
def test_rich_association
2005-06-10 09:54:58 -04:00
jamis = developers ( :jamis )
jamis . projects . push_with_attributes ( projects ( :action_controller ) , :joined_on = > Date . today )
2006-01-21 18:20:00 -05:00
assert_date_from_db Date . today , jamis . projects . select { | p | p . name == projects ( :action_controller ) . name } . first . joined_on
assert_date_from_db Date . today , developers ( :jamis ) . projects . select { | p | p . name == projects ( :action_controller ) . name } . first . joined_on
2004-11-23 20:04:44 -05:00
end
def test_associations_with_conditions
2006-03-15 22:24:40 -05:00
assert_equal 3 , projects ( :active_record ) . developers . size
2005-06-10 09:54:58 -04:00
assert_equal 1 , projects ( :active_record ) . developers_named_david . size
2005-06-13 17:51:43 -04:00
assert_equal developers ( :david ) , projects ( :active_record ) . developers_named_david . find ( developers ( :david ) . id )
assert_equal developers ( :david ) , projects ( :active_record ) . salaried_developers . find ( developers ( :david ) . id )
2005-06-10 09:54:58 -04:00
projects ( :active_record ) . developers_named_david . clear
2006-03-15 22:24:40 -05:00
assert_equal 2 , projects ( :active_record , :reload ) . developers . size
2004-11-23 20:04:44 -05:00
end
def test_find_in_association
# Using sql
2005-06-10 09:54:58 -04:00
assert_equal developers ( :david ) , projects ( :active_record ) . developers . find ( developers ( :david ) . id ) , " SQL find "
2004-11-23 20:04:44 -05:00
# Using ruby
2005-06-10 09:54:58 -04:00
active_record = projects ( :active_record )
active_record . developers . reload
assert_equal developers ( :david ) , active_record . developers . find ( developers ( :david ) . id ) , " Ruby find "
2004-11-23 20:04:44 -05:00
end
2005-04-18 11:31:20 -04:00
2005-09-13 06:15:54 -04:00
def test_find_in_association_with_custom_finder_sql
assert_equal developers ( :david ) , projects ( :active_record ) . developers_with_finder_sql . find ( developers ( :david ) . id ) , " SQL find "
active_record = projects ( :active_record )
active_record . developers_with_finder_sql . reload
assert_equal developers ( :david ) , active_record . developers_with_finder_sql . find ( developers ( :david ) . id ) , " Ruby find "
end
def test_find_in_association_with_custom_finder_sql_and_string_id
assert_equal developers ( :david ) , projects ( :active_record ) . developers_with_finder_sql . find ( developers ( :david ) . id . to_s ) , " SQL find "
end
2005-12-12 19:39:51 -05:00
def test_find_with_merged_options
assert_equal 1 , projects ( :active_record ) . limited_developers . size
assert_equal 1 , projects ( :active_record ) . limited_developers . find ( :all ) . size
2006-03-15 22:24:40 -05:00
assert_equal 3 , projects ( :active_record ) . limited_developers . find ( :all , :limit = > nil ) . size
2005-12-12 19:39:51 -05:00
end
2005-09-13 06:15:54 -04:00
2005-06-21 08:58:27 -04:00
def test_new_with_values_in_collection
jamis = DeveloperForProjectWithAfterCreateHook . find_by_name ( 'Jamis' )
david = DeveloperForProjectWithAfterCreateHook . find_by_name ( 'David' )
project = ProjectWithAfterCreateHook . new ( :name = > " Cooking with Bertie " )
project . developers << jamis
project . save!
project . reload
assert project . developers . include? ( jamis )
assert project . developers . include? ( david )
end
2006-03-15 22:24:40 -05:00
def test_find_in_association_with_options
2005-06-10 09:54:58 -04:00
developers = projects ( :active_record ) . developers . find ( :all )
2006-03-15 22:24:40 -05:00
assert_equal 3 , developers . size
2005-04-18 11:31:20 -04:00
2006-03-15 22:24:40 -05:00
assert_equal developers ( :poor_jamis ) , projects ( :active_record ) . developers . find ( :first , :conditions = > " salary < 10000 " )
assert_equal developers ( :jamis ) , projects ( :active_record ) . developers . find ( :first , :order = > " salary DESC " )
2005-04-18 11:31:20 -04:00
end
2005-06-16 01:35:10 -04:00
def test_replace_with_less
david = developers ( :david )
david . projects = [ projects ( :action_controller ) ]
assert david . save
assert_equal 1 , david . projects . length
end
def test_replace_with_new
david = developers ( :david )
david . projects = [ projects ( :action_controller ) , Project . new ( " name " = > " ActionWebSearch " ) ]
david . save
assert_equal 2 , david . projects . length
assert ! david . projects . include? ( projects ( :active_record ) )
end
def test_replace_on_new_object
new_developer = Developer . new ( " name " = > " Matz " )
new_developer . projects = [ projects ( :action_controller ) , Project . new ( " name " = > " ActionWebSearch " ) ]
new_developer . save
assert_equal 2 , new_developer . projects . length
end
2005-07-03 04:52:59 -04:00
def test_consider_type
developer = Developer . find ( :first )
special_project = SpecialProject . create ( " name " = > " Special Project " )
other_project = developer . projects . first
developer . special_projects << special_project
developer . reload
assert developer . projects . include? ( special_project )
assert developer . special_projects . include? ( special_project )
assert ! developer . special_projects . include? ( other_project )
end
2005-11-23 16:03:25 -05:00
def test_update_attributes_after_push_without_duplicate_join_table_rows
developer = Developer . new ( " name " = > " Kano " )
project = SpecialProject . create ( " name " = > " Special Project " )
assert developer . save
developer . projects << project
developer . update_attribute ( " name " , " Bruza " )
2005-12-08 18:23:34 -05:00
assert_equal 1 , Developer . connection . select_value ( <<-end_sql).to_i
SELECT count ( * ) FROM developers_projects
WHERE project_id = #{project.id}
AND developer_id = #{developer.id}
end_sql
2005-11-23 16:03:25 -05:00
end
2006-04-01 15:03:10 -05:00
def test_updating_attributes_on_non_rich_associations
welcome = categories ( :technology ) . posts . first
welcome . title = " Something else "
assert welcome . save!
end
def test_updating_attributes_on_rich_associations
david = projects ( :action_controller ) . developers . first
david . name = " DHH "
assert_raises ( ActiveRecord :: ReadOnlyRecord ) { david . save! }
end
def test_updating_attributes_on_rich_associations_with_limited_find
david = projects ( :action_controller ) . developers . find ( :all , :select = > " developers.* " ) . first
david . name = " DHH "
assert david . save!
end
2006-03-15 23:18:12 -05:00
def test_join_table_alias
2006-03-18 02:31:01 -05:00
assert_equal 3 , Developer . find ( :all , :include = > { :projects = > :developers } , :conditions = > 'developers_projects_join.joined_on IS NOT NULL' ) . size
2006-03-15 23:18:12 -05:00
end
2006-07-04 21:43:47 -04:00
def test_join_with_group
2006-07-07 06:42:14 -04:00
group = Developer . columns . inject ( [ ] ) do | g , c |
g << " developers. #{ c . name } "
g << " developers_projects_2. #{ c . name } "
end
Project . columns . each { | c | group << " projects. #{ c . name } " }
assert_equal 3 , Developer . find ( :all , :include = > { :projects = > :developers } , :conditions = > 'developers_projects_join.joined_on IS NOT NULL' , :group = > group . join ( " , " ) ) . size
2006-07-04 21:43:47 -04:00
end
2005-06-10 09:54:58 -04:00
end