2 require 'models/company'
3 require 'models/project'
4 require 'models/subscriber'
6 class InheritanceTest
< ActiveRecord
::TestCase
7 fixtures
:companies, :projects, :subscribers, :accounts
9 def test_class_with_store_full_sti_class_returns_full_name
10 old
= ActiveRecord
::Base.store_full_sti_class
11 ActiveRecord
::Base.store_full_sti_class
= true
12 assert_equal
'Namespaced::Company', Namespaced
::Company.sti_name
14 ActiveRecord
::Base.store_full_sti_class
= old
17 def test_class_without_store_full_sti_class_returns_demodulized_name
18 old
= ActiveRecord
::Base.store_full_sti_class
19 ActiveRecord
::Base.store_full_sti_class
= false
20 assert_equal
'Company', Namespaced
::Company.sti_name
22 ActiveRecord
::Base.store_full_sti_class
= old
25 def test_should_store_demodulized_class_name_with_store_full_sti_class_option_disabled
26 old
= ActiveRecord
::Base.store_full_sti_class
27 ActiveRecord
::Base.store_full_sti_class
= false
28 item
= Namespaced
::Company.new
29 assert_equal
'Company', item
[:type]
31 ActiveRecord
::Base.store_full_sti_class
= old
34 def test_should_store_full_class_name_with_store_full_sti_class_option_enabled
35 old
= ActiveRecord
::Base.store_full_sti_class
36 ActiveRecord
::Base.store_full_sti_class
= true
37 item
= Namespaced
::Company.new
38 assert_equal
'Namespaced::Company', item
[:type]
40 ActiveRecord
::Base.store_full_sti_class
= old
43 def test_different_namespace_subclass_should_load_correctly_with_store_full_sti_class_option
44 old
= ActiveRecord
::Base.store_full_sti_class
45 ActiveRecord
::Base.store_full_sti_class
= true
46 item
= Namespaced
::Company.create
:name => "Wolverine 2"
47 assert_not_nil Company
.find(item
.id
)
48 assert_not_nil Namespaced
::Company.find(item
.id
)
50 ActiveRecord
::Base.store_full_sti_class
= old
53 def test_company_descends_from_active_record
54 assert_raise(NoMethodError
) { ActiveRecord
::Base.descends_from_active_record
? }
55 assert AbstractCompany
.descends_from_active_record
?, 'AbstractCompany should descend from ActiveRecord::Base'
56 assert Company
.descends_from_active_record
?, 'Company should descend from ActiveRecord::Base'
57 assert
!Class
.new(Company
).descends_from_active_record
?, 'Company subclass should not descend from ActiveRecord::Base'
60 def test_a_bad_type_column
61 #SQLServer need to turn Identity Insert On before manually inserting into the Identity column
62 if current_adapter
?(:SybaseAdapter)
63 Company
.connection
.execute
"SET IDENTITY_INSERT companies ON"
65 Company
.connection
.insert
"INSERT INTO companies (id, #{QUOTED_TYPE}, name) VALUES(100, 'bad_class!', 'Not happening')"
67 #We then need to turn it back Off before continuing.
68 if current_adapter
?(:SybaseAdapter)
69 Company
.connection
.execute
"SET IDENTITY_INSERT companies OFF"
71 assert_raises(ActiveRecord
::SubclassNotFound) { Company
.find(100) }
74 def test_inheritance_find
75 assert Company
.find(1).kind_of
?(Firm
), "37signals should be a firm"
76 assert Firm
.find(1).kind_of
?(Firm
), "37signals should be a firm"
77 assert Company
.find(2).kind_of
?(Client
), "Summit should be a client"
78 assert Client
.find(2).kind_of
?(Client
), "Summit should be a client"
81 def test_alt_inheritance_find
82 switch_to_alt_inheritance_column
84 switch_to_default_inheritance_column
87 def test_inheritance_find_all
88 companies
= Company
.find(:all, :order => 'id')
89 assert companies
[0].kind_of
?(Firm
), "37signals should be a firm"
90 assert companies
[1].kind_of
?(Client
), "Summit should be a client"
93 def test_alt_inheritance_find_all
94 switch_to_alt_inheritance_column
95 test_inheritance_find_all
96 switch_to_default_inheritance_column
99 def test_inheritance_save
101 firm
.name
= "Next Angle"
104 next_angle
= Company
.find(firm
.id
)
105 assert next_angle
.kind_of
?(Firm
), "Next Angle should be a firm"
108 def test_alt_inheritance_save
109 switch_to_alt_inheritance_column
110 test_inheritance_save
111 switch_to_default_inheritance_column
114 def test_inheritance_condition
115 assert_equal
9, Company
.count
116 assert_equal
2, Firm
.count
117 assert_equal
3, Client
.count
120 def test_alt_inheritance_condition
121 switch_to_alt_inheritance_column
122 test_inheritance_condition
123 switch_to_default_inheritance_column
126 def test_finding_incorrect_type_data
127 assert_raises(ActiveRecord
::RecordNotFound) { Firm
.find(2) }
128 assert_nothing_raised
{ Firm
.find(1) }
131 def test_alt_finding_incorrect_type_data
132 switch_to_alt_inheritance_column
133 test_finding_incorrect_type_data
134 switch_to_default_inheritance_column
137 def test_update_all_within_inheritance
138 Client
.update_all
"name = 'I am a client'"
139 assert_equal
"I am a client", Client
.find(:all).first
.name
140 assert_equal
"37signals", Firm
.find(:all).first
.name
143 def test_alt_update_all_within_inheritance
144 switch_to_alt_inheritance_column
145 test_update_all_within_inheritance
146 switch_to_default_inheritance_column
149 def test_destroy_all_within_inheritance
151 assert_equal
0, Client
.count
152 assert_equal
2, Firm
.count
155 def test_alt_destroy_all_within_inheritance
156 switch_to_alt_inheritance_column
157 test_destroy_all_within_inheritance
158 switch_to_default_inheritance_column
161 def test_find_first_within_inheritance
162 assert_kind_of Firm
, Company
.find(:first, :conditions => "name = '37signals'")
163 assert_kind_of Firm
, Firm
.find(:first, :conditions => "name = '37signals'")
164 assert_nil Client
.find(:first, :conditions => "name = '37signals'")
167 def test_alt_find_first_within_inheritance
168 switch_to_alt_inheritance_column
169 test_find_first_within_inheritance
170 switch_to_default_inheritance_column
173 def test_complex_inheritance
174 very_special_client
= VerySpecialClient
.create("name" => "veryspecial")
175 assert_equal very_special_client
, VerySpecialClient
.find(:first, :conditions => "name = 'veryspecial'")
176 assert_equal very_special_client
, SpecialClient
.find(:first, :conditions => "name = 'veryspecial'")
177 assert_equal very_special_client
, Company
.find(:first, :conditions => "name = 'veryspecial'")
178 assert_equal very_special_client
, Client
.find(:first, :conditions => "name = 'veryspecial'")
179 assert_equal
1, Client
.find(:all, :conditions => "name = 'Summit'").size
180 assert_equal very_special_client
, Client
.find(very_special_client
.id
)
183 def test_alt_complex_inheritance
184 switch_to_alt_inheritance_column
185 test_complex_inheritance
186 switch_to_default_inheritance_column
189 def test_eager_load_belongs_to_something_inherited
190 account
= Account
.find(1, :include => :firm)
191 assert_not_nil account
.instance_variable_get("@firm"), "nil proves eager load failed"
194 def test_eager_load_belongs_to_primary_key_quoting
195 con
= Account
.connection
196 assert_sql(/\(#{con.quote_table_name('companies')}.#{con.quote_column_name('id')} = 1\)/) do
197 Account
.find(1, :include => :firm)
201 def test_alt_eager_loading
202 switch_to_alt_inheritance_column
203 test_eager_load_belongs_to_something_inherited
204 switch_to_default_inheritance_column
207 def test_inheritance_without_mapping
208 assert_kind_of SpecialSubscriber
, SpecialSubscriber
.find("webster132")
209 assert_nothing_raised
{ s
= SpecialSubscriber
.new("name" => "And breaaaaathe!"); s
.id
= 'roger'; s
.save
}
213 def switch_to_alt_inheritance_column
214 # we don't want misleading test results, so get rid of the values in the type column
215 Company
.find(:all, :order => 'id').each
do |c
|
219 [ Company
, Firm
, Client
].each
{ |klass
| klass
.reset_column_information
}
220 Company
.set_inheritance_column('ruby_type')
222 def switch_to_default_inheritance_column
223 [ Company
, Firm
, Client
].each
{ |klass
| klass
.reset_column_information
}
224 Company
.set_inheritance_column('type')
229 class InheritanceComputeTypeTest
< ActiveRecord
::TestCase
233 ActiveSupport
::Dependencies.log_activity
= true
237 ActiveSupport
::Dependencies.log_activity
= false
238 self.class.const_remove
:FirmOnTheFly rescue nil
239 Firm
.const_remove
:FirmOnTheFly rescue nil
242 def test_instantiation_doesnt_try_to_require_corresponding_file
243 foo
= Firm
.find(:first).clone
244 foo
.ruby_type
= foo
.type
= 'FirmOnTheFly'
247 # Should fail without FirmOnTheFly in the type condition.
248 assert_raise(ActiveRecord
::RecordNotFound) { Firm
.find(foo
.id
) }
250 # Nest FirmOnTheFly in the test case where Dependencies won't see it.
251 self.class.const_set
:FirmOnTheFly, Class
.new(Firm
)
252 assert_raise(ActiveRecord
::SubclassNotFound) { Firm
.find(foo
.id
) }
254 # Nest FirmOnTheFly in Firm where Dependencies will see it.
255 # This is analogous to nesting models in a migration.
256 Firm
.const_set
:FirmOnTheFly, Class
.new(Firm
)
258 # And instantiate will find the existing constant rather than trying
259 # to require firm_on_the_fly.
260 assert_nothing_raised
{ assert_kind_of Firm
::FirmOnTheFly, Firm
.find(foo
.id
) }