Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 5116

Show
Ignore:
Timestamp:
09/15/06 07:02:05 (2 years ago)
Author:
bitsweat
Message:

Deprecation tests. Remove warnings for dynamic finders and for the foo_count ethod if it's also an attribute.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/CHANGELOG

    r5106 r5116  
    11*SVN* 
     2 
     3* Deprecation tests. Remove warnings for dynamic finders and for the foo_count method if it's also an attribute. [Jeremy Kemper] 
    24 
    35* Mock Time.now for more accurate Touch mixin tests.  #6213 [Dan Peterson] 
  • trunk/activerecord/lib/active_record/acts/tree.rb

    r4310 r5116  
    33    module Tree #:nodoc: 
    44      def self.included(base) 
    5         base.extend(ClassMethods)               
    6       end   
     5        base.extend(ClassMethods) 
     6      end 
    77 
    8       # Specify this act if you want to model a tree structure by providing a parent association and a children  
     8      # Specify this act if you want to model a tree structure by providing a parent association and a children 
    99      # association. This act requires that you have a foreign key column, which by default is called parent_id. 
    10       #  
     10      # 
    1111      #   class Category < ActiveRecord::Base 
    1212      #     acts_as_tree :order => "name" 
    1313      #   end 
    14       #    
    15       #   Example :  
     14      # 
     15      #   Example: 
    1616      #   root 
    17       #    \_ child1  
     17      #    \_ child1 
    1818      #         \_ subchild1 
    1919      #         \_ subchild2 
     
    2828      #   root.children.first.children.first # => subchild1 
    2929      # 
    30       # In addition to the parent and children associations, the following instance methods are added to the class  
     30      # In addition to the parent and children associations, the following instance methods are added to the class 
    3131      # after specifying the act: 
    3232      # * siblings          : Returns all the children of the parent, excluding the current node ([ subchild2 ] when called from subchild1) 
     
    4949          class_eval <<-EOV 
    5050            include ActiveRecord::Acts::Tree::InstanceMethods 
    51              
     51 
    5252            def self.roots 
    5353              find(:all, :conditions => "#{configuration[:foreign_key]} IS NULL", :order => #{configuration[:order].nil? ? "nil" : %Q{"#{configuration[:order]}"}}) 
     
    6767        def ancestors 
    6868          node, nodes = self, [] 
    69           nodes << node = node.parent until not node.has_parent? 
     69          nodes << node = node.parent while node.parent 
    7070          nodes 
    7171        end 
     
    7373        def root 
    7474          node = self 
    75           node = node.parent until not node.has_parent? 
     75          node = node.parent while node.parent 
    7676          node 
    7777        end 
     
    8282 
    8383        def self_and_siblings 
    84           has_parent? ? parent.children : self.class.roots 
     84          parent ? parent.children : self.class.roots 
    8585        end 
    8686      end 
  • trunk/activerecord/lib/active_record/associations.rb

    r5075 r5116  
    992992 
    993993        def configure_dependency_for_has_many(reflection) 
     994          if reflection.options[:dependent] == true 
     995            ::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) 
     996          end 
     997 
    994998          if reflection.options[:dependent] && reflection.options[:exclusively_dependent] 
    995999            raise ArgumentError, ':dependent and :exclusively_dependent are mutually exclusive options.  You may specify one or the other.' 
     
    10111015 
    10121016          case reflection.options[:dependent] 
    1013             when :destroy, true   
     1017            when :destroy, true 
    10141018              module_eval "before_destroy '#{reflection.name}.each { |o| o.destroy }'" 
    10151019            when :delete_all 
     
    10201024              # pass 
    10211025            else 
    1022               raise ArgumentError, 'The :dependent option expects either :destroy, :delete_all, or :nullify'  
    1023           end 
    1024         end 
    1025          
     1026              raise ArgumentError, 'The :dependent option expects either :destroy, :delete_all, or :nullify' 
     1027          end 
     1028        end 
     1029 
    10261030        def configure_dependency_for_has_one(reflection) 
    10271031          case reflection.options[:dependent] 
  • trunk/activerecord/lib/active_record/associations/has_many_association.rb

    r5018 r5116  
    3232        end 
    3333      end 
     34      deprecate :find_all 
    3435 
    3536      # DEPRECATED. Find the first associated record.  All arguments are optional. 
     
    3738        find_all(conditions, orderings, 1).first 
    3839      end 
     40      deprecate :find_first 
    3941 
    4042      # Count the number of associated records. All arguments are optional. 
  • trunk/activerecord/lib/active_record/base.rb

    r5075 r5116  
    11991199                options = { :conditions => conditions } 
    12001200                set_readonly_option!(options) 
    1201                 send(finder, options) 
     1201                ActiveSupport::Deprecation.silence { send(finder, options) } 
    12021202 
    12031203              when Hash 
     
    12081208                if extra_options[:conditions] 
    12091209                  with_scope(:find => { :conditions => extra_options[:conditions] }) do 
    1210                     send(finder, finder_options) 
     1210                    ActiveSupport::Deprecation.silence { send(finder, finder_options) } 
    12111211                  end 
    12121212                else 
    1213                   send(finder, finder_options) 
     1213                  ActiveSupport::Deprecation.silence { send(finder, finder_options) } 
    12141214                end 
    12151215 
    12161216              else 
    1217                 send(deprecated_finder, conditions, *arguments[attribute_names.length..-1]) # deprecated API 
     1217                ActiveSupport::Deprecation.silence do 
     1218                  send(deprecated_finder, conditions, *arguments[attribute_names.length..-1]) 
     1219                end 
    12181220            end 
    12191221          elsif match = /find_or_(initialize|create)_by_([_a-zA-Z]\w*)/.match(method_id.to_s) 
  • trunk/activerecord/lib/active_record/deprecated_associations.rb

    r1146 r5116  
    55        module_eval <<-"end_eval", __FILE__, __LINE__ 
    66          def #{collection_name}_count(force_reload = false) 
     7            unless has_attribute?(:#{collection_name}_count) 
     8              ActiveSupport::Deprecation.warn :#{collection_name}_count 
     9            end 
    710            #{collection_name}.reload if force_reload 
    811            #{collection_name}.size 
     
    1013        end_eval 
    1114      end 
    12        
     15 
    1316      def deprecated_add_association_relation(association_name)# :nodoc: 
    1417        module_eval <<-"end_eval", __FILE__, __LINE__ 
     
    1619            #{association_name}.concat(items) 
    1720          end 
     21          deprecate :add_#{association_name} 
    1822        end_eval 
    1923      end 
    20      
     24 
    2125      def deprecated_remove_association_relation(association_name)# :nodoc: 
    2226        module_eval <<-"end_eval", __FILE__, __LINE__ 
     
    2428            #{association_name}.delete(items) 
    2529          end 
     30          deprecate :remove_#{association_name} 
    2631        end_eval 
    2732      end 
    28      
     33 
    2934      def deprecated_has_collection_method(collection_name)# :nodoc: 
    3035        module_eval <<-"end_eval", __FILE__, __LINE__ 
     
    3237            !#{collection_name}(force_reload).empty? 
    3338          end 
     39          deprecate :has_#{collection_name}? 
    3440        end_eval 
    3541      end 
    36        
     42 
    3743      def deprecated_find_in_collection_method(collection_name)# :nodoc: 
    3844        module_eval <<-"end_eval", __FILE__, __LINE__ 
     
    4046            #{collection_name}.find(association_id) 
    4147          end 
     48          deprecate :find_in_#{collection_name} 
    4249        end_eval 
    4350      end 
    44        
     51 
    4552      def deprecated_find_all_in_collection_method(collection_name)# :nodoc: 
    4653        module_eval <<-"end_eval", __FILE__, __LINE__ 
    4754          def find_all_in_#{collection_name}(runtime_conditions = nil, orderings = nil, limit = nil, joins = nil) 
    48             #{collection_name}.find_all(runtime_conditions, orderings, limit, joins) 
     55            ActiveSupport::Deprecation.silence do 
     56              #{collection_name}.find_all(runtime_conditions, orderings, limit, joins) 
     57            end 
    4958          end 
     59          deprecate :find_all_in_#{collection_name} 
    5060        end_eval 
    5161      end 
    52      
     62 
    5363      def deprecated_collection_create_method(collection_name)# :nodoc: 
    5464        module_eval <<-"end_eval", __FILE__, __LINE__ 
     
    5666            #{collection_name}.create(attributes) 
    5767          end 
     68          deprecate :create_in_#{collection_name} 
    5869        end_eval 
    5970      end 
    60      
     71 
    6172      def deprecated_collection_build_method(collection_name)# :nodoc: 
    6273        module_eval <<-"end_eval", __FILE__, __LINE__ 
     
    6475            #{collection_name}.build(attributes) 
    6576          end 
     77          deprecate :build_to_#{collection_name} 
    6678        end_eval 
    6779      end 
     
    7688            end 
    7789          end 
     90          deprecate :#{association_name}? 
    7891        end_eval 
    7992      end 
    80          
     93 
    8194      def deprecated_has_association_method(association_name) # :nodoc: 
    8295        module_eval <<-"end_eval", __FILE__, __LINE__ 
     
    8497            !#{association_name}(force_reload).nil? 
    8598          end 
     99          deprecate :has_#{association_name}? 
    86100        end_eval 
    87       end       
     101      end 
    88102    end 
    89103  end 
  • trunk/activerecord/test/association_callbacks_test.rb

    r2580 r5116  
    112112   
    113113  def test_push_with_attributes 
    114     david = developers(:david) 
    115     activerecord = projects(:active_record) 
    116     assert activerecord.developers_log.empty? 
    117     activerecord.developers_with_callbacks.push_with_attributes(david, {}) 
    118     assert_equal ["before_adding#{david.id}", "after_adding#{david.id}"], activerecord.developers_log 
    119     activerecord.developers_with_callbacks.push_with_attributes(david, {}) 
    120     assert_equal ["before_adding#{david.id}", "after_adding#{david.id}", "before_adding#{david.id}",  
    121                   "after_adding#{david.id}"], activerecord.developers_log 
     114    assert_deprecated 'push_with_attributes' do 
     115      david = developers(:david) 
     116      activerecord = projects(:active_record) 
     117      assert activerecord.developers_log.empty? 
     118      activerecord.developers_with_callbacks.push_with_attributes(david, {}) 
     119      assert_equal ["before_adding#{david.id}", "after_adding#{david.id}"], activerecord.developers_log 
     120      activerecord.developers_with_callbacks.push_with_attributes(david, {}) 
     121      assert_equal ["before_adding#{david.id}", "after_adding#{david.id}", "before_adding#{david.id}",  
     122                    "after_adding#{david.id}"], activerecord.developers_log 
     123    end 
    122124  end 
    123125end 
  • trunk/activerecord/test/associations_test.rb

    r5018 r5116  
    3131    firm.clients.each {|c|} # forcing to load all clients 
    3232    assert firm.clients.empty?, "New firm shouldn't have client objects" 
    33     assert !firm.has_clients?, "New firm shouldn't have clients" 
     33    assert_deprecated do 
     34      assert !firm.has_clients?, "New firm shouldn't have clients" 
     35    end 
    3436    assert_equal 0, firm.clients.size, "New firm should have 0 clients" 
    3537 
     
    3840 
    3941    assert firm.clients.empty?, "New firm should have cached no client objects" 
    40     assert !firm.has_clients?, "New firm should have cached a no-clients response" 
     42    assert_deprecated do 
     43      assert !firm.has_clients?, "New firm should have cached a no-clients response" 
     44    end 
    4145    assert_equal 0, firm.clients.size, "New firm should have cached 0 clients count" 
    4246 
     
    481485 
    482486  def test_find_all 
    483     firm = Firm.find_first 
    484     assert_equal firm.clients, firm.clients.find_all 
    485     assert_equal 2, firm.clients.find(:all, :conditions => "#{QUOTED_TYPE} = 'Client'").length 
    486     assert_equal 1, firm.clients.find(:all, :conditions => "name = 'Summit'").length 
     487    assert_deprecated 'find_all' do 
     488      firm = Firm.find_first 
     489      assert_equal firm.clients, firm.clients.find_all 
     490      assert_equal 2, firm.clients.find(:all, :conditions => "#{QUOTED_TYPE} = 'Client'").length 
     491      assert_equal 1, firm.clients.find(:all, :conditions => "name = 'Summit'").length 
     492    end 
    487493  end 
    488494 
    489495  def test_find_all_sanitized 
    490     firm = Firm.find_first 
    491     assert_equal firm.clients.find_all("name = 'Summit'"), firm.clients.find_all(["name = '%s'", "Summit"]) 
    492     summit = firm.clients.find(:all, :conditions => "name = 'Summit'") 
    493     assert_equal summit, firm.clients.find(:all, :conditions => ["name = ?", "Summit"]) 
    494     assert_equal summit, firm.clients.find(:all, :conditions => ["name = :name", { :name => "Summit" }]) 
     496    assert_deprecated 'find_all' do 
     497      firm = Firm.find_first 
     498      assert_equal firm.clients.find_all("name = 'Summit'"), firm.clients.find_all(["name = '%s'", "Summit"]) 
     499      summit = firm.clients.find(:all, :conditions => "name = 'Summit'") 
     500      assert_equal summit, firm.clients.find(:all, :conditions => ["name = ?", "Summit"]) 
     501      assert_equal summit, firm.clients.find(:all, :conditions => ["name = :name", { :name => "Summit" }]) 
     502    end 
    495503  end 
    496504 
    497505  def test_find_first 
    498     firm = Firm.find_first 
    499     client2 = Client.find(2) 
    500     assert_equal firm.clients.first, firm.clients.find_first 
    501     assert_equal client2, firm.clients.find_first("#{QUOTED_TYPE} = 'Client'") 
    502     assert_equal client2, firm.clients.find(:first, :conditions => "#{QUOTED_TYPE} = 'Client'") 
     506    assert_deprecated 'find_first' do 
     507      firm = Firm.find_first 
     508      client2 = Client.find(2) 
     509      assert_equal firm.clients.first, firm.clients.find_first 
     510      assert_equal client2, firm.clients.find_first("#{QUOTED_TYPE} = 'Client'") 
     511      assert_equal client2, firm.clients.find(:first, :conditions => "#{QUOTED_TYPE} = 'Client'") 
     512    end 
    503513  end 
    504514 
    505515  def test_find_first_sanitized 
    506     firm = Firm.find_first 
    507     client2 = Client.find(2) 
    508     assert_equal client2, firm.clients.find_first(["#{QUOTED_TYPE} = ?", "Client"]) 
    509     assert_equal client2, firm.clients.find(:first, :conditions => ["#{QUOTED_TYPE} = ?", 'Client']) 
    510     assert_equal client2, firm.clients.find(:first, :conditions => ["#{QUOTED_TYPE} = :type", { :type => 'Client' }]) 
     516    assert_deprecated 'find_first' do 
     517      firm = Firm.find_first 
     518      client2 = Client.find(2) 
     519      assert_deprecated(/find_first/) do 
     520        assert_equal client2, firm.clients.find_first(["#{QUOTED_TYPE} = ?", "Client"]) 
     521      end 
     522      assert_equal client2, firm.clients.find(:first, :conditions => ["#{QUOTED_TYPE} = ?", 'Client']) 
     523      assert_equal client2, firm.clients.find(:first, :conditions => ["#{QUOTED_TYPE} = :type", { :type => 'Client' }]) 
     524    end 
    511525  end 
    512526 
     
    780794  def test_deleting_a_item_which_is_not_in_the_collection 
    781795    force_signal37_to_load_all_clients_of_firm 
    782     summit = Client.find_first("name = 'Summit'"
     796    summit = Client.find_by_name('Summit'
    783797    companies(:first_firm).clients_of_firm.delete(summit) 
    784798    assert_equal 1, companies(:first_firm).clients_of_firm.size 
     
    13471361    ac = projects(:action_controller) 
    13481362    assert !developers(:jamis).projects.include?(ac) 
    1349     developers(:jamis).projects.push_with_attributes(ac, :access_level => 3) 
     1363    assert_deprecated do 
     1364      developers(:jamis).projects.push_with_attributes(ac, :access_level => 3) 
     1365    end 
    13501366 
    13511367    assert developers(:jamis, :reload).projects.include?(ac) 
     
    13901406    now = Date.today 
    13911407    ken = Developer.new("name" => "Ken") 
    1392     ken.projects.push_with_attributes( Project.find(1), :joined_on => now ) 
     1408    assert_deprecated do 
     1409      ken.projects.push_with_attributes( Project.find(1), :joined_on => now ) 
     1410    end 
    13931411    p = Project.new("name" => "Foomatic") 
    1394     ken.projects.push_with_attributes( p, :joined_on => now ) 
     1412    assert_deprecated do 
     1413      ken.projects.push_with_attributes( p, :joined_on => now ) 
     1414    end 
    13951415    assert ken.new_record? 
    13961416    assert p.new_record? 
     
    15551575  def test_rich_association 
    15561576    jamis = developers(:jamis) 
    1557     jamis.projects.push_with_attributes(projects(:action_controller), :joined_on => Date.today) 
    1558      
     1577    assert_deprecated 'push_with_attributes' do 
     1578      jamis.projects.push_with_attributes(projects(:action_controller), :joined_on => Date.today) 
     1579    end 
     1580 
    15591581    assert_date_from_db Date.today, jamis.projects.select { |p| p.name == projects(:action_controller).name }.first.joined_on 
    15601582    assert_date_from_db Date.today, developers(:jamis).projects.select { |p| p.name == projects(:action_controller).name }.first.joined_on 
  • trunk/activerecord/test/base_test.rb

    r5018 r5116  
    569569  end 
    570570 
    571   def test_utc_as_time_zone 
    572     # Oracle and SQLServer do not have a TIME datatype. 
    573     return true if current_adapter?(:SQLServerAdapter, :OracleAdapter) 
    574  
    575     Topic.default_timezone = :utc 
    576     attributes = { "bonus_time" => "5:42:00AM" } 
    577     topic = Topic.find(1) 
    578     topic.attributes = attributes 
    579     assert_equal Time.utc(2000, 1, 1, 5, 42, 0), topic.bonus_time 
    580     Topic.default_timezone = :local 
    581   end 
    582  
    583   def test_utc_as_time_zone_and_new 
    584     # Oracle and SQLServer do not have a TIME datatype. 
    585     return true if current_adapter?(:SQLServerAdapter, :OracleAdapter) 
    586  
    587     Topic.default_timezone = :utc 
    588     attributes = { "bonus_time(1i)"=>"2000", 
    589                    "bonus_time(2i)"=>"1", 
    590                    "bonus_time(3i)"=>"1", 
    591                    "bonus_time(4i)"=>"10", 
    592                    "bonus_time(5i)"=>"35", 
    593                    "bonus_time(6i)"=>"50" } 
    594     topic = Topic.new(attributes) 
    595     assert_equal Time.utc(2000, 1, 1, 10, 35, 50), topic.bonus_time 
    596     Topic.default_timezone = :local 
     571  # Oracle and SQLServer do not have a TIME datatype. 
     572  unless current_adapter?(:SQLServerAdapter, :OracleAdapter) 
     573    def test_utc_as_time_zone 
     574      Topic.default_timezone = :utc 
     575      attributes = { "bonus_time" => "5:42:00AM" } 
     576      topic = Topic.find(1) 
     577      topic.attributes = attributes 
     578      assert_equal Time.utc(2000, 1, 1, 5, 42, 0), topic.bonus_time 
     579      Topic.default_timezone = :local 
     580    end 
     581 
     582    def test_utc_as_time_zone_and_new 
     583      Topic.default_timezone = :utc 
     584      attributes = { "bonus_time(1i)"=>"2000", 
     585                     "bonus_time(2i)"=>"1", 
     586                     "bonus_time(3i)"=>"1", 
     587                     "bonus_time(4i)"=>"10", 
     588                     "bonus_time(5i)"=>"35", 
     589                     "bonus_time(6i)"=>"50" } 
     590      topic = Topic.new(attributes) 
     591      assert_equal Time.utc(2000, 1, 1, 10, 35, 50), topic.bonus_time 
     592      Topic.default_timezone = :local 
     593    end 
    597594  end 
    598595 
     
    13201317 
    13211318  def test_to_xml_skipping_attributes 
    1322     xml = topics(:first).to_xml(:indent => 0, :skip_instruct => true, :except => :title
     1319    xml = topics(:first).to_xml(:indent => 0, :skip_instruct => true, :except => [:title, :replies_count]
    13231320    assert_equal "<topic>", xml.first(7) 
    13241321    assert !xml.include?(%(<title>The First Topic</title>)) 
    13251322    assert xml.include?(%(<author-name>David</author-name>))     
    13261323 
    1327     xml = topics(:first).to_xml(:indent => 0, :skip_instruct => true, :except => [ :title, :author_name ]) 
     1324    xml = topics(:first).to_xml(:indent => 0, :skip_instruct => true, :except => [:title, :author_name, :replies_count]) 
    13281325    assert !xml.include?(%(<title>The First Topic</title>)) 
    13291326    assert !xml.include?(%(<author-name>David</author-name>))     
    13301327  end 
    1331    
     1328 
    13321329  def test_to_xml_including_has_many_association 
    1333     xml = topics(:first).to_xml(:indent => 0, :skip_instruct => true, :include => :replies
     1330    xml = topics(:first).to_xml(:indent => 0, :skip_instruct => true, :include => :replies, :except => :replies_count
    13341331    assert_equal "<topic>", xml.first(7) 
    13351332    assert xml.include?(%(<replies><reply>)) 
  • trunk/activerecord/test/deprecated_associations_test.rb

    r3890 r5116  
    1616 
    1717 
     18class DeprecatedAssociationWarningsTest < Test::Unit::TestCase 
     19  def test_deprecation_warnings 
     20    assert_deprecated('find_first') { Firm.find_first } 
     21    assert_deprecated('find_all') { Firm.find_all } 
     22    assert_deprecated('has_account?') { Firm.find(:first).has_account? } 
     23    assert_deprecated('has_clients?') { Firm.find(:first).has_clients? } 
     24  end 
     25end 
     26 
    1827class DeprecatedAssociationsTest < Test::Unit::TestCase 
     28  #include SilenceDeprecationWarnings 
     29 
    1930  fixtures :accounts, :companies, :developers, :projects, :topics, 
    2031           :developers_projects 
    2132 
     33  def setup 
     34    @firm = companies(:first_firm) 
     35  end 
     36 
    2237  def test_has_many_find 
    23     assert_equal 2, Firm.find_first.clients.length 
     38    assert_equal 2, @firm.clients.length 
    2439  end 
    2540 
    2641  def test_has_many_orders 
    27     assert_equal "Summit", Firm.find_first.clients.first.name 
     42    assert_equal "Summit", @firm.clients.first.name 
    2843  end 
    2944 
    3045  def test_has_many_class_name 
    31     assert_equal "Microsoft", Firm.find_first.clients_sorted_desc.first.name 
     46    assert_equal "Microsoft", @firm.clients_sorted_desc.first.name 
    3247  end 
    3348 
    3449  def test_has_many_foreign_key 
    35     assert_equal "Microsoft", Firm.find_first.clients_of_firm.first.name 
     50    assert_equal "Microsoft", @firm.clients_of_firm.first.name 
    3651  end 
    3752 
    3853  def test_has_many_conditions 
    39     assert_equal "Microsoft", Firm.find_first.clients_like_ms.first.name 
     54    assert_equal "Microsoft", @firm.clients_like_ms.first.name 
    4055  end 
    4156 
    4257  def test_has_many_sql 
    43     firm = Firm.find_first 
    44     assert_equal "Microsoft", firm.clients_using_sql.first.name 
    45     assert_equal 1, firm.clients_using_sql_count 
    46     assert_equal 1, Firm.find_first.clients_using_sql_count 
     58    assert_equal "Microsoft", @firm.clients_using_sql.first.name 
     59    assert_equal 1, @firm.clients_using_sql.count 
     60    assert_equal 1, @firm.clients_using_sql.count 
    4761  end 
    4862 
    4963  def test_has_many_counter_sql 
    50     assert_equal 1, Firm.find_first.clients_using_counter_sql_count 
     64    assert_equal 1, @firm.clients_using_counter_sql.count 
    5165  end 
    5266 
    5367  def test_has_many_queries 
    54     assert Firm.find_first.has_clients? 
    55     firm = Firm.find_first 
    56     assert_equal 2, firm.clients_count # tests using class count 
    57     firm.clients 
    58     assert firm.has_clients? 
    59     assert_equal 2, firm.clients_count # tests using collection length 
     68    assert !@firm.clients.loaded? 
     69    assert_deprecated 'has_clients?' do 
     70      assert_queries(1) { assert @firm.has_clients? } 
     71    end 
     72    assert !@firm.clients.loaded? 
     73    assert_deprecated 'clients_count' do 
     74      assert_queries(1) { assert_equal 2, @firm.clients_count } 
     75    end 
     76    assert !@firm.clients.loaded? 
     77    assert_queries(1) { @firm.clients.size } 
     78    assert !@firm.clients.loaded? 
     79    assert_queries(0) { @firm.clients } 
     80    assert !@firm.clients.loaded? 
     81    assert_queries(1) { @firm.clients.reload } 
     82    assert @firm.clients.loaded? 
     83    assert_queries(0) { @firm.clients.size } 
     84    assert_queries(1) { @firm.clients.count } 
    6085  end 
    6186 
    6287  def test_has_many_dependence 
    63     assert_equal 3, Client.find_all.length 
    64     Firm.find_first.destroy 
    65     assert_equal 1, Client.find_all.length 
     88    count = Client.count 
     89    Firm.find(:first).destroy 
     90    assert_equal count - 2, Client.count 
    6691  end 
    6792 
    6893  uses_transaction :test_has_many_dependence_with_transaction_support_on_failure 
    6994  def test_has_many_dependence_with_transaction_support_on_failure 
    70     assert_equal 3, Client.find_all.length 
    71  
    72     firm = Firm.find_first 
    73     clients = firm.clients 
     95    count = Client.count 
     96 
     97    clients = @firm.clients 
    7498    clients.last.instance_eval { def before_destroy() raise "Trigger rollback" end } 
    7599 
    76     firm.destroy rescue "do nothing" 
    77  
    78     assert_equal 3, Client.find_all.length 
     100    @firm.destroy rescue "do nothing" 
     101 
     102    assert_equal count, Client.count 
    79103  end 
    80104 
    81105  def test_has_one_dependence 
    82106    num_accounts = Account.count 
    83     firm = Firm.find(1) 
    84     assert firm.has_account? 
    85     firm.destroy             
     107    assert_not_nil @firm.account 
     108    @firm.destroy 
    86109    assert_equal num_accounts - 1, Account.count 
    87110  end 
     
    89112  def test_has_one_dependence_with_missing_association 
    90113    Account.destroy_all 
    91     firm = Firm.find(1) 
    92     assert !firm.has_account? 
    93     firm.destroy 
     114    assert_nil @firm.account 
     115    @firm.destroy 
    94116  end 
    95117 
    96118  def test_belongs_to 
    97     assert_equal companies(:first_firm).name, Client.find(3).firm.name 
    98     assert Client.find(3).has_firm?, "Microsoft should have a firm" 
    99     # assert !Company.find(1).has_firm?, "37signals shouldn't have a firm" 
     119    client = companies(:second_client) 
     120    assert_deprecated('has_firm?') do 
     121      assert companies(:second_client).has_firm?, "Microsoft should have a firm" 
     122    end 
     123    assert_equal companies(:first_firm), client.firm, "Microsoft should have a firm" 
    100124  end 
    101125 
    102126  def test_belongs_to_with_different_class_name 
    103     assert_equal Company.find(1).name, Company.find(3).firm_with_other_name.name 
    104     assert Company.find(3).has_firm_with_other_name?, "Microsoft should have a firm" 
     127    assert_equal @firm, companies(:second_client).firm_with_other_name 
    105128  end 
    106129 
    107130  def test_belongs_to_with_condition 
    108     assert_equal Company.find(1).name, Company.find(3).firm_with_condition.name 
    109     assert Company.find(3).has_firm_with_condition?, "Microsoft should have a firm" 
     131    assert_equal @firm, companies(:second_client).firm_with_condition 
    110132  end 
    111133 
    112134  def test_belongs_to_equality 
    113     assert Company.find(3).firm?(Company.find(1)), "Microsoft should have 37signals as firm" 
    114     assert_raises(RuntimeError) { !Company.find(3).firm?(Company.find(3)) } # "Summit shouldn't have itself as firm" 
     135    assert_equal @firm, companies(:second_client).firm, 'Microsoft should have 37signals as firm' 
    115136  end 
    116137 
    117138  def test_has_one 
    118     assert companies(:first_firm).account?(Account.find(1)) 
    119     assert_equal Account.find(1).credit_limit, companies(:first_firm).account.credit_limit 
    120     assert companies(:first_firm).has_account?, "37signals should have an account" 
    121     assert Account.find(1).firm?(companies(:first_firm)), "37signals account should be able to backtrack" 
    122     assert Account.find(1).has_firm?, "37signals account should be able to backtrack" 
    123  
    124     assert !Account.find(2).has_firm?, "Unknown isn't linked" 
    125     assert !Account.find(2).firm?(companies(:first_firm)), "Unknown isn't linked" 
    126   end 
    127  
    128   def test_has_many_dependence_on_account     
     139    assert_equal accounts(:signals37), @firm.account 
     140    assert_deprecated 'has_account?' do 
     141      assert @firm.has_account?, "37signals should have an account" 
     142    end 
     143    assert_deprecated 'firm?' do 
     144      assert accounts(:signals37).firm?(@firm), "37signals account should be able to backtrack" 
     145    end 
     146    assert_deprecated 'has_firm?' do 
     147      assert accounts(:signals37).has_firm?, "37signals account should be able to backtrack" 
     148    end 
     149 
     150    assert_nil accounts(:unknown).firm, "Unknown isn't linked" 
     151  end 
     152 
     153  def test_has_many_dependence_on_account 
    129154    num_accounts = Account.count 
    130     companies(:first_firm).destroy 
     155    @firm.destroy 
    131156    assert_equal num_accounts - 1, Account.count 
    132157  end 
    133158 
    134159  def test_find_in 
    135     assert_equal Client.find(2).name, companies(:first_firm).find_in_clients(2).name 
    136     assert_raises(ActiveRecord::RecordNotFound) { companies(:first_firm).find_in_clients(6) } 
     160    assert_deprecated 'find_in_clients' do 
     161      assert_equal companies(:first_client), @firm.find_in_clients(2) 
     162      assert_raises(ActiveRecord::RecordNotFound) { @firm.find_in_clients(6) } 
     163    end 
    137164  end 
    138165 
    139166  def test_force_reload 
    140     firm = Firm.new("name" => "A New Firm, Inc") 
    141     firm.save 
    142     firm.clients.each {|c|} # forcing to load all clients 
    143     assert firm.clients.empty?, "New firm shouldn't have client objects" 
    144     assert !firm.has_clients?, "New firm shouldn't have clients" 
    145     assert_equal 0, firm.clients_count, "New firm should have 0 clients" 
    146  
    147     client = Client.new("name" => "TheClient.com", "firm_id" => firm.id) 
    148     client.save 
    149  
    150     assert firm.clients.empty?, "New firm should have cached no client objects" 
    151     assert !firm.has_clients?, "New firm should have cached a no-clients response" 
    152     assert_equal 0, firm.clients_count, "New firm should have cached 0 clients count" 
    153  
    154     assert !firm.clients(true).empty?, "New firm should have reloaded client objects" 
    155     assert firm.has_clients?(true), "New firm should have reloaded with a have-clients response" 
    156     assert_equal 1, firm.clients_count(true), "New firm should have reloaded clients count" 
     167    ActiveSupport::Deprecation.silence do 
     168      firm = Firm.new("name" => "A New Firm, Inc") 
     169      firm.save 
     170      firm.clients.each {|c|} # forcing to load all clients 
     171      assert firm.clients.empty?, "New firm shouldn't have client objects" 
     172      assert !firm.has_clients?, "New firm shouldn't have clients" 
     173      assert_equal 0, firm.clients_count, "New firm should have 0 clients" 
     174 
     175      client = Client.new("name" => "TheClient.com", "firm_id" => firm.id) 
     176      client.save 
     177 
     178      assert firm.clients.empty?, "New firm should have cached no client objects" 
     179      assert !firm.has_clients?, "New firm should have cached a no-clients response" 
     180      assert_equal 0, firm.clients_count, "New firm should have cached 0 clients count" 
     181 
     182      assert !firm.clients(true).empty?, "New firm should have reloaded client objects" 
     183      assert firm.has_clients?(true), "New firm should have reloaded with a have-clients response" 
     184      assert_equal 1, firm.clients_count(true), "New firm should have reloaded clients count" 
     185    end 
    157186  end 
    158187 
    159188  def test_included_in_collection 
    160     assert companies(:first_firm).clients.include?(Client.find(2)) 
     189    assert @firm.clients.include?(Client.find(2)) 
    161190  end 
    162191 
    163192  def test_build_to_collection 
    164     assert_equal 1, companies(:first_firm).clients_of_firm_count 
    165     new_client = companies(:first_firm).build_to_clients_of_firm("name" => "Another Client") 
     193    count = @firm.clients_of_firm.count 
     194    new_client = nil 
     195    assert_deprecated 'build_to_clients_of_firm' do 
     196      new_client = @firm.build_to_clients_of_firm("name" => "Another Client") 
     197    end 
    166198    assert_equal "Another Client", new_client.name 
    167199    assert new_client.save 
    168200 
    169     assert new_client.firm?(companies(:first_firm)) 
    170     assert_equal 2, companies(:first_firm).clients_of_firm_count(true) 
     201    assert_equal @firm, new_client.firm 
     202    assert_equal count + 1, @firm.clients_of_firm.count 
    171203  end 
    172204 
    173205  def test_create_in_collection 
    174     assert_equal companies(:first_firm).create_in_clients_of_firm("name" => "Another Client"), companies(:first_firm).clients_of_firm(true).last 
     206    assert_deprecated 'create_in_clients_of_firm' do 
     207      assert_equal @firm.create_in_clients_of_firm("name" => "Another Client"), @firm.clients_of_firm(true).last 
     208    end 
    175209  end 
    176210 
    177211  def test_has_and_belongs_to_many 
    178212    david = Developer.find(1) 
    179     assert david.has_projects? 
    180     assert_equal 2, david.projects_count 
     213    assert_deprecated 'has_projects?' do 
     214      assert david.has_projects? 
     215    end 
     216    assert_deprecated 'projects_count' do 
     217      assert_equal 2, david.projects_count 
     218    end 
    181219 
    182220    active_record = Project.find(1) 
    183     assert active_record.has_developers? 
    184     assert_equal 3, active_record.developers_count 
     221    assert_deprecated 'has_developers?' do 
     222      assert active_record.has_developers? 
     223    end 
     224    assert_deprecated 'developers_count' do 
     225      assert_equal 3, active_record.developers_count 
     226    end 
    185227    assert active_record.developers.include?(david) 
    186228  end 
     
    190232    active_record = Project.find(1) 
    191233 
    192     david.remove_projects(active_record) 
    193  
    194     assert_equal 1, david.projects_count 
    195     assert_equal 2, active_record.developers_count 
     234    assert_deprecated do 
     235      david.remove_projects(active_record) 
     236      assert_equal 1, david.projects_count 
     237      assert_equal 2, active_record.developers_count 
     238    end 
    196239  end 
    197240 
    198241  def test_has_and_belongs_to_many_zero 
    199242    david = Developer.find(1) 
    200     david.remove_projects(Project.find_all) 
    201  
    202     assert_equal 0, david.projects_count 
    203     assert !david.has_projects? 
     243    assert_deprecated do 
     244      david.remove_projects Project.find_all 
     245      assert_equal 0, david.projects_count 
     246      assert !david.has_projects? 
     247    end 
    204248  end 
    205249 
     
    208252    action_controller = Project.find(2) 
    209253 
    210     jamis.add_projects(action_controller) 
    211  
    212     assert_equal 2, jamis.projects_count 
    213     assert_equal 2, action_controller.developers_count 
     254    assert_deprecated do 
     255      jamis.add_projects(action_controller) 
     256      assert_equal 2, jamis.projects_count 
     257      assert_equal 2, action_controller.developers_count 
     258    end 
    214259  end 
    215260 
     
    218263    action_controller = Project.find(2) 
    219264 
    220     action_controller.add_developers(jamis) 
    221  
    222     assert_equal 2, jamis.projects_count 
    223     assert_equal 2, action_controller.developers_count 
     265    assert_deprecated do 
     266      action_controller.add_developers(jamis) 
     267      assert_equal 2, jamis.projects_count 
     268      assert_equal 2, action_controller.developers_count 
     269    end 
    224270  end 
    225271 
     
    228274    aredridel.save 
    229275 
    230     aredridel.add_projects([ Project.find(1), Project.find(2) ]) 
    231     assert_equal 2, aredridel.projects_count 
     276    assert_deprecated do 
     277      aredridel.add_projects([ Project.find(1), Project.find(2) ]) 
     278      assert_equal 2, aredridel.projects_count 
     279    end 
    232280  end 
    233281 
     
    236284    assert_equal 0, topic.send(:read_attribute, "replies_count"), "No replies yet" 
    237285 
    238     reply = topic.create_in_replies("title" => "I'm saying no!", "content" => "over here") 
     286    reply = assert_deprecated { topic.create_in_replies("title" => "I'm saying no!", "content" => "over here") } 
    239287    assert_equal 1, Topic.find(topic.id).send(:read_attribute, "replies_count"), "First reply created" 
    240288 
     
    313361 
    314362  def test_has_many_find_all 
    315     assert_equal 2, Firm.find_first.find_all_in_clients("#{QUOTED_TYPE} = 'Client'").length 
    316     assert_equal 1, Firm.find_first.find_all_in_clients("name = 'Summit'").length 
     363    assert_deprecated 'find_all_in_clients' do 
     364      assert_equal 2, @firm.find_all_in_clients("#{QUOTED_TYPE} = 'Client'").length 
     365      assert_equal 1, @firm.find_all_in_clients("name = 'Summit'").length 
     366    end 
    317367  end 
    318368 
    319369  def test_has_one 
    320     assert companies(:first_firm).account?(Account.find(1)) 
    321     assert companies(:first_firm).has_account?, "37signals should have an account" 
    322     assert Account.find(1).firm?(companies(:first_firm)), "37signals account should be able to backtrack" 
    323     assert Account.find(1).has_firm?, "37signals account should be able to backtrack" 
    324  
    325     assert !Account.find(2).has_firm?, "Unknown isn't linked" 
    326     assert !Account.find(2).firm?(companies(:first_firm)), "Unknown isn't linked" 
     370    assert_equal Account.find(1), @firm.account, "37signals should have an account" 
     371    assert_equal @firm, Account.find(1).firm, "37signals account should be able to backtrack" 
     372    assert_nil Account.find(2).firm, "Unknown isn't linked" 
    327373  end 
    328374 
     
    331377    assert firm.save 
    332378 
    333     account = firm.build_account("credit_limit" => 1000) 
     379    account = firm.build_account(:credit_limit => 1000) 
    334380    assert account.save 
    335381    assert_equal account, firm.account 
     
    339385    firm = Firm.new("name" => "GlobalMegaCorp") 
    340386    firm.save 
    341      
     387 
    342388    account = firm.build_account 
    343389    assert !account.save 
  • trunk/activerecord/test/deprecated_finder_test.rb

    r4291 r5116  
    1010 
    1111  def test_find_all_with_limit 
    12     entrants = Entrant.find_all nil, "id ASC", 2 
    13      
    14     assert_equal(2, entrants.size) 
    15     assert_equal(entrants(:first).name, entrants.first.name) 
     12    entrants = assert_deprecated { Entrant.find_all nil, "id ASC", 2 } 
     13    assert_equal 2, entrants.size 
     14    assert_equal entrants(:first), entrants.first 
    1615  end 
    1716 
    1817  def test_find_all_with_prepared_limit_and_offset 
    19     entrants = Entrant.find_all nil, "id ASC", [2, 1] 
    20  
    21     assert_equal(2, entrants.size) 
    22     assert_equal(entrants(:second).name, entrants.first.name) 
     18    entrants = assert_deprecated { Entrant.find_all nil, "id ASC", [2, 1] } 
     19    assert_equal 2, entrants.size 
     20    assert_equal entrants(:second), entrants.first 
    2321  end 
    2422 
    2523  def test_find_first 
    26     first = Topic.find_first "title = 'The First Topic'" 
    27     assert_equal(topics(:first).title, first.title) 
     24    first = assert_deprecated { Topic.find_first "title = 'The First Topic'" } 
     25    assert_equal topics(:first), first 
    2826  end 
    29    
     27 
    3028  def test_find_first_failing 
    31     first = Topic.find_first "title = 'The First Topic!'" 
    32     assert_nil(first) 
     29    first = assert_deprecated { Topic.find_first "title = 'The First