inheritance_test.rb 10.5 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/teapot'
D
Initial  
David Heinemeier Hansson 已提交
8

9
class InheritanceTest < ActiveRecord::TestCase
10
  fixtures :companies, :projects, :subscribers, :accounts
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!
30
    company = Company.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 71 72 73 74 75
  def test_company_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

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
  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_active_record_model_included_base_class
    assert_equal Teapot, Teapot.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
    klass = Class.new {
      extend ActiveRecord::Configuration
      include ActiveRecord::Inheritance
    }

    assert_raise(ActiveRecord::ActiveRecordError) { klass.base_class }
  end

103
  def test_a_bad_type_column
104
    #SQLServer need to turn Identity Insert On before manually inserting into the Identity column
105
    if current_adapter?(:SybaseAdapter)
106 107
      Company.connection.execute "SET IDENTITY_INSERT companies ON"
    end
108
    Company.connection.insert "INSERT INTO companies (id, #{QUOTED_TYPE}, name) VALUES(100, 'bad_class!', 'Not happening')"
109

110
    #We then need to turn it back Off before continuing.
111
    if current_adapter?(:SybaseAdapter)
112 113
      Company.connection.execute "SET IDENTITY_INSERT companies OFF"
    end
114
    assert_raise(ActiveRecord::SubclassNotFound) { Company.find(100) }
D
Initial  
David Heinemeier Hansson 已提交
115 116 117
  end

  def test_inheritance_find
118 119 120 121
    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 已提交
122
  end
J
Jeremy Kemper 已提交
123

D
Initial  
David Heinemeier Hansson 已提交
124 125 126
  def test_alt_inheritance_find
    switch_to_alt_inheritance_column
    test_inheritance_find
127
    switch_to_default_inheritance_column
D
Initial  
David Heinemeier Hansson 已提交
128 129 130
  end

  def test_inheritance_find_all
131
    companies = Company.scoped(:order => 'id').to_a
132 133
    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 已提交
134
  end
J
Jeremy Kemper 已提交
135

D
Initial  
David Heinemeier Hansson 已提交
136 137 138
  def test_alt_inheritance_find_all
    switch_to_alt_inheritance_column
    test_inheritance_find_all
139
    switch_to_default_inheritance_column
D
Initial  
David Heinemeier Hansson 已提交
140 141 142 143 144 145
  end

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

D
Initial  
David Heinemeier Hansson 已提交
147
    next_angle = Company.find(firm.id)
148
    assert_kind_of Firm, next_angle, "Next Angle should be a firm"
D
Initial  
David Heinemeier Hansson 已提交
149
  end
J
Jeremy Kemper 已提交
150

D
Initial  
David Heinemeier Hansson 已提交
151 152 153
  def test_alt_inheritance_save
    switch_to_alt_inheritance_column
    test_inheritance_save
154
    switch_to_default_inheritance_column
D
Initial  
David Heinemeier Hansson 已提交
155 156 157
  end

  def test_inheritance_condition
158
    assert_equal 10, Company.count
159
    assert_equal 2, Firm.count
160
    assert_equal 4, Client.count
D
Initial  
David Heinemeier Hansson 已提交
161
  end
J
Jeremy Kemper 已提交
162

D
Initial  
David Heinemeier Hansson 已提交
163 164 165
  def test_alt_inheritance_condition
    switch_to_alt_inheritance_column
    test_inheritance_condition
166
    switch_to_default_inheritance_column
D
Initial  
David Heinemeier Hansson 已提交
167 168 169
  end

  def test_finding_incorrect_type_data
170
    assert_raise(ActiveRecord::RecordNotFound) { Firm.find(2) }
D
Initial  
David Heinemeier Hansson 已提交
171 172
    assert_nothing_raised   { Firm.find(1) }
  end
J
Jeremy Kemper 已提交
173

D
Initial  
David Heinemeier Hansson 已提交
174 175 176
  def test_alt_finding_incorrect_type_data
    switch_to_alt_inheritance_column
    test_finding_incorrect_type_data
177
    switch_to_default_inheritance_column
D
Initial  
David Heinemeier Hansson 已提交
178 179 180 181
  end

  def test_update_all_within_inheritance
    Client.update_all "name = 'I am a client'"
182
    assert_equal "I am a client", Client.to_a.first.name
183
    # Order by added as otherwise Oracle tests were failing because of different order of results
184
    assert_equal "37signals", Firm.scoped(:order => "id").to_a.first.name
D
Initial  
David Heinemeier Hansson 已提交
185
  end
J
Jeremy Kemper 已提交
186

D
Initial  
David Heinemeier Hansson 已提交
187 188 189
  def test_alt_update_all_within_inheritance
    switch_to_alt_inheritance_column
    test_update_all_within_inheritance
190
    switch_to_default_inheritance_column
D
Initial  
David Heinemeier Hansson 已提交
191 192 193 194
  end

  def test_destroy_all_within_inheritance
    Client.destroy_all
195 196
    assert_equal 0, Client.count
    assert_equal 2, Firm.count
D
Initial  
David Heinemeier Hansson 已提交
197
  end
J
Jeremy Kemper 已提交
198

D
Initial  
David Heinemeier Hansson 已提交
199 200 201
  def test_alt_destroy_all_within_inheritance
    switch_to_alt_inheritance_column
    test_destroy_all_within_inheritance
202
    switch_to_default_inheritance_column
D
Initial  
David Heinemeier Hansson 已提交
203 204 205
  end

  def test_find_first_within_inheritance
J
Jon Leighton 已提交
206 207 208
    assert_kind_of Firm, Company.scoped(:where => "name = '37signals'").first
    assert_kind_of Firm, Firm.scoped(:where => "name = '37signals'").first
    assert_nil Client.scoped(:where => "name = '37signals'").first
D
Initial  
David Heinemeier Hansson 已提交
209
  end
J
Jeremy Kemper 已提交
210

D
Initial  
David Heinemeier Hansson 已提交
211 212 213
  def test_alt_find_first_within_inheritance
    switch_to_alt_inheritance_column
    test_find_first_within_inheritance
214
    switch_to_default_inheritance_column
D
Initial  
David Heinemeier Hansson 已提交
215 216 217 218
  end

  def test_complex_inheritance
    very_special_client = VerySpecialClient.create("name" => "veryspecial")
219
    assert_equal very_special_client, VerySpecialClient.where("name = 'veryspecial'").first
J
Jon Leighton 已提交
220 221 222
    assert_equal very_special_client, SpecialClient.scoped(:where => "name = 'veryspecial'").first
    assert_equal very_special_client, Company.scoped(:where => "name = 'veryspecial'").first
    assert_equal very_special_client, Client.scoped(:where => "name = 'veryspecial'").first
223
    assert_equal 1, Client.scoped(:where => "name = 'Summit'").to_a.size
D
Initial  
David Heinemeier Hansson 已提交
224 225 226 227 228 229
    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
230
    switch_to_default_inheritance_column
D
Initial  
David Heinemeier Hansson 已提交
231
  end
J
Jeremy Kemper 已提交
232

233
  def test_eager_load_belongs_to_something_inherited
J
Jon Leighton 已提交
234
    account = Account.scoped(:includes => :firm).find(1)
235
    assert account.association_cache.key?(:firm), "nil proves eager load failed"
236
  end
J
Jeremy Kemper 已提交
237

238 239
  def test_eager_load_belongs_to_primary_key_quoting
    con = Account.connection
J
Jon Leighton 已提交
240
    assert_sql(/#{con.quote_table_name('companies')}.#{con.quote_column_name('id')} IN \(1\)/) do
J
Jon Leighton 已提交
241
      Account.scoped(:includes => :firm).find(1)
242 243 244
    end
  end

245 246 247 248 249
  def test_alt_eager_loading
    switch_to_alt_inheritance_column
    test_eager_load_belongs_to_something_inherited
    switch_to_default_inheritance_column
  end
250

251 252 253 254
  def test_inherits_custom_primary_key
    assert_equal Subscriber.primary_key, SpecialSubscriber.primary_key
  end

255
  def test_inheritance_without_mapping
256
    assert_kind_of SpecialSubscriber, SpecialSubscriber.find("webster132")
257
    assert_nothing_raised { s = SpecialSubscriber.new("name" => "And breaaaaathe!"); s.id = 'roger'; s.save }
258 259 260 261 262
  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
263
      Company.scoped(:order => 'id').to_a.each do |c|
264 265 266
        c['type'] = nil
        c.save
      end
267
      [ Company, Firm, Client].each { |klass| klass.reset_column_information }
268
      Company.inheritance_column = 'ruby_type'
269 270 271
    end
    def switch_to_default_inheritance_column
      [ Company, Firm, Client].each { |klass| klass.reset_column_information }
272
      Company.inheritance_column = 'type'
273
    end
274
end
275 276


277
class InheritanceComputeTypeTest < ActiveRecord::TestCase
278 279 280
  fixtures :companies

  def setup
281
    ActiveSupport::Dependencies.log_activity = true
282 283 284
  end

  def teardown
285
    ActiveSupport::Dependencies.log_activity = false
286 287 288 289 290
    self.class.const_remove :FirmOnTheFly rescue nil
    Firm.const_remove :FirmOnTheFly rescue nil
  end

  def test_instantiation_doesnt_try_to_require_corresponding_file
291
    ActiveRecord::Base.store_full_sti_class = false
292
    foo = Firm.first.clone
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
    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) }
310 311
  ensure
    ActiveRecord::Base.store_full_sti_class = true
312 313
  end
end