class InheritanceTest

Public Instance Methods

test_a_bad_type_column() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 195
def test_a_bad_type_column
  Company.connection.insert "INSERT INTO companies (id, #{QUOTED_TYPE}, name) VALUES(100, 'bad_class!', 'Not happening')"

  assert_raise(ActiveRecord::SubclassNotFound) { Company.find(100) }
end
test_abstract_class() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 169
def test_abstract_class
  assert !ActiveRecord::Base.abstract_class?
  assert LoosePerson.abstract_class?
  assert !LooseDescendant.abstract_class?
end
test_abstract_inheritance_base_class() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 183
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
test_alt_becomes_bang_resets_inheritance_type_column() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 228
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
test_alt_becomes_works_with_sti() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 215
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
test_alt_complex_inheritance() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 406
def test_alt_complex_inheritance
  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)
end
test_alt_destroy_all_within_inheritance() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 378
def test_alt_destroy_all_within_inheritance
  Cabbage.destroy_all
  assert_equal 0, Cabbage.count
  assert_equal 1, Cucumber.count
end
test_alt_eager_loading() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 421
def test_alt_eager_loading
  cabbage = RedCabbage.all.merge!(includes: :seller).find(4)
  assert cabbage.association(:seller).loaded?, "association was not eager loaded"
end
test_alt_find_first_within_inheritance() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 390
def test_alt_find_first_within_inheritance
  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
end
test_alt_finding_incorrect_type_data() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 354
def test_alt_finding_incorrect_type_data
  assert_raise(ActiveRecord::RecordNotFound) { Cucumber.find(2) }
  assert_nothing_raised   { Cucumber.find(1) }
end
test_alt_inheritance_condition() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 343
def test_alt_inheritance_condition
  assert_equal 4, Vegetable.count
  assert_equal 1, Cucumber.count
  assert_equal 3, Cabbage.count
end
test_alt_inheritance_find() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 208
def test_alt_inheritance_find
  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)
end
test_alt_inheritance_find_all() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 245
def test_alt_inheritance_find_all
  companies = Vegetable.all.merge!(order: "id").to_a
  assert_kind_of Cucumber, companies[0]
  assert_kind_of Cabbage, companies[1]
end
test_alt_inheritance_save() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 260
def test_alt_inheritance_save
  cabbage = Cabbage.new(name: "Savoy")
  cabbage.save!

  savoy = Vegetable.find(cabbage.id)
  assert_kind_of Cabbage, savoy
end
test_alt_update_all_within_inheritance() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 366
def test_alt_update_all_within_inheritance
  Cabbage.update_all "name = 'the cabbage'"
  assert_equal "the cabbage", Cabbage.first.name
  assert_equal ["my cucumber"], Cucumber.all.map(&:name).uniq
end
test_base_class_activerecord_error() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 190
def test_base_class_activerecord_error
  klass = Class.new { include ActiveRecord::Inheritance }
  assert_raise(ActiveRecord::ActiveRecordError) { klass.base_class }
end
test_becomes_and_change_tracking_for_inheritance_columns() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 222
def test_becomes_and_change_tracking_for_inheritance_columns
  cucumber = Vegetable.find(1)
  cabbage = cucumber.becomes!(Cabbage)
  assert_equal ["Cucumber", "Cabbage"], cabbage.custom_type_change
end
test_class_with_blank_sti_name() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 43
def test_class_with_blank_sti_name
  company = Company.first
  company = company.dup
  company.extend(Module.new {
    def _read_attribute(name)
      return "  " if name == "type"
      super
    end
  })
  company.save!
  company = Company.all.to_a.find { |x| x.id == company.id }
  assert_equal "  ", company.type
end
test_class_with_store_full_sti_class_returns_full_name() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 37
def test_class_with_store_full_sti_class_returns_full_name
  with_store_full_sti_class do
    assert_equal "Namespaced::Company", Namespaced::Company.sti_name
  end
end
test_class_without_store_full_sti_class_returns_demodulized_name() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 57
def test_class_without_store_full_sti_class_returns_demodulized_name
  without_store_full_sti_class do
    assert_equal "Company", Namespaced::Company.sti_name
  end
end
test_company_descends_from_active_record() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 162
def test_company_descends_from_active_record
  assert !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
test_complex_inheritance() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 396
def test_complex_inheritance
  very_special_client = VerySpecialClient.create("name" => "veryspecial")
  assert_equal very_special_client, VerySpecialClient.where("name = 'veryspecial'").first
  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
  assert_equal very_special_client, Client.find(very_special_client.id)
end
test_compute_type_argument_error() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 102
def test_compute_type_argument_error
  ActiveSupport::Dependencies.stub(:safe_constantize, proc { raise ArgumentError }) do
    assert_raises ArgumentError do
      Company.send :compute_type, "InvalidModel"
    end
  end
end
test_compute_type_no_method_error() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 75
def test_compute_type_no_method_error
  ActiveSupport::Dependencies.stub(:safe_constantize, proc { raise NoMethodError }) do
    assert_raises NoMethodError do
      Company.send :compute_type, "InvalidModel"
    end
  end
end
test_compute_type_nonexistent_constant() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 67
def test_compute_type_nonexistent_constant
  e = assert_raises NameError do
    Company.send :compute_type, "NonexistentModel"
  end
  assert_equal "uninitialized constant Company::NonexistentModel", e.message
  assert_equal "Company::NonexistentModel", e.name
end
test_compute_type_on_undefined_method() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 83
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
      Company.send :compute_type, "InvalidModel"
    end
    assert_equal error.message, exception.message
  end
end
test_compute_type_success() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 63
def test_compute_type_success
  assert_equal Author, Company.send(:compute_type, "Author")
end
test_descends_from_active_record() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 132
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?

  # Concrete subclasses of a concrete class which has a type column.
  assert !StiPost.descends_from_active_record?
  assert !SubStiPost.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 !AbstractStiPost.descends_from_active_record?

  # Concrete subclass of an abstract class which has a type column.
  assert !SubAbstractStiPost.descends_from_active_record?
end
test_destroy_all_within_inheritance() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 372
def test_destroy_all_within_inheritance
  Client.destroy_all
  assert_equal 0, Client.count
  assert_equal 2, Firm.count
end
test_different_namespace_subclass_should_load_correctly_with_store_full_sti_class_option() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 124
def test_different_namespace_subclass_should_load_correctly_with_store_full_sti_class_option
  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
end
test_eager_load_belongs_to_primary_key_quoting() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 426
def test_eager_load_belongs_to_primary_key_quoting
  con = Account.connection
  bind_param = Arel::Nodes::BindParam.new(nil)
  assert_sql(/#{con.quote_table_name('companies')}\.#{con.quote_column_name('id')} = (?:#{Regexp.quote(bind_param.to_sql)}|1)/) do
    Account.all.merge!(includes: :firm).find(1)
  end
end
test_eager_load_belongs_to_something_inherited() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 416
def test_eager_load_belongs_to_something_inherited
  account = Account.all.merge!(includes: :firm).find(1)
  assert account.association(:firm).loaded?, "association was not eager loaded"
end
test_find_first_within_inheritance() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 384
def test_find_first_within_inheritance
  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
end
test_finding_incorrect_type_data() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 349
def test_finding_incorrect_type_data
  assert_raise(ActiveRecord::RecordNotFound) { Firm.find(2) }
  assert_nothing_raised   { Firm.find(1) }
end
test_inheritance_base_class() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 175
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 Post, SubStiPost.base_class
  assert_equal SubAbstractStiPost, SubAbstractStiPost.base_class
end
test_inheritance_condition() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 337
def test_inheritance_condition
  assert_equal 11, Company.count
  assert_equal 2, Firm.count
  assert_equal 5, Client.count
end
test_inheritance_find() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 201
def test_inheritance_find
  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"
end
test_inheritance_find_all() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 239
def test_inheritance_find_all
  companies = Company.all.merge!(order: "id").to_a
  assert_kind_of Firm, companies[0], "37signals should be a firm"
  assert_kind_of Client, companies[1], "Summit should be a client"
end
test_inheritance_new_with_base_class() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 273
def test_inheritance_new_with_base_class
  company = Company.new(type: "Company")
  assert_equal Company, company.class
end
test_inheritance_new_with_default_class() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 268
def test_inheritance_new_with_default_class
  company = Company.new
  assert_equal Company, company.class
end
test_inheritance_new_with_subclass() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 278
def test_inheritance_new_with_subclass
  firm = Company.new(type: "Firm")
  assert_equal Firm, firm.class
end
test_inheritance_save() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 251
def test_inheritance_save
  firm = Firm.new
  firm.name = "Next Angle"
  firm.save

  next_angle = Company.find(firm.id)
  assert_kind_of Firm, next_angle, "Next Angle should be a firm"
end
test_inheritance_with_default_scope() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 448
def test_inheritance_with_default_scope
  assert_equal 1, SelectedMembership.count(:all)
end
test_inheritance_without_mapping() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 438
def test_inheritance_without_mapping
  assert_kind_of SpecialSubscriber, SpecialSubscriber.find("webster132")
  assert_nothing_raised { s = SpecialSubscriber.new("name" => "And breaaaaathe!"); s.id = "roger"; s.save }
end
test_inherits_custom_primary_key() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 434
def test_inherits_custom_primary_key
  assert_equal Subscriber.primary_key, SpecialSubscriber.primary_key
end
test_new_with_abstract_class() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 283
def test_new_with_abstract_class
  e = assert_raises(NotImplementedError) do
    AbstractCompany.new
  end
  assert_equal("AbstractCompany is an abstract class and cannot be instantiated.", e.message)
end
test_new_with_ar_base() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 290
def test_new_with_ar_base
  e = assert_raises(NotImplementedError) do
    ActiveRecord::Base.new
  end
  assert_equal("ActiveRecord::Base is an abstract class and cannot be instantiated.", e.message)
end
test_new_with_autoload_paths() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 326
def test_new_with_autoload_paths
  path = File.expand_path("../models/autoloadable", __dir__)
  ActiveSupport::Dependencies.autoload_paths << path

  firm = Company.new(type: "ExtraFirm")
  assert_equal ExtraFirm, firm.class
ensure
  ActiveSupport::Dependencies.autoload_paths.reject! { |p| p == path }
  ActiveSupport::Dependencies.clear
end
test_new_with_complex_inheritance() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 315
def test_new_with_complex_inheritance
  assert_nothing_raised { Client.new(type: "VerySpecialClient") }
end
test_new_with_invalid_type() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 297
def test_new_with_invalid_type
  assert_raise(ActiveRecord::SubclassNotFound) { Company.new(type: "InvalidType") }
end
test_new_with_unrelated_namespaced_type() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 305
def test_new_with_unrelated_namespaced_type
  without_store_full_sti_class do
    e = assert_raises ActiveRecord::SubclassNotFound do
      Namespaced::Company.new(type: "Firm")
    end

    assert_equal "Invalid single-table inheritance type: Namespaced::Firm is not a subclass of Namespaced::Company", e.message
  end
end
test_new_with_unrelated_type() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 301
def test_new_with_unrelated_type
  assert_raise(ActiveRecord::SubclassNotFound) { Company.new(type: "Account") }
end
test_new_without_storing_full_sti_class() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 319
def test_new_without_storing_full_sti_class
  without_store_full_sti_class do
    item = Company.new(type: "SpecialCo")
    assert_instance_of Company::SpecialCo, item
  end
end
test_scope_inherited_properly() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 443
def test_scope_inherited_properly
  assert_nothing_raised { Company.of_first_firm }
  assert_nothing_raised { Client.of_first_firm }
end
test_should_store_demodulized_class_name_with_store_full_sti_class_option_disabled() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 110
def test_should_store_demodulized_class_name_with_store_full_sti_class_option_disabled
  without_store_full_sti_class do
    item = Namespaced::Company.new
    assert_equal "Company", item[:type]
  end
end
test_should_store_full_class_name_with_store_full_sti_class_option_enabled() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 117
def test_should_store_full_class_name_with_store_full_sti_class_option_enabled
  with_store_full_sti_class do
    item = Namespaced::Company.new
    assert_equal "Namespaced::Company", item[:type]
  end
end
test_update_all_within_inheritance() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 359
def test_update_all_within_inheritance
  Client.update_all "name = 'I am a client'"
  assert_equal "I am a client", Client.first.name
  # Order by added as otherwise Oracle tests were failing because of different order of results
  assert_equal "37signals", Firm.all.merge!(order: "id").to_a.first.name
end