提交 3f1acf49 编写于 作者: J Jeremy Kemper

Deprecation tests. Remove warnings for dynamic finders and for the foo_count...

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
上级 d0c000ab
*SVN*
* Deprecation tests. Remove warnings for dynamic finders and for the foo_count method if it's also an attribute. [Jeremy Kemper]
* Mock Time.now for more accurate Touch mixin tests. #6213 [Dan Peterson]
* Improve yaml fixtures error reporting. #6205 [Bruce Williams]
......
......@@ -2,19 +2,19 @@ module ActiveRecord
module Acts #:nodoc:
module Tree #:nodoc:
def self.included(base)
base.extend(ClassMethods)
end
base.extend(ClassMethods)
end
# Specify this act if you want to model a tree structure by providing a parent association and a children
# Specify this act if you want to model a tree structure by providing a parent association and a children
# association. This act requires that you have a foreign key column, which by default is called parent_id.
#
#
# class Category < ActiveRecord::Base
# acts_as_tree :order => "name"
# end
#
# Example :
#
# Example:
# root
# \_ child1
# \_ child1
# \_ subchild1
# \_ subchild2
#
......@@ -27,7 +27,7 @@ def self.included(base)
# root.children # => [child1]
# root.children.first.children.first # => subchild1
#
# In addition to the parent and children associations, the following instance methods are added to the class
# In addition to the parent and children associations, the following instance methods are added to the class
# after specifying the act:
# * siblings : Returns all the children of the parent, excluding the current node ([ subchild2 ] when called from subchild1)
# * self_and_siblings : Returns all the children of the parent, including the current node ([ subchild1, subchild2 ] when called from subchild1)
......@@ -48,7 +48,7 @@ def acts_as_tree(options = {})
class_eval <<-EOV
include ActiveRecord::Acts::Tree::InstanceMethods
def self.roots
find(:all, :conditions => "#{configuration[:foreign_key]} IS NULL", :order => #{configuration[:order].nil? ? "nil" : %Q{"#{configuration[:order]}"}})
end
......@@ -66,13 +66,13 @@ module InstanceMethods
# subchild1.ancestors # => [child1, root]
def ancestors
node, nodes = self, []
nodes << node = node.parent until not node.has_parent?
nodes << node = node.parent while node.parent
nodes
end
def root
node = self
node = node.parent until not node.has_parent?
node = node.parent while node.parent
node
end
......@@ -81,7 +81,7 @@ def siblings
end
def self_and_siblings
has_parent? ? parent.children : self.class.roots
parent ? parent.children : self.class.roots
end
end
end
......
......@@ -991,6 +991,10 @@ def find_with_associations(options = {})
end
def configure_dependency_for_has_many(reflection)
if reflection.options[:dependent] == true
::ActiveSupport::Deprecation.warn("The :dependent => true option is deprecated and will be removed from Rails 2.0. Please use :dependent => :destroy instead. See http://www.rubyonrails.org/deprecation for details.", caller)
end
if reflection.options[:dependent] && reflection.options[:exclusively_dependent]
raise ArgumentError, ':dependent and :exclusively_dependent are mutually exclusive options. You may specify one or the other.'
end
......@@ -1010,7 +1014,7 @@ def configure_dependency_for_has_many(reflection)
end
case reflection.options[:dependent]
when :destroy, true
when :destroy, true
module_eval "before_destroy '#{reflection.name}.each { |o| o.destroy }'"
when :delete_all
module_eval "before_destroy { |record| #{reflection.class_name}.delete_all(%(#{dependent_conditions})) }"
......@@ -1019,10 +1023,10 @@ def configure_dependency_for_has_many(reflection)
when nil, false
# pass
else
raise ArgumentError, 'The :dependent option expects either :destroy, :delete_all, or :nullify'
raise ArgumentError, 'The :dependent option expects either :destroy, :delete_all, or :nullify'
end
end
def configure_dependency_for_has_one(reflection)
case reflection.options[:dependent]
when :destroy, true
......
......@@ -31,11 +31,13 @@ def find_all(runtime_conditions = nil, orderings = nil, limit = nil, joins = nil
@reflection.klass.find_all(conditions, orderings, limit, joins)
end
end
deprecate :find_all
# DEPRECATED. Find the first associated record. All arguments are optional.
def find_first(conditions = nil, orderings = nil)
find_all(conditions, orderings, 1).first
end
deprecate :find_first
# Count the number of associated records. All arguments are optional.
def count(*args)
......
......@@ -1198,7 +1198,7 @@ def method_missing(method_id, *arguments)
when nil
options = { :conditions => conditions }
set_readonly_option!(options)
send(finder, options)
ActiveSupport::Deprecation.silence { send(finder, options) }
when Hash
finder_options = extra_options.merge(:conditions => conditions)
......@@ -1207,14 +1207,16 @@ def method_missing(method_id, *arguments)
if extra_options[:conditions]
with_scope(:find => { :conditions => extra_options[:conditions] }) do
send(finder, finder_options)
ActiveSupport::Deprecation.silence { send(finder, finder_options) }
end
else
send(finder, finder_options)
ActiveSupport::Deprecation.silence { send(finder, finder_options) }
end
else
send(deprecated_finder, conditions, *arguments[attribute_names.length..-1]) # deprecated API
ActiveSupport::Deprecation.silence do
send(deprecated_finder, conditions, *arguments[attribute_names.length..-1])
end
end
elsif match = /find_or_(initialize|create)_by_([_a-zA-Z]\w*)/.match(method_id.to_s)
instantiator = determine_instantiator(match)
......
......@@ -4,65 +4,77 @@ module ClassMethods
def deprecated_collection_count_method(collection_name)# :nodoc:
module_eval <<-"end_eval", __FILE__, __LINE__
def #{collection_name}_count(force_reload = false)
unless has_attribute?(:#{collection_name}_count)
ActiveSupport::Deprecation.warn :#{collection_name}_count
end
#{collection_name}.reload if force_reload
#{collection_name}.size
end
end_eval
end
def deprecated_add_association_relation(association_name)# :nodoc:
module_eval <<-"end_eval", __FILE__, __LINE__
def add_#{association_name}(*items)
#{association_name}.concat(items)
end
deprecate :add_#{association_name}
end_eval
end
def deprecated_remove_association_relation(association_name)# :nodoc:
module_eval <<-"end_eval", __FILE__, __LINE__
def remove_#{association_name}(*items)
#{association_name}.delete(items)
end
deprecate :remove_#{association_name}
end_eval
end
def deprecated_has_collection_method(collection_name)# :nodoc:
module_eval <<-"end_eval", __FILE__, __LINE__
def has_#{collection_name}?(force_reload = false)
!#{collection_name}(force_reload).empty?
end
deprecate :has_#{collection_name}?
end_eval
end
def deprecated_find_in_collection_method(collection_name)# :nodoc:
module_eval <<-"end_eval", __FILE__, __LINE__
def find_in_#{collection_name}(association_id)
#{collection_name}.find(association_id)
end
deprecate :find_in_#{collection_name}
end_eval
end
def deprecated_find_all_in_collection_method(collection_name)# :nodoc:
module_eval <<-"end_eval", __FILE__, __LINE__
def find_all_in_#{collection_name}(runtime_conditions = nil, orderings = nil, limit = nil, joins = nil)
#{collection_name}.find_all(runtime_conditions, orderings, limit, joins)
ActiveSupport::Deprecation.silence do
#{collection_name}.find_all(runtime_conditions, orderings, limit, joins)
end
end
deprecate :find_all_in_#{collection_name}
end_eval
end
def deprecated_collection_create_method(collection_name)# :nodoc:
module_eval <<-"end_eval", __FILE__, __LINE__
def create_in_#{collection_name}(attributes = {})
#{collection_name}.create(attributes)
end
deprecate :create_in_#{collection_name}
end_eval
end
def deprecated_collection_build_method(collection_name)# :nodoc:
module_eval <<-"end_eval", __FILE__, __LINE__
def build_to_#{collection_name}(attributes = {})
#{collection_name}.build(attributes)
end
deprecate :build_to_#{collection_name}
end_eval
end
......@@ -75,16 +87,18 @@ def #{association_name}?(comparison_object, force_reload = false)
raise "Comparison object is a #{association_class_name}, should have been \#{comparison_object.class.name}"
end
end
deprecate :#{association_name}?
end_eval
end
def deprecated_has_association_method(association_name) # :nodoc:
module_eval <<-"end_eval", __FILE__, __LINE__
def has_#{association_name}?(force_reload = false)
!#{association_name}(force_reload).nil?
end
deprecate :has_#{association_name}?
end_eval
end
end
end
end
end
......@@ -111,14 +111,16 @@ def test_dont_add_if_before_callback_raises_exception
end
def test_push_with_attributes
david = developers(:david)
activerecord = projects(:active_record)
assert activerecord.developers_log.empty?
activerecord.developers_with_callbacks.push_with_attributes(david, {})
assert_equal ["before_adding#{david.id}", "after_adding#{david.id}"], activerecord.developers_log
activerecord.developers_with_callbacks.push_with_attributes(david, {})
assert_equal ["before_adding#{david.id}", "after_adding#{david.id}", "before_adding#{david.id}",
"after_adding#{david.id}"], activerecord.developers_log
assert_deprecated 'push_with_attributes' do
david = developers(:david)
activerecord = projects(:active_record)
assert activerecord.developers_log.empty?
activerecord.developers_with_callbacks.push_with_attributes(david, {})
assert_equal ["before_adding#{david.id}", "after_adding#{david.id}"], activerecord.developers_log
activerecord.developers_with_callbacks.push_with_attributes(david, {})
assert_equal ["before_adding#{david.id}", "after_adding#{david.id}", "before_adding#{david.id}",
"after_adding#{david.id}"], activerecord.developers_log
end
end
end
......@@ -30,14 +30,18 @@ def test_force_reload
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_deprecated do
assert !firm.has_clients?, "New firm shouldn't have clients"
end
assert_equal 0, firm.clients.size, "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_deprecated do
assert !firm.has_clients?, "New firm should have cached a no-clients response"
end
assert_equal 0, firm.clients.size, "New firm should have cached 0 clients count"
assert !firm.clients(true).empty?, "New firm should have reloaded client objects"
......@@ -480,34 +484,44 @@ def test_find_ids
end
def test_find_all
firm = Firm.find_first
assert_equal firm.clients, firm.clients.find_all
assert_equal 2, firm.clients.find(:all, :conditions => "#{QUOTED_TYPE} = 'Client'").length
assert_equal 1, firm.clients.find(:all, :conditions => "name = 'Summit'").length
assert_deprecated 'find_all' do
firm = Firm.find_first
assert_equal firm.clients, firm.clients.find_all
assert_equal 2, firm.clients.find(:all, :conditions => "#{QUOTED_TYPE} = 'Client'").length
assert_equal 1, firm.clients.find(:all, :conditions => "name = 'Summit'").length
end
end
def test_find_all_sanitized
firm = Firm.find_first
assert_equal firm.clients.find_all("name = 'Summit'"), firm.clients.find_all(["name = '%s'", "Summit"])
summit = firm.clients.find(:all, :conditions => "name = 'Summit'")
assert_equal summit, firm.clients.find(:all, :conditions => ["name = ?", "Summit"])
assert_equal summit, firm.clients.find(:all, :conditions => ["name = :name", { :name => "Summit" }])
assert_deprecated 'find_all' do
firm = Firm.find_first
assert_equal firm.clients.find_all("name = 'Summit'"), firm.clients.find_all(["name = '%s'", "Summit"])
summit = firm.clients.find(:all, :conditions => "name = 'Summit'")
assert_equal summit, firm.clients.find(:all, :conditions => ["name = ?", "Summit"])
assert_equal summit, firm.clients.find(:all, :conditions => ["name = :name", { :name => "Summit" }])
end
end
def test_find_first
firm = Firm.find_first
client2 = Client.find(2)
assert_equal firm.clients.first, firm.clients.find_first
assert_equal client2, firm.clients.find_first("#{QUOTED_TYPE} = 'Client'")
assert_equal client2, firm.clients.find(:first, :conditions => "#{QUOTED_TYPE} = 'Client'")
assert_deprecated 'find_first' do
firm = Firm.find_first
client2 = Client.find(2)
assert_equal firm.clients.first, firm.clients.find_first
assert_equal client2, firm.clients.find_first("#{QUOTED_TYPE} = 'Client'")
assert_equal client2, firm.clients.find(:first, :conditions => "#{QUOTED_TYPE} = 'Client'")
end
end
def test_find_first_sanitized
firm = Firm.find_first
client2 = Client.find(2)
assert_equal client2, firm.clients.find_first(["#{QUOTED_TYPE} = ?", "Client"])
assert_equal client2, firm.clients.find(:first, :conditions => ["#{QUOTED_TYPE} = ?", 'Client'])
assert_equal client2, firm.clients.find(:first, :conditions => ["#{QUOTED_TYPE} = :type", { :type => 'Client' }])
assert_deprecated 'find_first' do
firm = Firm.find_first
client2 = Client.find(2)
assert_deprecated(/find_first/) do
assert_equal client2, firm.clients.find_first(["#{QUOTED_TYPE} = ?", "Client"])
end
assert_equal client2, firm.clients.find(:first, :conditions => ["#{QUOTED_TYPE} = ?", 'Client'])
assert_equal client2, firm.clients.find(:first, :conditions => ["#{QUOTED_TYPE} = :type", { :type => 'Client' }])
end
end
def test_find_in_collection
......@@ -779,7 +793,7 @@ def test_clearing_without_initial_access
def test_deleting_a_item_which_is_not_in_the_collection
force_signal37_to_load_all_clients_of_firm
summit = Client.find_first("name = 'Summit'")
summit = Client.find_by_name('Summit')
companies(:first_firm).clients_of_firm.delete(summit)
assert_equal 1, companies(:first_firm).clients_of_firm.size
assert_equal 1, companies(:first_firm).clients_of_firm(true).size
......@@ -1346,7 +1360,9 @@ def test_adding_uses_default_values_on_join_table
def test_adding_uses_explicit_values_on_join_table
ac = projects(:action_controller)
assert !developers(:jamis).projects.include?(ac)
developers(:jamis).projects.push_with_attributes(ac, :access_level => 3)
assert_deprecated do
developers(:jamis).projects.push_with_attributes(ac, :access_level => 3)
end
assert developers(:jamis, :reload).projects.include?(ac)
project = developers(:jamis).projects.detect { |p| p == ac }
......@@ -1389,9 +1405,13 @@ def test_habtm_adding_before_save_with_join_attributes
no_of_projects = Project.count
now = Date.today
ken = Developer.new("name" => "Ken")
ken.projects.push_with_attributes( Project.find(1), :joined_on => now )
assert_deprecated do
ken.projects.push_with_attributes( Project.find(1), :joined_on => now )
end
p = Project.new("name" => "Foomatic")
ken.projects.push_with_attributes( p, :joined_on => now )
assert_deprecated do
ken.projects.push_with_attributes( p, :joined_on => now )
end
assert ken.new_record?
assert p.new_record?
assert ken.save
......@@ -1554,8 +1574,10 @@ def test_destroy_all
def test_rich_association
jamis = developers(:jamis)
jamis.projects.push_with_attributes(projects(:action_controller), :joined_on => Date.today)
assert_deprecated 'push_with_attributes' do
jamis.projects.push_with_attributes(projects(:action_controller), :joined_on => Date.today)
end
assert_date_from_db Date.today, jamis.projects.select { |p| p.name == projects(:action_controller).name }.first.joined_on
assert_date_from_db Date.today, developers(:jamis).projects.select { |p| p.name == projects(:action_controller).name }.first.joined_on
end
......
......@@ -568,32 +568,29 @@ def test_default_values
end
end
def test_utc_as_time_zone
# Oracle and SQLServer do not have a TIME datatype.
return true if current_adapter?(:SQLServerAdapter, :OracleAdapter)
Topic.default_timezone = :utc
attributes = { "bonus_time" => "5:42:00AM" }
topic = Topic.find(1)
topic.attributes = attributes
assert_equal Time.utc(2000, 1, 1, 5, 42, 0), topic.bonus_time
Topic.default_timezone = :local
end
def test_utc_as_time_zone_and_new
# Oracle and SQLServer do not have a TIME datatype.
return true if current_adapter?(:SQLServerAdapter, :OracleAdapter)
# Oracle and SQLServer do not have a TIME datatype.
unless current_adapter?(:SQLServerAdapter, :OracleAdapter)
def test_utc_as_time_zone
Topic.default_timezone = :utc
attributes = { "bonus_time" => "5:42:00AM" }
topic = Topic.find(1)
topic.attributes = attributes
assert_equal Time.utc(2000, 1, 1, 5, 42, 0), topic.bonus_time
Topic.default_timezone = :local
end
Topic.default_timezone = :utc
attributes = { "bonus_time(1i)"=>"2000",
"bonus_time(2i)"=>"1",
"bonus_time(3i)"=>"1",
"bonus_time(4i)"=>"10",
"bonus_time(5i)"=>"35",
"bonus_time(6i)"=>"50" }
topic = Topic.new(attributes)
assert_equal Time.utc(2000, 1, 1, 10, 35, 50), topic.bonus_time
Topic.default_timezone = :local
def test_utc_as_time_zone_and_new
Topic.default_timezone = :utc
attributes = { "bonus_time(1i)"=>"2000",
"bonus_time(2i)"=>"1",
"bonus_time(3i)"=>"1",
"bonus_time(4i)"=>"10",
"bonus_time(5i)"=>"35",
"bonus_time(6i)"=>"50" }
topic = Topic.new(attributes)
assert_equal Time.utc(2000, 1, 1, 10, 35, 50), topic.bonus_time
Topic.default_timezone = :local
end
end
def test_default_values_on_empty_strings
......@@ -1319,18 +1316,18 @@ def test_to_xml
end
def test_to_xml_skipping_attributes
xml = topics(:first).to_xml(:indent => 0, :skip_instruct => true, :except => :title)
xml = topics(:first).to_xml(:indent => 0, :skip_instruct => true, :except => [:title, :replies_count])
assert_equal "<topic>", xml.first(7)
assert !xml.include?(%(<title>The First Topic</title>))
assert xml.include?(%(<author-name>David</author-name>))
xml = topics(:first).to_xml(:indent => 0, :skip_instruct => true, :except => [ :title, :author_name ])
xml = topics(:first).to_xml(:indent => 0, :skip_instruct => true, :except => [:title, :author_name, :replies_count])
assert !xml.include?(%(<title>The First Topic</title>))
assert !xml.include?(%(<author-name>David</author-name>))
end
def test_to_xml_including_has_many_association
xml = topics(:first).to_xml(:indent => 0, :skip_instruct => true, :include => :replies)
xml = topics(:first).to_xml(:indent => 0, :skip_instruct => true, :include => :replies, :except => :replies_count)
assert_equal "<topic>", xml.first(7)
assert xml.include?(%(<replies><reply>))
assert xml.include?(%(<title>The Second Topic's of the day</title>))
......
......@@ -15,173 +15,215 @@ class Car < ActiveRecord::Base; has_many :wheels, :name => "wheels"; 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 @@ def test_has_and_belongs_to_many_removing
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 @@ def test_storing_in_pstore
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 @@ def test_has_one_build
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")
......
......@@ -9,72 +9,82 @@ class DeprecatedFinderTest < Test::Unit::TestCase
fixtures :companies, :topics, :entrants, :developers
def test_find_all_with_limit
entrants = Entrant.find_all nil, "id ASC", 2
assert_equal(2, entrants.size)
assert_equal(entrants(:first).name, entrants.first.name)
entrants = assert_deprecated { Entrant.find_all nil, "id ASC", 2 }
assert_equal 2, entrants.size
assert_equal entrants(:first), entrants.first
end
def test_find_all_with_prepared_limit_and_offset
entrants = Entrant.find_all nil, "id ASC", [2, 1]
assert_equal(2, entrants.size)
assert_equal(entrants(:second).name, entrants.first.name)
entrants = assert_deprecated { Entrant.find_all nil, "id ASC", [2, 1] }
assert_equal 2, entrants.size
assert_equal entrants(:second), entrants.first
end
def test_find_first
first = Topic.find_first "title = 'The First Topic'"
assert_equal(topics(:first).title, first.title)
first = assert_deprecated { Topic.find_first "title = 'The First Topic'" }
assert_equal topics(:first), first
end
def test_find_first_failing
first = Topic.find_first "title = 'The First Topic!'"
assert_nil(first)
first = assert_deprecated { Topic.find_first "title = 'The First Topic!'" }
assert_nil first
end
def test_deprecated_find_on_conditions
assert Topic.find_on_conditions(1, ["approved = ?", false])
assert_raises(ActiveRecord::RecordNotFound) { Topic.find_on_conditions(1, ["approved = ?", true]) }
assert_deprecated 'find_on_conditions' do
assert Topic.find_on_conditions(1, ["approved = ?", false])
assert_raises(ActiveRecord::RecordNotFound) { Topic.find_on_conditions(1, ["approved = ?", true]) }
end
end
def test_condition_interpolation
assert_kind_of Firm, Company.find_first(["name = '%s'", "37signals"])
assert_nil Company.find_first(["name = '%s'", "37signals!"])
assert_nil Company.find_first(["name = '%s'", "37signals!' OR 1=1"])
assert_kind_of Time, Topic.find_first(["id = %d", 1]).written_on
assert_deprecated do
assert_kind_of Firm, Company.find_first(["name = '%s'", "37signals"])
assert_nil Company.find_first(["name = '%s'", "37signals!"])
assert_nil Company.find_first(["name = '%s'", "37signals!' OR 1=1"])
assert_kind_of Time, Topic.find_first(["id = %d", 1]).written_on
end
end
def test_bind_variables
assert_kind_of Firm, Company.find_first(["name = ?", "37signals"])
assert_nil Company.find_first(["name = ?", "37signals!"])
assert_nil Company.find_first(["name = ?", "37signals!' OR 1=1"])
assert_kind_of Time, Topic.find_first(["id = ?", 1]).written_on
assert_raises(ActiveRecord::PreparedStatementInvalid) {
Company.find_first(["id=? AND name = ?", 2])
}
assert_raises(ActiveRecord::PreparedStatementInvalid) {
Company.find_first(["id=?", 2, 3, 4])
}
assert_deprecated do
assert_kind_of Firm, Company.find_first(["name = ?", "37signals"])
assert_nil Company.find_first(["name = ?", "37signals!"])
assert_nil Company.find_first(["name = ?", "37signals!' OR 1=1"])
assert_kind_of Time, Topic.find_first(["id = ?", 1]).written_on
assert_raises(ActiveRecord::PreparedStatementInvalid) {
Company.find_first(["id=? AND name = ?", 2])
}
assert_raises(ActiveRecord::PreparedStatementInvalid) {
Company.find_first(["id=?", 2, 3, 4])
}
end
end
def test_bind_variables_with_quotes
Company.create("name" => "37signals' go'es agains")
assert Company.find_first(["name = ?", "37signals' go'es agains"])
assert_deprecated do
assert_not_nil Company.find_first(["name = ?", "37signals' go'es agains"])
end
end
def test_named_bind_variables_with_quotes
Company.create("name" => "37signals' go'es agains")
assert Company.find_first(["name = :name", {:name => "37signals' go'es agains"}])
assert_deprecated do
assert_not_nil Company.find_first(["name = :name", {:name => "37signals' go'es agains"}])
end
end
def test_named_bind_variables
assert_equal '1', bind(':a', :a => 1) # ' ruby-mode
assert_equal '1 1', bind(':a :a', :a => 1) # ' ruby-mode
assert_kind_of Firm, Company.find_first(["name = :name", { :name => "37signals" }])
assert_nil Company.find_first(["name = :name", { :name => "37signals!" }])
assert_nil Company.find_first(["name = :name", { :name => "37signals!' OR 1=1" }])
assert_kind_of Time, Topic.find_first(["id = :id", { :id => 1 }]).written_on
assert_deprecated do
assert_kind_of Firm, Company.find_first(["name = :name", { :name => "37signals" }])
assert_nil Company.find_first(["name = :name", { :name => "37signals!" }])
assert_nil Company.find_first(["name = :name", { :name => "37signals!' OR 1=1" }])
assert_kind_of Time, Topic.find_first(["id = :id", { :id => 1 }]).written_on
end
end
def test_count
......@@ -90,38 +100,44 @@ def test_count_by_sql
end
def test_find_all_with_limit
first_five_developers = Developer.find_all nil, 'id ASC', 5
assert_equal 5, first_five_developers.length
assert_equal 'David', first_five_developers.first.name
assert_equal 'fixture_5', first_five_developers.last.name
no_developers = Developer.find_all nil, 'id ASC', 0
assert_equal 0, no_developers.length
assert_equal first_five_developers, Developer.find_all(nil, 'id ASC', [5])
assert_equal no_developers, Developer.find_all(nil, 'id ASC', [0])
assert_deprecated do
first_five_developers = Developer.find_all nil, 'id ASC', 5
assert_equal 5, first_five_developers.length
assert_equal 'David', first_five_developers.first.name
assert_equal 'fixture_5', first_five_developers.last.name
no_developers = Developer.find_all nil, 'id ASC', 0
assert_equal 0, no_developers.length
assert_equal first_five_developers, Developer.find_all(nil, 'id ASC', [5])
assert_equal no_developers, Developer.find_all(nil, 'id ASC', [0])
end
end
def test_find_all_with_limit_and_offset
first_three_developers = Developer.find_all nil, 'id ASC', [3, 0]
second_three_developers = Developer.find_all nil, 'id ASC', [3, 3]
last_two_developers = Developer.find_all nil, 'id ASC', [2, 8]
assert_equal 3, first_three_developers.length
assert_equal 3, second_three_developers.length
assert_equal 2, last_two_developers.length
assert_equal 'David', first_three_developers.first.name
assert_equal 'fixture_4', second_three_developers.first.name
assert_equal 'fixture_9', last_two_developers.first.name
assert_deprecated do
first_three_developers = Developer.find_all nil, 'id ASC', [3, 0]
second_three_developers = Developer.find_all nil, 'id ASC', [3, 3]
last_two_developers = Developer.find_all nil, 'id ASC', [2, 8]
assert_equal 3, first_three_developers.length
assert_equal 3, second_three_developers.length
assert_equal 2, last_two_developers.length
assert_equal 'David', first_three_developers.first.name
assert_equal 'fixture_4', second_three_developers.first.name
assert_equal 'fixture_9', last_two_developers.first.name
end
end
def test_find_all_by_one_attribute_with_options
topics = Topic.find_all_by_content("Have a nice day", "id DESC")
assert topics(:first), topics.last
assert_not_deprecated do
topics = Topic.find_all_by_content("Have a nice day", "id DESC")
assert topics(:first), topics.last
topics = Topic.find_all_by_content("Have a nice day", "id DESC")
assert topics(:first), topics.first
topics = Topic.find_all_by_content("Have a nice day", "id DESC")
assert topics(:first), topics.first
end
end
protected
......
......@@ -46,11 +46,13 @@ class Firm < ActiveRecord::Base
end
class Account < ActiveRecord::Base
belongs_to :firm, :class_name => 'MyApplication::Business::Firm'
belongs_to :qualified_billing_firm, :class_name => 'MyApplication::Billing::Firm'
belongs_to :unqualified_billing_firm, :class_name => 'Firm'
belongs_to :nested_qualified_billing_firm, :class_name => 'MyApplication::Billing::Nested::Firm'
belongs_to :nested_unqualified_billing_firm, :class_name => 'Nested::Firm'
with_options(:foreign_key => :firm_id) do |i|
i.belongs_to :firm, :class_name => 'MyApplication::Business::Firm'
i.belongs_to :qualified_billing_firm, :class_name => 'MyApplication::Billing::Firm'
i.belongs_to :unqualified_billing_firm, :class_name => 'Firm'
i.belongs_to :nested_qualified_billing_firm, :class_name => 'MyApplication::Billing::Nested::Firm'
i.belongs_to :nested_unqualified_billing_firm, :class_name => 'Nested::Firm'
end
protected
def validate
......
......@@ -5,7 +5,7 @@ def greeting
end
end
belongs_to :author_with_posts, :class_name => "Author", :include => :posts
belongs_to :author_with_posts, :class_name => "Author", :foreign_key => :author_id, :include => :posts
has_many :comments, :order => "body" do
def find_most_recent
......
......@@ -217,10 +217,12 @@ class TreeTest < Test::Unit::TestCase
fixtures :mixins
def test_has_child
assert_equal true, mixins(:tree_1).has_children?
assert_equal true, mixins(:tree_2).has_children?
assert_equal false, mixins(:tree_3).has_children?
assert_equal false, mixins(:tree_4).has_children?
assert_deprecated 'has_children?' do
assert_equal true, mixins(:tree_1).has_children?
assert_equal true, mixins(:tree_2).has_children?
assert_equal false, mixins(:tree_3).has_children?
assert_equal false, mixins(:tree_4).has_children?
end
end
def test_children
......@@ -231,10 +233,12 @@ def test_children
end
def test_has_parent
assert_equal false, mixins(:tree_1).has_parent?
assert_equal true, mixins(:tree_2).has_parent?
assert_equal true, mixins(:tree_3).has_parent?
assert_equal true, mixins(:tree_4).has_parent?
assert_deprecated 'has_parent?' do
assert_equal false, mixins(:tree_1).has_parent?
assert_equal true, mixins(:tree_2).has_parent?
assert_equal true, mixins(:tree_3).has_parent?
assert_equal true, mixins(:tree_4).has_parent?
end
end
def test_parent
......
......@@ -5,10 +5,9 @@ class ModulesTest < Test::Unit::TestCase
fixtures :accounts, :companies, :projects, :developers
def test_module_spanning_associations
assert MyApplication::Business::Firm.find(:first).has_clients?, "Firm should have clients"
firm = MyApplication::Business::Firm.find(:first)
assert !firm.clients.empty?, "Firm should have clients"
assert_nil firm.class.table_name.match('::'), "Firm shouldn't have the module appear in its table name"
assert_equal 2, firm.clients_count, "Firm should have two clients"
end
def test_module_spanning_has_and_belongs_to_many_associations
......
......@@ -39,11 +39,11 @@ def test_find
def test_associations
c1 = Course.find(1)
assert_equal 2, c1.entrants_count
assert_equal 2, c1.entrants.count
e1 = Entrant.find(1)
assert_equal e1.course.id, c1.id
c2 = Course.find(2)
assert_equal 1, c2.entrants_count
assert_equal 1, c2.entrants.count
e3 = Entrant.find(3)
assert_equal e3.course.id, c2.id
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册