PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/rails/activerecord/test/inheritance_test.rb

https://github.com/millbanksystems/hansard
Ruby | 212 lines | 167 code | 36 blank | 9 comment | 2 complexity | bc7f4d155201ac582696ff7205c1f964 MD5 | raw file
Possible License(s): Apache-2.0, MPL-2.0-no-copyleft-exception
  1. require 'abstract_unit'
  2. require 'fixtures/company'
  3. require 'fixtures/project'
  4. require 'fixtures/subscriber'
  5. class InheritanceTest < Test::Unit::TestCase
  6. fixtures :companies, :projects, :subscribers, :accounts
  7. def test_company_descends_from_active_record
  8. assert_raise(NoMethodError) { ActiveRecord::Base.descends_from_active_record? }
  9. assert AbstractCompany.descends_from_active_record?, 'AbstractCompany should descend from ActiveRecord::Base'
  10. assert Company.descends_from_active_record?, 'Company should descend from ActiveRecord::Base'
  11. assert !Class.new(Company).descends_from_active_record?, 'Company subclass should not descend from ActiveRecord::Base'
  12. end
  13. def test_a_bad_type_column
  14. #SQLServer need to turn Identity Insert On before manually inserting into the Identity column
  15. if current_adapter?(:SQLServerAdapter, :SybaseAdapter)
  16. Company.connection.execute "SET IDENTITY_INSERT companies ON"
  17. end
  18. Company.connection.insert "INSERT INTO companies (id, #{QUOTED_TYPE}, name) VALUES(100, 'bad_class!', 'Not happening')"
  19. #We then need to turn it back Off before continuing.
  20. if current_adapter?(:SQLServerAdapter, :SybaseAdapter)
  21. Company.connection.execute "SET IDENTITY_INSERT companies OFF"
  22. end
  23. assert_raises(ActiveRecord::SubclassNotFound) { Company.find(100) }
  24. end
  25. def test_inheritance_find
  26. assert Company.find(1).kind_of?(Firm), "37signals should be a firm"
  27. assert Firm.find(1).kind_of?(Firm), "37signals should be a firm"
  28. assert Company.find(2).kind_of?(Client), "Summit should be a client"
  29. assert Client.find(2).kind_of?(Client), "Summit should be a client"
  30. end
  31. def test_alt_inheritance_find
  32. switch_to_alt_inheritance_column
  33. test_inheritance_find
  34. switch_to_default_inheritance_column
  35. end
  36. def test_inheritance_find_all
  37. companies = Company.find(:all, :order => 'id')
  38. assert companies[0].kind_of?(Firm), "37signals should be a firm"
  39. assert companies[1].kind_of?(Client), "Summit should be a client"
  40. end
  41. def test_alt_inheritance_find_all
  42. switch_to_alt_inheritance_column
  43. test_inheritance_find_all
  44. switch_to_default_inheritance_column
  45. end
  46. def test_inheritance_save
  47. firm = Firm.new
  48. firm.name = "Next Angle"
  49. firm.save
  50. next_angle = Company.find(firm.id)
  51. assert next_angle.kind_of?(Firm), "Next Angle should be a firm"
  52. end
  53. def test_alt_inheritance_save
  54. switch_to_alt_inheritance_column
  55. test_inheritance_save
  56. switch_to_default_inheritance_column
  57. end
  58. def test_inheritance_condition
  59. assert_equal 9, Company.count
  60. assert_equal 2, Firm.count
  61. assert_equal 3, Client.count
  62. end
  63. def test_alt_inheritance_condition
  64. switch_to_alt_inheritance_column
  65. test_inheritance_condition
  66. switch_to_default_inheritance_column
  67. end
  68. def test_finding_incorrect_type_data
  69. assert_raises(ActiveRecord::RecordNotFound) { Firm.find(2) }
  70. assert_nothing_raised { Firm.find(1) }
  71. end
  72. def test_alt_finding_incorrect_type_data
  73. switch_to_alt_inheritance_column
  74. test_finding_incorrect_type_data
  75. switch_to_default_inheritance_column
  76. end
  77. def test_update_all_within_inheritance
  78. Client.update_all "name = 'I am a client'"
  79. assert_equal "I am a client", Client.find(:all).first.name
  80. assert_equal "37signals", Firm.find(:all).first.name
  81. end
  82. def test_alt_update_all_within_inheritance
  83. switch_to_alt_inheritance_column
  84. test_update_all_within_inheritance
  85. switch_to_default_inheritance_column
  86. end
  87. def test_destroy_all_within_inheritance
  88. Client.destroy_all
  89. assert_equal 0, Client.count
  90. assert_equal 2, Firm.count
  91. end
  92. def test_alt_destroy_all_within_inheritance
  93. switch_to_alt_inheritance_column
  94. test_destroy_all_within_inheritance
  95. switch_to_default_inheritance_column
  96. end
  97. def test_find_first_within_inheritance
  98. assert_kind_of Firm, Company.find(:first, :conditions => "name = '37signals'")
  99. assert_kind_of Firm, Firm.find(:first, :conditions => "name = '37signals'")
  100. assert_nil Client.find(:first, :conditions => "name = '37signals'")
  101. end
  102. def test_alt_find_first_within_inheritance
  103. switch_to_alt_inheritance_column
  104. test_find_first_within_inheritance
  105. switch_to_default_inheritance_column
  106. end
  107. def test_complex_inheritance
  108. very_special_client = VerySpecialClient.create("name" => "veryspecial")
  109. assert_equal very_special_client, VerySpecialClient.find(:first, :conditions => "name = 'veryspecial'")
  110. assert_equal very_special_client, SpecialClient.find(:first, :conditions => "name = 'veryspecial'")
  111. assert_equal very_special_client, Company.find(:first, :conditions => "name = 'veryspecial'")
  112. assert_equal very_special_client, Client.find(:first, :conditions => "name = 'veryspecial'")
  113. assert_equal 1, Client.find(:all, :conditions => "name = 'Summit'").size
  114. assert_equal very_special_client, Client.find(very_special_client.id)
  115. end
  116. def test_alt_complex_inheritance
  117. switch_to_alt_inheritance_column
  118. test_complex_inheritance
  119. switch_to_default_inheritance_column
  120. end
  121. def test_eager_load_belongs_to_something_inherited
  122. account = Account.find(1, :include => :firm)
  123. assert_not_nil account.instance_variable_get("@firm"), "nil proves eager load failed"
  124. end
  125. def test_alt_eager_loading
  126. switch_to_alt_inheritance_column
  127. test_eager_load_belongs_to_something_inherited
  128. switch_to_default_inheritance_column
  129. ActiveRecord::Base.logger.debug "cocksucker"
  130. end
  131. def test_inheritance_without_mapping
  132. assert_kind_of SpecialSubscriber, SpecialSubscriber.find("webster132")
  133. assert_nothing_raised { s = SpecialSubscriber.new("name" => "And breaaaaathe!"); s.id = 'roger'; s.save }
  134. end
  135. private
  136. def switch_to_alt_inheritance_column
  137. # we don't want misleading test results, so get rid of the values in the type column
  138. Company.find(:all, :order => 'id').each do |c|
  139. c['type'] = nil
  140. c.save
  141. end
  142. [ Company, Firm, Client].each { |klass| klass.reset_column_information }
  143. Company.set_inheritance_column('ruby_type')
  144. end
  145. def switch_to_default_inheritance_column
  146. [ Company, Firm, Client].each { |klass| klass.reset_column_information }
  147. Company.set_inheritance_column('type')
  148. end
  149. end
  150. class InheritanceComputeTypeTest < Test::Unit::TestCase
  151. fixtures :companies
  152. def setup
  153. Dependencies.log_activity = true
  154. end
  155. def teardown
  156. Dependencies.log_activity = false
  157. self.class.const_remove :FirmOnTheFly rescue nil
  158. Firm.const_remove :FirmOnTheFly rescue nil
  159. end
  160. def test_instantiation_doesnt_try_to_require_corresponding_file
  161. foo = Firm.find(:first).clone
  162. foo.ruby_type = foo.type = 'FirmOnTheFly'
  163. foo.save!
  164. # Should fail without FirmOnTheFly in the type condition.
  165. assert_raise(ActiveRecord::RecordNotFound) { Firm.find(foo.id) }
  166. # Nest FirmOnTheFly in the test case where Dependencies won't see it.
  167. self.class.const_set :FirmOnTheFly, Class.new(Firm)
  168. assert_raise(ActiveRecord::SubclassNotFound) { Firm.find(foo.id) }
  169. # Nest FirmOnTheFly in Firm where Dependencies will see it.
  170. # This is analogous to nesting models in a migration.
  171. Firm.const_set :FirmOnTheFly, Class.new(Firm)
  172. # And instantiate will find the existing constant rather than trying
  173. # to require firm_on_the_fly.
  174. assert_nothing_raised { assert_kind_of Firm::FirmOnTheFly, Firm.find(foo.id) }
  175. end
  176. end