2004-11-23 20:04:44 -05:00
|
|
|
require 'abstract_unit'
|
|
|
|
require 'fixtures/company'
|
2004-12-14 07:32:29 -05:00
|
|
|
require 'fixtures/project'
|
2005-07-03 04:52:59 -04:00
|
|
|
require 'fixtures/subscriber'
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
class InheritanceTest < Test::Unit::TestCase
|
2005-07-03 04:52:59 -04:00
|
|
|
fixtures :companies, :projects, :subscribers
|
2004-11-23 20:04:44 -05:00
|
|
|
|
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
|
2005-10-27 04:18:41 -04:00
|
|
|
if current_adapter?(:SQLServerAdapter)
|
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.
|
2005-10-27 04:18:41 -04:00
|
|
|
if current_adapter?(:SQLServerAdapter)
|
2005-07-01 13:20:04 -04:00
|
|
|
Company.connection.execute "SET IDENTITY_INSERT companies OFF"
|
|
|
|
end
|
2004-12-14 07:32:29 -05:00
|
|
|
assert_raises(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
|
|
|
|
|
|
|
|
def test_alt_inheritance_find
|
|
|
|
switch_to_alt_inheritance_column
|
|
|
|
test_inheritance_find
|
|
|
|
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
|
|
|
|
|
|
|
|
def test_alt_inheritance_find_all
|
|
|
|
switch_to_alt_inheritance_column
|
|
|
|
test_inheritance_find_all
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_inheritance_save
|
|
|
|
firm = Firm.new
|
|
|
|
firm.name = "Next Angle"
|
|
|
|
firm.save
|
|
|
|
|
|
|
|
next_angle = Company.find(firm.id)
|
|
|
|
assert next_angle.kind_of?(Firm), "Next Angle should be a firm"
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_alt_inheritance_save
|
|
|
|
switch_to_alt_inheritance_column
|
|
|
|
test_inheritance_save
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_inheritance_condition
|
2005-10-14 20:46:55 -04:00
|
|
|
assert_equal 8, Company.count
|
2005-06-26 07:25:32 -04:00
|
|
|
assert_equal 2, Firm.count
|
|
|
|
assert_equal 3, Client.count
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_alt_inheritance_condition
|
|
|
|
switch_to_alt_inheritance_column
|
|
|
|
test_inheritance_condition
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_finding_incorrect_type_data
|
|
|
|
assert_raises(ActiveRecord::RecordNotFound) { Firm.find(2) }
|
|
|
|
assert_nothing_raised { Firm.find(1) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_alt_finding_incorrect_type_data
|
|
|
|
switch_to_alt_inheritance_column
|
|
|
|
test_finding_incorrect_type_data
|
|
|
|
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
|
|
|
|
assert_equal "37signals", Firm.find(:all).first.name
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_alt_update_all_within_inheritance
|
|
|
|
switch_to_alt_inheritance_column
|
|
|
|
test_update_all_within_inheritance
|
|
|
|
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
|
|
|
|
|
|
|
|
def test_alt_destroy_all_within_inheritance
|
|
|
|
switch_to_alt_inheritance_column
|
|
|
|
test_destroy_all_within_inheritance
|
|
|
|
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
|
|
|
|
|
|
|
|
def test_alt_find_first_within_inheritance
|
|
|
|
switch_to_alt_inheritance_column
|
|
|
|
test_find_first_within_inheritance
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
|
|
|
def Company.inheritance_column() "ruby_type" end
|
|
|
|
end
|
2005-06-13 08:03:33 -04:00
|
|
|
end
|