mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
rewrite inheritance tests with a custom inheritance_column
previously the tests with and without a custom `inheritance_column` used the same models. Since the model then has both fields this can lead to false positives.
This commit is contained in:
parent
30a8f0d5b6
commit
77ca2815f5
4 changed files with 61 additions and 49 deletions
|
@ -123,9 +123,10 @@ class InheritanceTest < ActiveRecord::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_alt_inheritance_find
|
def test_alt_inheritance_find
|
||||||
switch_to_alt_inheritance_column
|
assert_kind_of Cucumber, Vegetable.find(1)
|
||||||
test_inheritance_find
|
assert_kind_of Cucumber, Cucumber.find(1)
|
||||||
switch_to_default_inheritance_column
|
assert_kind_of Cabbage, Vegetable.find(2)
|
||||||
|
assert_kind_of Cabbage, Cabbage.find(2)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_alt_becomes_works_with_sti
|
def test_alt_becomes_works_with_sti
|
||||||
|
@ -142,9 +143,9 @@ class InheritanceTest < ActiveRecord::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_alt_inheritance_find_all
|
def test_alt_inheritance_find_all
|
||||||
switch_to_alt_inheritance_column
|
companies = Vegetable.all.merge!(:order => 'id').to_a
|
||||||
test_inheritance_find_all
|
assert_kind_of Cucumber, companies[0]
|
||||||
switch_to_default_inheritance_column
|
assert_kind_of Cabbage, companies[1]
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_inheritance_save
|
def test_inheritance_save
|
||||||
|
@ -157,9 +158,11 @@ class InheritanceTest < ActiveRecord::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_alt_inheritance_save
|
def test_alt_inheritance_save
|
||||||
switch_to_alt_inheritance_column
|
cabbage = Cabbage.new(:name => 'Savoy')
|
||||||
test_inheritance_save
|
cabbage.save!
|
||||||
switch_to_default_inheritance_column
|
|
||||||
|
savoy = Vegetable.find(cabbage.id)
|
||||||
|
assert_kind_of Cabbage, savoy
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_inheritance_condition
|
def test_inheritance_condition
|
||||||
|
@ -169,9 +172,9 @@ class InheritanceTest < ActiveRecord::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_alt_inheritance_condition
|
def test_alt_inheritance_condition
|
||||||
switch_to_alt_inheritance_column
|
assert_equal 4, Vegetable.count
|
||||||
test_inheritance_condition
|
assert_equal 1, Cucumber.count
|
||||||
switch_to_default_inheritance_column
|
assert_equal 3, Cabbage.count
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_finding_incorrect_type_data
|
def test_finding_incorrect_type_data
|
||||||
|
@ -180,9 +183,8 @@ class InheritanceTest < ActiveRecord::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_alt_finding_incorrect_type_data
|
def test_alt_finding_incorrect_type_data
|
||||||
switch_to_alt_inheritance_column
|
assert_raise(ActiveRecord::RecordNotFound) { Cucumber.find(2) }
|
||||||
test_finding_incorrect_type_data
|
assert_nothing_raised { Cucumber.find(1) }
|
||||||
switch_to_default_inheritance_column
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_update_all_within_inheritance
|
def test_update_all_within_inheritance
|
||||||
|
@ -193,9 +195,9 @@ class InheritanceTest < ActiveRecord::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_alt_update_all_within_inheritance
|
def test_alt_update_all_within_inheritance
|
||||||
switch_to_alt_inheritance_column
|
Cabbage.update_all "name = 'the cabbage'"
|
||||||
test_update_all_within_inheritance
|
assert_equal "the cabbage", Cabbage.first.name
|
||||||
switch_to_default_inheritance_column
|
assert_equal ["my cucumber"], Cucumber.all.map(&:name).uniq
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_destroy_all_within_inheritance
|
def test_destroy_all_within_inheritance
|
||||||
|
@ -205,9 +207,9 @@ class InheritanceTest < ActiveRecord::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_alt_destroy_all_within_inheritance
|
def test_alt_destroy_all_within_inheritance
|
||||||
switch_to_alt_inheritance_column
|
Cabbage.destroy_all
|
||||||
test_destroy_all_within_inheritance
|
assert_equal 0, Cabbage.count
|
||||||
switch_to_default_inheritance_column
|
assert_equal 1, Cucumber.count
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_find_first_within_inheritance
|
def test_find_first_within_inheritance
|
||||||
|
@ -217,9 +219,9 @@ class InheritanceTest < ActiveRecord::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_alt_find_first_within_inheritance
|
def test_alt_find_first_within_inheritance
|
||||||
switch_to_alt_inheritance_column
|
assert_kind_of Cabbage, Vegetable.all.merge!(:where => "name = 'his cabbage'").first
|
||||||
test_find_first_within_inheritance
|
assert_kind_of Cabbage, Cabbage.all.merge!(:where => "name = 'his cabbage'").first
|
||||||
switch_to_default_inheritance_column
|
assert_nil Cucumber.all.merge!(:where => "name = 'his cabbage'").first
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_complex_inheritance
|
def test_complex_inheritance
|
||||||
|
@ -233,9 +235,13 @@ class InheritanceTest < ActiveRecord::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_alt_complex_inheritance
|
def test_alt_complex_inheritance
|
||||||
switch_to_alt_inheritance_column
|
king_cole = KingCole.create("name" => "uniform heads")
|
||||||
test_complex_inheritance
|
assert_equal king_cole, KingCole.where("name = 'uniform heads'").first
|
||||||
switch_to_default_inheritance_column
|
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
|
end
|
||||||
|
|
||||||
def test_eager_load_belongs_to_something_inherited
|
def test_eager_load_belongs_to_something_inherited
|
||||||
|
@ -243,6 +249,11 @@ class InheritanceTest < ActiveRecord::TestCase
|
||||||
assert account.association_cache.key?(:firm), "nil proves eager load failed"
|
assert account.association_cache.key?(:firm), "nil proves eager load failed"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_alt_eager_loading
|
||||||
|
cabbage = RedCabbage.all.merge!(:includes => :seller).find(4)
|
||||||
|
assert cabbage.association_cache.key?(:seller), "nil proves eager load failed"
|
||||||
|
end
|
||||||
|
|
||||||
def test_eager_load_belongs_to_primary_key_quoting
|
def test_eager_load_belongs_to_primary_key_quoting
|
||||||
con = Account.connection
|
con = Account.connection
|
||||||
assert_sql(/#{con.quote_table_name('companies')}.#{con.quote_column_name('id')} IN \(1\)/) do
|
assert_sql(/#{con.quote_table_name('companies')}.#{con.quote_column_name('id')} IN \(1\)/) do
|
||||||
|
@ -250,12 +261,6 @@ class InheritanceTest < ActiveRecord::TestCase
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_alt_eager_loading
|
|
||||||
switch_to_alt_inheritance_column
|
|
||||||
test_eager_load_belongs_to_something_inherited
|
|
||||||
switch_to_default_inheritance_column
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_inherits_custom_primary_key
|
def test_inherits_custom_primary_key
|
||||||
assert_equal Subscriber.primary_key, SpecialSubscriber.primary_key
|
assert_equal Subscriber.primary_key, SpecialSubscriber.primary_key
|
||||||
end
|
end
|
||||||
|
@ -264,21 +269,6 @@ class InheritanceTest < ActiveRecord::TestCase
|
||||||
assert_kind_of SpecialSubscriber, SpecialSubscriber.find("webster132")
|
assert_kind_of SpecialSubscriber, SpecialSubscriber.find("webster132")
|
||||||
assert_nothing_raised { s = SpecialSubscriber.new("name" => "And breaaaaathe!"); s.id = 'roger'; s.save }
|
assert_nothing_raised { s = SpecialSubscriber.new("name" => "And breaaaaathe!"); s.id = 'roger'; s.save }
|
||||||
end
|
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
|
|
||||||
Company.all.merge!(:order => 'id').to_a.each do |c|
|
|
||||||
c['type'] = nil
|
|
||||||
c.save
|
|
||||||
end
|
|
||||||
[ Company, Firm, Client].each { |klass| klass.reset_column_information }
|
|
||||||
Company.inheritance_column = 'ruby_type'
|
|
||||||
end
|
|
||||||
def switch_to_default_inheritance_column
|
|
||||||
[ Company, Firm, Client].each { |klass| klass.reset_column_information }
|
|
||||||
Company.inheritance_column = 'type'
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
13
activerecord/test/fixtures/vegetables.yml
vendored
13
activerecord/test/fixtures/vegetables.yml
vendored
|
@ -6,4 +6,15 @@ first_cucumber:
|
||||||
first_cabbage:
|
first_cabbage:
|
||||||
id: 2
|
id: 2
|
||||||
custom_type: Cabbage
|
custom_type: Cabbage
|
||||||
name: 'my cabbage'
|
name: 'my cabbage'
|
||||||
|
|
||||||
|
second_cabbage:
|
||||||
|
id: 3
|
||||||
|
custom_type: Cabbage
|
||||||
|
name: 'his cabbage'
|
||||||
|
|
||||||
|
red_cabbage:
|
||||||
|
id: 4
|
||||||
|
custom_type: RedCabbage
|
||||||
|
name: 'red cabbage'
|
||||||
|
seller_id: 3
|
|
@ -12,3 +12,13 @@ end
|
||||||
|
|
||||||
class Cabbage < Vegetable
|
class Cabbage < Vegetable
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class GreenCabbage < Cabbage
|
||||||
|
end
|
||||||
|
|
||||||
|
class KingCole < GreenCabbage
|
||||||
|
end
|
||||||
|
|
||||||
|
class RedCabbage < Cabbage
|
||||||
|
belongs_to :seller, :class_name => 'Company'
|
||||||
|
end
|
||||||
|
|
|
@ -186,6 +186,7 @@ ActiveRecord::Schema.define do
|
||||||
|
|
||||||
create_table :vegetables, :force => true do |t|
|
create_table :vegetables, :force => true do |t|
|
||||||
t.string :name
|
t.string :name
|
||||||
|
t.integer :seller_id
|
||||||
t.string :custom_type
|
t.string :custom_type
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue