Skip to content

Commit

Permalink
Merge pull request #22131 from yui-knk/move_test_to_inheritance_test
Browse files Browse the repository at this point in the history
Move some AR test cases to inheritance_test.rb
  • Loading branch information
pixeltrix committed Nov 2, 2015
2 parents 46b08da + 2fe8baf commit 5b7afb8
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 80 deletions.
79 changes: 0 additions & 79 deletions activerecord/test/cases/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1204,42 +1204,10 @@ def test_find_symbol_ordered_last
assert_equal last, Developer.all.merge!(:order => :salary).to_a.last
end

def test_abstract_class
assert !ActiveRecord::Base.abstract_class?
assert LoosePerson.abstract_class?
assert !LooseDescendant.abstract_class?
end

def test_abstract_class_table_name
assert_nil AbstractCompany.table_name
end

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

def test_find_on_abstract_base_class_doesnt_use_type_condition
old_class = LooseDescendant
Object.send :remove_const, :LooseDescendant
Expand Down Expand Up @@ -1284,53 +1252,6 @@ def test_benchmark_with_use_silence
ActiveRecord::Base.logger = original_logger
end

def test_compute_type_success
assert_equal Author, ActiveRecord::Base.send(:compute_type, 'Author')
end

def test_compute_type_nonexistent_constant
e = assert_raises NameError do
ActiveRecord::Base.send :compute_type, 'NonexistentModel'
end
assert_equal 'uninitialized constant ActiveRecord::Base::NonexistentModel', e.message
assert_equal 'ActiveRecord::Base::NonexistentModel', e.name
end

def test_compute_type_no_method_error
ActiveSupport::Dependencies.stub(:safe_constantize, proc{ raise NoMethodError }) do
assert_raises NoMethodError do
ActiveRecord::Base.send :compute_type, 'InvalidModel'
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
ActiveRecord::Base.send :compute_type, 'InvalidModel'
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
ActiveRecord::Base.send :compute_type, 'InvalidModel'
end
end
end

def test_clear_cache!
# preheat cache
c1 = Post.connection.schema_cache.columns('posts')
Expand Down
81 changes: 80 additions & 1 deletion activerecord/test/cases/inheritance_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'cases/helper'
require 'models/author'
require 'models/company'
require 'models/person'
require 'models/post'
Expand Down Expand Up @@ -55,6 +56,53 @@ def test_class_without_store_full_sti_class_returns_demodulized_name
end
end

def test_compute_type_success
assert_equal Author, ActiveRecord::Base.send(:compute_type, 'Author')
end

def test_compute_type_nonexistent_constant
e = assert_raises NameError do
ActiveRecord::Base.send :compute_type, 'NonexistentModel'
end
assert_equal 'uninitialized constant ActiveRecord::Base::NonexistentModel', e.message
assert_equal 'ActiveRecord::Base::NonexistentModel', e.name
end

def test_compute_type_no_method_error
ActiveSupport::Dependencies.stub(:safe_constantize, proc{ raise NoMethodError }) do
assert_raises NoMethodError do
ActiveRecord::Base.send :compute_type, 'InvalidModel'
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
ActiveRecord::Base.send :compute_type, 'InvalidModel'
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
ActiveRecord::Base.send :compute_type, 'InvalidModel'
end
end
end

def test_should_store_demodulized_class_name_with_store_full_sti_class_option_disabled
without_store_full_sti_class do
item = Namespaced::Company.new
Expand All @@ -77,13 +125,45 @@ def test_different_namespace_subclass_should_load_correctly_with_store_full_sti_
end
end

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

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

def test_abstract_class
assert !ActiveRecord::Base.abstract_class?
assert LoosePerson.abstract_class?
assert !LooseDescendant.abstract_class?
end

def test_inheritance_base_class
assert_equal Post, Post.base_class
assert_equal Post, SpecialPost.base_class
Expand Down Expand Up @@ -223,7 +303,6 @@ def test_new_with_unrelated_namespaced_type
end
end


def test_new_with_complex_inheritance
assert_nothing_raised { Client.new(type: 'VerySpecialClient') }
end
Expand Down

0 comments on commit 5b7afb8

Please sign in to comment.