Changeset 5116
- Timestamp:
- 09/15/06 07:02:05 (2 years ago)
- Files:
-
- trunk/activerecord/CHANGELOG (modified) (1 diff)
- trunk/activerecord/lib/active_record/acts/tree.rb (modified) (6 diffs)
- trunk/activerecord/lib/active_record/associations.rb (modified) (3 diffs)
- trunk/activerecord/lib/active_record/associations/has_many_association.rb (modified) (2 diffs)
- trunk/activerecord/lib/active_record/base.rb (modified) (2 diffs)
- trunk/activerecord/lib/active_record/deprecated_associations.rb (modified) (10 diffs)
- trunk/activerecord/test/association_callbacks_test.rb (modified) (1 diff)
- trunk/activerecord/test/associations_test.rb (modified) (7 diffs)
- trunk/activerecord/test/base_test.rb (modified) (2 diffs)
- trunk/activerecord/test/deprecated_associations_test.rb (modified) (10 diffs)
- trunk/activerecord/test/deprecated_finder_test.rb (modified) (3 diffs)
- trunk/activerecord/test/fixtures/company_in_module.rb (modified) (1 diff)
- trunk/activerecord/test/fixtures/post.rb (modified) (1 diff)
- trunk/activerecord/test/mixin_test.rb (modified) (2 diffs)
- trunk/activerecord/test/modules_test.rb (modified) (1 diff)
- trunk/activerecord/test/multiple_db_test.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/activerecord/CHANGELOG
r5106 r5116 1 1 *SVN* 2 3 * Deprecation tests. Remove warnings for dynamic finders and for the foo_count method if it's also an attribute. [Jeremy Kemper] 2 4 3 5 * Mock Time.now for more accurate Touch mixin tests. #6213 [Dan Peterson] trunk/activerecord/lib/active_record/acts/tree.rb
r4310 r5116 3 3 module Tree #:nodoc: 4 4 def self.included(base) 5 base.extend(ClassMethods) 6 end 5 base.extend(ClassMethods) 6 end 7 7 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 9 9 # association. This act requires that you have a foreign key column, which by default is called parent_id. 10 # 10 # 11 11 # class Category < ActiveRecord::Base 12 12 # acts_as_tree :order => "name" 13 13 # end 14 # 15 # Example :14 # 15 # Example: 16 16 # root 17 # \_ child1 17 # \_ child1 18 18 # \_ subchild1 19 19 # \_ subchild2 … … 28 28 # root.children.first.children.first # => subchild1 29 29 # 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 31 31 # after specifying the act: 32 32 # * siblings : Returns all the children of the parent, excluding the current node ([ subchild2 ] when called from subchild1) … … 49 49 class_eval <<-EOV 50 50 include ActiveRecord::Acts::Tree::InstanceMethods 51 51 52 52 def self.roots 53 53 find(:all, :conditions => "#{configuration[:foreign_key]} IS NULL", :order => #{configuration[:order].nil? ? "nil" : %Q{"#{configuration[:order]}"}}) … … 67 67 def ancestors 68 68 node, nodes = self, [] 69 nodes << node = node.parent until not node.has_parent?69 nodes << node = node.parent while node.parent 70 70 nodes 71 71 end … … 73 73 def root 74 74 node = self 75 node = node.parent until not node.has_parent?75 node = node.parent while node.parent 76 76 node 77 77 end … … 82 82 83 83 def self_and_siblings 84 has_parent?? parent.children : self.class.roots84 parent ? parent.children : self.class.roots 85 85 end 86 86 end trunk/activerecord/lib/active_record/associations.rb
r5075 r5116 992 992 993 993 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 994 998 if reflection.options[:dependent] && reflection.options[:exclusively_dependent] 995 999 raise ArgumentError, ':dependent and :exclusively_dependent are mutually exclusive options. You may specify one or the other.' … … 1011 1015 1012 1016 case reflection.options[:dependent] 1013 when :destroy, true 1017 when :destroy, true 1014 1018 module_eval "before_destroy '#{reflection.name}.each { |o| o.destroy }'" 1015 1019 when :delete_all … … 1020 1024 # pass 1021 1025 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 1026 1030 def configure_dependency_for_has_one(reflection) 1027 1031 case reflection.options[:dependent] trunk/activerecord/lib/active_record/associations/has_many_association.rb
r5018 r5116 32 32 end 33 33 end 34 deprecate :find_all 34 35 35 36 # DEPRECATED. Find the first associated record. All arguments are optional. … … 37 38 find_all(conditions, orderings, 1).first 38 39 end 40 deprecate :find_first 39 41 40 42 # Count the number of associated records. All arguments are optional. trunk/activerecord/lib/active_record/base.rb
r5075 r5116 1199 1199 options = { :conditions => conditions } 1200 1200 set_readonly_option!(options) 1201 send(finder, options)1201 ActiveSupport::Deprecation.silence { send(finder, options) } 1202 1202 1203 1203 when Hash … … 1208 1208 if extra_options[:conditions] 1209 1209 with_scope(:find => { :conditions => extra_options[:conditions] }) do 1210 send(finder, finder_options)1210 ActiveSupport::Deprecation.silence { send(finder, finder_options) } 1211 1211 end 1212 1212 else 1213 send(finder, finder_options)1213 ActiveSupport::Deprecation.silence { send(finder, finder_options) } 1214 1214 end 1215 1215 1216 1216 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 1218 1220 end 1219 1221 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 5 5 module_eval <<-"end_eval", __FILE__, __LINE__ 6 6 def #{collection_name}_count(force_reload = false) 7 unless has_attribute?(:#{collection_name}_count) 8 ActiveSupport::Deprecation.warn :#{collection_name}_count 9 end 7 10 #{collection_name}.reload if force_reload 8 11 #{collection_name}.size … … 10 13 end_eval 11 14 end 12 15 13 16 def deprecated_add_association_relation(association_name)# :nodoc: 14 17 module_eval <<-"end_eval", __FILE__, __LINE__ … … 16 19 #{association_name}.concat(items) 17 20 end 21 deprecate :add_#{association_name} 18 22 end_eval 19 23 end 20 24 21 25 def deprecated_remove_association_relation(association_name)# :nodoc: 22 26 module_eval <<-"end_eval", __FILE__, __LINE__ … … 24 28 #{association_name}.delete(items) 25 29 end 30 deprecate :remove_#{association_name} 26 31 end_eval 27 32 end 28 33 29 34 def deprecated_has_collection_method(collection_name)# :nodoc: 30 35 module_eval <<-"end_eval", __FILE__, __LINE__ … … 32 37 !#{collection_name}(force_reload).empty? 33 38 end 39 deprecate :has_#{collection_name}? 34 40 end_eval 35 41 end 36 42 37 43 def deprecated_find_in_collection_method(collection_name)# :nodoc: 38 44 module_eval <<-"end_eval", __FILE__, __LINE__ … … 40 46 #{collection_name}.find(association_id) 41 47 end 48 deprecate :find_in_#{collection_name} 42 49 end_eval 43 50 end 44 51 45 52 def deprecated_find_all_in_collection_method(collection_name)# :nodoc: 46 53 module_eval <<-"end_eval", __FILE__, __LINE__ 47 54 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 49 58 end 59 deprecate :find_all_in_#{collection_name} 50 60 end_eval 51 61 end 52 62 53 63 def deprecated_collection_create_method(collection_name)# :nodoc: 54 64 module_eval <<-"end_eval", __FILE__, __LINE__ … … 56 66 #{collection_name}.create(attributes) 57 67 end 68 deprecate :create_in_#{collection_name} 58 69 end_eval 59 70 end 60 71 61 72 def deprecated_collection_build_method(collection_name)# :nodoc: 62 73 module_eval <<-"end_eval", __FILE__, __LINE__ … … 64 75 #{collection_name}.build(attributes) 65 76 end 77 deprecate :build_to_#{collection_name} 66 78 end_eval 67 79 end … … 76 88 end 77 89 end 90 deprecate :#{association_name}? 78 91 end_eval 79 92 end 80 93 81 94 def deprecated_has_association_method(association_name) # :nodoc: 82 95 module_eval <<-"end_eval", __FILE__, __LINE__ … … 84 97 !#{association_name}(force_reload).nil? 85 98 end 99 deprecate :has_#{association_name}? 86 100 end_eval 87 end 101 end 88 102 end 89 103 end trunk/activerecord/test/association_callbacks_test.rb
r2580 r5116 112 112 113 113 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 122 124 end 123 125 end trunk/activerecord/test/associations_test.rb
r5018 r5116 31 31 firm.clients.each {|c|} # forcing to load all clients 32 32 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 34 36 assert_equal 0, firm.clients.size, "New firm should have 0 clients" 35 37 … … 38 40 39 41 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 41 45 assert_equal 0, firm.clients.size, "New firm should have cached 0 clients count" 42 46 … … 481 485 482 486 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 487 493 end 488 494 489 495 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 495 503 end 496 504 497 505 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 503 513 end 504 514 505 515 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 511 525 end 512 526 … … 780 794 def test_deleting_a_item_which_is_not_in_the_collection 781 795 force_signal37_to_load_all_clients_of_firm 782 summit = Client.find_ first("name = 'Summit'")796 summit = Client.find_by_name('Summit') 783 797 companies(:first_firm).clients_of_firm.delete(summit) 784 798 assert_equal 1, companies(:first_firm).clients_of_firm.size … … 1347 1361 ac = projects(:action_controller) 1348 1362 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 1350 1366 1351 1367 assert developers(:jamis, :reload).projects.include?(ac) … … 1390 1406 now = Date.today 1391 1407 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 1393 1411 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 1395 1415 assert ken.new_record? 1396 1416 assert p.new_record? … … 1555 1575 def test_rich_association 1556 1576 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 1559 1581 assert_date_from_db Date.today, jamis.projects.select { |p| p.name == projects(:action_controller).name }.first.joined_on 1560 1582 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 569 569 end 570 570 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 597 594 end 598 595 … … 1320 1317 1321 1318 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]) 1323 1320 assert_equal "<topic>", xml.first(7) 1324 1321 assert !xml.include?(%(<title>The First Topic</title>)) 1325 1322 assert xml.include?(%(<author-name>David</author-name>)) 1326 1323 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]) 1328 1325 assert !xml.include?(%(<title>The First Topic</title>)) 1329 1326 assert !xml.include?(%(<author-name>David</author-name>)) 1330 1327 end 1331 1328 1332 1329 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) 1334 1331 assert_equal "<topic>", xml.first(7) 1335 1332 assert xml.include?(%(<replies><reply>)) trunk/activerecord/test/deprecated_associations_test.rb
r3890 r5116 16 16 17 17 18 class 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 25 end 26 18 27 class DeprecatedAssociationsTest < Test::Unit::TestCase 28 #include SilenceDeprecationWarnings 29 19 30 fixtures :accounts, :companies, :developers, :projects, :topics, 20 31 :developers_projects 21 32 33 def setup 34 @firm = companies(:first_firm) 35 end 36 22 37 def test_has_many_find 23 assert_equal 2, Firm.find_first.clients.length38 assert_equal 2, @firm.clients.length 24 39 end 25 40 26 41 def test_has_many_orders 27 assert_equal "Summit", Firm.find_first.clients.first.name42 assert_equal "Summit", @firm.clients.first.name 28 43 end 29 44 30 45 def test_has_many_class_name 31 assert_equal "Microsoft", Firm.find_first.clients_sorted_desc.first.name46 assert_equal "Microsoft", @firm.clients_sorted_desc.first.name 32 47 end 33 48 34 49 def test_has_many_foreign_key 35 assert_equal "Microsoft", Firm.find_first.clients_of_firm.first.name50 assert_equal "Microsoft", @firm.clients_of_firm.first.name 36 51 end 37 52 38 53 def test_has_many_conditions 39 assert_equal "Microsoft", Firm.find_first.clients_like_ms.first.name54 assert_equal "Microsoft", @firm.clients_like_ms.first.name 40 55 end 41 56 42 57 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 47 61 end 48 62 49 63 def test_has_many_counter_sql 50 assert_equal 1, Firm.find_first.clients_using_counter_sql_count64 assert_equal 1, @firm.clients_using_counter_sql.count 51 65 end 52 66 53 67 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 } 60 85 end 61 86 62 87 def test_has_many_dependence 63 assert_equal 3, Client.find_all.length64 Firm.find _first.destroy65 assert_equal 1, Client.find_all.length88 count = Client.count 89 Firm.find(:first).destroy 90 assert_equal count - 2, Client.count 66 91 end 67 92 68 93 uses_transaction :test_has_many_dependence_with_transaction_support_on_failure 69 94 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 74 98 clients.last.instance_eval { def before_destroy() raise "Trigger rollback" end } 75 99 76 firm.destroy rescue "do nothing"77 78 assert_equal 3, Client.find_all.length100 @firm.destroy rescue "do nothing" 101 102 assert_equal count, Client.count 79 103 end 80 104 81 105 def test_has_one_dependence 82 106 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 86 109 assert_equal num_accounts - 1, Account.count 87 110 end … … 89 112 def test_has_one_dependence_with_missing_association 90 113 Account.destroy_all 91 firm = Firm.find(1) 92 assert !firm.has_account? 93 firm.destroy 114 assert_nil @firm.account 115 @firm.destroy 94 116 end 95 117 96 118 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" 100 124 end 101 125 102 126 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 105 128 end 106 129 107 130 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 110 132 end 111 133 112 134 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' 115 136 end 116 137 117 138 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 129 154 num_accounts = Account.count 130 companies(:first_firm).destroy155 @firm.destroy 131 156 assert_equal num_accounts - 1, Account.count 132 157 end 133 158 134 159 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 137 164 end 138 165 139 166 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 157 186 end 158 187 159 188 def test_included_in_collection 160 assert companies(:first_firm).clients.include?(Client.find(2))189 assert @firm.clients.include?(Client.find(2)) 161 190 end 162 191 163 192 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 166 198 assert_equal "Another Client", new_client.name 167 199 assert new_client.save 168 200 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 171 203 end 172 204 173 205 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 175 209 end 176 210 177 211 def test_has_and_belongs_to_many 178 212 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 181 219 182 220 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 185 227 assert active_record.developers.include?(david) 186 228 end … … 190 232 active_record = Project.find(1) 191 233 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 196 239 end 197 240 198 241 def test_has_and_belongs_to_many_zero 199 242 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 204 248 end 205 249 … … 208 252 action_controller = Project.find(2) 209 253 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 214 259 end 215 260 … … 218 263 action_controller = Project.find(2) 219 264 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 224 270 end 225 271 … … 228 274 aredridel.save 229 275 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 232 280 end 233 281 … … 236 284 assert_equal 0, topic.send(:read_attribute, "replies_count"), "No replies yet" 237 285 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") } 239 287 assert_equal 1, Topic.find(topic.id).send(:read_attribute, "replies_count"), "First reply created" 240 288 … … 313 361 314 362 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 317 367 end 318 368 319 369 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" 327 373 end 328 374 … … 331 377 assert firm.save 332 378 333 account = firm.build_account( "credit_limit"=> 1000)379 account = firm.build_account(:credit_limit => 1000) 334 380 assert account.save 335 381 assert_equal account, firm.account … … 339 385 firm = Firm.new("name" => "GlobalMegaCorp") 340 386 firm.save 341 387 342 388 account = firm.build_account 343 389 assert !account.save trunk/activerecord/test/deprecated_finder_test.rb
r4291 r5116 10 10 11 11 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 16 15 end 17 16 18 17 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 23 21 end 24 22 25 23 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 28 26 end 29 27 30 28 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