| 33 | | def test_has_many_find |
|---|
| 34 | | assert_equal 2, @firm.clients.length |
|---|
| 35 | | end |
|---|
| 36 | | |
|---|
| 37 | | def test_has_many_orders |
|---|
| 38 | | assert_equal "Summit", @firm.clients.first.name |
|---|
| 39 | | end |
|---|
| 40 | | |
|---|
| 41 | | def test_has_many_class_name |
|---|
| 42 | | assert_equal "Microsoft", @firm.clients_sorted_desc.first.name |
|---|
| 43 | | end |
|---|
| 44 | | |
|---|
| 45 | | def test_has_many_foreign_key |
|---|
| 46 | | assert_equal "Microsoft", @firm.clients_of_firm.first.name |
|---|
| 47 | | end |
|---|
| 48 | | |
|---|
| 49 | | def test_has_many_conditions |
|---|
| 50 | | assert_equal "Microsoft", @firm.clients_like_ms.first.name |
|---|
| 51 | | end |
|---|
| 52 | | |
|---|
| 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 |
|---|
| 58 | | |
|---|
| 59 | | def test_has_many_counter_sql |
|---|
| 60 | | assert_equal 1, @firm.clients_using_counter_sql.count |
|---|
| 61 | | end |
|---|
| 62 | | |
|---|
| 63 | | def test_has_many_queries |
|---|
| | 33 | def test_has_many |
|---|
| 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 |
|---|
| 82 | | |
|---|
| 83 | | def test_has_many_dependence |
|---|
| 84 | | count = Client.count |
|---|
| 85 | | Firm.find(:first).destroy |
|---|
| 86 | | assert_equal count - 2, Client.count |
|---|
| 87 | | end |
|---|
| 88 | | |
|---|
| 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 |
|---|
| 92 | | |
|---|
| 93 | | clients = @firm.clients |
|---|
| 94 | | clients.last.instance_eval { def before_destroy() raise "Trigger rollback" end } |
|---|
| 95 | | |
|---|
| 96 | | @firm.destroy rescue "do nothing" |
|---|
| 97 | | |
|---|
| 98 | | assert_equal count, Client.count |
|---|
| 99 | | end |
|---|
| 100 | | |
|---|
| 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 |
|---|
| 107 | | |
|---|
| 108 | | def test_has_one_dependence_with_missing_association |
|---|
| 109 | | Account.destroy_all |
|---|
| 110 | | assert_nil @firm.account |
|---|
| 111 | | @firm.destroy |
|---|
| 160 | | end |
|---|
| 161 | | |
|---|
| 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" |
|---|
| 170 | | |
|---|
| 171 | | client = Client.new("name" => "TheClient.com", "firm_id" => firm.id) |
|---|
| 172 | | client.save |
|---|
| 173 | | |
|---|
| 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" |
|---|
| 177 | | |
|---|
| 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 |
|---|
| 183 | | |
|---|
| 184 | | def test_included_in_collection |
|---|
| 185 | | assert @firm.clients.include?(Client.find(2)) |
|---|
| 288 | | |
|---|
| 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 |
|---|
| 295 | | |
|---|
| 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 |
|---|
| 302 | | |
|---|
| 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 |
|---|
| 314 | | |
|---|
| 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 |
|---|
| 322 | | |
|---|
| 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) |
|---|
| 330 | | |
|---|
| 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 |
|---|
| 339 | | |
|---|
| 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 |
|---|
| 346 | | |
|---|
| 347 | | db = PStore.new(File.join(Dir.tmpdir, "ar-pstore-association-test")) |
|---|
| 348 | | db.transaction do |
|---|
| 349 | | db["apple"] = apple |
|---|
| 350 | | end |
|---|
| 351 | | |
|---|
| 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 |
|---|
| 357 | | |
|---|
| 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 |
|---|
| 363 | | |
|---|
| 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 |
|---|
| 369 | | |
|---|
| 370 | | def test_has_one_build |
|---|
| 371 | | firm = Firm.new("name" => "GlobalMegaCorp") |
|---|
| 372 | | assert firm.save |
|---|
| 373 | | |
|---|
| 374 | | account = firm.build_account(:credit_limit => 1000) |
|---|
| 375 | | assert account.save |
|---|
| 376 | | assert_equal account, firm.account |
|---|
| 377 | | end |
|---|
| 378 | | |
|---|
| 379 | | def test_has_one_failing_build_association |
|---|
| 380 | | firm = Firm.new("name" => "GlobalMegaCorp") |
|---|
| 381 | | firm.save |
|---|
| 382 | | |
|---|
| 383 | | account = firm.build_account |
|---|
| 384 | | assert !account.save |
|---|
| 385 | | assert_equal "can't be empty", account.errors.on("credit_limit") |
|---|
| 386 | | end |
|---|
| 387 | | |
|---|
| 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 |
|---|
| | 174 | |
|---|