removing log dir from .gitignore
[monkeycharger.git] / vendor / rails / activerecord / test / deprecated_associations_test.rb
blob5887a7b53d5510dd6b874b20f5bb19be9e7d179d
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
10 begin
11   class Car < ActiveRecord::Base; has_many :wheels, :name => "wheels"; end
12 rescue ArgumentError
13   bad_collection_keys = true
14 end
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? }
22   end
23 end
25 class DeprecatedAssociationsTest < Test::Unit::TestCase
26   fixtures :accounts, :companies, :developers, :projects, :topics,
27            :developers_projects
29   def setup
30     @firm = companies(:first_firm)
31   end
33   def test_has_many_find
34     assert_equal 2, @firm.clients.length
35   end
37   def test_has_many_orders
38     assert_equal "Summit", @firm.clients.first.name
39   end
41   def test_has_many_class_name
42     assert_equal "Microsoft", @firm.clients_sorted_desc.first.name
43   end
45   def test_has_many_foreign_key
46     assert_equal "Microsoft", @firm.clients_of_firm.first.name
47   end
49   def test_has_many_conditions
50     assert_equal "Microsoft", @firm.clients_like_ms.first.name
51   end
53   def test_has_many_sql
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
57   end
59   def test_has_many_counter_sql
60     assert_equal 1, @firm.clients_using_counter_sql.count
61   end
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? }
67     end
68     assert !@firm.clients.loaded?
69     assert_deprecated 'clients_count' do
70       assert_queries(1) { assert_equal 2, @firm.clients_count }
71     end
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 }
81   end
83   def test_has_many_dependence
84     count = Client.count
85     Firm.find(:first).destroy
86     assert_equal count - 2, Client.count
87   end
89   uses_transaction :test_has_many_dependence_with_transaction_support_on_failure
90   def test_has_many_dependence_with_transaction_support_on_failure
91     count = Client.count
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
99   end
101   def test_has_one_dependence
102     num_accounts = Account.count
103     assert_not_nil @firm.account
104     @firm.destroy
105     assert_equal num_accounts - 1, Account.count
106   end
108   def test_has_one_dependence_with_missing_association
109     Account.destroy_all
110     assert_nil @firm.account
111     @firm.destroy
112   end
114   def test_belongs_to
115     client = companies(:second_client)
116     assert_deprecated('has_firm?') do
117       assert companies(:second_client).has_firm?, "Microsoft should have a firm"
118     end
119     assert_equal companies(:first_firm), client.firm, "Microsoft should have a firm"
120   end
122   def test_belongs_to_with_different_class_name
123     assert_equal @firm, companies(:second_client).firm_with_other_name
124   end
126   def test_belongs_to_with_condition
127     assert_equal @firm, companies(:second_client).firm_with_condition
128   end
130   def test_belongs_to_equality
131     assert_equal @firm, companies(:second_client).firm, 'Microsoft should have 37signals as firm'
132   end
134   def test_has_one
135     assert_equal accounts(:signals37), @firm.account
136     assert_deprecated 'has_account?' do
137       assert @firm.has_account?, "37signals should have an account"
138     end
139     assert_deprecated 'firm?' do
140       assert accounts(:signals37).firm?(@firm), "37signals account should be able to backtrack"
141     end
142     assert_deprecated 'has_firm?' do
143       assert accounts(:signals37).has_firm?, "37signals account should be able to backtrack"
144     end
146     assert_nil accounts(:unknown).firm, "Unknown isn't linked"
147   end
149   def test_has_many_dependence_on_account
150     num_accounts = Account.count
151     @firm.destroy
152     assert_equal num_accounts - 1, Account.count
153   end
155   def test_find_in
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) }
159     end
160   end
162   def test_force_reload
163     ActiveSupport::Deprecation.silence do
164       firm = Firm.new("name" => "A New Firm, Inc")
165       firm.save
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)
172       client.save
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"
181     end
182   end
184   def test_included_in_collection
185     assert @firm.clients.include?(Client.find(2))
186   end
188   def test_build_to_collection
189     count = @firm.clients_of_firm.count
190     new_client = nil
191     assert_deprecated 'build_to_clients_of_firm' do
192       new_client = @firm.build_to_clients_of_firm("name" => "Another Client")
193     end
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
199   end
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
204     end
205   end
207   def test_has_and_belongs_to_many
208     david = Developer.find(1)
209     assert_deprecated 'has_projects?' do
210       assert david.has_projects?
211     end
212     assert_deprecated 'projects_count' do
213       assert_equal 2, david.projects_count
214     end
216     active_record = Project.find(1)
217     assert_deprecated 'has_developers?' do
218       assert active_record.has_developers?
219     end
220     assert_deprecated 'developers_count' do
221       assert_equal 3, active_record.developers_count
222     end
223     assert active_record.developers.include?(david)
224   end
226   def test_has_and_belongs_to_many_removing
227     david = Developer.find(1)
228     active_record = Project.find(1)
230     assert_deprecated do
231       david.remove_projects(active_record)
232       assert_equal 1, david.projects_count
233       assert_equal 2, active_record.developers_count
234     end
235   end
237   def test_has_and_belongs_to_many_zero
238     david = Developer.find(1)
239     assert_deprecated do
240       david.remove_projects Project.find(:all)
241       assert_equal 0, david.projects_count
242       assert !david.has_projects?
243     end
244   end
246   def test_has_and_belongs_to_many_adding
247     jamis = Developer.find(2)
248     action_controller = Project.find(2)
250     assert_deprecated do
251       jamis.add_projects(action_controller)
252       assert_equal 2, jamis.projects_count
253       assert_equal 2, action_controller.developers_count
254     end
255   end
257   def test_has_and_belongs_to_many_adding_from_the_project
258     jamis = Developer.find(2)
259     action_controller = Project.find(2)
261     assert_deprecated do
262       action_controller.add_developers(jamis)
263       assert_equal 2, jamis.projects_count
264       assert_equal 2, action_controller.developers_count
265     end
266   end
268   def test_has_and_belongs_to_many_adding_a_collection
269     aredridel = Developer.new("name" => "Aredridel")
270     aredridel.save
272     assert_deprecated do
273       aredridel.add_projects([ Project.find(1), Project.find(2) ])
274       assert_equal 2, aredridel.projects_count
275     end
276   end
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"
285     reply.destroy
286     assert_equal 0, Topic.find(topic.id).send(:read_attribute, "replies_count"), "First reply deleted"
287   end
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
294   end
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
301   end
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)
312     }
313   end
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)
334     }
335     assert_raises(ActiveRecord::RecordNotFound) {
336       Developer.find(john.id).projects.find(ap.id)
337     }
338   end
340   def test_storing_in_pstore
341     require "pstore"
342     require "tmpdir"
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"))
348     db.transaction do
349       db["apple"] = apple
350     end
352     db = PStore.new(File.join(Dir.tmpdir, "ar-pstore-association-test"))
353     db.transaction do
354       assert_equal "Natural Company", db["apple"].clients.first.name
355     end
356   end
358   def test_has_many_find_all
359     assert_raise(NoMethodError) do
360       @firm.find_all_in_clients("#{QUOTED_TYPE} = 'Client'")
361     end
362   end
364   def test_has_one
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"
368   end
370   def test_has_one_build
371     firm = Firm.new("name" => "GlobalMegaCorp")
372     assert firm.save
374     account = firm.build_account(:credit_limit => 1000)
375     assert account.save
376     assert_equal account, firm.account
377   end
379   def test_has_one_failing_build_association
380     firm = Firm.new("name" => "GlobalMegaCorp")
381     firm.save
383     account = firm.build_account
384     assert !account.save
385     assert_equal "can't be empty", account.errors.on("credit_limit")
386   end
388   def test_has_one_create
389     firm = Firm.new("name" => "GlobalMegaCorp")
390     firm.save
391     assert_equal firm.create_account("credit_limit" => 1000), firm.account
392   end