2016-08-06 16:26:20 +00:00
|
|
|
require "cases/helper"
|
|
|
|
require "models/author"
|
|
|
|
require "models/company"
|
|
|
|
require "models/person"
|
|
|
|
require "models/post"
|
|
|
|
require "models/project"
|
|
|
|
require "models/subscriber"
|
|
|
|
require "models/vegetables"
|
|
|
|
require "models/shop"
|
|
|
|
require "models/sponsor"
|
2004-11-24 01:04:44 +00:00
|
|
|
|
2015-05-07 13:04:49 +00:00
|
|
|
module InheritanceTestHelper
|
|
|
|
def with_store_full_sti_class(&block)
|
|
|
|
assign_store_full_sti_class true, &block
|
|
|
|
end
|
|
|
|
|
|
|
|
def without_store_full_sti_class(&block)
|
|
|
|
assign_store_full_sti_class false, &block
|
|
|
|
end
|
|
|
|
|
|
|
|
def assign_store_full_sti_class(flag)
|
|
|
|
old_store_full_sti_class = ActiveRecord::Base.store_full_sti_class
|
|
|
|
ActiveRecord::Base.store_full_sti_class = flag
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
ActiveRecord::Base.store_full_sti_class = old_store_full_sti_class
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-01-21 17:20:51 +00:00
|
|
|
class InheritanceTest < ActiveRecord::TestCase
|
2015-05-07 13:04:49 +00:00
|
|
|
include InheritanceTestHelper
|
2012-09-02 15:19:50 +00:00
|
|
|
fixtures :companies, :projects, :subscribers, :accounts, :vegetables
|
2008-06-01 00:13:11 +00:00
|
|
|
|
|
|
|
def test_class_with_store_full_sti_class_returns_full_name
|
2015-05-07 13:04:49 +00:00
|
|
|
with_store_full_sti_class do
|
2016-08-06 16:26:20 +00:00
|
|
|
assert_equal "Namespaced::Company", Namespaced::Company.sti_name
|
2015-05-07 13:04:49 +00:00
|
|
|
end
|
2008-06-01 00:13:11 +00:00
|
|
|
end
|
|
|
|
|
2010-09-30 23:02:49 +00:00
|
|
|
def test_class_with_blank_sti_name
|
2012-04-26 17:32:55 +00:00
|
|
|
company = Company.first
|
2010-11-23 19:57:33 +00:00
|
|
|
company = company.dup
|
2010-09-30 23:02:49 +00:00
|
|
|
company.extend(Module.new {
|
2014-11-18 23:19:15 +00:00
|
|
|
def _read_attribute(name)
|
2016-08-06 16:26:20 +00:00
|
|
|
return " " if name == "type"
|
2010-09-30 23:02:49 +00:00
|
|
|
super
|
|
|
|
end
|
|
|
|
})
|
|
|
|
company.save!
|
2012-08-03 10:51:52 +00:00
|
|
|
company = Company.all.to_a.find { |x| x.id == company.id }
|
2016-08-06 16:26:20 +00:00
|
|
|
assert_equal " ", company.type
|
2010-09-30 23:02:49 +00:00
|
|
|
end
|
|
|
|
|
2008-06-01 00:13:11 +00:00
|
|
|
def test_class_without_store_full_sti_class_returns_demodulized_name
|
2015-05-07 13:04:49 +00:00
|
|
|
without_store_full_sti_class do
|
2016-08-06 16:26:20 +00:00
|
|
|
assert_equal "Company", Namespaced::Company.sti_name
|
2015-05-07 13:04:49 +00:00
|
|
|
end
|
2008-06-01 00:13:11 +00:00
|
|
|
end
|
|
|
|
|
2015-10-30 23:30:18 +00:00
|
|
|
def test_compute_type_success
|
2016-08-06 16:26:20 +00:00
|
|
|
assert_equal Author, ActiveRecord::Base.send(:compute_type, "Author")
|
2015-10-30 23:30:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_compute_type_nonexistent_constant
|
|
|
|
e = assert_raises NameError do
|
2016-08-06 16:26:20 +00:00
|
|
|
ActiveRecord::Base.send :compute_type, "NonexistentModel"
|
2015-10-30 23:30:18 +00:00
|
|
|
end
|
2016-08-06 16:26:20 +00:00
|
|
|
assert_equal "uninitialized constant ActiveRecord::Base::NonexistentModel", e.message
|
|
|
|
assert_equal "ActiveRecord::Base::NonexistentModel", e.name
|
2015-10-30 23:30:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_compute_type_no_method_error
|
|
|
|
ActiveSupport::Dependencies.stub(:safe_constantize, proc{ raise NoMethodError }) do
|
|
|
|
assert_raises NoMethodError do
|
2016-08-06 16:26:20 +00:00
|
|
|
ActiveRecord::Base.send :compute_type, "InvalidModel"
|
2015-10-30 23:30:18 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_compute_type_on_undefined_method
|
|
|
|
error = nil
|
|
|
|
begin
|
|
|
|
Class.new(Author) do
|
|
|
|
alias_method :foo, :bar
|
|
|
|
end
|
|
|
|
rescue => e
|
|
|
|
error = e
|
|
|
|
end
|
|
|
|
|
|
|
|
ActiveSupport::Dependencies.stub(:safe_constantize, proc{ raise e }) do
|
|
|
|
|
|
|
|
exception = assert_raises NameError do
|
2016-08-06 16:26:20 +00:00
|
|
|
ActiveRecord::Base.send :compute_type, "InvalidModel"
|
2015-10-30 23:30:18 +00:00
|
|
|
end
|
|
|
|
assert_equal error.message, exception.message
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_compute_type_argument_error
|
|
|
|
ActiveSupport::Dependencies.stub(:safe_constantize, proc{ raise ArgumentError }) do
|
|
|
|
assert_raises ArgumentError do
|
2016-08-06 16:26:20 +00:00
|
|
|
ActiveRecord::Base.send :compute_type, "InvalidModel"
|
2015-10-30 23:30:18 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-04-11 15:35:09 +00:00
|
|
|
def test_should_store_demodulized_class_name_with_store_full_sti_class_option_disabled
|
2015-05-07 13:04:49 +00:00
|
|
|
without_store_full_sti_class do
|
|
|
|
item = Namespaced::Company.new
|
2016-08-06 16:26:20 +00:00
|
|
|
assert_equal "Company", item[:type]
|
2015-05-07 13:04:49 +00:00
|
|
|
end
|
2008-04-11 15:35:09 +00:00
|
|
|
end
|
2009-04-29 22:39:53 +00:00
|
|
|
|
2008-04-11 15:35:09 +00:00
|
|
|
def test_should_store_full_class_name_with_store_full_sti_class_option_enabled
|
2015-05-07 13:04:49 +00:00
|
|
|
with_store_full_sti_class do
|
|
|
|
item = Namespaced::Company.new
|
2016-08-06 16:26:20 +00:00
|
|
|
assert_equal "Namespaced::Company", item[:type]
|
2015-05-07 13:04:49 +00:00
|
|
|
end
|
2008-04-11 15:35:09 +00:00
|
|
|
end
|
2009-04-29 22:39:53 +00:00
|
|
|
|
2008-04-11 15:35:09 +00:00
|
|
|
def test_different_namespace_subclass_should_load_correctly_with_store_full_sti_class_option
|
2015-05-07 13:04:49 +00:00
|
|
|
with_store_full_sti_class do
|
|
|
|
item = Namespaced::Company.create name: "Wolverine 2"
|
|
|
|
assert_not_nil Company.find(item.id)
|
|
|
|
assert_not_nil Namespaced::Company.find(item.id)
|
|
|
|
end
|
2008-04-11 15:35:09 +00:00
|
|
|
end
|
2004-11-24 01:04:44 +00:00
|
|
|
|
2015-10-30 23:30:18 +00:00
|
|
|
def test_descends_from_active_record
|
|
|
|
assert !ActiveRecord::Base.descends_from_active_record?
|
|
|
|
|
|
|
|
# Abstract subclass of AR::Base.
|
|
|
|
assert LoosePerson.descends_from_active_record?
|
|
|
|
|
|
|
|
# Concrete subclass of an abstract class.
|
|
|
|
assert LooseDescendant.descends_from_active_record?
|
|
|
|
|
|
|
|
# Concrete subclass of AR::Base.
|
|
|
|
assert TightPerson.descends_from_active_record?
|
|
|
|
|
|
|
|
# Concrete subclass of a concrete class but has no type column.
|
|
|
|
assert TightDescendant.descends_from_active_record?
|
|
|
|
|
|
|
|
# Concrete subclass of AR::Base.
|
|
|
|
assert Post.descends_from_active_record?
|
|
|
|
|
|
|
|
# Abstract subclass of a concrete class which has a type column.
|
|
|
|
# This is pathological, as you'll never have Sub < Abstract < Concrete.
|
|
|
|
assert !StiPost.descends_from_active_record?
|
|
|
|
|
|
|
|
# Concrete subclasses an abstract class which has a type column.
|
|
|
|
assert !SubStiPost.descends_from_active_record?
|
|
|
|
end
|
|
|
|
|
2007-01-23 04:19:16 +00:00
|
|
|
def test_company_descends_from_active_record
|
2013-03-11 11:27:13 +00:00
|
|
|
assert !ActiveRecord::Base.descends_from_active_record?
|
2016-08-06 16:26:20 +00:00
|
|
|
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"
|
2007-01-23 04:19:16 +00:00
|
|
|
end
|
|
|
|
|
2015-10-30 23:30:18 +00:00
|
|
|
def test_abstract_class
|
|
|
|
assert !ActiveRecord::Base.abstract_class?
|
|
|
|
assert LoosePerson.abstract_class?
|
|
|
|
assert !LooseDescendant.abstract_class?
|
|
|
|
end
|
|
|
|
|
2012-07-26 17:46:49 +00:00
|
|
|
def test_inheritance_base_class
|
|
|
|
assert_equal Post, Post.base_class
|
|
|
|
assert_equal Post, SpecialPost.base_class
|
|
|
|
assert_equal Post, StiPost.base_class
|
|
|
|
assert_equal SubStiPost, SubStiPost.base_class
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_abstract_inheritance_base_class
|
|
|
|
assert_equal LoosePerson, LoosePerson.base_class
|
|
|
|
assert_equal LooseDescendant, LooseDescendant.base_class
|
|
|
|
assert_equal TightPerson, TightPerson.base_class
|
|
|
|
assert_equal TightPerson, TightDescendant.base_class
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_base_class_activerecord_error
|
2012-10-26 14:51:02 +00:00
|
|
|
klass = Class.new { include ActiveRecord::Inheritance }
|
2012-07-26 17:46:49 +00:00
|
|
|
assert_raise(ActiveRecord::ActiveRecordError) { klass.base_class }
|
|
|
|
end
|
|
|
|
|
2004-12-14 12:32:29 +00:00
|
|
|
def test_a_bad_type_column
|
2005-11-16 08:16:54 +00:00
|
|
|
Company.connection.insert "INSERT INTO companies (id, #{QUOTED_TYPE}, name) VALUES(100, 'bad_class!', 'Not happening')"
|
2005-10-27 08:18:41 +00:00
|
|
|
|
2009-03-08 20:11:58 +00:00
|
|
|
assert_raise(ActiveRecord::SubclassNotFound) { Company.find(100) }
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_inheritance_find
|
2010-05-19 19:14:51 +00:00
|
|
|
assert_kind_of Firm, Company.find(1), "37signals should be a firm"
|
|
|
|
assert_kind_of Firm, Firm.find(1), "37signals should be a firm"
|
|
|
|
assert_kind_of Client, Company.find(2), "Summit should be a client"
|
|
|
|
assert_kind_of Client, Client.find(2), "Summit should be a client"
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
2008-01-18 07:30:42 +00:00
|
|
|
|
2004-11-24 01:04:44 +00:00
|
|
|
def test_alt_inheritance_find
|
2012-09-03 18:30:43 +00:00
|
|
|
assert_kind_of Cucumber, Vegetable.find(1)
|
|
|
|
assert_kind_of Cucumber, Cucumber.find(1)
|
|
|
|
assert_kind_of Cabbage, Vegetable.find(2)
|
|
|
|
assert_kind_of Cabbage, Cabbage.find(2)
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
|
|
|
|
2012-09-02 15:19:50 +00:00
|
|
|
def test_alt_becomes_works_with_sti
|
|
|
|
vegetable = Vegetable.find(1)
|
|
|
|
assert_kind_of Vegetable, vegetable
|
|
|
|
cabbage = vegetable.becomes(Cabbage)
|
|
|
|
assert_kind_of Cabbage, cabbage
|
|
|
|
end
|
|
|
|
|
2014-10-02 08:02:30 +00:00
|
|
|
def test_becomes_and_change_tracking_for_inheritance_columns
|
|
|
|
cucumber = Vegetable.find(1)
|
|
|
|
cabbage = cucumber.becomes!(Cabbage)
|
2016-08-06 16:26:20 +00:00
|
|
|
assert_equal ["Cucumber", "Cabbage"], cabbage.custom_type_change
|
2014-10-02 08:02:30 +00:00
|
|
|
end
|
|
|
|
|
2014-01-13 14:04:23 +00:00
|
|
|
def test_alt_becomes_bang_resets_inheritance_type_column
|
|
|
|
vegetable = Vegetable.create!(name: "Red Pepper")
|
|
|
|
assert_nil vegetable.custom_type
|
|
|
|
|
|
|
|
cabbage = vegetable.becomes!(Cabbage)
|
|
|
|
assert_equal "Cabbage", cabbage.custom_type
|
|
|
|
|
|
|
|
vegetable = cabbage.becomes!(Vegetable)
|
|
|
|
assert_nil cabbage.custom_type
|
|
|
|
end
|
|
|
|
|
2004-11-24 01:04:44 +00:00
|
|
|
def test_inheritance_find_all
|
2016-08-06 16:26:20 +00:00
|
|
|
companies = Company.all.merge!(:order => "id").to_a
|
2010-05-19 19:14:51 +00:00
|
|
|
assert_kind_of Firm, companies[0], "37signals should be a firm"
|
|
|
|
assert_kind_of Client, companies[1], "Summit should be a client"
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
2008-01-18 07:30:42 +00:00
|
|
|
|
2004-11-24 01:04:44 +00:00
|
|
|
def test_alt_inheritance_find_all
|
2016-08-06 16:26:20 +00:00
|
|
|
companies = Vegetable.all.merge!(:order => "id").to_a
|
2012-09-03 18:30:43 +00:00
|
|
|
assert_kind_of Cucumber, companies[0]
|
|
|
|
assert_kind_of Cabbage, companies[1]
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_inheritance_save
|
|
|
|
firm = Firm.new
|
|
|
|
firm.name = "Next Angle"
|
|
|
|
firm.save
|
2008-01-18 07:30:42 +00:00
|
|
|
|
2004-11-24 01:04:44 +00:00
|
|
|
next_angle = Company.find(firm.id)
|
2010-05-19 19:14:51 +00:00
|
|
|
assert_kind_of Firm, next_angle, "Next Angle should be a firm"
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
2008-01-18 07:30:42 +00:00
|
|
|
|
2004-11-24 01:04:44 +00:00
|
|
|
def test_alt_inheritance_save
|
2016-08-06 16:26:20 +00:00
|
|
|
cabbage = Cabbage.new(:name => "Savoy")
|
2012-09-03 18:30:43 +00:00
|
|
|
cabbage.save!
|
|
|
|
|
|
|
|
savoy = Vegetable.find(cabbage.id)
|
|
|
|
assert_kind_of Cabbage, savoy
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
|
|
|
|
2012-04-05 19:21:48 +00:00
|
|
|
def test_inheritance_new_with_default_class
|
|
|
|
company = Company.new
|
2012-11-29 11:24:05 +00:00
|
|
|
assert_equal Company, company.class
|
2012-04-05 19:21:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_inheritance_new_with_base_class
|
2016-08-06 16:26:20 +00:00
|
|
|
company = Company.new(:type => "Company")
|
2012-11-29 11:24:05 +00:00
|
|
|
assert_equal Company, company.class
|
2012-04-05 19:21:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_inheritance_new_with_subclass
|
2016-08-06 16:26:20 +00:00
|
|
|
firm = Company.new(:type => "Firm")
|
2012-11-29 11:24:05 +00:00
|
|
|
assert_equal Firm, firm.class
|
2012-04-05 19:21:48 +00:00
|
|
|
end
|
|
|
|
|
2013-03-18 06:45:39 +00:00
|
|
|
def test_new_with_abstract_class
|
|
|
|
e = assert_raises(NotImplementedError) do
|
|
|
|
AbstractCompany.new
|
|
|
|
end
|
2014-01-03 22:02:31 +00:00
|
|
|
assert_equal("AbstractCompany is an abstract class and cannot be instantiated.", e.message)
|
2013-03-18 06:45:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_new_with_ar_base
|
|
|
|
e = assert_raises(NotImplementedError) do
|
|
|
|
ActiveRecord::Base.new
|
|
|
|
end
|
2014-01-03 22:02:31 +00:00
|
|
|
assert_equal("ActiveRecord::Base is an abstract class and cannot be instantiated.", e.message)
|
2013-03-18 06:45:39 +00:00
|
|
|
end
|
|
|
|
|
2012-04-05 19:21:48 +00:00
|
|
|
def test_new_with_invalid_type
|
2016-08-06 16:26:20 +00:00
|
|
|
assert_raise(ActiveRecord::SubclassNotFound) { Company.new(:type => "InvalidType") }
|
2012-04-05 19:21:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_new_with_unrelated_type
|
2016-08-06 16:26:20 +00:00
|
|
|
assert_raise(ActiveRecord::SubclassNotFound) { Company.new(:type => "Account") }
|
2012-04-05 19:21:48 +00:00
|
|
|
end
|
|
|
|
|
2015-05-13 09:35:34 +00:00
|
|
|
def test_new_with_unrelated_namespaced_type
|
|
|
|
without_store_full_sti_class do
|
|
|
|
e = assert_raises ActiveRecord::SubclassNotFound do
|
2016-08-06 16:26:20 +00:00
|
|
|
Namespaced::Company.new(type: "Firm")
|
2015-05-13 09:35:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal "Invalid single-table inheritance type: Namespaced::Firm is not a subclass of Namespaced::Company", e.message
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-04-01 23:19:11 +00:00
|
|
|
def test_new_with_complex_inheritance
|
2016-08-06 16:26:20 +00:00
|
|
|
assert_nothing_raised { Client.new(type: "VerySpecialClient") }
|
2013-04-01 23:19:11 +00:00
|
|
|
end
|
|
|
|
|
2015-03-26 17:26:19 +00:00
|
|
|
def test_new_without_storing_full_sti_class
|
|
|
|
without_store_full_sti_class do
|
2016-08-06 16:26:20 +00:00
|
|
|
item = Company.new(type: "SpecialCo")
|
2015-03-26 17:26:19 +00:00
|
|
|
assert_instance_of Company::SpecialCo, item
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-28 22:32:38 +00:00
|
|
|
def test_new_with_autoload_paths
|
2016-08-06 16:26:20 +00:00
|
|
|
path = File.expand_path("../../models/autoloadable", __FILE__)
|
2013-02-28 22:32:38 +00:00
|
|
|
ActiveSupport::Dependencies.autoload_paths << path
|
|
|
|
|
2016-08-06 16:26:20 +00:00
|
|
|
firm = Company.new(:type => "ExtraFirm")
|
2013-02-28 22:32:38 +00:00
|
|
|
assert_equal ExtraFirm, firm.class
|
|
|
|
ensure
|
|
|
|
ActiveSupport::Dependencies.autoload_paths.reject! { |p| p == path }
|
|
|
|
ActiveSupport::Dependencies.clear
|
|
|
|
end
|
|
|
|
|
2004-11-24 01:04:44 +00:00
|
|
|
def test_inheritance_condition
|
Ensure AR #second, #third, etc. finders work through associations
This commit fixes two regressions introduced in cafe31a078 where
newly created finder methods #second, #third, #forth, and #fifth
caused a NoMethodError error on reload associations and where we
were pulling the wrong element out of cached associations.
Examples:
some_book.authors.reload.second
# Before
# => NoMethodError: undefined method 'first' for nil:NilClass
# After
# => #<Author id: 2, name: "Sally Second", ...>
some_book.first.authors.first
some_book.first.authors.second
# Before
# => #<Author id: 1, name: "Freddy First", ...>
# => #<Author id: 1, name: "Freddy First", ...>
# After
# => #<Author id: 1, name: "Freddy First", ...>
# => #<Author id: 2, name: "Sally Second", ...>
Fixes #13783.
2014-01-21 22:34:39 +00:00
|
|
|
assert_equal 11, Company.count
|
2005-06-26 11:25:32 +00:00
|
|
|
assert_equal 2, Firm.count
|
Ensure AR #second, #third, etc. finders work through associations
This commit fixes two regressions introduced in cafe31a078 where
newly created finder methods #second, #third, #forth, and #fifth
caused a NoMethodError error on reload associations and where we
were pulling the wrong element out of cached associations.
Examples:
some_book.authors.reload.second
# Before
# => NoMethodError: undefined method 'first' for nil:NilClass
# After
# => #<Author id: 2, name: "Sally Second", ...>
some_book.first.authors.first
some_book.first.authors.second
# Before
# => #<Author id: 1, name: "Freddy First", ...>
# => #<Author id: 1, name: "Freddy First", ...>
# After
# => #<Author id: 1, name: "Freddy First", ...>
# => #<Author id: 2, name: "Sally Second", ...>
Fixes #13783.
2014-01-21 22:34:39 +00:00
|
|
|
assert_equal 5, Client.count
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
2008-01-18 07:30:42 +00:00
|
|
|
|
2004-11-24 01:04:44 +00:00
|
|
|
def test_alt_inheritance_condition
|
2012-09-03 18:30:43 +00:00
|
|
|
assert_equal 4, Vegetable.count
|
|
|
|
assert_equal 1, Cucumber.count
|
|
|
|
assert_equal 3, Cabbage.count
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_finding_incorrect_type_data
|
2009-03-08 20:11:58 +00:00
|
|
|
assert_raise(ActiveRecord::RecordNotFound) { Firm.find(2) }
|
2004-11-24 01:04:44 +00:00
|
|
|
assert_nothing_raised { Firm.find(1) }
|
|
|
|
end
|
2008-01-18 07:30:42 +00:00
|
|
|
|
2004-11-24 01:04:44 +00:00
|
|
|
def test_alt_finding_incorrect_type_data
|
2012-09-03 18:30:43 +00:00
|
|
|
assert_raise(ActiveRecord::RecordNotFound) { Cucumber.find(2) }
|
|
|
|
assert_nothing_raised { Cucumber.find(1) }
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_update_all_within_inheritance
|
|
|
|
Client.update_all "name = 'I am a client'"
|
2012-08-03 10:51:52 +00:00
|
|
|
assert_equal "I am a client", Client.first.name
|
2009-03-22 22:14:42 +00:00
|
|
|
# Order by added as otherwise Oracle tests were failing because of different order of results
|
2012-07-27 16:27:47 +00:00
|
|
|
assert_equal "37signals", Firm.all.merge!(:order => "id").to_a.first.name
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
2008-01-18 07:30:42 +00:00
|
|
|
|
2004-11-24 01:04:44 +00:00
|
|
|
def test_alt_update_all_within_inheritance
|
2012-09-03 18:30:43 +00:00
|
|
|
Cabbage.update_all "name = 'the cabbage'"
|
|
|
|
assert_equal "the cabbage", Cabbage.first.name
|
|
|
|
assert_equal ["my cucumber"], Cucumber.all.map(&:name).uniq
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_destroy_all_within_inheritance
|
|
|
|
Client.destroy_all
|
2005-06-26 11:25:32 +00:00
|
|
|
assert_equal 0, Client.count
|
|
|
|
assert_equal 2, Firm.count
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
2008-01-18 07:30:42 +00:00
|
|
|
|
2004-11-24 01:04:44 +00:00
|
|
|
def test_alt_destroy_all_within_inheritance
|
2012-09-03 18:30:43 +00:00
|
|
|
Cabbage.destroy_all
|
|
|
|
assert_equal 0, Cabbage.count
|
|
|
|
assert_equal 1, Cucumber.count
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_find_first_within_inheritance
|
2012-07-27 16:27:47 +00:00
|
|
|
assert_kind_of Firm, Company.all.merge!(:where => "name = '37signals'").first
|
|
|
|
assert_kind_of Firm, Firm.all.merge!(:where => "name = '37signals'").first
|
|
|
|
assert_nil Client.all.merge!(:where => "name = '37signals'").first
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
2008-01-18 07:30:42 +00:00
|
|
|
|
2004-11-24 01:04:44 +00:00
|
|
|
def test_alt_find_first_within_inheritance
|
2012-09-03 18:30:43 +00:00
|
|
|
assert_kind_of Cabbage, Vegetable.all.merge!(:where => "name = 'his cabbage'").first
|
|
|
|
assert_kind_of Cabbage, Cabbage.all.merge!(:where => "name = 'his cabbage'").first
|
|
|
|
assert_nil Cucumber.all.merge!(:where => "name = 'his cabbage'").first
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_complex_inheritance
|
|
|
|
very_special_client = VerySpecialClient.create("name" => "veryspecial")
|
2010-06-29 15:18:55 +00:00
|
|
|
assert_equal very_special_client, VerySpecialClient.where("name = 'veryspecial'").first
|
2012-07-27 16:27:47 +00:00
|
|
|
assert_equal very_special_client, SpecialClient.all.merge!(:where => "name = 'veryspecial'").first
|
|
|
|
assert_equal very_special_client, Company.all.merge!(:where => "name = 'veryspecial'").first
|
|
|
|
assert_equal very_special_client, Client.all.merge!(:where => "name = 'veryspecial'").first
|
|
|
|
assert_equal 1, Client.all.merge!(:where => "name = 'Summit'").to_a.size
|
2004-11-24 01:04:44 +00:00
|
|
|
assert_equal very_special_client, Client.find(very_special_client.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_alt_complex_inheritance
|
2012-09-03 18:30:43 +00:00
|
|
|
king_cole = KingCole.create("name" => "uniform heads")
|
|
|
|
assert_equal king_cole, KingCole.where("name = 'uniform heads'").first
|
|
|
|
assert_equal king_cole, GreenCabbage.all.merge!(:where => "name = 'uniform heads'").first
|
|
|
|
assert_equal king_cole, Cabbage.all.merge!(:where => "name = 'uniform heads'").first
|
|
|
|
assert_equal king_cole, Vegetable.all.merge!(:where => "name = 'uniform heads'").first
|
|
|
|
assert_equal 1, Cabbage.all.merge!(:where => "name = 'his cabbage'").to_a.size
|
|
|
|
assert_equal king_cole, Cabbage.find(king_cole.id)
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
2008-01-18 07:30:42 +00:00
|
|
|
|
2006-12-19 03:10:39 +00:00
|
|
|
def test_eager_load_belongs_to_something_inherited
|
2012-07-27 16:27:47 +00:00
|
|
|
account = Account.all.merge!(:includes => :firm).find(1)
|
2015-01-26 18:35:28 +00:00
|
|
|
assert account.association(:firm).loaded?, "association was not eager loaded"
|
2006-12-19 03:10:39 +00:00
|
|
|
end
|
2008-01-18 07:30:42 +00:00
|
|
|
|
2012-09-03 18:30:43 +00:00
|
|
|
def test_alt_eager_loading
|
|
|
|
cabbage = RedCabbage.all.merge!(:includes => :seller).find(4)
|
2015-01-26 18:35:28 +00:00
|
|
|
assert cabbage.association(:seller).loaded?, "association was not eager loaded"
|
2012-09-03 18:30:43 +00:00
|
|
|
end
|
|
|
|
|
2008-07-11 03:18:41 +00:00
|
|
|
def test_eager_load_belongs_to_primary_key_quoting
|
|
|
|
con = Account.connection
|
2014-12-26 23:10:47 +00:00
|
|
|
assert_sql(/#{con.quote_table_name('companies')}.#{con.quote_column_name('id')} = 1/) do
|
2012-07-27 16:27:47 +00:00
|
|
|
Account.all.merge!(:includes => :firm).find(1)
|
2008-07-11 03:18:41 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-12-25 05:15:41 +00:00
|
|
|
def test_inherits_custom_primary_key
|
|
|
|
assert_equal Subscriber.primary_key, SpecialSubscriber.primary_key
|
|
|
|
end
|
|
|
|
|
2004-12-14 12:32:29 +00:00
|
|
|
def test_inheritance_without_mapping
|
2005-07-03 08:52:59 +00:00
|
|
|
assert_kind_of SpecialSubscriber, SpecialSubscriber.find("webster132")
|
2016-08-06 16:26:20 +00:00
|
|
|
assert_nothing_raised { s = SpecialSubscriber.new("name" => "And breaaaaathe!"); s.id = "roger"; s.save }
|
2004-12-14 12:32:29 +00:00
|
|
|
end
|
2006-12-19 19:23:56 +00:00
|
|
|
|
2013-10-03 18:05:23 +00:00
|
|
|
def test_scope_inherited_properly
|
2015-10-27 03:12:03 +00:00
|
|
|
assert_nothing_raised { Company.of_first_firm }
|
|
|
|
assert_nothing_raised { Client.of_first_firm }
|
2013-10-03 18:05:23 +00:00
|
|
|
end
|
|
|
|
end
|
2006-12-19 19:23:56 +00:00
|
|
|
|
2008-01-21 17:20:51 +00:00
|
|
|
class InheritanceComputeTypeTest < ActiveRecord::TestCase
|
2015-05-07 13:04:49 +00:00
|
|
|
include InheritanceTestHelper
|
2006-12-19 19:23:56 +00:00
|
|
|
fixtures :companies
|
|
|
|
|
2014-03-14 04:35:58 +00:00
|
|
|
teardown do
|
2006-12-19 19:23:56 +00:00
|
|
|
self.class.const_remove :FirmOnTheFly rescue nil
|
|
|
|
Firm.const_remove :FirmOnTheFly rescue nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_instantiation_doesnt_try_to_require_corresponding_file
|
2015-05-07 13:04:49 +00:00
|
|
|
without_store_full_sti_class do
|
|
|
|
foo = Firm.first.clone
|
2016-08-06 16:26:20 +00:00
|
|
|
foo.type = "FirmOnTheFly"
|
2015-05-07 13:04:49 +00:00
|
|
|
foo.save!
|
2006-12-19 19:23:56 +00:00
|
|
|
|
2015-05-07 13:04:49 +00:00
|
|
|
# Should fail without FirmOnTheFly in the type condition.
|
|
|
|
assert_raise(ActiveRecord::RecordNotFound) { Firm.find(foo.id) }
|
2006-12-19 19:23:56 +00:00
|
|
|
|
2015-05-07 13:04:49 +00:00
|
|
|
# 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) }
|
2006-12-19 19:23:56 +00:00
|
|
|
|
2015-05-07 13:04:49 +00:00
|
|
|
# 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)
|
2006-12-19 19:23:56 +00:00
|
|
|
|
2015-05-07 13:04:49 +00:00
|
|
|
# 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) }
|
|
|
|
end
|
2006-12-19 19:23:56 +00:00
|
|
|
end
|
2014-01-14 13:23:45 +00:00
|
|
|
|
|
|
|
def test_sti_type_from_attributes_disabled_in_non_sti_class
|
2016-08-06 16:26:20 +00:00
|
|
|
phone = Shop::Product::Type.new(name: "Phone")
|
2014-01-14 13:23:45 +00:00
|
|
|
product = Shop::Product.new(:type => phone)
|
|
|
|
assert product.save
|
|
|
|
end
|
2015-12-02 10:38:19 +00:00
|
|
|
|
|
|
|
def test_inheritance_new_with_subclass_as_default
|
|
|
|
original_type = Company.columns_hash["type"].default
|
2016-08-06 16:26:20 +00:00
|
|
|
ActiveRecord::Base.connection.change_column_default :companies, :type, "Firm"
|
2015-12-02 10:38:19 +00:00
|
|
|
Company.reset_column_information
|
2015-12-02 12:31:20 +00:00
|
|
|
|
|
|
|
firm = Company.new # without arguments
|
2016-08-06 16:26:20 +00:00
|
|
|
assert_equal "Firm", firm.type
|
2015-12-02 10:38:19 +00:00
|
|
|
assert_instance_of Firm, firm
|
2015-12-02 12:31:20 +00:00
|
|
|
|
2016-08-06 16:26:20 +00:00
|
|
|
firm = Company.new(firm_name: "Shri Hans Plastic") # with arguments
|
|
|
|
assert_equal "Firm", firm.type
|
2015-12-02 10:38:19 +00:00
|
|
|
assert_instance_of Firm, firm
|
2015-12-02 12:31:20 +00:00
|
|
|
|
2016-01-27 18:26:20 +00:00
|
|
|
client = Client.new
|
2016-08-06 16:26:20 +00:00
|
|
|
assert_equal "Client", client.type
|
2016-01-27 18:26:20 +00:00
|
|
|
assert_instance_of Client, client
|
|
|
|
|
2016-08-06 16:26:20 +00:00
|
|
|
firm = Company.new(type: "Client") # overwrite the default type
|
|
|
|
assert_equal "Client", firm.type
|
2015-12-02 10:38:19 +00:00
|
|
|
assert_instance_of Client, firm
|
|
|
|
ensure
|
|
|
|
ActiveRecord::Base.connection.change_column_default :companies, :type, original_type
|
|
|
|
Company.reset_column_information
|
|
|
|
end
|
2006-12-19 19:23:56 +00:00
|
|
|
end
|
2015-12-02 16:02:11 +00:00
|
|
|
|
|
|
|
class InheritanceAttributeTest < ActiveRecord::TestCase
|
|
|
|
|
|
|
|
class Company < ActiveRecord::Base
|
2016-08-06 16:26:20 +00:00
|
|
|
self.table_name = "companies"
|
2015-12-02 16:02:11 +00:00
|
|
|
attribute :type, :string, default: "InheritanceAttributeTest::Startup"
|
|
|
|
end
|
|
|
|
|
|
|
|
class Startup < Company
|
|
|
|
end
|
|
|
|
|
|
|
|
class Empire < Company
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_inheritance_new_with_subclass_as_default
|
|
|
|
startup = Company.new # without arguments
|
2016-08-06 16:26:20 +00:00
|
|
|
assert_equal "InheritanceAttributeTest::Startup", startup.type
|
2015-12-02 16:02:11 +00:00
|
|
|
assert_instance_of Startup, startup
|
|
|
|
|
2016-08-06 16:26:20 +00:00
|
|
|
empire = Company.new(type: "InheritanceAttributeTest::Empire") # without arguments
|
|
|
|
assert_equal "InheritanceAttributeTest::Empire", empire.type
|
2015-12-02 16:02:11 +00:00
|
|
|
assert_instance_of Empire, empire
|
|
|
|
end
|
|
|
|
end
|
2016-01-19 16:27:21 +00:00
|
|
|
|
|
|
|
class InheritanceAttributeMappingTest < ActiveRecord::TestCase
|
|
|
|
setup do
|
|
|
|
@old_registry = ActiveRecord::Type.registry
|
|
|
|
ActiveRecord::Type.registry = ActiveRecord::Type::AdapterSpecificRegistry.new
|
|
|
|
ActiveRecord::Type.register :omg_sti, InheritanceAttributeMappingTest::OmgStiType
|
|
|
|
Company.delete_all
|
|
|
|
Sponsor.delete_all
|
|
|
|
end
|
|
|
|
|
|
|
|
teardown do
|
|
|
|
ActiveRecord::Type.registry = @old_registry
|
|
|
|
end
|
|
|
|
|
|
|
|
class OmgStiType < ActiveRecord::Type::String
|
|
|
|
def cast_value(value)
|
|
|
|
if value =~ /\Aomg_(.+)\z/
|
|
|
|
$1.classify
|
|
|
|
else
|
|
|
|
value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def serialize(value)
|
|
|
|
if value
|
|
|
|
"omg_%s" % value.underscore
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Company < ActiveRecord::Base
|
2016-08-06 16:26:20 +00:00
|
|
|
self.table_name = "companies"
|
2016-01-19 16:27:21 +00:00
|
|
|
attribute :type, :omg_sti
|
|
|
|
end
|
|
|
|
|
|
|
|
class Startup < Company; end
|
|
|
|
class Empire < Company; end
|
|
|
|
|
|
|
|
class Sponsor < ActiveRecord::Base
|
2016-08-06 16:26:20 +00:00
|
|
|
self.table_name = "sponsors"
|
2016-01-19 16:27:21 +00:00
|
|
|
attribute :sponsorable_type, :omg_sti
|
|
|
|
|
|
|
|
belongs_to :sponsorable, polymorphic: true
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_sti_with_custom_type
|
2016-08-06 16:26:20 +00:00
|
|
|
Startup.create! name: "a Startup"
|
|
|
|
Empire.create! name: "an Empire"
|
2016-01-19 16:27:21 +00:00
|
|
|
|
|
|
|
assert_equal [["a Startup", "omg_inheritance_attribute_mapping_test/startup"],
|
2016-08-06 16:26:20 +00:00
|
|
|
["an Empire", "omg_inheritance_attribute_mapping_test/empire"]], ActiveRecord::Base.connection.select_rows("SELECT name, type FROM companies").sort
|
2016-01-19 16:27:21 +00:00
|
|
|
assert_equal [["a Startup", "InheritanceAttributeMappingTest::Startup"],
|
2016-01-19 17:15:25 +00:00
|
|
|
["an Empire", "InheritanceAttributeMappingTest::Empire"]], Company.all.map { |a| [a.name, a.type] }.sort
|
2016-01-19 16:27:21 +00:00
|
|
|
|
|
|
|
startup = Startup.first
|
|
|
|
startup.becomes! Empire
|
|
|
|
startup.save!
|
|
|
|
|
|
|
|
assert_equal [["a Startup", "omg_inheritance_attribute_mapping_test/empire"],
|
2016-08-06 16:26:20 +00:00
|
|
|
["an Empire", "omg_inheritance_attribute_mapping_test/empire"]], ActiveRecord::Base.connection.select_rows("SELECT name, type FROM companies").sort
|
2016-01-19 17:15:25 +00:00
|
|
|
|
2016-01-19 16:27:21 +00:00
|
|
|
assert_equal [["a Startup", "InheritanceAttributeMappingTest::Empire"],
|
2016-01-19 17:15:25 +00:00
|
|
|
["an Empire", "InheritanceAttributeMappingTest::Empire"]], Company.all.map { |a| [a.name, a.type] }.sort
|
2016-01-19 16:27:21 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_polymorphic_associations_custom_type
|
2016-08-06 16:26:20 +00:00
|
|
|
startup = Startup.create! name: "a Startup"
|
2016-01-19 16:27:21 +00:00
|
|
|
sponsor = Sponsor.create! sponsorable: startup
|
|
|
|
|
2016-08-06 16:26:20 +00:00
|
|
|
assert_equal ["omg_inheritance_attribute_mapping_test/company"], ActiveRecord::Base.connection.select_values("SELECT sponsorable_type FROM sponsors")
|
2016-01-19 16:27:21 +00:00
|
|
|
|
|
|
|
sponsor = Sponsor.first
|
|
|
|
assert_equal startup, sponsor.sponsorable
|
|
|
|
end
|
|
|
|
end
|