Froze rails gems
[depot.git] / vendor / rails / activerecord / test / cases / inheritance_test.rb
1 require "cases/helper"
2 require 'models/company'
3 require 'models/project'
4 require 'models/subscriber'
5
6 class InheritanceTest < ActiveRecord::TestCase
7 fixtures :companies, :projects, :subscribers, :accounts
8
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
13 ensure
14 ActiveRecord::Base.store_full_sti_class = old
15 end
16
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
21 ensure
22 ActiveRecord::Base.store_full_sti_class = old
23 end
24
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]
30 ensure
31 ActiveRecord::Base.store_full_sti_class = old
32 end
33
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]
39 ensure
40 ActiveRecord::Base.store_full_sti_class = old
41 end
42
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)
49 ensure
50 ActiveRecord::Base.store_full_sti_class = old
51 end
52
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'
58 end
59
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"
64 end
65 Company.connection.insert "INSERT INTO companies (id, #{QUOTED_TYPE}, name) VALUES(100, 'bad_class!', 'Not happening')"
66
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"
70 end
71 assert_raises(ActiveRecord::SubclassNotFound) { Company.find(100) }
72 end
73
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"
79 end
80
81 def test_alt_inheritance_find
82 switch_to_alt_inheritance_column
83 test_inheritance_find
84 switch_to_default_inheritance_column
85 end
86
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"
91 end
92
93 def test_alt_inheritance_find_all
94 switch_to_alt_inheritance_column
95 test_inheritance_find_all
96 switch_to_default_inheritance_column
97 end
98
99 def test_inheritance_save
100 firm = Firm.new
101 firm.name = "Next Angle"
102 firm.save
103
104 next_angle = Company.find(firm.id)
105 assert next_angle.kind_of?(Firm), "Next Angle should be a firm"
106 end
107
108 def test_alt_inheritance_save
109 switch_to_alt_inheritance_column
110 test_inheritance_save
111 switch_to_default_inheritance_column
112 end
113
114 def test_inheritance_condition
115 assert_equal 9, Company.count
116 assert_equal 2, Firm.count
117 assert_equal 3, Client.count
118 end
119
120 def test_alt_inheritance_condition
121 switch_to_alt_inheritance_column
122 test_inheritance_condition
123 switch_to_default_inheritance_column
124 end
125
126 def test_finding_incorrect_type_data
127 assert_raises(ActiveRecord::RecordNotFound) { Firm.find(2) }
128 assert_nothing_raised { Firm.find(1) }
129 end
130
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
135 end
136
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
141 end
142
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
147 end
148
149 def test_destroy_all_within_inheritance
150 Client.destroy_all
151 assert_equal 0, Client.count
152 assert_equal 2, Firm.count
153 end
154
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
159 end
160
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'")
165 end
166
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
171 end
172
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)
181 end
182
183 def test_alt_complex_inheritance
184 switch_to_alt_inheritance_column
185 test_complex_inheritance
186 switch_to_default_inheritance_column
187 end
188
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"
192 end
193
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)
198 end
199 end
200
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
205 end
206
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 }
210 end
211
212 private
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|
216 c['type'] = nil
217 c.save
218 end
219 [ Company, Firm, Client].each { |klass| klass.reset_column_information }
220 Company.set_inheritance_column('ruby_type')
221 end
222 def switch_to_default_inheritance_column
223 [ Company, Firm, Client].each { |klass| klass.reset_column_information }
224 Company.set_inheritance_column('type')
225 end
226 end
227
228
229 class InheritanceComputeTypeTest < ActiveRecord::TestCase
230 fixtures :companies
231
232 def setup
233 ActiveSupport::Dependencies.log_activity = true
234 end
235
236 def teardown
237 ActiveSupport::Dependencies.log_activity = false
238 self.class.const_remove :FirmOnTheFly rescue nil
239 Firm.const_remove :FirmOnTheFly rescue nil
240 end
241
242 def test_instantiation_doesnt_try_to_require_corresponding_file
243 foo = Firm.find(:first).clone
244 foo.ruby_type = foo.type = 'FirmOnTheFly'
245 foo.save!
246
247 # Should fail without FirmOnTheFly in the type condition.
248 assert_raise(ActiveRecord::RecordNotFound) { Firm.find(foo.id) }
249
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) }
253
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)
257
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) }
261 end
262 end