1 require 'abstract_unit'
2 require 'fixtures/developer'
3 require 'fixtures/project'
4 require 'fixtures/company'
5 require 'fixtures/topic'
6 require 'fixtures/reply'
8 # Can't declare new classes in test case methods, so tests before that
9 bad_collection_keys = false
11 class Car < ActiveRecord::Base; has_many :wheels, :name => "wheels"; end
13 bad_collection_keys = true
15 raise "ActiveRecord should have barked on bad collection keys" unless bad_collection_keys
18 class DeprecatedAssociationWarningsTest < Test::Unit::TestCase
19 def test_deprecation_warnings
20 assert_deprecated('has_account?') { Firm.find(:first).has_account? }
21 assert_deprecated('has_clients?') { Firm.find(:first).has_clients? }
25 class DeprecatedAssociationsTest < Test::Unit::TestCase
26 fixtures :accounts, :companies, :developers, :projects, :topics,
30 @firm = companies(:first_firm)
33 def test_has_many_find
34 assert_equal 2, @firm.clients.length
37 def test_has_many_orders
38 assert_equal "Summit", @firm.clients.first.name
41 def test_has_many_class_name
42 assert_equal "Microsoft", @firm.clients_sorted_desc.first.name
45 def test_has_many_foreign_key
46 assert_equal "Microsoft", @firm.clients_of_firm.first.name
49 def test_has_many_conditions
50 assert_equal "Microsoft", @firm.clients_like_ms.first.name
54 assert_equal "Microsoft", @firm.clients_using_sql.first.name
55 assert_equal 1, @firm.clients_using_sql.count
56 assert_equal 1, @firm.clients_using_sql.count
59 def test_has_many_counter_sql
60 assert_equal 1, @firm.clients_using_counter_sql.count
63 def test_has_many_queries
64 assert !@firm.clients.loaded?
65 assert_deprecated 'has_clients?' do
66 assert_queries(1) { assert @firm.has_clients? }
68 assert !@firm.clients.loaded?
69 assert_deprecated 'clients_count' do
70 assert_queries(1) { assert_equal 2, @firm.clients_count }
72 assert !@firm.clients.loaded?
73 assert_queries(1) { @firm.clients.size }
74 assert !@firm.clients.loaded?
75 assert_queries(0) { @firm.clients }
76 assert !@firm.clients.loaded?
77 assert_queries(1) { @firm.clients.reload }
78 assert @firm.clients.loaded?
79 assert_queries(0) { @firm.clients.size }
80 assert_queries(1) { @firm.clients.count }
83 def test_has_many_dependence
85 Firm.find(:first).destroy
86 assert_equal count - 2, Client.count
89 uses_transaction :test_has_many_dependence_with_transaction_support_on_failure
90 def test_has_many_dependence_with_transaction_support_on_failure
93 clients = @firm.clients
94 clients.last.instance_eval { def before_destroy() raise "Trigger rollback" end }
96 @firm.destroy rescue "do nothing"
98 assert_equal count, Client.count
101 def test_has_one_dependence
102 num_accounts = Account.count
103 assert_not_nil @firm.account
105 assert_equal num_accounts - 1, Account.count
108 def test_has_one_dependence_with_missing_association
110 assert_nil @firm.account
115 client = companies(:second_client)
116 assert_deprecated('has_firm?') do
117 assert companies(:second_client).has_firm?, "Microsoft should have a firm"
119 assert_equal companies(:first_firm), client.firm, "Microsoft should have a firm"
122 def test_belongs_to_with_different_class_name
123 assert_equal @firm, companies(:second_client).firm_with_other_name
126 def test_belongs_to_with_condition
127 assert_equal @firm, companies(:second_client).firm_with_condition
130 def test_belongs_to_equality
131 assert_equal @firm, companies(:second_client).firm, 'Microsoft should have 37signals as firm'
135 assert_equal accounts(:signals37), @firm.account
136 assert_deprecated 'has_account?' do
137 assert @firm.has_account?, "37signals should have an account"
139 assert_deprecated 'firm?' do
140 assert accounts(:signals37).firm?(@firm), "37signals account should be able to backtrack"
142 assert_deprecated 'has_firm?' do
143 assert accounts(:signals37).has_firm?, "37signals account should be able to backtrack"
146 assert_nil accounts(:unknown).firm, "Unknown isn't linked"
149 def test_has_many_dependence_on_account
150 num_accounts = Account.count
152 assert_equal num_accounts - 1, Account.count
156 assert_deprecated 'find_in_clients' do
157 assert_equal companies(:first_client), @firm.find_in_clients(2)
158 assert_raises(ActiveRecord::RecordNotFound) { @firm.find_in_clients(6) }
162 def test_force_reload
163 ActiveSupport::Deprecation.silence do
164 firm = Firm.new("name" => "A New Firm, Inc")
166 firm.clients.each {|c|} # forcing to load all clients
167 assert firm.clients.empty?, "New firm shouldn't have client objects"
168 assert !firm.has_clients?, "New firm shouldn't have clients"
169 assert_equal 0, firm.clients_count, "New firm should have 0 clients"
171 client = Client.new("name" => "TheClient.com", "firm_id" => firm.id)
174 assert firm.clients.empty?, "New firm should have cached no client objects"
175 assert !firm.has_clients?, "New firm should have cached a no-clients response"
176 assert_equal 0, firm.clients_count, "New firm should have cached 0 clients count"
178 assert !firm.clients(true).empty?, "New firm should have reloaded client objects"
179 assert firm.has_clients?(true), "New firm should have reloaded with a have-clients response"
180 assert_equal 1, firm.clients_count(true), "New firm should have reloaded clients count"
184 def test_included_in_collection
185 assert @firm.clients.include?(Client.find(2))
188 def test_build_to_collection
189 count = @firm.clients_of_firm.count
191 assert_deprecated 'build_to_clients_of_firm' do
192 new_client = @firm.build_to_clients_of_firm("name" => "Another Client")
194 assert_equal "Another Client", new_client.name
195 assert new_client.save
197 assert_equal @firm, new_client.firm
198 assert_equal count + 1, @firm.clients_of_firm.count
201 def test_create_in_collection
202 assert_deprecated 'create_in_clients_of_firm' do
203 assert_equal @firm.create_in_clients_of_firm("name" => "Another Client"), @firm.clients_of_firm(true).last
207 def test_has_and_belongs_to_many
208 david = Developer.find(1)
209 assert_deprecated 'has_projects?' do
210 assert david.has_projects?
212 assert_deprecated 'projects_count' do
213 assert_equal 2, david.projects_count
216 active_record = Project.find(1)
217 assert_deprecated 'has_developers?' do
218 assert active_record.has_developers?
220 assert_deprecated 'developers_count' do
221 assert_equal 3, active_record.developers_count
223 assert active_record.developers.include?(david)
226 def test_has_and_belongs_to_many_removing
227 david = Developer.find(1)
228 active_record = Project.find(1)
231 david.remove_projects(active_record)
232 assert_equal 1, david.projects_count
233 assert_equal 2, active_record.developers_count
237 def test_has_and_belongs_to_many_zero
238 david = Developer.find(1)
240 david.remove_projects Project.find(:all)
241 assert_equal 0, david.projects_count
242 assert !david.has_projects?
246 def test_has_and_belongs_to_many_adding
247 jamis = Developer.find(2)
248 action_controller = Project.find(2)
251 jamis.add_projects(action_controller)
252 assert_equal 2, jamis.projects_count
253 assert_equal 2, action_controller.developers_count
257 def test_has_and_belongs_to_many_adding_from_the_project
258 jamis = Developer.find(2)
259 action_controller = Project.find(2)
262 action_controller.add_developers(jamis)
263 assert_equal 2, jamis.projects_count
264 assert_equal 2, action_controller.developers_count
268 def test_has_and_belongs_to_many_adding_a_collection
269 aredridel = Developer.new("name" => "Aredridel")
273 aredridel.add_projects([ Project.find(1), Project.find(2) ])
274 assert_equal 2, aredridel.projects_count
278 def test_belongs_to_counter
279 topic = Topic.create("title" => "Apple", "content" => "hello world")
280 assert_equal 0, topic.send(:read_attribute, "replies_count"), "No replies yet"
282 reply = assert_deprecated { topic.create_in_replies("title" => "I'm saying no!", "content" => "over here") }
283 assert_equal 1, Topic.find(topic.id).send(:read_attribute, "replies_count"), "First reply created"
286 assert_equal 0, Topic.find(topic.id).send(:read_attribute, "replies_count"), "First reply deleted"
289 def test_natural_assignment_of_has_one
290 apple = Firm.create("name" => "Apple")
291 citibank = Account.create("credit_limit" => 10)
292 apple.account = citibank
293 assert_equal apple.id, citibank.firm_id
296 def test_natural_assignment_of_belongs_to
297 apple = Firm.create("name" => "Apple")
298 citibank = Account.create("credit_limit" => 10)
299 citibank.firm = apple
300 assert_equal apple.id, citibank.firm_id
303 def test_natural_assignment_of_has_many
304 apple = Firm.create("name" => "Apple")
305 natural = Client.create("name" => "Natural Company")
306 apple.clients << natural
307 assert_equal apple.id, natural.firm_id
308 assert_equal Client.find(natural.id), Firm.find(apple.id).clients.find(natural.id)
309 apple.clients.delete natural
310 assert_raises(ActiveRecord::RecordNotFound) {
311 Firm.find(apple.id).clients.find(natural.id)
315 def test_natural_adding_of_has_and_belongs_to_many
316 rails = Project.create("name" => "Rails")
317 ap = Project.create("name" => "Action Pack")
318 john = Developer.create("name" => "John")
319 mike = Developer.create("name" => "Mike")
320 rails.developers << john
321 rails.developers << mike
323 assert_equal Developer.find(john.id), Project.find(rails.id).developers.find(john.id)
324 assert_equal Developer.find(mike.id), Project.find(rails.id).developers.find(mike.id)
325 assert_equal Project.find(rails.id), Developer.find(mike.id).projects.find(rails.id)
326 assert_equal Project.find(rails.id), Developer.find(john.id).projects.find(rails.id)
327 ap.developers << john
328 assert_equal Developer.find(john.id), Project.find(ap.id).developers.find(john.id)
329 assert_equal Project.find(ap.id), Developer.find(john.id).projects.find(ap.id)
331 ap.developers.delete john
332 assert_raises(ActiveRecord::RecordNotFound) {
333 Project.find(ap.id).developers.find(john.id)
335 assert_raises(ActiveRecord::RecordNotFound) {
336 Developer.find(john.id).projects.find(ap.id)
340 def test_storing_in_pstore
343 apple = Firm.create("name" => "Apple")
344 natural = Client.new("name" => "Natural Company")
345 apple.clients << natural
347 db = PStore.new(File.join(Dir.tmpdir, "ar-pstore-association-test"))
352 db = PStore.new(File.join(Dir.tmpdir, "ar-pstore-association-test"))
354 assert_equal "Natural Company", db["apple"].clients.first.name
358 def test_has_many_find_all
359 assert_raise(NoMethodError) do
360 @firm.find_all_in_clients("#{QUOTED_TYPE} = 'Client'")
365 assert_equal Account.find(1), @firm.account, "37signals should have an account"
366 assert_equal @firm, Account.find(1).firm, "37signals account should be able to backtrack"
367 assert_nil Account.find(2).firm, "Unknown isn't linked"
370 def test_has_one_build
371 firm = Firm.new("name" => "GlobalMegaCorp")
374 account = firm.build_account(:credit_limit => 1000)
376 assert_equal account, firm.account
379 def test_has_one_failing_build_association
380 firm = Firm.new("name" => "GlobalMegaCorp")
383 account = firm.build_account
385 assert_equal "can't be empty", account.errors.on("credit_limit")
388 def test_has_one_create
389 firm = Firm.new("name" => "GlobalMegaCorp")
391 assert_equal firm.create_account("credit_limit" => 1000), firm.account