aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/deprecated_associations_test.rb
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2006-09-15 07:02:05 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2006-09-15 07:02:05 +0000
commit3f1acf49bd4cfa902388f000ca96c848c3666017 (patch)
treeaf197c1f4ea786e76da1f0679225572ea1953a58 /activerecord/test/deprecated_associations_test.rb
parentd0c000ab9e90be4896744cb6ec107dffa573f2dd (diff)
downloadrails-3f1acf49bd4cfa902388f000ca96c848c3666017.tar.gz
rails-3f1acf49bd4cfa902388f000ca96c848c3666017.tar.bz2
rails-3f1acf49bd4cfa902388f000ca96c848c3666017.zip
Deprecation tests. Remove warnings for dynamic finders and for the foo_count ethod if it's also an attribute.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5116 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/test/deprecated_associations_test.rb')
-rwxr-xr-xactiverecord/test/deprecated_associations_test.rb260
1 files changed, 153 insertions, 107 deletions
diff --git a/activerecord/test/deprecated_associations_test.rb b/activerecord/test/deprecated_associations_test.rb
index d3abe145ed..516076f570 100755
--- a/activerecord/test/deprecated_associations_test.rb
+++ b/activerecord/test/deprecated_associations_test.rb
@@ -15,173 +15,215 @@ end
raise "ActiveRecord should have barked on bad collection keys" unless bad_collection_keys
+class DeprecatedAssociationWarningsTest < Test::Unit::TestCase
+ def test_deprecation_warnings
+ assert_deprecated('find_first') { Firm.find_first }
+ assert_deprecated('find_all') { Firm.find_all }
+ assert_deprecated('has_account?') { Firm.find(:first).has_account? }
+ assert_deprecated('has_clients?') { Firm.find(:first).has_clients? }
+ end
+end
+
class DeprecatedAssociationsTest < Test::Unit::TestCase
+ #include SilenceDeprecationWarnings
+
fixtures :accounts, :companies, :developers, :projects, :topics,
:developers_projects
+ def setup
+ @firm = companies(:first_firm)
+ end
+
def test_has_many_find
- assert_equal 2, Firm.find_first.clients.length
+ assert_equal 2, @firm.clients.length
end
def test_has_many_orders
- assert_equal "Summit", Firm.find_first.clients.first.name
+ assert_equal "Summit", @firm.clients.first.name
end
def test_has_many_class_name
- assert_equal "Microsoft", Firm.find_first.clients_sorted_desc.first.name
+ assert_equal "Microsoft", @firm.clients_sorted_desc.first.name
end
def test_has_many_foreign_key
- assert_equal "Microsoft", Firm.find_first.clients_of_firm.first.name
+ assert_equal "Microsoft", @firm.clients_of_firm.first.name
end
def test_has_many_conditions
- assert_equal "Microsoft", Firm.find_first.clients_like_ms.first.name
+ assert_equal "Microsoft", @firm.clients_like_ms.first.name
end
def test_has_many_sql
- firm = Firm.find_first
- assert_equal "Microsoft", firm.clients_using_sql.first.name
- assert_equal 1, firm.clients_using_sql_count
- assert_equal 1, Firm.find_first.clients_using_sql_count
+ assert_equal "Microsoft", @firm.clients_using_sql.first.name
+ assert_equal 1, @firm.clients_using_sql.count
+ assert_equal 1, @firm.clients_using_sql.count
end
def test_has_many_counter_sql
- assert_equal 1, Firm.find_first.clients_using_counter_sql_count
+ assert_equal 1, @firm.clients_using_counter_sql.count
end
def test_has_many_queries
- assert Firm.find_first.has_clients?
- firm = Firm.find_first
- assert_equal 2, firm.clients_count # tests using class count
- firm.clients
- assert firm.has_clients?
- assert_equal 2, firm.clients_count # tests using collection length
+ assert !@firm.clients.loaded?
+ assert_deprecated 'has_clients?' do
+ assert_queries(1) { assert @firm.has_clients? }
+ end
+ assert !@firm.clients.loaded?
+ assert_deprecated 'clients_count' do
+ assert_queries(1) { assert_equal 2, @firm.clients_count }
+ end
+ assert !@firm.clients.loaded?
+ assert_queries(1) { @firm.clients.size }
+ assert !@firm.clients.loaded?
+ assert_queries(0) { @firm.clients }
+ assert !@firm.clients.loaded?
+ assert_queries(1) { @firm.clients.reload }
+ assert @firm.clients.loaded?
+ assert_queries(0) { @firm.clients.size }
+ assert_queries(1) { @firm.clients.count }
end
def test_has_many_dependence
- assert_equal 3, Client.find_all.length
- Firm.find_first.destroy
- assert_equal 1, Client.find_all.length
+ count = Client.count
+ Firm.find(:first).destroy
+ assert_equal count - 2, Client.count
end
uses_transaction :test_has_many_dependence_with_transaction_support_on_failure
def test_has_many_dependence_with_transaction_support_on_failure
- assert_equal 3, Client.find_all.length
+ count = Client.count
- firm = Firm.find_first
- clients = firm.clients
+ clients = @firm.clients
clients.last.instance_eval { def before_destroy() raise "Trigger rollback" end }
- firm.destroy rescue "do nothing"
+ @firm.destroy rescue "do nothing"
- assert_equal 3, Client.find_all.length
+ assert_equal count, Client.count
end
def test_has_one_dependence
num_accounts = Account.count
- firm = Firm.find(1)
- assert firm.has_account?
- firm.destroy
+ assert_not_nil @firm.account
+ @firm.destroy
assert_equal num_accounts - 1, Account.count
end
def test_has_one_dependence_with_missing_association
Account.destroy_all
- firm = Firm.find(1)
- assert !firm.has_account?
- firm.destroy
+ assert_nil @firm.account
+ @firm.destroy
end
def test_belongs_to
- assert_equal companies(:first_firm).name, Client.find(3).firm.name
- assert Client.find(3).has_firm?, "Microsoft should have a firm"
- # assert !Company.find(1).has_firm?, "37signals shouldn't have a firm"
+ client = companies(:second_client)
+ assert_deprecated('has_firm?') do
+ assert companies(:second_client).has_firm?, "Microsoft should have a firm"
+ end
+ assert_equal companies(:first_firm), client.firm, "Microsoft should have a firm"
end
def test_belongs_to_with_different_class_name
- assert_equal Company.find(1).name, Company.find(3).firm_with_other_name.name
- assert Company.find(3).has_firm_with_other_name?, "Microsoft should have a firm"
+ assert_equal @firm, companies(:second_client).firm_with_other_name
end
def test_belongs_to_with_condition
- assert_equal Company.find(1).name, Company.find(3).firm_with_condition.name
- assert Company.find(3).has_firm_with_condition?, "Microsoft should have a firm"
+ assert_equal @firm, companies(:second_client).firm_with_condition
end
def test_belongs_to_equality
- assert Company.find(3).firm?(Company.find(1)), "Microsoft should have 37signals as firm"
- assert_raises(RuntimeError) { !Company.find(3).firm?(Company.find(3)) } # "Summit shouldn't have itself as firm"
+ assert_equal @firm, companies(:second_client).firm, 'Microsoft should have 37signals as firm'
end
def test_has_one
- assert companies(:first_firm).account?(Account.find(1))
- assert_equal Account.find(1).credit_limit, companies(:first_firm).account.credit_limit
- assert companies(:first_firm).has_account?, "37signals should have an account"
- assert Account.find(1).firm?(companies(:first_firm)), "37signals account should be able to backtrack"
- assert Account.find(1).has_firm?, "37signals account should be able to backtrack"
+ assert_equal accounts(:signals37), @firm.account
+ assert_deprecated 'has_account?' do
+ assert @firm.has_account?, "37signals should have an account"
+ end
+ assert_deprecated 'firm?' do
+ assert accounts(:signals37).firm?(@firm), "37signals account should be able to backtrack"
+ end
+ assert_deprecated 'has_firm?' do
+ assert accounts(:signals37).has_firm?, "37signals account should be able to backtrack"
+ end
- assert !Account.find(2).has_firm?, "Unknown isn't linked"
- assert !Account.find(2).firm?(companies(:first_firm)), "Unknown isn't linked"
+ assert_nil accounts(:unknown).firm, "Unknown isn't linked"
end
- def test_has_many_dependence_on_account
+ def test_has_many_dependence_on_account
num_accounts = Account.count
- companies(:first_firm).destroy
+ @firm.destroy
assert_equal num_accounts - 1, Account.count
end
def test_find_in
- assert_equal Client.find(2).name, companies(:first_firm).find_in_clients(2).name
- assert_raises(ActiveRecord::RecordNotFound) { companies(:first_firm).find_in_clients(6) }
+ assert_deprecated 'find_in_clients' do
+ assert_equal companies(:first_client), @firm.find_in_clients(2)
+ assert_raises(ActiveRecord::RecordNotFound) { @firm.find_in_clients(6) }
+ end
end
def test_force_reload
- firm = Firm.new("name" => "A New Firm, Inc")
- firm.save
- firm.clients.each {|c|} # forcing to load all clients
- assert firm.clients.empty?, "New firm shouldn't have client objects"
- assert !firm.has_clients?, "New firm shouldn't have clients"
- assert_equal 0, firm.clients_count, "New firm should have 0 clients"
-
- client = Client.new("name" => "TheClient.com", "firm_id" => firm.id)
- client.save
-
- assert firm.clients.empty?, "New firm should have cached no client objects"
- assert !firm.has_clients?, "New firm should have cached a no-clients response"
- assert_equal 0, firm.clients_count, "New firm should have cached 0 clients count"
-
- assert !firm.clients(true).empty?, "New firm should have reloaded client objects"
- assert firm.has_clients?(true), "New firm should have reloaded with a have-clients response"
- assert_equal 1, firm.clients_count(true), "New firm should have reloaded clients count"
+ ActiveSupport::Deprecation.silence do
+ firm = Firm.new("name" => "A New Firm, Inc")
+ firm.save
+ firm.clients.each {|c|} # forcing to load all clients
+ assert firm.clients.empty?, "New firm shouldn't have client objects"
+ assert !firm.has_clients?, "New firm shouldn't have clients"
+ assert_equal 0, firm.clients_count, "New firm should have 0 clients"
+
+ client = Client.new("name" => "TheClient.com", "firm_id" => firm.id)
+ client.save
+
+ assert firm.clients.empty?, "New firm should have cached no client objects"
+ assert !firm.has_clients?, "New firm should have cached a no-clients response"
+ assert_equal 0, firm.clients_count, "New firm should have cached 0 clients count"
+
+ assert !firm.clients(true).empty?, "New firm should have reloaded client objects"
+ assert firm.has_clients?(true), "New firm should have reloaded with a have-clients response"
+ assert_equal 1, firm.clients_count(true), "New firm should have reloaded clients count"
+ end
end
def test_included_in_collection
- assert companies(:first_firm).clients.include?(Client.find(2))
+ assert @firm.clients.include?(Client.find(2))
end
def test_build_to_collection
- assert_equal 1, companies(:first_firm).clients_of_firm_count
- new_client = companies(:first_firm).build_to_clients_of_firm("name" => "Another Client")
+ count = @firm.clients_of_firm.count
+ new_client = nil
+ assert_deprecated 'build_to_clients_of_firm' do
+ new_client = @firm.build_to_clients_of_firm("name" => "Another Client")
+ end
assert_equal "Another Client", new_client.name
assert new_client.save
- assert new_client.firm?(companies(:first_firm))
- assert_equal 2, companies(:first_firm).clients_of_firm_count(true)
+ assert_equal @firm, new_client.firm
+ assert_equal count + 1, @firm.clients_of_firm.count
end
def test_create_in_collection
- assert_equal companies(:first_firm).create_in_clients_of_firm("name" => "Another Client"), companies(:first_firm).clients_of_firm(true).last
+ assert_deprecated 'create_in_clients_of_firm' do
+ assert_equal @firm.create_in_clients_of_firm("name" => "Another Client"), @firm.clients_of_firm(true).last
+ end
end
def test_has_and_belongs_to_many
david = Developer.find(1)
- assert david.has_projects?
- assert_equal 2, david.projects_count
+ assert_deprecated 'has_projects?' do
+ assert david.has_projects?
+ end
+ assert_deprecated 'projects_count' do
+ assert_equal 2, david.projects_count
+ end
active_record = Project.find(1)
- assert active_record.has_developers?
- assert_equal 3, active_record.developers_count
+ assert_deprecated 'has_developers?' do
+ assert active_record.has_developers?
+ end
+ assert_deprecated 'developers_count' do
+ assert_equal 3, active_record.developers_count
+ end
assert active_record.developers.include?(david)
end
@@ -189,53 +231,59 @@ class DeprecatedAssociationsTest < Test::Unit::TestCase
david = Developer.find(1)
active_record = Project.find(1)
- david.remove_projects(active_record)
-
- assert_equal 1, david.projects_count
- assert_equal 2, active_record.developers_count
+ assert_deprecated do
+ david.remove_projects(active_record)
+ assert_equal 1, david.projects_count
+ assert_equal 2, active_record.developers_count
+ end
end
def test_has_and_belongs_to_many_zero
david = Developer.find(1)
- david.remove_projects(Project.find_all)
-
- assert_equal 0, david.projects_count
- assert !david.has_projects?
+ assert_deprecated do
+ david.remove_projects Project.find_all
+ assert_equal 0, david.projects_count
+ assert !david.has_projects?
+ end
end
def test_has_and_belongs_to_many_adding
jamis = Developer.find(2)
action_controller = Project.find(2)
- jamis.add_projects(action_controller)
-
- assert_equal 2, jamis.projects_count
- assert_equal 2, action_controller.developers_count
+ assert_deprecated do
+ jamis.add_projects(action_controller)
+ assert_equal 2, jamis.projects_count
+ assert_equal 2, action_controller.developers_count
+ end
end
def test_has_and_belongs_to_many_adding_from_the_project
jamis = Developer.find(2)
action_controller = Project.find(2)
- action_controller.add_developers(jamis)
-
- assert_equal 2, jamis.projects_count
- assert_equal 2, action_controller.developers_count
+ assert_deprecated do
+ action_controller.add_developers(jamis)
+ assert_equal 2, jamis.projects_count
+ assert_equal 2, action_controller.developers_count
+ end
end
def test_has_and_belongs_to_many_adding_a_collection
aredridel = Developer.new("name" => "Aredridel")
aredridel.save
- aredridel.add_projects([ Project.find(1), Project.find(2) ])
- assert_equal 2, aredridel.projects_count
+ assert_deprecated do
+ aredridel.add_projects([ Project.find(1), Project.find(2) ])
+ assert_equal 2, aredridel.projects_count
+ end
end
def test_belongs_to_counter
topic = Topic.create("title" => "Apple", "content" => "hello world")
assert_equal 0, topic.send(:read_attribute, "replies_count"), "No replies yet"
- reply = topic.create_in_replies("title" => "I'm saying no!", "content" => "over here")
+ reply = assert_deprecated { topic.create_in_replies("title" => "I'm saying no!", "content" => "over here") }
assert_equal 1, Topic.find(topic.id).send(:read_attribute, "replies_count"), "First reply created"
reply.destroy
@@ -312,25 +360,23 @@ class DeprecatedAssociationsTest < Test::Unit::TestCase
end
def test_has_many_find_all
- assert_equal 2, Firm.find_first.find_all_in_clients("#{QUOTED_TYPE} = 'Client'").length
- assert_equal 1, Firm.find_first.find_all_in_clients("name = 'Summit'").length
+ assert_deprecated 'find_all_in_clients' do
+ assert_equal 2, @firm.find_all_in_clients("#{QUOTED_TYPE} = 'Client'").length
+ assert_equal 1, @firm.find_all_in_clients("name = 'Summit'").length
+ end
end
def test_has_one
- assert companies(:first_firm).account?(Account.find(1))
- assert companies(:first_firm).has_account?, "37signals should have an account"
- assert Account.find(1).firm?(companies(:first_firm)), "37signals account should be able to backtrack"
- assert Account.find(1).has_firm?, "37signals account should be able to backtrack"
-
- assert !Account.find(2).has_firm?, "Unknown isn't linked"
- assert !Account.find(2).firm?(companies(:first_firm)), "Unknown isn't linked"
+ assert_equal Account.find(1), @firm.account, "37signals should have an account"
+ assert_equal @firm, Account.find(1).firm, "37signals account should be able to backtrack"
+ assert_nil Account.find(2).firm, "Unknown isn't linked"
end
def test_has_one_build
firm = Firm.new("name" => "GlobalMegaCorp")
assert firm.save
- account = firm.build_account("credit_limit" => 1000)
+ account = firm.build_account(:credit_limit => 1000)
assert account.save
assert_equal account, firm.account
end
@@ -338,7 +384,7 @@ class DeprecatedAssociationsTest < Test::Unit::TestCase
def test_has_one_failing_build_association
firm = Firm.new("name" => "GlobalMegaCorp")
firm.save
-
+
account = firm.build_account
assert !account.save
assert_equal "can't be empty", account.errors.on("credit_limit")