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

9
class InheritanceTest < ActiveRecord::TestCase
10
  fixtures :companies, :projects, :subscribers, :accounts, :vegetables
11 12 13 14 15 16 17 18 19

  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

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

34 35 36 37 38 39 40 41
  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

42 43 44 45 46 47 48 49
  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
50

51 52 53 54 55 56 57 58
  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
59

60 61 62 63 64 65 66 67 68
  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 已提交
69

70
  def test_company_descends_from_active_record
71
    assert !ActiveRecord::Base.descends_from_active_record?
72 73 74 75 76
    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

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
  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
J
Jon Leighton 已提交
92
    klass = Class.new { include ActiveRecord::Inheritance }
93 94 95
    assert_raise(ActiveRecord::ActiveRecordError) { klass.base_class }
  end

96
  def test_a_bad_type_column
97
    #SQLServer need to turn Identity Insert On before manually inserting into the Identity column
98
    if current_adapter?(:SybaseAdapter)
99 100
      Company.connection.execute "SET IDENTITY_INSERT companies ON"
    end
101
    Company.connection.insert "INSERT INTO companies (id, #{QUOTED_TYPE}, name) VALUES(100, 'bad_class!', 'Not happening')"
102

103
    #We then need to turn it back Off before continuing.
104
    if current_adapter?(:SybaseAdapter)
105 106
      Company.connection.execute "SET IDENTITY_INSERT companies OFF"
    end
107
    assert_raise(ActiveRecord::SubclassNotFound) { Company.find(100) }
D
Initial  
David Heinemeier Hansson 已提交
108 109 110
  end

  def test_inheritance_find
111 112 113 114
    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 已提交
115
  end
J
Jeremy Kemper 已提交
116

D
Initial  
David Heinemeier Hansson 已提交
117
  def test_alt_inheritance_find
118 119 120 121
    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)
D
Initial  
David Heinemeier Hansson 已提交
122 123
  end

124 125 126 127 128 129 130
  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

131 132 133 134 135 136 137 138 139 140 141
  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

D
Initial  
David Heinemeier Hansson 已提交
142
  def test_inheritance_find_all
143
    companies = Company.all.merge!(:order => 'id').to_a
144 145
    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 已提交
146
  end
J
Jeremy Kemper 已提交
147

D
Initial  
David Heinemeier Hansson 已提交
148
  def test_alt_inheritance_find_all
149 150 151
    companies = Vegetable.all.merge!(:order => 'id').to_a
    assert_kind_of Cucumber, companies[0]
    assert_kind_of Cabbage, companies[1]
D
Initial  
David Heinemeier Hansson 已提交
152 153 154 155 156 157
  end

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

D
Initial  
David Heinemeier Hansson 已提交
159
    next_angle = Company.find(firm.id)
160
    assert_kind_of Firm, next_angle, "Next Angle should be a firm"
D
Initial  
David Heinemeier Hansson 已提交
161
  end
J
Jeremy Kemper 已提交
162

D
Initial  
David Heinemeier Hansson 已提交
163
  def test_alt_inheritance_save
164 165 166 167 168
    cabbage = Cabbage.new(:name => 'Savoy')
    cabbage.save!

    savoy = Vegetable.find(cabbage.id)
    assert_kind_of Cabbage, savoy
D
Initial  
David Heinemeier Hansson 已提交
169 170
  end

171 172
  def test_inheritance_new_with_default_class
    company = Company.new
173
    assert_equal Company, company.class
174 175 176 177
  end

  def test_inheritance_new_with_base_class
    company = Company.new(:type => 'Company')
178
    assert_equal Company, company.class
179 180 181 182
  end

  def test_inheritance_new_with_subclass
    firm = Company.new(:type => 'Firm')
183
    assert_equal Firm, firm.class
184 185
  end

186 187 188 189
  def test_new_with_abstract_class
    e = assert_raises(NotImplementedError) do
      AbstractCompany.new
    end
190
    assert_equal("AbstractCompany is an abstract class and cannot be instantiated.", e.message)
191 192 193 194 195 196
  end

  def test_new_with_ar_base
    e = assert_raises(NotImplementedError) do
      ActiveRecord::Base.new
    end
197
    assert_equal("ActiveRecord::Base is an abstract class and cannot be instantiated.", e.message)
198 199
  end

200 201 202 203 204 205 206 207
  def test_new_with_invalid_type
    assert_raise(ActiveRecord::SubclassNotFound) { Company.new(:type => 'InvalidType') }
  end

  def test_new_with_unrelated_type
    assert_raise(ActiveRecord::SubclassNotFound) { Company.new(:type => 'Account') }
  end

208 209 210 211
  def test_new_with_complex_inheritance
    assert_nothing_raised { Client.new(type: 'VerySpecialClient') }
  end

212 213 214 215 216 217 218 219 220 221 222
  def test_new_with_autoload_paths
    path = File.expand_path('../../models/autoloadable', __FILE__)
    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

D
Initial  
David Heinemeier Hansson 已提交
223
  def test_inheritance_condition
224
    assert_equal 10, Company.count
225
    assert_equal 2, Firm.count
226
    assert_equal 4, Client.count
D
Initial  
David Heinemeier Hansson 已提交
227
  end
J
Jeremy Kemper 已提交
228

D
Initial  
David Heinemeier Hansson 已提交
229
  def test_alt_inheritance_condition
230 231 232
    assert_equal 4, Vegetable.count
    assert_equal 1, Cucumber.count
    assert_equal 3, Cabbage.count
D
Initial  
David Heinemeier Hansson 已提交
233 234 235
  end

  def test_finding_incorrect_type_data
236
    assert_raise(ActiveRecord::RecordNotFound) { Firm.find(2) }
D
Initial  
David Heinemeier Hansson 已提交
237 238
    assert_nothing_raised   { Firm.find(1) }
  end
J
Jeremy Kemper 已提交
239

D
Initial  
David Heinemeier Hansson 已提交
240
  def test_alt_finding_incorrect_type_data
241 242
    assert_raise(ActiveRecord::RecordNotFound) { Cucumber.find(2) }
    assert_nothing_raised   { Cucumber.find(1) }
D
Initial  
David Heinemeier Hansson 已提交
243 244 245 246
  end

  def test_update_all_within_inheritance
    Client.update_all "name = 'I am a client'"
J
Jon Leighton 已提交
247
    assert_equal "I am a client", Client.first.name
248
    # Order by added as otherwise Oracle tests were failing because of different order of results
249
    assert_equal "37signals", Firm.all.merge!(:order => "id").to_a.first.name
D
Initial  
David Heinemeier Hansson 已提交
250
  end
J
Jeremy Kemper 已提交
251

D
Initial  
David Heinemeier Hansson 已提交
252
  def test_alt_update_all_within_inheritance
253 254 255
    Cabbage.update_all "name = 'the cabbage'"
    assert_equal "the cabbage", Cabbage.first.name
    assert_equal ["my cucumber"], Cucumber.all.map(&:name).uniq
D
Initial  
David Heinemeier Hansson 已提交
256 257 258 259
  end

  def test_destroy_all_within_inheritance
    Client.destroy_all
260 261
    assert_equal 0, Client.count
    assert_equal 2, Firm.count
D
Initial  
David Heinemeier Hansson 已提交
262
  end
J
Jeremy Kemper 已提交
263

D
Initial  
David Heinemeier Hansson 已提交
264
  def test_alt_destroy_all_within_inheritance
265 266 267
    Cabbage.destroy_all
    assert_equal 0, Cabbage.count
    assert_equal 1, Cucumber.count
D
Initial  
David Heinemeier Hansson 已提交
268 269 270
  end

  def test_find_first_within_inheritance
271 272 273
    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
D
Initial  
David Heinemeier Hansson 已提交
274
  end
J
Jeremy Kemper 已提交
275

D
Initial  
David Heinemeier Hansson 已提交
276
  def test_alt_find_first_within_inheritance
277 278 279
    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
D
Initial  
David Heinemeier Hansson 已提交
280 281 282 283
  end

  def test_complex_inheritance
    very_special_client = VerySpecialClient.create("name" => "veryspecial")
284
    assert_equal very_special_client, VerySpecialClient.where("name = 'veryspecial'").first
285 286 287 288
    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
D
Initial  
David Heinemeier Hansson 已提交
289 290 291 292
    assert_equal very_special_client, Client.find(very_special_client.id)
  end

  def test_alt_complex_inheritance
293 294 295 296 297 298 299
    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)
D
Initial  
David Heinemeier Hansson 已提交
300
  end
J
Jeremy Kemper 已提交
301

302
  def test_eager_load_belongs_to_something_inherited
303
    account = Account.all.merge!(:includes => :firm).find(1)
304
    assert account.association_cache.key?(:firm), "nil proves eager load failed"
305
  end
J
Jeremy Kemper 已提交
306

307 308 309 310 311
  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

312 313
  def test_eager_load_belongs_to_primary_key_quoting
    con = Account.connection
J
Jon Leighton 已提交
314
    assert_sql(/#{con.quote_table_name('companies')}.#{con.quote_column_name('id')} IN \(1\)/) do
315
      Account.all.merge!(:includes => :firm).find(1)
316 317 318
    end
  end

319 320 321 322
  def test_inherits_custom_primary_key
    assert_equal Subscriber.primary_key, SpecialSubscriber.primary_key
  end

323
  def test_inheritance_without_mapping
324
    assert_kind_of SpecialSubscriber, SpecialSubscriber.find("webster132")
325
    assert_nothing_raised { s = SpecialSubscriber.new("name" => "And breaaaaathe!"); s.id = 'roger'; s.save }
326
  end
327

328 329 330 331 332
  def test_scope_inherited_properly
    assert_nothing_raised { Company.of_first_firm }
    assert_nothing_raised { Client.of_first_firm }
  end
end
333

334
class InheritanceComputeTypeTest < ActiveRecord::TestCase
335 336 337
  fixtures :companies

  def setup
338
    ActiveSupport::Dependencies.log_activity = true
339 340 341
  end

  def teardown
342
    ActiveSupport::Dependencies.log_activity = false
343 344 345 346 347
    self.class.const_remove :FirmOnTheFly rescue nil
    Firm.const_remove :FirmOnTheFly rescue nil
  end

  def test_instantiation_doesnt_try_to_require_corresponding_file
348
    ActiveRecord::Base.store_full_sti_class = false
349
    foo = Firm.first.clone
350
    foo.type = 'FirmOnTheFly'
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
    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) }
367 368
  ensure
    ActiveRecord::Base.store_full_sti_class = true
369 370
  end
end