ibm_db 0.10.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGES +15 -0
- data/README +3 -3
- data/lib/active_record/connection_adapters/ibm_db_adapter.rb +287 -125
- data/test/cases/associations/cascaded_eager_loading_test.rb +13 -1
- data/test/cases/associations/eager_test.rb +34 -1
- data/test/cases/associations/has_and_belongs_to_many_associations_test.rb +29 -5
- data/test/cases/associations/has_many_through_associations_test.rb +202 -0
- data/test/cases/associations/join_model_test.rb +6 -0
- data/test/cases/base_test.rb +89 -48
- data/test/cases/calculations_test.rb +55 -3
- data/test/cases/finder_test.rb +483 -468
- data/test/cases/migration_test.rb +221 -61
- data/test/cases/query_cache_test.rb +77 -76
- data/test/cases/schema_dumper_test.rb +186 -0
- data/test/cases/validations_test.rb +37 -5
- data/test/schema/schema.rb +8 -0
- metadata +35 -32
@@ -1,6 +1,7 @@
|
|
1
1
|
require "cases/helper"
|
2
2
|
require 'models/company'
|
3
3
|
require 'models/topic'
|
4
|
+
require 'models/edge'
|
4
5
|
|
5
6
|
Company.has_many :accounts
|
6
7
|
|
@@ -17,8 +18,8 @@ class CalculationsTest < ActiveRecord::TestCase
|
|
17
18
|
|
18
19
|
def test_should_average_field
|
19
20
|
value = Account.average(:credit_limit)
|
20
|
-
assert_kind_of
|
21
|
-
|
21
|
+
assert_kind_of BigDecimal, value
|
22
|
+
assert_equal BigDecimal.new('53.0'), value
|
22
23
|
end
|
23
24
|
|
24
25
|
def test_should_return_nil_as_average
|
@@ -99,6 +100,12 @@ class CalculationsTest < ActiveRecord::TestCase
|
|
99
100
|
|
100
101
|
def test_should_return_zero_if_sum_conditions_return_nothing
|
101
102
|
assert_equal 0, Account.sum(:credit_limit, :conditions => '1 = 2')
|
103
|
+
assert_equal 0, companies(:rails_core).companies.sum(:id, :conditions => '1 = 2')
|
104
|
+
end
|
105
|
+
|
106
|
+
def test_sum_should_return_valid_values_for_decimals
|
107
|
+
NumericData.create(:bank_balance => 19.83)
|
108
|
+
assert_equal 19.83, NumericData.sum(:bank_balance)
|
102
109
|
end
|
103
110
|
|
104
111
|
def test_should_group_by_summed_field_with_conditions
|
@@ -269,7 +276,52 @@ class CalculationsTest < ActiveRecord::TestCase
|
|
269
276
|
if current_adapter?(:IBM_DBAdapter) && !ActiveRecord::Base.connection.servertype.class.name.include?('::IBM_IDS')
|
270
277
|
assert_equal 636, Account.sum("2 * credit_limit")
|
271
278
|
else
|
272
|
-
assert_equal
|
279
|
+
assert_equal '636', Account.sum("2 * credit_limit")
|
273
280
|
end
|
274
281
|
end
|
282
|
+
|
283
|
+
def test_count_with_from_option
|
284
|
+
assert_equal Company.count(:all), Company.count(:all, :from => 'companies')
|
285
|
+
assert_equal Account.count(:all, :conditions => "credit_limit = 50"),
|
286
|
+
Account.count(:all, :from => 'accounts', :conditions => "credit_limit = 50")
|
287
|
+
assert_equal Company.count(:type, :conditions => {:type => "Firm"}),
|
288
|
+
Company.count(:type, :conditions => {:type => "Firm"}, :from => 'companies')
|
289
|
+
end
|
290
|
+
|
291
|
+
def test_sum_with_from_option
|
292
|
+
assert_equal Account.sum(:credit_limit), Account.sum(:credit_limit, :from => 'accounts')
|
293
|
+
assert_equal Account.sum(:credit_limit, :conditions => "credit_limit > 50"),
|
294
|
+
Account.sum(:credit_limit, :from => 'accounts', :conditions => "credit_limit > 50")
|
295
|
+
end
|
296
|
+
|
297
|
+
def test_average_with_from_option
|
298
|
+
assert_equal Account.average(:credit_limit), Account.average(:credit_limit, :from => 'accounts')
|
299
|
+
assert_equal Account.average(:credit_limit, :conditions => "credit_limit > 50"),
|
300
|
+
Account.average(:credit_limit, :from => 'accounts', :conditions => "credit_limit > 50")
|
301
|
+
end
|
302
|
+
|
303
|
+
def test_minimum_with_from_option
|
304
|
+
assert_equal Account.minimum(:credit_limit), Account.minimum(:credit_limit, :from => 'accounts')
|
305
|
+
assert_equal Account.minimum(:credit_limit, :conditions => "credit_limit > 50"),
|
306
|
+
Account.minimum(:credit_limit, :from => 'accounts', :conditions => "credit_limit > 50")
|
307
|
+
end
|
308
|
+
|
309
|
+
def test_maximum_with_from_option
|
310
|
+
assert_equal Account.maximum(:credit_limit), Account.maximum(:credit_limit, :from => 'accounts')
|
311
|
+
assert_equal Account.maximum(:credit_limit, :conditions => "credit_limit > 50"),
|
312
|
+
Account.maximum(:credit_limit, :from => 'accounts', :conditions => "credit_limit > 50")
|
313
|
+
end
|
314
|
+
|
315
|
+
def test_from_option_with_specified_index
|
316
|
+
if Edge.connection.adapter_name == 'MySQL'
|
317
|
+
assert_equal Edge.count(:all), Edge.count(:all, :from => 'edges USE INDEX(unique_edge_index)')
|
318
|
+
assert_equal Edge.count(:all, :conditions => 'sink_id < 5'),
|
319
|
+
Edge.count(:all, :from => 'edges USE INDEX(unique_edge_index)', :conditions => 'sink_id < 5')
|
320
|
+
end
|
321
|
+
end
|
322
|
+
|
323
|
+
def test_from_option_with_table_different_than_class
|
324
|
+
assert_equal Account.count(:all), Company.count(:all, :from => 'accounts')
|
325
|
+
end
|
326
|
+
|
275
327
|
end
|
data/test/cases/finder_test.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require "cases/helper"
|
2
2
|
require 'models/author'
|
3
|
+
require 'models/categorization'
|
3
4
|
require 'models/comment'
|
4
5
|
require 'models/company'
|
5
6
|
require 'models/topic'
|
@@ -51,11 +52,11 @@ class FinderTest < ActiveRecord::TestCase
|
|
51
52
|
def test_exists_with_aggregate_having_three_mappings_with_one_difference
|
52
53
|
existing_address = customers(:david).address
|
53
54
|
assert !Customer.exists?(:address =>
|
54
|
-
|
55
|
+
Address.new(existing_address.street, existing_address.city, existing_address.country + "1"))
|
55
56
|
assert !Customer.exists?(:address =>
|
56
|
-
|
57
|
+
Address.new(existing_address.street, existing_address.city + "1", existing_address.country))
|
57
58
|
assert !Customer.exists?(:address =>
|
58
|
-
|
59
|
+
Address.new(existing_address.street + "1", existing_address.city, existing_address.country))
|
59
60
|
end
|
60
61
|
|
61
62
|
def test_find_by_array_of_one_id
|
@@ -323,7 +324,7 @@ class FinderTest < ActiveRecord::TestCase
|
|
323
324
|
Company.find(:first, :conditions => ["id=? AND name = ?", 2])
|
324
325
|
}
|
325
326
|
assert_raises(ActiveRecord::PreparedStatementInvalid) {
|
326
|
-
|
327
|
+
Company.find(:first, :conditions => ["id=?", 2, 3, 4])
|
327
328
|
}
|
328
329
|
end
|
329
330
|
|
@@ -385,7 +386,15 @@ class FinderTest < ActiveRecord::TestCase
|
|
385
386
|
def test_bind_string
|
386
387
|
assert_equal ActiveRecord::Base.connection.quote(''), bind('?', '')
|
387
388
|
end
|
388
|
-
|
389
|
+
|
390
|
+
def test_bind_string_with_nl
|
391
|
+
assert_equal ActiveRecord::Base.connection.quote("a\nb"), bind('?', "a\nb")
|
392
|
+
end
|
393
|
+
|
394
|
+
def test_bind_mb_string_with_nl
|
395
|
+
assert_equal ActiveRecord::Base.connection.quote("a\nb"), bind('?', "a\nb".chars)
|
396
|
+
end
|
397
|
+
|
389
398
|
def test_bind_record
|
390
399
|
o = Struct.new(:quoted_id).new(1)
|
391
400
|
assert_equal '1', bind('?', o)
|
@@ -393,7 +402,13 @@ class FinderTest < ActiveRecord::TestCase
|
|
393
402
|
os = [o] * 3
|
394
403
|
assert_equal '1,1,1', bind('?', os)
|
395
404
|
end
|
396
|
-
|
405
|
+
|
406
|
+
def test_named_bind_with_postgresql_type_casts
|
407
|
+
l = Proc.new { bind(":a::integer '2009-01-01'::date", :a => '10') }
|
408
|
+
assert_nothing_raised(&l)
|
409
|
+
assert_equal "#{ActiveRecord::Base.quote_value('10')}::integer '2009-01-01'::date", l.call
|
410
|
+
end
|
411
|
+
|
397
412
|
def test_string_sanitation
|
398
413
|
assert_not_equal "#{ActiveRecord::Base.connection.quoted_string_prefix}'something ' 1=1'", ActiveRecord::Base.sanitize("something ' 1=1")
|
399
414
|
assert_equal "#{ActiveRecord::Base.connection.quoted_string_prefix}'something; select table'", ActiveRecord::Base.sanitize("something; select table")
|
@@ -419,467 +434,467 @@ class FinderTest < ActiveRecord::TestCase
|
|
419
434
|
def test_find_by_one_attribute_caches_dynamic_finder
|
420
435
|
# ensure this test can run independently of order
|
421
436
|
class << Topic; self; end.send(:remove_method, :find_by_title) if Topic.public_methods.any? { |m| m.to_s == 'find_by_title' }
|
422
|
-
|
423
|
-
|
424
|
-
|
437
|
+
assert !Topic.public_methods.any? { |m| m.to_s == 'find_by_title' }
|
438
|
+
t = Topic.find_by_title("The First Topic")
|
439
|
+
assert Topic.public_methods.any? { |m| m.to_s == 'find_by_title' }
|
440
|
+
end
|
441
|
+
|
442
|
+
def test_dynamic_finder_returns_same_results_after_caching
|
443
|
+
# ensure this test can run independently of order
|
444
|
+
class << Topic; self; end.send(:remove_method, :find_by_title) if Topic.public_method_defined?(:find_by_title)
|
445
|
+
t = Topic.find_by_title("The First Topic")
|
446
|
+
assert_equal t, Topic.find_by_title("The First Topic") # find_by_title has been cached
|
447
|
+
end
|
448
|
+
|
449
|
+
def test_find_by_one_attribute_with_order_option
|
450
|
+
assert_equal accounts(:signals37), Account.find_by_credit_limit(50, :order => 'id')
|
451
|
+
assert_equal accounts(:rails_core_account), Account.find_by_credit_limit(50, :order => 'id DESC')
|
452
|
+
end
|
453
|
+
|
454
|
+
def test_find_by_one_attribute_with_conditions
|
455
|
+
assert_equal accounts(:rails_core_account), Account.find_by_credit_limit(50, :conditions => ['firm_id = ?', 6])
|
456
|
+
end
|
457
|
+
|
458
|
+
def test_find_by_one_attribute_that_is_an_aggregate
|
459
|
+
address = customers(:david).address
|
460
|
+
assert_kind_of Address, address
|
461
|
+
found_customer = Customer.find_by_address(address)
|
462
|
+
assert_equal customers(:david), found_customer
|
463
|
+
end
|
464
|
+
|
465
|
+
def test_find_by_one_attribute_that_is_an_aggregate_with_one_attribute_difference
|
466
|
+
address = customers(:david).address
|
467
|
+
assert_kind_of Address, address
|
468
|
+
missing_address = Address.new(address.street, address.city, address.country + "1")
|
469
|
+
assert_nil Customer.find_by_address(missing_address)
|
470
|
+
missing_address = Address.new(address.street, address.city + "1", address.country)
|
471
|
+
assert_nil Customer.find_by_address(missing_address)
|
472
|
+
missing_address = Address.new(address.street + "1", address.city, address.country)
|
473
|
+
assert_nil Customer.find_by_address(missing_address)
|
474
|
+
end
|
475
|
+
|
476
|
+
def test_find_by_two_attributes_that_are_both_aggregates
|
477
|
+
balance = customers(:david).balance
|
478
|
+
address = customers(:david).address
|
479
|
+
assert_kind_of Money, balance
|
480
|
+
assert_kind_of Address, address
|
481
|
+
found_customer = Customer.find_by_balance_and_address(balance, address)
|
482
|
+
assert_equal customers(:david), found_customer
|
483
|
+
end
|
484
|
+
|
485
|
+
def test_find_by_two_attributes_with_one_being_an_aggregate
|
486
|
+
balance = customers(:david).balance
|
487
|
+
assert_kind_of Money, balance
|
488
|
+
found_customer = Customer.find_by_balance_and_name(balance, customers(:david).name)
|
489
|
+
assert_equal customers(:david), found_customer
|
490
|
+
end
|
491
|
+
|
492
|
+
def test_dynamic_finder_on_one_attribute_with_conditions_caches_method
|
493
|
+
# ensure this test can run independently of order
|
494
|
+
class << Account; self; end.send(:remove_method, :find_by_credit_limit) if Account.public_methods.any? { |m| m.to_s == 'find_by_credit_limit' }
|
495
|
+
assert !Account.public_methods.any? { |m| m.to_s == 'find_by_credit_limit' }
|
496
|
+
a = Account.find_by_credit_limit(50, :conditions => ['firm_id = ?', 6])
|
497
|
+
assert Account.public_methods.any? { |m| m.to_s == 'find_by_credit_limit' }
|
498
|
+
end
|
499
|
+
|
500
|
+
def test_dynamic_finder_on_one_attribute_with_conditions_returns_same_results_after_caching
|
501
|
+
# ensure this test can run independently of order
|
502
|
+
class << Account; self; end.send(:remove_method, :find_by_credit_limit) if Account.public_methods.any? { |m| m.to_s == 'find_by_credit_limit' }
|
503
|
+
a = Account.find_by_credit_limit(50, :conditions => ['firm_id = ?', 6])
|
504
|
+
assert_equal a, Account.find_by_credit_limit(50, :conditions => ['firm_id = ?', 6]) # find_by_credit_limit has been cached
|
505
|
+
end
|
506
|
+
|
507
|
+
def test_find_by_one_attribute_with_several_options
|
508
|
+
assert_equal accounts(:unknown), Account.find_by_credit_limit(50, :order => 'id DESC', :conditions => ['id != ?', 3])
|
509
|
+
end
|
510
|
+
|
511
|
+
def test_find_by_one_missing_attribute
|
512
|
+
assert_raises(NoMethodError) { Topic.find_by_undertitle("The First Topic!") }
|
513
|
+
end
|
514
|
+
|
515
|
+
def test_find_by_invalid_method_syntax
|
516
|
+
assert_raises(NoMethodError) { Topic.fail_to_find_by_title("The First Topic") }
|
517
|
+
assert_raises(NoMethodError) { Topic.find_by_title?("The First Topic") }
|
518
|
+
assert_raises(NoMethodError) { Topic.fail_to_find_or_create_by_title("Nonexistent Title") }
|
519
|
+
assert_raises(NoMethodError) { Topic.find_or_create_by_title?("Nonexistent Title") }
|
520
|
+
end
|
521
|
+
|
522
|
+
def test_find_by_two_attributes
|
523
|
+
assert_equal topics(:first), Topic.find_by_title_and_author_name("The First Topic", "David")
|
524
|
+
assert_nil Topic.find_by_title_and_author_name("The First Topic", "Mary")
|
525
|
+
end
|
526
|
+
|
527
|
+
def test_find_all_by_one_attribute
|
528
|
+
topics = Topic.find_all_by_content("Have a nice day")
|
529
|
+
assert_equal 2, topics.size
|
530
|
+
assert topics.include?(topics(:first))
|
531
|
+
|
532
|
+
assert_equal [], Topic.find_all_by_title("The First Topic!!")
|
533
|
+
end
|
534
|
+
|
535
|
+
def test_find_all_by_one_attribute_that_is_an_aggregate
|
536
|
+
balance = customers(:david).balance
|
537
|
+
assert_kind_of Money, balance
|
538
|
+
found_customers = Customer.find_all_by_balance(balance)
|
539
|
+
assert_equal 1, found_customers.size
|
540
|
+
assert_equal customers(:david), found_customers.first
|
541
|
+
end
|
542
|
+
|
543
|
+
def test_find_all_by_two_attributes_that_are_both_aggregates
|
544
|
+
balance = customers(:david).balance
|
545
|
+
address = customers(:david).address
|
546
|
+
assert_kind_of Money, balance
|
547
|
+
assert_kind_of Address, address
|
548
|
+
found_customers = Customer.find_all_by_balance_and_address(balance, address)
|
549
|
+
assert_equal 1, found_customers.size
|
550
|
+
assert_equal customers(:david), found_customers.first
|
551
|
+
end
|
552
|
+
|
553
|
+
def test_find_all_by_two_attributes_with_one_being_an_aggregate
|
554
|
+
balance = customers(:david).balance
|
555
|
+
assert_kind_of Money, balance
|
556
|
+
found_customers = Customer.find_all_by_balance_and_name(balance, customers(:david).name)
|
557
|
+
assert_equal 1, found_customers.size
|
558
|
+
assert_equal customers(:david), found_customers.first
|
559
|
+
end
|
560
|
+
|
561
|
+
def test_find_all_by_one_attribute_with_options
|
562
|
+
topics = Topic.find_all_by_content("Have a nice day", :order => "id DESC")
|
563
|
+
assert topics(:first), topics.last
|
564
|
+
|
565
|
+
topics = Topic.find_all_by_content("Have a nice day", :order => "id")
|
566
|
+
assert topics(:first), topics.first
|
567
|
+
end
|
568
|
+
|
569
|
+
def test_find_all_by_array_attribute
|
570
|
+
assert_equal 2, Topic.find_all_by_title(["The First Topic", "The Second Topic of the day"]).size
|
571
|
+
end
|
572
|
+
|
573
|
+
def test_find_all_by_boolean_attribute
|
574
|
+
topics = Topic.find_all_by_approved(false)
|
575
|
+
assert_equal 1, topics.size
|
576
|
+
assert topics.include?(topics(:first))
|
577
|
+
|
578
|
+
topics = Topic.find_all_by_approved(true)
|
579
|
+
assert_equal 3, topics.size
|
580
|
+
assert topics.include?(topics(:second))
|
581
|
+
end
|
582
|
+
|
583
|
+
def test_find_by_nil_attribute
|
584
|
+
topic = Topic.find_by_last_read nil
|
585
|
+
assert_not_nil topic
|
586
|
+
assert_nil topic.last_read
|
587
|
+
end
|
588
|
+
|
589
|
+
def test_find_all_by_nil_attribute
|
590
|
+
topics = Topic.find_all_by_last_read nil
|
591
|
+
assert_equal 3, topics.size
|
592
|
+
assert topics.collect(&:last_read).all?(&:nil?)
|
593
|
+
end
|
594
|
+
|
595
|
+
def test_find_by_nil_and_not_nil_attributes
|
596
|
+
topic = Topic.find_by_last_read_and_author_name nil, "Mary"
|
597
|
+
assert_equal "Mary", topic.author_name
|
598
|
+
end
|
599
|
+
|
600
|
+
def test_find_all_by_nil_and_not_nil_attributes
|
601
|
+
topics = Topic.find_all_by_last_read_and_author_name nil, "Mary"
|
602
|
+
assert_equal 1, topics.size
|
603
|
+
assert_equal "Mary", topics[0].author_name
|
604
|
+
end
|
605
|
+
|
606
|
+
def test_find_or_create_from_one_attribute
|
607
|
+
number_of_companies = Company.count
|
608
|
+
sig38 = Company.find_or_create_by_name("38signals")
|
609
|
+
assert_equal number_of_companies + 1, Company.count
|
610
|
+
assert_equal sig38, Company.find_or_create_by_name("38signals")
|
611
|
+
assert !sig38.new_record?
|
612
|
+
end
|
613
|
+
|
614
|
+
def test_find_or_create_from_two_attributes
|
615
|
+
number_of_topics = Topic.count
|
616
|
+
another = Topic.find_or_create_by_title_and_author_name("Another topic","John")
|
617
|
+
assert_equal number_of_topics + 1, Topic.count
|
618
|
+
assert_equal another, Topic.find_or_create_by_title_and_author_name("Another topic", "John")
|
619
|
+
assert !another.new_record?
|
620
|
+
end
|
621
|
+
|
622
|
+
def test_find_or_create_from_two_attributes_with_one_being_an_aggregate
|
623
|
+
number_of_customers = Customer.count
|
624
|
+
created_customer = Customer.find_or_create_by_balance_and_name(Money.new(123), "Elizabeth")
|
625
|
+
assert_equal number_of_customers + 1, Customer.count
|
626
|
+
assert_equal created_customer, Customer.find_or_create_by_balance(Money.new(123), "Elizabeth")
|
627
|
+
assert !created_customer.new_record?
|
628
|
+
end
|
629
|
+
|
630
|
+
def test_find_or_create_from_one_attribute_and_hash
|
631
|
+
number_of_companies = Company.count
|
632
|
+
sig38 = Company.find_or_create_by_name({:name => "38signals", :firm_id => 17, :client_of => 23})
|
633
|
+
assert_equal number_of_companies + 1, Company.count
|
634
|
+
assert_equal sig38, Company.find_or_create_by_name({:name => "38signals", :firm_id => 17, :client_of => 23})
|
635
|
+
assert !sig38.new_record?
|
636
|
+
assert_equal "38signals", sig38.name
|
637
|
+
assert_equal 17, sig38.firm_id
|
638
|
+
assert_equal 23, sig38.client_of
|
639
|
+
end
|
640
|
+
|
641
|
+
def test_find_or_create_from_one_aggregate_attribute
|
642
|
+
number_of_customers = Customer.count
|
643
|
+
created_customer = Customer.find_or_create_by_balance(Money.new(123))
|
644
|
+
assert_equal number_of_customers + 1, Customer.count
|
645
|
+
assert_equal created_customer, Customer.find_or_create_by_balance(Money.new(123))
|
646
|
+
assert !created_customer.new_record?
|
647
|
+
end
|
648
|
+
|
649
|
+
def test_find_or_create_from_one_aggregate_attribute_and_hash
|
650
|
+
number_of_customers = Customer.count
|
651
|
+
balance = Money.new(123)
|
652
|
+
name = "Elizabeth"
|
653
|
+
created_customer = Customer.find_or_create_by_balance({:balance => balance, :name => name})
|
654
|
+
assert_equal number_of_customers + 1, Customer.count
|
655
|
+
assert_equal created_customer, Customer.find_or_create_by_balance({:balance => balance, :name => name})
|
656
|
+
assert !created_customer.new_record?
|
657
|
+
assert_equal balance, created_customer.balance
|
658
|
+
assert_equal name, created_customer.name
|
659
|
+
end
|
660
|
+
|
661
|
+
def test_find_or_initialize_from_one_attribute
|
662
|
+
sig38 = Company.find_or_initialize_by_name("38signals")
|
663
|
+
assert_equal "38signals", sig38.name
|
664
|
+
assert sig38.new_record?
|
665
|
+
end
|
666
|
+
|
667
|
+
def test_find_or_initialize_from_one_aggregate_attribute
|
668
|
+
new_customer = Customer.find_or_initialize_by_balance(Money.new(123))
|
669
|
+
assert_equal 123, new_customer.balance.amount
|
670
|
+
assert new_customer.new_record?
|
671
|
+
end
|
672
|
+
|
673
|
+
def test_find_or_initialize_from_one_attribute_should_not_set_attribute_even_when_protected
|
674
|
+
c = Company.find_or_initialize_by_name({:name => "Fortune 1000", :rating => 1000})
|
675
|
+
assert_equal "Fortune 1000", c.name
|
676
|
+
assert_not_equal 1000, c.rating
|
677
|
+
assert c.valid?
|
678
|
+
assert c.new_record?
|
679
|
+
end
|
680
|
+
|
681
|
+
def test_find_or_create_from_one_attribute_should_set_not_attribute_even_when_protected
|
682
|
+
c = Company.find_or_create_by_name({:name => "Fortune 1000", :rating => 1000})
|
683
|
+
assert_equal "Fortune 1000", c.name
|
684
|
+
assert_not_equal 1000, c.rating
|
685
|
+
assert c.valid?
|
686
|
+
assert !c.new_record?
|
687
|
+
end
|
688
|
+
|
689
|
+
def test_find_or_initialize_from_one_attribute_should_set_attribute_even_when_protected
|
690
|
+
c = Company.find_or_initialize_by_name_and_rating("Fortune 1000", 1000)
|
691
|
+
assert_equal "Fortune 1000", c.name
|
692
|
+
assert_equal 1000, c.rating
|
693
|
+
assert c.valid?
|
694
|
+
assert c.new_record?
|
695
|
+
end
|
696
|
+
|
697
|
+
def test_find_or_create_from_one_attribute_should_set_attribute_even_when_protected
|
698
|
+
c = Company.find_or_create_by_name_and_rating("Fortune 1000", 1000)
|
699
|
+
assert_equal "Fortune 1000", c.name
|
700
|
+
assert_equal 1000, c.rating
|
701
|
+
assert c.valid?
|
702
|
+
assert !c.new_record?
|
703
|
+
end
|
704
|
+
|
705
|
+
def test_find_or_initialize_should_set_protected_attributes_if_given_as_block
|
706
|
+
c = Company.find_or_initialize_by_name(:name => "Fortune 1000") { |f| f.rating = 1000 }
|
707
|
+
assert_equal "Fortune 1000", c.name
|
708
|
+
assert_equal 1000.to_f, c.rating.to_f
|
709
|
+
assert c.valid?
|
710
|
+
assert c.new_record?
|
711
|
+
end
|
712
|
+
|
713
|
+
def test_find_or_create_should_set_protected_attributes_if_given_as_block
|
714
|
+
c = Company.find_or_create_by_name(:name => "Fortune 1000") { |f| f.rating = 1000 }
|
715
|
+
assert_equal "Fortune 1000", c.name
|
716
|
+
assert_equal 1000.to_f, c.rating.to_f
|
717
|
+
assert c.valid?
|
718
|
+
assert !c.new_record?
|
719
|
+
end
|
720
|
+
|
721
|
+
def test_dynamic_find_or_initialize_from_one_attribute_caches_method
|
722
|
+
class << Company; self; end.send(:remove_method, :find_or_initialize_by_name) if Company.public_methods.any? { |m| m.to_s == 'find_or_initialize_by_name' }
|
723
|
+
assert !Company.public_methods.any? { |m| m.to_s == 'find_or_initialize_by_name' }
|
724
|
+
sig38 = Company.find_or_initialize_by_name("38signals")
|
725
|
+
assert Company.public_methods.any? { |m| m.to_s == 'find_or_initialize_by_name' }
|
726
|
+
end
|
727
|
+
|
728
|
+
def test_find_or_initialize_from_two_attributes
|
729
|
+
another = Topic.find_or_initialize_by_title_and_author_name("Another topic","John")
|
730
|
+
assert_equal "Another topic", another.title
|
731
|
+
assert_equal "John", another.author_name
|
732
|
+
assert another.new_record?
|
733
|
+
end
|
734
|
+
|
735
|
+
def test_find_or_initialize_from_one_aggregate_attribute_and_one_not
|
736
|
+
new_customer = Customer.find_or_initialize_by_balance_and_name(Money.new(123), "Elizabeth")
|
737
|
+
assert_equal 123, new_customer.balance.amount
|
738
|
+
assert_equal "Elizabeth", new_customer.name
|
739
|
+
assert new_customer.new_record?
|
740
|
+
end
|
741
|
+
|
742
|
+
def test_find_or_initialize_from_one_attribute_and_hash
|
743
|
+
sig38 = Company.find_or_initialize_by_name({:name => "38signals", :firm_id => 17, :client_of => 23})
|
744
|
+
assert_equal "38signals", sig38.name
|
745
|
+
assert_equal 17, sig38.firm_id
|
746
|
+
assert_equal 23, sig38.client_of
|
747
|
+
assert sig38.new_record?
|
748
|
+
end
|
749
|
+
|
750
|
+
def test_find_or_initialize_from_one_aggregate_attribute_and_hash
|
751
|
+
balance = Money.new(123)
|
752
|
+
name = "Elizabeth"
|
753
|
+
new_customer = Customer.find_or_initialize_by_balance({:balance => balance, :name => name})
|
754
|
+
assert_equal balance, new_customer.balance
|
755
|
+
assert_equal name, new_customer.name
|
756
|
+
assert new_customer.new_record?
|
757
|
+
end
|
758
|
+
|
759
|
+
def test_find_with_bad_sql
|
760
|
+
assert_raises(ActiveRecord::StatementInvalid) { Topic.find_by_sql "select 1 from badtable" }
|
761
|
+
end
|
762
|
+
|
763
|
+
def test_find_with_invalid_params
|
764
|
+
assert_raises(ArgumentError) { Topic.find :first, :join => "It should be `joins'" }
|
765
|
+
assert_raises(ArgumentError) { Topic.find :first, :conditions => '1 = 1', :join => "It should be `joins'" }
|
766
|
+
end
|
767
|
+
|
768
|
+
def test_dynamic_finder_with_invalid_params
|
769
|
+
assert_raises(ArgumentError) { Topic.find_by_title 'No Title', :join => "It should be `joins'" }
|
770
|
+
end
|
771
|
+
|
772
|
+
def test_find_all_with_limit
|
773
|
+
first_five_developers = Developer.find :all, :order => 'id ASC', :limit => 5
|
774
|
+
assert_equal 5, first_five_developers.length
|
775
|
+
assert_equal 'David', first_five_developers.first.name
|
776
|
+
assert_equal 'fixture_5', first_five_developers.last.name
|
777
|
+
|
778
|
+
no_developers = Developer.find :all, :order => 'id ASC', :limit => 0
|
779
|
+
assert_equal 0, no_developers.length
|
780
|
+
end
|
781
|
+
|
782
|
+
def test_find_all_with_limit_and_offset
|
783
|
+
first_three_developers = Developer.find :all, :order => 'id ASC', :limit => 3, :offset => 0
|
784
|
+
second_three_developers = Developer.find :all, :order => 'id ASC', :limit => 3, :offset => 3
|
785
|
+
last_two_developers = Developer.find :all, :order => 'id ASC', :limit => 2, :offset => 8
|
786
|
+
|
787
|
+
assert_equal 3, first_three_developers.length
|
788
|
+
assert_equal 3, second_three_developers.length
|
789
|
+
assert_equal 2, last_two_developers.length
|
790
|
+
|
791
|
+
assert_equal 'David', first_three_developers.first.name
|
792
|
+
assert_equal 'fixture_4', second_three_developers.first.name
|
793
|
+
assert_equal 'fixture_9', last_two_developers.first.name
|
794
|
+
end
|
795
|
+
|
796
|
+
def test_find_all_with_limit_and_offset_and_multiple_order_clauses
|
797
|
+
first_three_posts = Post.find :all, :order => 'author_id, id', :limit => 3, :offset => 0
|
798
|
+
second_three_posts = Post.find :all, :order => ' author_id,id ', :limit => 3, :offset => 3
|
799
|
+
last_posts = Post.find :all, :order => ' author_id, id ', :limit => 3, :offset => 6
|
800
|
+
|
801
|
+
assert_equal [[0,3],[1,1],[1,2]], first_three_posts.map { |p| [p.author_id, p.id] }
|
802
|
+
assert_equal [[1,4],[1,5],[1,6]], second_three_posts.map { |p| [p.author_id, p.id] }
|
803
|
+
assert_equal [[2,7]], last_posts.map { |p| [p.author_id, p.id] }
|
804
|
+
end
|
805
|
+
|
806
|
+
def test_find_all_with_join
|
807
|
+
developers_on_project_one = Developer.find(
|
808
|
+
:all,
|
809
|
+
:joins => 'LEFT JOIN developers_projects ON developers.id = developers_projects.developer_id',
|
810
|
+
:conditions => 'project_id=1'
|
811
|
+
)
|
812
|
+
assert_equal 3, developers_on_project_one.length
|
813
|
+
developer_names = developers_on_project_one.map { |d| d.name }
|
814
|
+
assert developer_names.include?('David')
|
815
|
+
assert developer_names.include?('Jamis')
|
816
|
+
end
|
817
|
+
|
818
|
+
def test_joins_dont_clobber_id
|
819
|
+
first = Firm.find(
|
820
|
+
:first,
|
821
|
+
:joins => 'INNER JOIN companies AS clients ON clients.firm_id = companies.id',
|
822
|
+
:conditions => 'companies.id = 1'
|
823
|
+
)
|
824
|
+
assert_equal 1, first.id
|
825
|
+
end
|
826
|
+
|
827
|
+
def test_find_by_id_with_conditions_with_or
|
828
|
+
assert_nothing_raised do
|
829
|
+
Post.find([1,2,3],
|
830
|
+
:conditions => "posts.id <= 3 OR posts.#{QUOTED_TYPE} = 'Post'")
|
425
831
|
end
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
832
|
+
end
|
833
|
+
|
834
|
+
# http://dev.rubyonrails.org/ticket/6778
|
835
|
+
def test_find_ignores_previously_inserted_record
|
836
|
+
post = Post.create!(:title => 'test', :body => 'it out')
|
837
|
+
assert_equal [], Post.find_all_by_id(nil)
|
838
|
+
end
|
839
|
+
|
840
|
+
def test_find_by_empty_ids
|
841
|
+
assert_equal [], Post.find([])
|
842
|
+
end
|
843
|
+
|
844
|
+
def test_find_by_empty_in_condition
|
845
|
+
assert_equal [], Post.find(:all, :conditions => ['id in (?)', []])
|
846
|
+
end
|
847
|
+
|
848
|
+
def test_find_by_records
|
849
|
+
p1, p2 = Post.find(:all, :limit => 2, :order => 'id asc')
|
850
|
+
assert_equal [p1, p2], Post.find(:all, :conditions => ['id in (?)', [p1, p2]], :order => 'id asc')
|
851
|
+
assert_equal [p1, p2], Post.find(:all, :conditions => ['id in (?)', [p1, p2.id]], :order => 'id asc')
|
852
|
+
end
|
853
|
+
|
854
|
+
def test_select_value
|
855
|
+
assert_equal "37signals", Company.connection.select_value("SELECT name FROM companies WHERE id = 1")
|
856
|
+
assert_nil Company.connection.select_value("SELECT name FROM companies WHERE id = -1")
|
857
|
+
# make sure we didn't break count...
|
858
|
+
assert_equal 0, Company.count_by_sql("SELECT COUNT(*) FROM companies WHERE name = 'Halliburton'")
|
859
|
+
assert_equal 1, Company.count_by_sql("SELECT COUNT(*) FROM companies WHERE name = '37signals'")
|
860
|
+
end
|
861
|
+
|
862
|
+
def test_select_values
|
863
|
+
assert_equal ["1","2","3","4","5","6","7","8","9"], Company.connection.select_values("SELECT id FROM companies ORDER BY id").map! { |i| i.to_s }
|
864
|
+
assert_equal ["37signals","Summit","Microsoft", "Flamboyant Software", "Ex Nihilo", "RailsCore", "Leetsoft", "Jadedpixel", "Odegy"], Company.connection.select_values("SELECT name FROM companies ORDER BY id")
|
865
|
+
end
|
866
|
+
|
867
|
+
def test_select_rows
|
868
|
+
assert_equal(
|
869
|
+
[["1", nil, nil, "37signals"],
|
870
|
+
["2", "1", "2", "Summit"],
|
871
|
+
["3", "1", "1", "Microsoft"]],
|
872
|
+
Company.connection.select_rows("SELECT id, firm_id, client_of, name FROM companies WHERE id IN (1,2,3) ORDER BY id").map! {|i| i.map! {|j| j.to_s unless j.nil?}})
|
873
|
+
assert_equal [["1", "37signals"], ["2", "Summit"], ["3", "Microsoft"]],
|
874
|
+
Company.connection.select_rows("SELECT id, name FROM companies WHERE id IN (1,2,3) ORDER BY id").map! {|i| i.map! {|j| j.to_s unless j.nil?}}
|
875
|
+
end
|
876
|
+
|
877
|
+
unless current_adapter?(:IBM_DBAdapter)
|
878
|
+
def test_find_with_order_on_included_associations_with_construct_finder_sql_for_association_limiting_and_is_distinct
|
879
|
+
assert_equal 2, Post.find(:all, :include => { :authors => :author_address }, :order => ' author_addresses.id DESC ', :limit => 2).size
|
880
|
+
|
881
|
+
assert_equal 3, Post.find(:all, :include => { :author => :author_address, :authors => :author_address},
|
882
|
+
:order => ' author_addresses_authors.id DESC ', :limit => 3).size
|
883
|
+
end
|
884
|
+
end
|
885
|
+
|
886
|
+
def test_with_limiting_with_custom_select
|
887
|
+
posts = Post.find(:all, :include => :author, :select => ' posts.*, authors.id as "author_id"', :limit => 3, :order => 'posts.id')
|
888
|
+
assert_equal 3, posts.size
|
889
|
+
assert_equal [0, 1, 1], posts.map(&:author_id).sort
|
890
|
+
end
|
891
|
+
|
892
|
+
protected
|
893
|
+
def bind(statement, *vars)
|
894
|
+
if vars.first.is_a?(Hash)
|
895
|
+
ActiveRecord::Base.send(:replace_named_bind_variables, statement, vars.first)
|
896
|
+
else
|
897
|
+
ActiveRecord::Base.send(:replace_bind_variables, statement, vars)
|
475
898
|
end
|
476
|
-
|
477
|
-
|
478
|
-
# ensure this test can run independently of order
|
479
|
-
class << Account; self; end.send(:remove_method, :find_by_credit_limit) if Account.public_methods.any? { |m| m.to_s == 'find_by_credit_limit' }
|
480
|
-
assert !Account.public_methods.any? { |m| m.to_s == 'find_by_credit_limit' }
|
481
|
-
a = Account.find_by_credit_limit(50, :conditions => ['firm_id = ?', 6])
|
482
|
-
assert Account.public_methods.any? { |m| m.to_s == 'find_by_credit_limit' }
|
483
|
-
end
|
484
|
-
|
485
|
-
def test_dynamic_finder_on_one_attribute_with_conditions_returns_same_results_after_caching
|
486
|
-
# ensure this test can run independently of order
|
487
|
-
class << Account; self; end.send(:remove_method, :find_by_credit_limit) if Account.public_methods.any? { |m| m.to_s == 'find_by_credit_limit' }
|
488
|
-
a = Account.find_by_credit_limit(50, :conditions => ['firm_id = ?', 6])
|
489
|
-
assert_equal a, Account.find_by_credit_limit(50, :conditions => ['firm_id = ?', 6]) # find_by_credit_limit has been cached
|
490
|
-
end
|
491
|
-
|
492
|
-
def test_find_by_one_attribute_with_several_options
|
493
|
-
assert_equal accounts(:unknown), Account.find_by_credit_limit(50, :order => 'id DESC', :conditions => ['id != ?', 3])
|
494
|
-
end
|
495
|
-
|
496
|
-
def test_find_by_one_missing_attribute
|
497
|
-
assert_raises(NoMethodError) { Topic.find_by_undertitle("The First Topic!") }
|
498
|
-
end
|
499
|
-
|
500
|
-
def test_find_by_invalid_method_syntax
|
501
|
-
assert_raises(NoMethodError) { Topic.fail_to_find_by_title("The First Topic") }
|
502
|
-
assert_raises(NoMethodError) { Topic.find_by_title?("The First Topic") }
|
503
|
-
assert_raises(NoMethodError) { Topic.fail_to_find_or_create_by_title("Nonexistent Title") }
|
504
|
-
assert_raises(NoMethodError) { Topic.find_or_create_by_title?("Nonexistent Title") }
|
505
|
-
end
|
506
|
-
|
507
|
-
def test_find_by_two_attributes
|
508
|
-
assert_equal topics(:first), Topic.find_by_title_and_author_name("The First Topic", "David")
|
509
|
-
assert_nil Topic.find_by_title_and_author_name("The First Topic", "Mary")
|
510
|
-
end
|
511
|
-
|
512
|
-
def test_find_all_by_one_attribute
|
513
|
-
topics = Topic.find_all_by_content("Have a nice day")
|
514
|
-
assert_equal 2, topics.size
|
515
|
-
assert topics.include?(topics(:first))
|
516
|
-
|
517
|
-
assert_equal [], Topic.find_all_by_title("The First Topic!!")
|
518
|
-
end
|
519
|
-
|
520
|
-
def test_find_all_by_one_attribute_that_is_an_aggregate
|
521
|
-
balance = customers(:david).balance
|
522
|
-
assert_kind_of Money, balance
|
523
|
-
found_customers = Customer.find_all_by_balance(balance)
|
524
|
-
assert_equal 1, found_customers.size
|
525
|
-
assert_equal customers(:david), found_customers.first
|
526
|
-
end
|
527
|
-
|
528
|
-
def test_find_all_by_two_attributes_that_are_both_aggregates
|
529
|
-
balance = customers(:david).balance
|
530
|
-
address = customers(:david).address
|
531
|
-
assert_kind_of Money, balance
|
532
|
-
assert_kind_of Address, address
|
533
|
-
found_customers = Customer.find_all_by_balance_and_address(balance, address)
|
534
|
-
assert_equal 1, found_customers.size
|
535
|
-
assert_equal customers(:david), found_customers.first
|
536
|
-
end
|
537
|
-
|
538
|
-
def test_find_all_by_two_attributes_with_one_being_an_aggregate
|
539
|
-
balance = customers(:david).balance
|
540
|
-
assert_kind_of Money, balance
|
541
|
-
found_customers = Customer.find_all_by_balance_and_name(balance, customers(:david).name)
|
542
|
-
assert_equal 1, found_customers.size
|
543
|
-
assert_equal customers(:david), found_customers.first
|
544
|
-
end
|
545
|
-
|
546
|
-
def test_find_all_by_one_attribute_with_options
|
547
|
-
topics = Topic.find_all_by_content("Have a nice day", :order => "id DESC")
|
548
|
-
assert topics(:first), topics.last
|
549
|
-
|
550
|
-
topics = Topic.find_all_by_content("Have a nice day", :order => "id")
|
551
|
-
assert topics(:first), topics.first
|
552
|
-
end
|
553
|
-
|
554
|
-
def test_find_all_by_array_attribute
|
555
|
-
assert_equal 2, Topic.find_all_by_title(["The First Topic", "The Second Topic of the day"]).size
|
556
|
-
end
|
557
|
-
|
558
|
-
def test_find_all_by_boolean_attribute
|
559
|
-
topics = Topic.find_all_by_approved(false)
|
560
|
-
assert_equal 1, topics.size
|
561
|
-
assert topics.include?(topics(:first))
|
562
|
-
|
563
|
-
topics = Topic.find_all_by_approved(true)
|
564
|
-
assert_equal 3, topics.size
|
565
|
-
assert topics.include?(topics(:second))
|
566
|
-
end
|
567
|
-
|
568
|
-
def test_find_by_nil_attribute
|
569
|
-
topic = Topic.find_by_last_read nil
|
570
|
-
assert_not_nil topic
|
571
|
-
assert_nil topic.last_read
|
572
|
-
end
|
573
|
-
|
574
|
-
def test_find_all_by_nil_attribute
|
575
|
-
topics = Topic.find_all_by_last_read nil
|
576
|
-
assert_equal 3, topics.size
|
577
|
-
assert topics.collect(&:last_read).all?(&:nil?)
|
578
|
-
end
|
579
|
-
|
580
|
-
def test_find_by_nil_and_not_nil_attributes
|
581
|
-
topic = Topic.find_by_last_read_and_author_name nil, "Mary"
|
582
|
-
assert_equal "Mary", topic.author_name
|
583
|
-
end
|
584
|
-
|
585
|
-
def test_find_all_by_nil_and_not_nil_attributes
|
586
|
-
topics = Topic.find_all_by_last_read_and_author_name nil, "Mary"
|
587
|
-
assert_equal 1, topics.size
|
588
|
-
assert_equal "Mary", topics[0].author_name
|
589
|
-
end
|
590
|
-
|
591
|
-
def test_find_or_create_from_one_attribute
|
592
|
-
number_of_companies = Company.count
|
593
|
-
sig38 = Company.find_or_create_by_name("38signals")
|
594
|
-
assert_equal number_of_companies + 1, Company.count
|
595
|
-
assert_equal sig38, Company.find_or_create_by_name("38signals")
|
596
|
-
assert !sig38.new_record?
|
597
|
-
end
|
598
|
-
|
599
|
-
def test_find_or_create_from_two_attributes
|
600
|
-
number_of_topics = Topic.count
|
601
|
-
another = Topic.find_or_create_by_title_and_author_name("Another topic","John")
|
602
|
-
assert_equal number_of_topics + 1, Topic.count
|
603
|
-
assert_equal another, Topic.find_or_create_by_title_and_author_name("Another topic", "John")
|
604
|
-
assert !another.new_record?
|
605
|
-
end
|
606
|
-
|
607
|
-
def test_find_or_create_from_two_attributes_with_one_being_an_aggregate
|
608
|
-
number_of_customers = Customer.count
|
609
|
-
created_customer = Customer.find_or_create_by_balance_and_name(Money.new(123), "Elizabeth")
|
610
|
-
assert_equal number_of_customers + 1, Customer.count
|
611
|
-
assert_equal created_customer, Customer.find_or_create_by_balance(Money.new(123), "Elizabeth")
|
612
|
-
assert !created_customer.new_record?
|
613
|
-
end
|
614
|
-
|
615
|
-
def test_find_or_create_from_one_attribute_and_hash
|
616
|
-
number_of_companies = Company.count
|
617
|
-
sig38 = Company.find_or_create_by_name({:name => "38signals", :firm_id => 17, :client_of => 23})
|
618
|
-
assert_equal number_of_companies + 1, Company.count
|
619
|
-
assert_equal sig38, Company.find_or_create_by_name({:name => "38signals", :firm_id => 17, :client_of => 23})
|
620
|
-
assert !sig38.new_record?
|
621
|
-
assert_equal "38signals", sig38.name
|
622
|
-
assert_equal 17, sig38.firm_id
|
623
|
-
assert_equal 23, sig38.client_of
|
624
|
-
end
|
625
|
-
|
626
|
-
def test_find_or_create_from_one_aggregate_attribute
|
627
|
-
number_of_customers = Customer.count
|
628
|
-
created_customer = Customer.find_or_create_by_balance(Money.new(123))
|
629
|
-
assert_equal number_of_customers + 1, Customer.count
|
630
|
-
assert_equal created_customer, Customer.find_or_create_by_balance(Money.new(123))
|
631
|
-
assert !created_customer.new_record?
|
632
|
-
end
|
633
|
-
|
634
|
-
def test_find_or_create_from_one_aggregate_attribute_and_hash
|
635
|
-
number_of_customers = Customer.count
|
636
|
-
balance = Money.new(123)
|
637
|
-
name = "Elizabeth"
|
638
|
-
created_customer = Customer.find_or_create_by_balance({:balance => balance, :name => name})
|
639
|
-
assert_equal number_of_customers + 1, Customer.count
|
640
|
-
assert_equal created_customer, Customer.find_or_create_by_balance({:balance => balance, :name => name})
|
641
|
-
assert !created_customer.new_record?
|
642
|
-
assert_equal balance, created_customer.balance
|
643
|
-
assert_equal name, created_customer.name
|
644
|
-
end
|
645
|
-
|
646
|
-
def test_find_or_initialize_from_one_attribute
|
647
|
-
sig38 = Company.find_or_initialize_by_name("38signals")
|
648
|
-
assert_equal "38signals", sig38.name
|
649
|
-
assert sig38.new_record?
|
650
|
-
end
|
651
|
-
|
652
|
-
def test_find_or_initialize_from_one_aggregate_attribute
|
653
|
-
new_customer = Customer.find_or_initialize_by_balance(Money.new(123))
|
654
|
-
assert_equal 123, new_customer.balance.amount
|
655
|
-
assert new_customer.new_record?
|
656
|
-
end
|
657
|
-
|
658
|
-
def test_find_or_initialize_from_one_attribute_should_not_set_attribute_even_when_protected
|
659
|
-
c = Company.find_or_initialize_by_name({:name => "Fortune 1000", :rating => 1000})
|
660
|
-
assert_equal "Fortune 1000", c.name
|
661
|
-
assert_not_equal 1000, c.rating
|
662
|
-
assert c.valid?
|
663
|
-
assert c.new_record?
|
664
|
-
end
|
665
|
-
|
666
|
-
def test_find_or_create_from_one_attribute_should_set_not_attribute_even_when_protected
|
667
|
-
c = Company.find_or_create_by_name({:name => "Fortune 1000", :rating => 1000})
|
668
|
-
assert_equal "Fortune 1000", c.name
|
669
|
-
assert_not_equal 1000, c.rating
|
670
|
-
assert c.valid?
|
671
|
-
assert !c.new_record?
|
672
|
-
end
|
673
|
-
|
674
|
-
def test_find_or_initialize_from_one_attribute_should_set_attribute_even_when_protected
|
675
|
-
c = Company.find_or_initialize_by_name_and_rating("Fortune 1000", 1000)
|
676
|
-
assert_equal "Fortune 1000", c.name
|
677
|
-
assert_equal 1000, c.rating
|
678
|
-
assert c.valid?
|
679
|
-
assert c.new_record?
|
680
|
-
end
|
681
|
-
|
682
|
-
def test_find_or_create_from_one_attribute_should_set_attribute_even_when_protected
|
683
|
-
c = Company.find_or_create_by_name_and_rating("Fortune 1000", 1000)
|
684
|
-
assert_equal "Fortune 1000", c.name
|
685
|
-
assert_equal 1000, c.rating
|
686
|
-
assert c.valid?
|
687
|
-
assert !c.new_record?
|
688
|
-
end
|
689
|
-
|
690
|
-
def test_find_or_initialize_should_set_protected_attributes_if_given_as_block
|
691
|
-
c = Company.find_or_initialize_by_name(:name => "Fortune 1000") { |f| f.rating = 1000 }
|
692
|
-
assert_equal "Fortune 1000", c.name
|
693
|
-
assert_equal 1000.to_f, c.rating.to_f
|
694
|
-
assert c.valid?
|
695
|
-
assert c.new_record?
|
696
|
-
end
|
697
|
-
|
698
|
-
def test_find_or_create_should_set_protected_attributes_if_given_as_block
|
699
|
-
c = Company.find_or_create_by_name(:name => "Fortune 1000") { |f| f.rating = 1000 }
|
700
|
-
assert_equal "Fortune 1000", c.name
|
701
|
-
assert_equal 1000.to_f, c.rating.to_f
|
702
|
-
assert c.valid?
|
703
|
-
assert !c.new_record?
|
704
|
-
end
|
705
|
-
|
706
|
-
def test_dynamic_find_or_initialize_from_one_attribute_caches_method
|
707
|
-
class << Company; self; end.send(:remove_method, :find_or_initialize_by_name) if Company.public_methods.any? { |m| m.to_s == 'find_or_initialize_by_name' }
|
708
|
-
assert !Company.public_methods.any? { |m| m.to_s == 'find_or_initialize_by_name' }
|
709
|
-
sig38 = Company.find_or_initialize_by_name("38signals")
|
710
|
-
assert Company.public_methods.any? { |m| m.to_s == 'find_or_initialize_by_name' }
|
711
|
-
end
|
712
|
-
|
713
|
-
def test_find_or_initialize_from_two_attributes
|
714
|
-
another = Topic.find_or_initialize_by_title_and_author_name("Another topic","John")
|
715
|
-
assert_equal "Another topic", another.title
|
716
|
-
assert_equal "John", another.author_name
|
717
|
-
assert another.new_record?
|
718
|
-
end
|
719
|
-
|
720
|
-
def test_find_or_initialize_from_one_aggregate_attribute_and_one_not
|
721
|
-
new_customer = Customer.find_or_initialize_by_balance_and_name(Money.new(123), "Elizabeth")
|
722
|
-
assert_equal 123, new_customer.balance.amount
|
723
|
-
assert_equal "Elizabeth", new_customer.name
|
724
|
-
assert new_customer.new_record?
|
725
|
-
end
|
726
|
-
|
727
|
-
def test_find_or_initialize_from_one_attribute_and_hash
|
728
|
-
sig38 = Company.find_or_initialize_by_name({:name => "38signals", :firm_id => 17, :client_of => 23})
|
729
|
-
assert_equal "38signals", sig38.name
|
730
|
-
assert_equal 17, sig38.firm_id
|
731
|
-
assert_equal 23, sig38.client_of
|
732
|
-
assert sig38.new_record?
|
733
|
-
end
|
734
|
-
|
735
|
-
def test_find_or_initialize_from_one_aggregate_attribute_and_hash
|
736
|
-
balance = Money.new(123)
|
737
|
-
name = "Elizabeth"
|
738
|
-
new_customer = Customer.find_or_initialize_by_balance({:balance => balance, :name => name})
|
739
|
-
assert_equal balance, new_customer.balance
|
740
|
-
assert_equal name, new_customer.name
|
741
|
-
assert new_customer.new_record?
|
742
|
-
end
|
743
|
-
|
744
|
-
def test_find_with_bad_sql
|
745
|
-
assert_raises(ActiveRecord::StatementInvalid) { Topic.find_by_sql "select 1 from badtable" }
|
746
|
-
end
|
747
|
-
|
748
|
-
def test_find_with_invalid_params
|
749
|
-
assert_raises(ArgumentError) { Topic.find :first, :join => "It should be `joins'" }
|
750
|
-
assert_raises(ArgumentError) { Topic.find :first, :conditions => '1 = 1', :join => "It should be `joins'" }
|
751
|
-
end
|
752
|
-
|
753
|
-
def test_dynamic_finder_with_invalid_params
|
754
|
-
assert_raises(ArgumentError) { Topic.find_by_title 'No Title', :join => "It should be `joins'" }
|
755
|
-
end
|
756
|
-
|
757
|
-
def test_find_all_with_limit
|
758
|
-
first_five_developers = Developer.find :all, :order => 'id ASC', :limit => 5
|
759
|
-
assert_equal 5, first_five_developers.length
|
760
|
-
assert_equal 'David', first_five_developers.first.name
|
761
|
-
assert_equal 'fixture_5', first_five_developers.last.name
|
762
|
-
|
763
|
-
no_developers = Developer.find :all, :order => 'id ASC', :limit => 0
|
764
|
-
assert_equal 0, no_developers.length
|
765
|
-
end
|
766
|
-
|
767
|
-
def test_find_all_with_limit_and_offset
|
768
|
-
first_three_developers = Developer.find :all, :order => 'id ASC', :limit => 3, :offset => 0
|
769
|
-
second_three_developers = Developer.find :all, :order => 'id ASC', :limit => 3, :offset => 3
|
770
|
-
last_two_developers = Developer.find :all, :order => 'id ASC', :limit => 2, :offset => 8
|
771
|
-
|
772
|
-
assert_equal 3, first_three_developers.length
|
773
|
-
assert_equal 3, second_three_developers.length
|
774
|
-
assert_equal 2, last_two_developers.length
|
775
|
-
|
776
|
-
assert_equal 'David', first_three_developers.first.name
|
777
|
-
assert_equal 'fixture_4', second_three_developers.first.name
|
778
|
-
assert_equal 'fixture_9', last_two_developers.first.name
|
779
|
-
end
|
780
|
-
|
781
|
-
def test_find_all_with_limit_and_offset_and_multiple_order_clauses
|
782
|
-
first_three_posts = Post.find :all, :order => 'author_id, id', :limit => 3, :offset => 0
|
783
|
-
second_three_posts = Post.find :all, :order => ' author_id,id ', :limit => 3, :offset => 3
|
784
|
-
last_posts = Post.find :all, :order => ' author_id, id ', :limit => 3, :offset => 6
|
785
|
-
|
786
|
-
assert_equal [[0,3],[1,1],[1,2]], first_three_posts.map { |p| [p.author_id, p.id] }
|
787
|
-
assert_equal [[1,4],[1,5],[1,6]], second_three_posts.map { |p| [p.author_id, p.id] }
|
788
|
-
assert_equal [[2,7]], last_posts.map { |p| [p.author_id, p.id] }
|
789
|
-
end
|
790
|
-
|
791
|
-
def test_find_all_with_join
|
792
|
-
developers_on_project_one = Developer.find(
|
793
|
-
:all,
|
794
|
-
:joins => 'LEFT JOIN developers_projects ON developers.id = developers_projects.developer_id',
|
795
|
-
:conditions => 'project_id=1'
|
796
|
-
)
|
797
|
-
assert_equal 3, developers_on_project_one.length
|
798
|
-
developer_names = developers_on_project_one.map { |d| d.name }
|
799
|
-
assert developer_names.include?('David')
|
800
|
-
assert developer_names.include?('Jamis')
|
801
|
-
end
|
802
|
-
|
803
|
-
def test_joins_dont_clobber_id
|
804
|
-
first = Firm.find(
|
805
|
-
:first,
|
806
|
-
:joins => 'INNER JOIN companies AS clients ON clients.firm_id = companies.id',
|
807
|
-
:conditions => 'companies.id = 1'
|
808
|
-
)
|
809
|
-
assert_equal 1, first.id
|
810
|
-
end
|
811
|
-
|
812
|
-
def test_find_by_id_with_conditions_with_or
|
813
|
-
assert_nothing_raised do
|
814
|
-
Post.find([1,2,3],
|
815
|
-
:conditions => "posts.id <= 3 OR posts.#{QUOTED_TYPE} = 'Post'")
|
816
|
-
end
|
817
|
-
end
|
818
|
-
|
819
|
-
# http://dev.rubyonrails.org/ticket/6778
|
820
|
-
def test_find_ignores_previously_inserted_record
|
821
|
-
post = Post.create!(:title => 'test', :body => 'it out')
|
822
|
-
assert_equal [], Post.find_all_by_id(nil)
|
823
|
-
end
|
824
|
-
|
825
|
-
def test_find_by_empty_ids
|
826
|
-
assert_equal [], Post.find([])
|
827
|
-
end
|
828
|
-
|
829
|
-
def test_find_by_empty_in_condition
|
830
|
-
assert_equal [], Post.find(:all, :conditions => ['id in (?)', []])
|
831
|
-
end
|
832
|
-
|
833
|
-
def test_find_by_records
|
834
|
-
p1, p2 = Post.find(:all, :limit => 2, :order => 'id asc')
|
835
|
-
assert_equal [p1, p2], Post.find(:all, :conditions => ['id in (?)', [p1, p2]], :order => 'id asc')
|
836
|
-
assert_equal [p1, p2], Post.find(:all, :conditions => ['id in (?)', [p1, p2.id]], :order => 'id asc')
|
837
|
-
end
|
838
|
-
|
839
|
-
def test_select_value
|
840
|
-
assert_equal "37signals", Company.connection.select_value("SELECT name FROM companies WHERE id = 1")
|
841
|
-
assert_nil Company.connection.select_value("SELECT name FROM companies WHERE id = -1")
|
842
|
-
# make sure we didn't break count...
|
843
|
-
assert_equal 0, Company.count_by_sql("SELECT COUNT(*) FROM companies WHERE name = 'Halliburton'")
|
844
|
-
assert_equal 1, Company.count_by_sql("SELECT COUNT(*) FROM companies WHERE name = '37signals'")
|
845
|
-
end
|
846
|
-
|
847
|
-
def test_select_values
|
848
|
-
assert_equal ["1","2","3","4","5","6","7","8","9"], Company.connection.select_values("SELECT id FROM companies ORDER BY id").map! { |i| i.to_s }
|
849
|
-
assert_equal ["37signals","Summit","Microsoft", "Flamboyant Software", "Ex Nihilo", "RailsCore", "Leetsoft", "Jadedpixel", "Odegy"], Company.connection.select_values("SELECT name FROM companies ORDER BY id")
|
850
|
-
end
|
851
|
-
|
852
|
-
def test_select_rows
|
853
|
-
assert_equal(
|
854
|
-
[["1", nil, nil, "37signals"],
|
855
|
-
["2", "1", "2", "Summit"],
|
856
|
-
["3", "1", "1", "Microsoft"]],
|
857
|
-
Company.connection.select_rows("SELECT id, firm_id, client_of, name FROM companies WHERE id IN (1,2,3) ORDER BY id").map! {|i| i.map! {|j| j.to_s unless j.nil?}})
|
858
|
-
assert_equal [["1", "37signals"], ["2", "Summit"], ["3", "Microsoft"]],
|
859
|
-
Company.connection.select_rows("SELECT id, name FROM companies WHERE id IN (1,2,3) ORDER BY id").map! {|i| i.map! {|j| j.to_s unless j.nil?}}
|
860
|
-
end
|
861
|
-
|
862
|
-
unless current_adapter?(:IBM_DBAdapter)
|
863
|
-
def test_find_with_order_on_included_associations_with_construct_finder_sql_for_association_limiting_and_is_distinct
|
864
|
-
assert_equal 2, Post.find(:all, :include => { :authors => :author_address }, :order => ' author_addresses.id DESC ', :limit => 2).size
|
865
|
-
|
866
|
-
assert_equal 3, Post.find(:all, :include => { :author => :author_address, :authors => :author_address},
|
867
|
-
:order => ' author_addresses_authors.id DESC ', :limit => 3).size
|
868
|
-
end
|
869
|
-
end
|
870
|
-
|
871
|
-
def test_with_limiting_with_custom_select
|
872
|
-
posts = Post.find(:all, :include => :author, :select => ' posts.*, authors.id as "author_id"', :limit => 3, :order => 'posts.id')
|
873
|
-
assert_equal 3, posts.size
|
874
|
-
assert_equal [0, 1, 1], posts.map(&:author_id).sort
|
875
|
-
end
|
876
|
-
|
877
|
-
protected
|
878
|
-
def bind(statement, *vars)
|
879
|
-
if vars.first.is_a?(Hash)
|
880
|
-
ActiveRecord::Base.send(:replace_named_bind_variables, statement, vars.first)
|
881
|
-
else
|
882
|
-
ActiveRecord::Base.send(:replace_bind_variables, statement, vars)
|
883
|
-
end
|
884
|
-
end
|
885
|
-
end
|
899
|
+
end
|
900
|
+
end
|