inheritance_test.rb 9.7 KB
Newer Older
1
require "cases/helper"
J
Jeremy Kemper 已提交
2 3 4
require 'models/company'
require 'models/project'
require 'models/subscriber'
D
Initial  
David Heinemeier Hansson 已提交
5

6
class InheritanceTest < ActiveRecord::TestCase
7
  fixtures :companies, :projects, :subscribers, :accounts
8 9 10 11 12 13 14 15 16

  def test_class_with_store_full_sti_class_returns_full_name
    old = ActiveRecord::Base.store_full_sti_class
    ActiveRecord::Base.store_full_sti_class = true
    assert_equal 'Namespaced::Company', Namespaced::Company.sti_name
  ensure
    ActiveRecord::Base.store_full_sti_class = old
  end

17 18
  def test_class_with_blank_sti_name
    company = Company.find(:first)
A
Aaron Patterson 已提交
19
    company = company.dup
20 21 22 23 24 25 26 27 28 29 30
    company.extend(Module.new {
      def read_attribute(name)
        return '  ' if name == 'type'
        super
      end
    })
    company.save!
    company = Company.find(:all).find { |x| x.id == company.id }
    assert_equal '  ', company.type
  end

31 32 33 34 35 36 37 38
  def test_class_without_store_full_sti_class_returns_demodulized_name
    old = ActiveRecord::Base.store_full_sti_class
    ActiveRecord::Base.store_full_sti_class = false
    assert_equal 'Company', Namespaced::Company.sti_name
  ensure
    ActiveRecord::Base.store_full_sti_class = old
  end

39 40 41 42 43 44 45 46
  def test_should_store_demodulized_class_name_with_store_full_sti_class_option_disabled
    old = ActiveRecord::Base.store_full_sti_class
    ActiveRecord::Base.store_full_sti_class = false
    item = Namespaced::Company.new
    assert_equal 'Company', item[:type]
  ensure
    ActiveRecord::Base.store_full_sti_class = old
  end
47

48 49 50 51 52 53 54 55
  def test_should_store_full_class_name_with_store_full_sti_class_option_enabled
    old = ActiveRecord::Base.store_full_sti_class
    ActiveRecord::Base.store_full_sti_class = true
    item = Namespaced::Company.new
    assert_equal 'Namespaced::Company', item[:type]
  ensure
    ActiveRecord::Base.store_full_sti_class = old
  end
56

57 58 59 60 61 62 63 64 65
  def test_different_namespace_subclass_should_load_correctly_with_store_full_sti_class_option
    old = ActiveRecord::Base.store_full_sti_class
    ActiveRecord::Base.store_full_sti_class = true
    item = Namespaced::Company.create :name => "Wolverine 2"
    assert_not_nil Company.find(item.id)
    assert_not_nil Namespaced::Company.find(item.id)
  ensure
    ActiveRecord::Base.store_full_sti_class = old
  end
D
Initial  
David Heinemeier Hansson 已提交
66

67 68 69 70 71 72 73
  def test_company_descends_from_active_record
    assert_raise(NoMethodError) { 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

74
  def test_a_bad_type_column
75
    #SQLServer need to turn Identity Insert On before manually inserting into the Identity column
76
    if current_adapter?(:SybaseAdapter)
77 78
      Company.connection.execute "SET IDENTITY_INSERT companies ON"
    end
79
    Company.connection.insert "INSERT INTO companies (id, #{QUOTED_TYPE}, name) VALUES(100, 'bad_class!', 'Not happening')"
80

81
    #We then need to turn it back Off before continuing.
82
    if current_adapter?(:SybaseAdapter)
83 84
      Company.connection.execute "SET IDENTITY_INSERT companies OFF"
    end
85
    assert_raise(ActiveRecord::SubclassNotFound) { Company.find(100) }
D
Initial  
David Heinemeier Hansson 已提交
86 87 88
  end

  def test_inheritance_find
89 90 91 92
    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"
D
Initial  
David Heinemeier Hansson 已提交
93
  end
J
Jeremy Kemper 已提交
94

D
Initial  
David Heinemeier Hansson 已提交
95 96 97
  def test_alt_inheritance_find
    switch_to_alt_inheritance_column
    test_inheritance_find
98
    switch_to_default_inheritance_column
D
Initial  
David Heinemeier Hansson 已提交
99 100 101
  end

  def test_inheritance_find_all
102
    companies = Company.find(:all, :order => 'id')
103 104
    assert_kind_of Firm, companies[0], "37signals should be a firm"
    assert_kind_of Client, companies[1], "Summit should be a client"
D
Initial  
David Heinemeier Hansson 已提交
105
  end
J
Jeremy Kemper 已提交
106

D
Initial  
David Heinemeier Hansson 已提交
107 108 109
  def test_alt_inheritance_find_all
    switch_to_alt_inheritance_column
    test_inheritance_find_all
110
    switch_to_default_inheritance_column
D
Initial  
David Heinemeier Hansson 已提交
111 112 113 114 115 116
  end

  def test_inheritance_save
    firm = Firm.new
    firm.name = "Next Angle"
    firm.save
J
Jeremy Kemper 已提交
117

D
Initial  
David Heinemeier Hansson 已提交
118
    next_angle = Company.find(firm.id)
119
    assert_kind_of Firm, next_angle, "Next Angle should be a firm"
D
Initial  
David Heinemeier Hansson 已提交
120
  end
J
Jeremy Kemper 已提交
121

D
Initial  
David Heinemeier Hansson 已提交
122 123 124
  def test_alt_inheritance_save
    switch_to_alt_inheritance_column
    test_inheritance_save
125
    switch_to_default_inheritance_column
D
Initial  
David Heinemeier Hansson 已提交
126 127 128
  end

  def test_inheritance_condition
129
    assert_equal 10, Company.count
130
    assert_equal 2, Firm.count
131
    assert_equal 4, Client.count
D
Initial  
David Heinemeier Hansson 已提交
132
  end
J
Jeremy Kemper 已提交
133

D
Initial  
David Heinemeier Hansson 已提交
134 135 136
  def test_alt_inheritance_condition
    switch_to_alt_inheritance_column
    test_inheritance_condition
137
    switch_to_default_inheritance_column
D
Initial  
David Heinemeier Hansson 已提交
138 139 140
  end

  def test_finding_incorrect_type_data
141
    assert_raise(ActiveRecord::RecordNotFound) { Firm.find(2) }
D
Initial  
David Heinemeier Hansson 已提交
142 143
    assert_nothing_raised   { Firm.find(1) }
  end
J
Jeremy Kemper 已提交
144

D
Initial  
David Heinemeier Hansson 已提交
145 146 147
  def test_alt_finding_incorrect_type_data
    switch_to_alt_inheritance_column
    test_finding_incorrect_type_data
148
    switch_to_default_inheritance_column
D
Initial  
David Heinemeier Hansson 已提交
149 150 151 152
  end

  def test_update_all_within_inheritance
    Client.update_all "name = 'I am a client'"
153
    assert_equal "I am a client", Client.find(:all).first.name
154 155
    # Order by added as otherwise Oracle tests were failing because of different order of results
    assert_equal "37signals", Firm.find(:all, :order => "id").first.name
D
Initial  
David Heinemeier Hansson 已提交
156
  end
J
Jeremy Kemper 已提交
157

D
Initial  
David Heinemeier Hansson 已提交
158 159 160
  def test_alt_update_all_within_inheritance
    switch_to_alt_inheritance_column
    test_update_all_within_inheritance
161
    switch_to_default_inheritance_column
D
Initial  
David Heinemeier Hansson 已提交
162 163 164 165
  end

  def test_destroy_all_within_inheritance
    Client.destroy_all
166 167
    assert_equal 0, Client.count
    assert_equal 2, Firm.count
D
Initial  
David Heinemeier Hansson 已提交
168
  end
J
Jeremy Kemper 已提交
169

D
Initial  
David Heinemeier Hansson 已提交
170 171 172
  def test_alt_destroy_all_within_inheritance
    switch_to_alt_inheritance_column
    test_destroy_all_within_inheritance
173
    switch_to_default_inheritance_column
D
Initial  
David Heinemeier Hansson 已提交
174 175 176
  end

  def test_find_first_within_inheritance
177 178 179
    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'")
D
Initial  
David Heinemeier Hansson 已提交
180
  end
J
Jeremy Kemper 已提交
181

D
Initial  
David Heinemeier Hansson 已提交
182 183 184
  def test_alt_find_first_within_inheritance
    switch_to_alt_inheritance_column
    test_find_first_within_inheritance
185
    switch_to_default_inheritance_column
D
Initial  
David Heinemeier Hansson 已提交
186 187 188 189
  end

  def test_complex_inheritance
    very_special_client = VerySpecialClient.create("name" => "veryspecial")
190
    assert_equal very_special_client, VerySpecialClient.where("name = 'veryspecial'").first
191 192 193 194
    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
D
Initial  
David Heinemeier Hansson 已提交
195 196 197 198 199 200
    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
201
    switch_to_default_inheritance_column
D
Initial  
David Heinemeier Hansson 已提交
202
  end
J
Jeremy Kemper 已提交
203

204 205 206 207
  def test_eager_load_belongs_to_something_inherited
    account = Account.find(1, :include => :firm)
    assert_not_nil account.instance_variable_get("@firm"), "nil proves eager load failed"
  end
J
Jeremy Kemper 已提交
208

209 210
  def test_eager_load_belongs_to_primary_key_quoting
    con = Account.connection
211
    assert_sql(/\(#{con.quote_table_name('companies')}.#{con.quote_column_name('id')} = 1\)/) do
212 213 214 215
      Account.find(1, :include => :firm)
    end
  end

216 217 218 219 220
  def test_alt_eager_loading
    switch_to_alt_inheritance_column
    test_eager_load_belongs_to_something_inherited
    switch_to_default_inheritance_column
  end
221 222

  def test_inheritance_without_mapping
223
    assert_kind_of SpecialSubscriber, SpecialSubscriber.find("webster132")
224
    assert_nothing_raised { s = SpecialSubscriber.new("name" => "And breaaaaathe!"); s.id = 'roger'; s.save }
225 226 227 228 229
  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
230
      Company.find(:all, :order => 'id').each do |c|
231 232 233
        c['type'] = nil
        c.save
      end
234
      [ Company, Firm, Client].each { |klass| klass.reset_column_information }
235
      Company.set_inheritance_column('ruby_type')
236 237 238
    end
    def switch_to_default_inheritance_column
      [ Company, Firm, Client].each { |klass| klass.reset_column_information }
239
      Company.set_inheritance_column('type')
240
    end
241
end
242 243


244
class InheritanceComputeTypeTest < ActiveRecord::TestCase
245 246 247
  fixtures :companies

  def setup
248
    ActiveSupport::Dependencies.log_activity = true
249 250 251
  end

  def teardown
252
    ActiveSupport::Dependencies.log_activity = false
253 254 255 256 257
    self.class.const_remove :FirmOnTheFly rescue nil
    Firm.const_remove :FirmOnTheFly rescue nil
  end

  def test_instantiation_doesnt_try_to_require_corresponding_file
258
    ActiveRecord::Base.store_full_sti_class = false
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
    foo = Firm.find(:first).clone
    foo.ruby_type = foo.type = 'FirmOnTheFly'
    foo.save!

    # Should fail without FirmOnTheFly in the type condition.
    assert_raise(ActiveRecord::RecordNotFound) { Firm.find(foo.id) }

    # 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) }

    # 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)

    # 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) }
277 278
  ensure
    ActiveRecord::Base.store_full_sti_class = true
279 280
  end
end