2008-01-21 12:20:51 -05:00
|
|
|
require "cases/helper"
|
2008-01-18 02:31:37 -05:00
|
|
|
require 'models/company'
|
|
|
|
require 'models/project'
|
|
|
|
require 'models/subscriber'
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class InheritanceTest < ActiveRecord::TestCase
|
2006-12-18 22:10:39 -05:00
|
|
|
fixtures :companies, :projects, :subscribers, :accounts
|
2008-05-31 20:13:11 -04:00
|
|
|
|
|
|
|
def test_class_with_store_full_sti_class_returns_full_name
|
|
|
|
old = ActiveRecord::Base.store_full_sti_class
|
|
|
|
ActiveRecord::Base.store_full_sti_class = true
|
|
|
|
assert_equal 'Namespaced::Company', Namespaced::Company.sti_name
|
|
|
|
ensure
|
|
|
|
ActiveRecord::Base.store_full_sti_class = old
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_class_without_store_full_sti_class_returns_demodulized_name
|
|
|
|
old = ActiveRecord::Base.store_full_sti_class
|
|
|
|
ActiveRecord::Base.store_full_sti_class = false
|
|
|
|
assert_equal 'Company', Namespaced::Company.sti_name
|
|
|
|
ensure
|
|
|
|
ActiveRecord::Base.store_full_sti_class = old
|
|
|
|
end
|
|
|
|
|
2008-04-11 11:35:09 -04:00
|
|
|
def test_should_store_demodulized_class_name_with_store_full_sti_class_option_disabled
|
|
|
|
old = ActiveRecord::Base.store_full_sti_class
|
|
|
|
ActiveRecord::Base.store_full_sti_class = false
|
|
|
|
item = Namespaced::Company.new
|
|
|
|
assert_equal 'Company', item[:type]
|
|
|
|
ensure
|
|
|
|
ActiveRecord::Base.store_full_sti_class = old
|
|
|
|
end
|
2009-04-29 18:39:53 -04:00
|
|
|
|
2008-04-11 11:35:09 -04:00
|
|
|
def test_should_store_full_class_name_with_store_full_sti_class_option_enabled
|
|
|
|
old = ActiveRecord::Base.store_full_sti_class
|
|
|
|
ActiveRecord::Base.store_full_sti_class = true
|
|
|
|
item = Namespaced::Company.new
|
|
|
|
assert_equal 'Namespaced::Company', item[:type]
|
|
|
|
ensure
|
|
|
|
ActiveRecord::Base.store_full_sti_class = old
|
|
|
|
end
|
2009-04-29 18:39:53 -04:00
|
|
|
|
2008-04-11 11:35:09 -04:00
|
|
|
def test_different_namespace_subclass_should_load_correctly_with_store_full_sti_class_option
|
|
|
|
old = ActiveRecord::Base.store_full_sti_class
|
|
|
|
ActiveRecord::Base.store_full_sti_class = true
|
|
|
|
item = Namespaced::Company.create :name => "Wolverine 2"
|
|
|
|
assert_not_nil Company.find(item.id)
|
|
|
|
assert_not_nil Namespaced::Company.find(item.id)
|
|
|
|
ensure
|
|
|
|
ActiveRecord::Base.store_full_sti_class = old
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2007-01-22 23:19:16 -05:00
|
|
|
def test_company_descends_from_active_record
|
|
|
|
assert_raise(NoMethodError) { ActiveRecord::Base.descends_from_active_record? }
|
|
|
|
assert AbstractCompany.descends_from_active_record?, 'AbstractCompany should descend from ActiveRecord::Base'
|
|
|
|
assert Company.descends_from_active_record?, 'Company should descend from ActiveRecord::Base'
|
|
|
|
assert !Class.new(Company).descends_from_active_record?, 'Company subclass should not descend from ActiveRecord::Base'
|
|
|
|
end
|
|
|
|
|
2004-12-14 07:32:29 -05:00
|
|
|
def test_a_bad_type_column
|
2005-07-01 13:20:04 -04:00
|
|
|
#SQLServer need to turn Identity Insert On before manually inserting into the Identity column
|
2008-11-19 11:09:44 -05:00
|
|
|
if current_adapter?(:SybaseAdapter)
|
2005-07-01 13:20:04 -04:00
|
|
|
Company.connection.execute "SET IDENTITY_INSERT companies ON"
|
|
|
|
end
|
2005-11-16 03:16:54 -05:00
|
|
|
Company.connection.insert "INSERT INTO companies (id, #{QUOTED_TYPE}, name) VALUES(100, 'bad_class!', 'Not happening')"
|
2005-10-27 04:18:41 -04:00
|
|
|
|
2005-07-01 13:20:04 -04:00
|
|
|
#We then need to turn it back Off before continuing.
|
2008-11-19 11:09:44 -05:00
|
|
|
if current_adapter?(:SybaseAdapter)
|
2005-07-01 13:20:04 -04:00
|
|
|
Company.connection.execute "SET IDENTITY_INSERT companies OFF"
|
|
|
|
end
|
2009-03-08 16:11:58 -04:00
|
|
|
assert_raise(ActiveRecord::SubclassNotFound) { Company.find(100) }
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_inheritance_find
|
|
|
|
assert Company.find(1).kind_of?(Firm), "37signals should be a firm"
|
|
|
|
assert Firm.find(1).kind_of?(Firm), "37signals should be a firm"
|
|
|
|
assert Company.find(2).kind_of?(Client), "Summit should be a client"
|
|
|
|
assert Client.find(2).kind_of?(Client), "Summit should be a client"
|
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def test_alt_inheritance_find
|
|
|
|
switch_to_alt_inheritance_column
|
|
|
|
test_inheritance_find
|
2006-11-09 14:31:31 -05:00
|
|
|
switch_to_default_inheritance_column
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_inheritance_find_all
|
2005-06-26 07:25:32 -04:00
|
|
|
companies = Company.find(:all, :order => 'id')
|
2004-11-23 20:04:44 -05:00
|
|
|
assert companies[0].kind_of?(Firm), "37signals should be a firm"
|
|
|
|
assert companies[1].kind_of?(Client), "Summit should be a client"
|
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def test_alt_inheritance_find_all
|
|
|
|
switch_to_alt_inheritance_column
|
|
|
|
test_inheritance_find_all
|
2006-11-09 14:31:31 -05:00
|
|
|
switch_to_default_inheritance_column
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_inheritance_save
|
|
|
|
firm = Firm.new
|
|
|
|
firm.name = "Next Angle"
|
|
|
|
firm.save
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
next_angle = Company.find(firm.id)
|
|
|
|
assert next_angle.kind_of?(Firm), "Next Angle should be a firm"
|
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def test_alt_inheritance_save
|
|
|
|
switch_to_alt_inheritance_column
|
|
|
|
test_inheritance_save
|
2006-11-09 14:31:31 -05:00
|
|
|
switch_to_default_inheritance_column
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_inheritance_condition
|
2009-06-30 18:55:09 -04:00
|
|
|
assert_equal 10, Company.count
|
2005-06-26 07:25:32 -04:00
|
|
|
assert_equal 2, Firm.count
|
2009-06-30 18:55:09 -04:00
|
|
|
assert_equal 4, Client.count
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def test_alt_inheritance_condition
|
|
|
|
switch_to_alt_inheritance_column
|
|
|
|
test_inheritance_condition
|
2006-11-09 14:31:31 -05:00
|
|
|
switch_to_default_inheritance_column
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_finding_incorrect_type_data
|
2009-03-08 16:11:58 -04:00
|
|
|
assert_raise(ActiveRecord::RecordNotFound) { Firm.find(2) }
|
2004-11-23 20:04:44 -05:00
|
|
|
assert_nothing_raised { Firm.find(1) }
|
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def test_alt_finding_incorrect_type_data
|
|
|
|
switch_to_alt_inheritance_column
|
|
|
|
test_finding_incorrect_type_data
|
2006-11-09 14:31:31 -05:00
|
|
|
switch_to_default_inheritance_column
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_update_all_within_inheritance
|
|
|
|
Client.update_all "name = 'I am a client'"
|
2005-06-26 07:25:32 -04:00
|
|
|
assert_equal "I am a client", Client.find(:all).first.name
|
2009-03-22 18:14:42 -04:00
|
|
|
# Order by added as otherwise Oracle tests were failing because of different order of results
|
|
|
|
assert_equal "37signals", Firm.find(:all, :order => "id").first.name
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def test_alt_update_all_within_inheritance
|
|
|
|
switch_to_alt_inheritance_column
|
|
|
|
test_update_all_within_inheritance
|
2006-11-09 14:31:31 -05:00
|
|
|
switch_to_default_inheritance_column
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_destroy_all_within_inheritance
|
|
|
|
Client.destroy_all
|
2005-06-26 07:25:32 -04:00
|
|
|
assert_equal 0, Client.count
|
|
|
|
assert_equal 2, Firm.count
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def test_alt_destroy_all_within_inheritance
|
|
|
|
switch_to_alt_inheritance_column
|
|
|
|
test_destroy_all_within_inheritance
|
2006-11-09 14:31:31 -05:00
|
|
|
switch_to_default_inheritance_column
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_find_first_within_inheritance
|
2005-06-26 07:25:32 -04:00
|
|
|
assert_kind_of Firm, Company.find(:first, :conditions => "name = '37signals'")
|
|
|
|
assert_kind_of Firm, Firm.find(:first, :conditions => "name = '37signals'")
|
|
|
|
assert_nil Client.find(:first, :conditions => "name = '37signals'")
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def test_alt_find_first_within_inheritance
|
|
|
|
switch_to_alt_inheritance_column
|
|
|
|
test_find_first_within_inheritance
|
2006-11-09 14:31:31 -05:00
|
|
|
switch_to_default_inheritance_column
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_complex_inheritance
|
|
|
|
very_special_client = VerySpecialClient.create("name" => "veryspecial")
|
2005-06-26 07:25:32 -04:00
|
|
|
assert_equal very_special_client, VerySpecialClient.find(:first, :conditions => "name = 'veryspecial'")
|
|
|
|
assert_equal very_special_client, SpecialClient.find(:first, :conditions => "name = 'veryspecial'")
|
|
|
|
assert_equal very_special_client, Company.find(:first, :conditions => "name = 'veryspecial'")
|
|
|
|
assert_equal very_special_client, Client.find(:first, :conditions => "name = 'veryspecial'")
|
|
|
|
assert_equal 1, Client.find(:all, :conditions => "name = 'Summit'").size
|
2004-11-23 20:04:44 -05:00
|
|
|
assert_equal very_special_client, Client.find(very_special_client.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_alt_complex_inheritance
|
|
|
|
switch_to_alt_inheritance_column
|
|
|
|
test_complex_inheritance
|
2006-11-09 14:31:31 -05:00
|
|
|
switch_to_default_inheritance_column
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2006-12-18 22:10:39 -05:00
|
|
|
def test_eager_load_belongs_to_something_inherited
|
|
|
|
account = Account.find(1, :include => :firm)
|
|
|
|
assert_not_nil account.instance_variable_get("@firm"), "nil proves eager load failed"
|
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2008-07-10 23:18:41 -04:00
|
|
|
def test_eager_load_belongs_to_primary_key_quoting
|
|
|
|
con = Account.connection
|
2008-09-10 14:22:05 -04:00
|
|
|
assert_sql(/\(#{con.quote_table_name('companies')}.#{con.quote_column_name('id')} = 1\)/) do
|
2008-07-10 23:18:41 -04:00
|
|
|
Account.find(1, :include => :firm)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-12-18 22:10:39 -05:00
|
|
|
def test_alt_eager_loading
|
|
|
|
switch_to_alt_inheritance_column
|
|
|
|
test_eager_load_belongs_to_something_inherited
|
|
|
|
switch_to_default_inheritance_column
|
|
|
|
end
|
2004-12-14 07:32:29 -05:00
|
|
|
|
|
|
|
def test_inheritance_without_mapping
|
2005-07-03 04:52:59 -04:00
|
|
|
assert_kind_of SpecialSubscriber, SpecialSubscriber.find("webster132")
|
2005-10-12 15:55:46 -04:00
|
|
|
assert_nothing_raised { s = SpecialSubscriber.new("name" => "And breaaaaathe!"); s.id = 'roger'; s.save }
|
2004-12-14 07:32:29 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def switch_to_alt_inheritance_column
|
|
|
|
# we don't want misleading test results, so get rid of the values in the type column
|
2005-06-26 07:25:32 -04:00
|
|
|
Company.find(:all, :order => 'id').each do |c|
|
2004-12-14 07:32:29 -05:00
|
|
|
c['type'] = nil
|
|
|
|
c.save
|
|
|
|
end
|
2006-11-09 14:31:31 -05:00
|
|
|
[ Company, Firm, Client].each { |klass| klass.reset_column_information }
|
2006-12-18 22:10:39 -05:00
|
|
|
Company.set_inheritance_column('ruby_type')
|
2006-11-09 14:31:31 -05:00
|
|
|
end
|
|
|
|
def switch_to_default_inheritance_column
|
|
|
|
[ Company, Firm, Client].each { |klass| klass.reset_column_information }
|
2006-12-18 22:10:39 -05:00
|
|
|
Company.set_inheritance_column('type')
|
2004-12-14 07:32:29 -05:00
|
|
|
end
|
2005-06-13 08:03:33 -04:00
|
|
|
end
|
2006-12-19 14:23:56 -05:00
|
|
|
|
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class InheritanceComputeTypeTest < ActiveRecord::TestCase
|
2006-12-19 14:23:56 -05:00
|
|
|
fixtures :companies
|
|
|
|
|
|
|
|
def setup
|
2008-06-03 14:32:53 -04:00
|
|
|
ActiveSupport::Dependencies.log_activity = true
|
2006-12-19 14:23:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
2008-06-03 14:32:53 -04:00
|
|
|
ActiveSupport::Dependencies.log_activity = false
|
2006-12-19 14:23:56 -05:00
|
|
|
self.class.const_remove :FirmOnTheFly rescue nil
|
|
|
|
Firm.const_remove :FirmOnTheFly rescue nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_instantiation_doesnt_try_to_require_corresponding_file
|
2010-01-03 22:30:28 -05:00
|
|
|
ActiveRecord::Base.store_full_sti_class = false
|
2006-12-19 14:23:56 -05:00
|
|
|
foo = Firm.find(:first).clone
|
|
|
|
foo.ruby_type = foo.type = 'FirmOnTheFly'
|
|
|
|
foo.save!
|
|
|
|
|
|
|
|
# Should fail without FirmOnTheFly in the type condition.
|
|
|
|
assert_raise(ActiveRecord::RecordNotFound) { Firm.find(foo.id) }
|
|
|
|
|
|
|
|
# Nest FirmOnTheFly in the test case where Dependencies won't see it.
|
|
|
|
self.class.const_set :FirmOnTheFly, Class.new(Firm)
|
|
|
|
assert_raise(ActiveRecord::SubclassNotFound) { Firm.find(foo.id) }
|
|
|
|
|
|
|
|
# Nest FirmOnTheFly in Firm where Dependencies will see it.
|
|
|
|
# This is analogous to nesting models in a migration.
|
|
|
|
Firm.const_set :FirmOnTheFly, Class.new(Firm)
|
|
|
|
|
|
|
|
# And instantiate will find the existing constant rather than trying
|
|
|
|
# to require firm_on_the_fly.
|
|
|
|
assert_nothing_raised { assert_kind_of Firm::FirmOnTheFly, Firm.find(foo.id) }
|
2010-01-03 22:30:28 -05:00
|
|
|
ensure
|
|
|
|
ActiveRecord::Base.store_full_sti_class = true
|
2006-12-19 14:23:56 -05:00
|
|
|
end
|
|
|
|
end
|