From 355f41d8aafd75d76db25cdda4736e0052b0605c Mon Sep 17 00:00:00 2001 From: Frederick Cheung Date: Tue, 9 Dec 2008 20:36:24 +0000 Subject: Rework ActiveSupport::OrderedHash to make lookups faster [#1352 state:committed] Signed-off-by: Jeremy Kemper --- activerecord/test/cases/calculations_test.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb index 8bd0dd0f6e..080f6a7007 100644 --- a/activerecord/test/cases/calculations_test.rb +++ b/activerecord/test/cases/calculations_test.rb @@ -171,8 +171,9 @@ class CalculationsTest < ActiveRecord::TestCase Account.expects(:columns).at_least_once.returns([column]) c = Account.count(:all, :group => :firm) - assert_equal Firm, c.first.first.class - assert_equal 1, c.first.last + first_key = c.keys.first + assert_equal Firm, first_key.class + assert_equal 1, c[first_key] end end -- cgit v1.2.3 From 05f2183747c8e75c9e8bbaadb9573b4bdf41ecfc Mon Sep 17 00:00:00 2001 From: Emilio Tagua Date: Tue, 2 Dec 2008 14:12:09 -0300 Subject: Fix: counter_cache should decrement on deleting associated records. [#1195 state:committed] Signed-off-by: Jeremy Kemper --- .../cases/associations/has_many_associations_test.rb | 20 ++++++++++++++++++++ .../has_many_through_associations_test.rb | 14 ++++++++++++++ 2 files changed, 34 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 816ceb6855..1f8b297e81 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -552,6 +552,18 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal 0, companies(:first_firm).clients_of_firm(true).size end + def test_deleting_updates_counter_cache + post = Post.first + + post.comments.delete(post.comments.first) + post.reload + assert_equal post.comments(true).size, post.comments_count + + post.comments.delete(post.comments.first) + post.reload + assert_equal 0, post.comments_count + end + def test_deleting_before_save new_firm = Firm.new("name" => "A New Firm, Inc.") new_client = new_firm.clients_of_firm.build("name" => "Another Client") @@ -605,6 +617,14 @@ class HasManyAssociationsTest < ActiveRecord::TestCase end end + def test_clearing_updates_counter_cache + post = Post.first + + post.comments.clear + post.reload + assert_equal 0, post.comments_count + end + def test_clearing_a_dependent_association_collection firm = companies(:first_firm) client_id = firm.dependent_clients_of_firm.first.id diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index a07f4bcbdd..ba3428a508 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -3,6 +3,9 @@ require 'models/post' require 'models/person' require 'models/reader' require 'models/comment' +require 'models/tag' +require 'models/tagging' +require 'models/author' class HasManyThroughAssociationsTest < ActiveRecord::TestCase fixtures :posts, :readers, :people, :comments, :authors @@ -84,6 +87,17 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase assert posts(:welcome).reload.people(true).empty? end + def test_deleting_updates_counter_cache + taggable = Tagging.first.taggable + taggable.taggings.push(Tagging.new) + taggable.reload + assert_equal 1, taggable.taggings_count + + taggable.taggings.delete(taggable.taggings.first) + taggable.reload + assert_equal 0, taggable.taggings_count + end + def test_replace_association assert_queries(4){posts(:welcome);people(:david);people(:michael); posts(:welcome).people(true)} -- cgit v1.2.3 From 96b815d7e81b9cef912ef94c96dca923fe43b0ba Mon Sep 17 00:00:00 2001 From: Emilio Tagua Date: Wed, 10 Dec 2008 16:04:29 -0300 Subject: Fix test names collision. [#1549 state:committed] Signed-off-by: Jeremy Kemper --- activerecord/test/cases/validations_i18n_test.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/validations_i18n_test.rb b/activerecord/test/cases/validations_i18n_test.rb index f59e3f7001..e893a704f1 100644 --- a/activerecord/test/cases/validations_i18n_test.rb +++ b/activerecord/test/cases/validations_i18n_test.rb @@ -506,7 +506,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase # validates_length_of :is w/o mocha - def test_validates_length_of_within_finds_custom_model_key_translation + def test_validates_length_of_is_finds_custom_model_key_translation I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:wrong_length => 'custom message'}}}}}} I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:wrong_length => 'global message'}}} @@ -515,7 +515,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase assert_equal 'custom message', @topic.errors.on(:title) end - def test_validates_length_of_within_finds_global_default_translation + def test_validates_length_of_is_finds_global_default_translation I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:wrong_length => 'global message'}}} Topic.validates_length_of :title, :is => 5 @@ -525,7 +525,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase # validates_uniqueness_of w/o mocha - def test_validates_length_of_within_finds_custom_model_key_translation + def test_validates_length_of_is_finds_custom_model_key_translation I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:wrong_length => 'custom message'}}}}}} I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:wrong_length => 'global message'}}} @@ -534,7 +534,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase assert_equal 'custom message', @topic.errors.on(:title) end - def test_validates_length_of_within_finds_global_default_translation + def test_validates_length_of_is_finds_global_default_translation I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:wrong_length => 'global message'}}} Topic.validates_length_of :title, :is => 5 -- cgit v1.2.3 From aa5cdb0d47fb5484bfdde8244df7efeb2175bf3a Mon Sep 17 00:00:00 2001 From: Bruce Krysiak Date: Mon, 8 Dec 2008 16:38:04 -0800 Subject: Added a :camelize option to ActiveRecord and Hash to_xml serialization and from_xml deserialization Signed-off-by: Michael Koziarski --- activerecord/test/cases/xml_serialization_test.rb | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/xml_serialization_test.rb b/activerecord/test/cases/xml_serialization_test.rb index 63f48865cc..39c6ea820d 100644 --- a/activerecord/test/cases/xml_serialization_test.rb +++ b/activerecord/test/cases/xml_serialization_test.rb @@ -31,6 +31,13 @@ class XmlSerializationTest < ActiveRecord::TestCase assert_match %r{ 'xml_contact', :camelize => true + assert_match %r{^}, @xml + assert_match %r{$}, @xml + assert_match %r{ Date: Wed, 10 Dec 2008 14:48:12 -0800 Subject: Revert "Fix: counter_cache should decrement on deleting associated records." [#1196 state:open] This reverts commit 05f2183747c8e75c9e8bbaadb9573b4bdf41ecfc. --- .../cases/associations/has_many_associations_test.rb | 20 -------------------- .../has_many_through_associations_test.rb | 14 -------------- 2 files changed, 34 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 1f8b297e81..816ceb6855 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -552,18 +552,6 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal 0, companies(:first_firm).clients_of_firm(true).size end - def test_deleting_updates_counter_cache - post = Post.first - - post.comments.delete(post.comments.first) - post.reload - assert_equal post.comments(true).size, post.comments_count - - post.comments.delete(post.comments.first) - post.reload - assert_equal 0, post.comments_count - end - def test_deleting_before_save new_firm = Firm.new("name" => "A New Firm, Inc.") new_client = new_firm.clients_of_firm.build("name" => "Another Client") @@ -617,14 +605,6 @@ class HasManyAssociationsTest < ActiveRecord::TestCase end end - def test_clearing_updates_counter_cache - post = Post.first - - post.comments.clear - post.reload - assert_equal 0, post.comments_count - end - def test_clearing_a_dependent_association_collection firm = companies(:first_firm) client_id = firm.dependent_clients_of_firm.first.id diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index ba3428a508..a07f4bcbdd 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -3,9 +3,6 @@ require 'models/post' require 'models/person' require 'models/reader' require 'models/comment' -require 'models/tag' -require 'models/tagging' -require 'models/author' class HasManyThroughAssociationsTest < ActiveRecord::TestCase fixtures :posts, :readers, :people, :comments, :authors @@ -87,17 +84,6 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase assert posts(:welcome).reload.people(true).empty? end - def test_deleting_updates_counter_cache - taggable = Tagging.first.taggable - taggable.taggings.push(Tagging.new) - taggable.reload - assert_equal 1, taggable.taggings_count - - taggable.taggings.delete(taggable.taggings.first) - taggable.reload - assert_equal 0, taggable.taggings_count - end - def test_replace_association assert_queries(4){posts(:welcome);people(:david);people(:michael); posts(:welcome).people(true)} -- cgit v1.2.3 From a392f34fb4069ab847ff631130d023cdaf896735 Mon Sep 17 00:00:00 2001 From: "Hongli Lai (Phusion)" Date: Mon, 15 Dec 2008 14:47:19 -0600 Subject: Require mocha >= 0.9.3, older versions don't work anymore [#1579 state:resolved] Signed-off-by: Joshua Peek --- activerecord/test/cases/helper.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/helper.rb b/activerecord/test/cases/helper.rb index 2382bfe4fe..afba715448 100644 --- a/activerecord/test/cases/helper.rb +++ b/activerecord/test/cases/helper.rb @@ -24,6 +24,7 @@ end def uses_mocha(description) require 'rubygems' + gem 'mocha', '>= 0.9.3' require 'mocha' yield rescue LoadError -- cgit v1.2.3 From 707d0dd3e1e8df7771073670e4257d933d2818f9 Mon Sep 17 00:00:00 2001 From: Frederick Cheung Date: Wed, 17 Dec 2008 23:39:09 +0000 Subject: Fix preloading of belongs_to with null foreign key generating useless query [#1027 state:resolved] --- activerecord/test/cases/associations/eager_test.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index 3c8408d14b..2fd51ef03a 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -145,7 +145,7 @@ class EagerAssociationTest < ActiveRecord::TestCase def test_finding_with_includes_on_null_belongs_to_association_with_same_include_includes_only_once post = posts(:welcome) post.update_attributes!(:author => nil) - post = assert_queries(2) { Post.find(post.id, :include => {:author_with_address => :author_address}) } # find the post, then find the author which is null so no query for the address + post = assert_queries(1) { Post.find(post.id, :include => {:author_with_address => :author_address}) } # find the post, then find the author which is null so no query for the author or address assert_no_queries do assert_equal nil, post.author_with_address end @@ -705,4 +705,5 @@ class EagerAssociationTest < ActiveRecord::TestCase def test_order_on_join_table_with_include_and_limit assert_equal 5, Developer.find(:all, :include => 'projects', :order => 'developers_projects.joined_on DESC', :limit => 5).size end + end -- cgit v1.2.3 From 9cf6b1b15e6335134a5af5d9d6296f8d9242e005 Mon Sep 17 00:00:00 2001 From: Emilio Tagua Date: Tue, 2 Dec 2008 13:47:53 -0300 Subject: Add missing model files so tests can run isolated [#1506 state:resolved] Signed-off-by: Frederick Cheung --- .../test/cases/associations/has_many_through_associations_test.rb | 3 +++ 1 file changed, 3 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index a07f4bcbdd..0bfda337b2 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -3,6 +3,9 @@ require 'models/post' require 'models/person' require 'models/reader' require 'models/comment' +require 'models/tag' +require 'models/tagging' +require 'models/author' class HasManyThroughAssociationsTest < ActiveRecord::TestCase fixtures :posts, :readers, :people, :comments, :authors -- cgit v1.2.3 From c9ab7098be7bdd748c0f4a49c8ef015b4aad3108 Mon Sep 17 00:00:00 2001 From: Frederick Cheung Date: Thu, 18 Dec 2008 19:07:55 +0000 Subject: Ensure :include checks joins when determining if it can preload [#528 state:resolved] --- activerecord/test/cases/associations/eager_test.rb | 65 ++++++++++++++++++++++ 1 file changed, 65 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index 2fd51ef03a..42063d18a3 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -1,6 +1,7 @@ require "cases/helper" require 'models/post' require 'models/tagging' +require 'models/tag' require 'models/comment' require 'models/author' require 'models/category' @@ -706,4 +707,68 @@ class EagerAssociationTest < ActiveRecord::TestCase assert_equal 5, Developer.find(:all, :include => 'projects', :order => 'developers_projects.joined_on DESC', :limit => 5).size end + def test_eager_loading_with_order_on_joined_table_preloads + posts = assert_queries(2) do + Post.find(:all, :joins => :comments, :include => :author, :order => 'comments.id DESC') + end + assert_equal posts(:eager_other), posts[0] + assert_equal authors(:mary), assert_no_queries { posts[0].author} + end + + def test_eager_loading_with_conditions_on_joined_table_preloads + posts = assert_queries(2) do + Post.find(:all, :select => 'distinct posts.*', :include => :author, :joins => [:comments], :conditions => "comments.body like 'Thank you%'", :order => 'posts.id') + end + assert_equal [posts(:welcome)], posts + assert_equal authors(:david), assert_no_queries { posts[0].author} + + posts = assert_queries(2) do + Post.find(:all, :select => 'distinct posts.*', :include => :author, :joins => [:comments], :conditions => "comments.body like 'Thank you%'", :order => 'posts.id') + end + assert_equal [posts(:welcome)], posts + assert_equal authors(:david), assert_no_queries { posts[0].author} + + posts = assert_queries(2) do + Post.find(:all, :include => :author, :joins => {:taggings => :tag}, :conditions => "tags.name = 'General'") + end + assert_equal posts(:welcome, :thinking), posts + + posts = assert_queries(2) do + Post.find(:all, :include => :author, :joins => {:taggings => {:tag => :taggings}}, :conditions => "taggings_tags.super_tag_id=2") + end + assert_equal posts(:welcome, :thinking), posts + + end + + def test_eager_loading_with_conditions_on_string_joined_table_preloads + posts = assert_queries(2) do + Post.find(:all, :select => 'distinct posts.*', :include => :author, :joins => "INNER JOIN comments on comments.post_id = posts.id", :conditions => "comments.body like 'Thank you%'", :order => 'posts.id') + end + assert_equal [posts(:welcome)], posts + assert_equal authors(:david), assert_no_queries { posts[0].author} + + posts = assert_queries(2) do + Post.find(:all, :select => 'distinct posts.*', :include => :author, :joins => ["INNER JOIN comments on comments.post_id = posts.id"], :conditions => "comments.body like 'Thank you%'", :order => 'posts.id') + end + assert_equal [posts(:welcome)], posts + assert_equal authors(:david), assert_no_queries { posts[0].author} + + end + + def test_eager_loading_with_select_on_joined_table_preloads + posts = assert_queries(2) do + Post.find(:all, :select => 'posts.*, authors.name as author_name', :include => :comments, :joins => :author, :order => 'posts.id') + end + assert_equal 'David', posts[0].author_name + assert_equal posts(:welcome).comments, assert_no_queries { posts[0].comments} + end + + def test_eager_loading_with_conditions_on_join_model_preloads + authors = assert_queries(2) do + Author.find(:all, :include => :author_address, :joins => :comments, :conditions => "posts.title like 'Welcome%'") + end + assert_equal authors(:david), authors[0] + assert_equal author_addresses(:david_address), authors[0].author_address + end + end -- cgit v1.2.3 From a9422cc1db9501a80ecf2c25a5d3b0c4f4f32763 Mon Sep 17 00:00:00 2001 From: Matt Jones Date: Tue, 2 Dec 2008 16:21:21 -0500 Subject: Fix preloading of has_one :through associations on belongs_to [#1507 state:resolved] Signed-off-by: Frederick Cheung --- .../associations/has_one_through_associations_test.rb | 17 ++++++++++++++++- activerecord/test/fixtures/member_types.yml | 6 ++++++ activerecord/test/fixtures/members.yml | 4 +++- activerecord/test/models/member.rb | 1 + activerecord/test/models/member_detail.rb | 1 + activerecord/test/models/member_type.rb | 3 +++ activerecord/test/schema/schema.rb | 5 +++++ 7 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 activerecord/test/fixtures/member_types.yml create mode 100644 activerecord/test/models/member_type.rb (limited to 'activerecord/test') diff --git a/activerecord/test/cases/associations/has_one_through_associations_test.rb b/activerecord/test/cases/associations/has_one_through_associations_test.rb index 7d418de965..f65d76e2ce 100644 --- a/activerecord/test/cases/associations/has_one_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_one_through_associations_test.rb @@ -1,5 +1,6 @@ require "cases/helper" require 'models/club' +require 'models/member_type' require 'models/member' require 'models/membership' require 'models/sponsor' @@ -7,7 +8,7 @@ require 'models/organization' require 'models/member_detail' class HasOneThroughAssociationsTest < ActiveRecord::TestCase - fixtures :members, :clubs, :memberships, :sponsors, :organizations + fixtures :member_types, :members, :clubs, :memberships, :sponsors, :organizations def setup @member = members(:groucho) @@ -158,4 +159,18 @@ class HasOneThroughAssociationsTest < ActiveRecord::TestCase assert @new_organization.members.include?(@member) end + def test_preloading_has_one_through_on_belongs_to + assert_not_nil @member.member_type + @organization = organizations(:nsa) + @member_detail = MemberDetail.new + @member.member_detail = @member_detail + @member.organization = @organization + @member_details = assert_queries(3) do + MemberDetail.find(:all, :include => :member_type) + end + @new_detail = @member_details[0] + assert @new_detail.loaded_member_type? + assert_not_nil assert_no_queries { @new_detail.member_type } + end + end diff --git a/activerecord/test/fixtures/member_types.yml b/activerecord/test/fixtures/member_types.yml new file mode 100644 index 0000000000..797a57430c --- /dev/null +++ b/activerecord/test/fixtures/member_types.yml @@ -0,0 +1,6 @@ +founding: + id: 1 + name: Founding +provisional: + id: 2 + name: Provisional diff --git a/activerecord/test/fixtures/members.yml b/activerecord/test/fixtures/members.yml index 67a6cc459a..6db945e61d 100644 --- a/activerecord/test/fixtures/members.yml +++ b/activerecord/test/fixtures/members.yml @@ -1,4 +1,6 @@ groucho: name: Groucho Marx + member_type_id: 1 some_other_guy: - name: Englebert Humperdink \ No newline at end of file + name: Englebert Humperdink + member_type_id: 2 diff --git a/activerecord/test/models/member.rb b/activerecord/test/models/member.rb index 77a37abb38..255fb569d7 100644 --- a/activerecord/test/models/member.rb +++ b/activerecord/test/models/member.rb @@ -8,4 +8,5 @@ class Member < ActiveRecord::Base has_one :sponsor_club, :through => :sponsor has_one :member_detail has_one :organization, :through => :member_detail + belongs_to :member_type end \ No newline at end of file diff --git a/activerecord/test/models/member_detail.rb b/activerecord/test/models/member_detail.rb index e731454556..94f59e5794 100644 --- a/activerecord/test/models/member_detail.rb +++ b/activerecord/test/models/member_detail.rb @@ -1,4 +1,5 @@ class MemberDetail < ActiveRecord::Base belongs_to :member belongs_to :organization + has_one :member_type, :through => :member end diff --git a/activerecord/test/models/member_type.rb b/activerecord/test/models/member_type.rb new file mode 100644 index 0000000000..a13561c72a --- /dev/null +++ b/activerecord/test/models/member_type.rb @@ -0,0 +1,3 @@ +class MemberType < ActiveRecord::Base + has_many :members +end diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index 6217e3bc1c..fbacc692b4 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -195,6 +195,7 @@ ActiveRecord::Schema.define do create_table :members, :force => true do |t| t.string :name + t.integer :member_type_id end create_table :member_details, :force => true do |t| @@ -210,6 +211,10 @@ ActiveRecord::Schema.define do t.string :type end + create_table :member_types, :force => true do |t| + t.string :name + end + create_table :references, :force => true do |t| t.integer :person_id t.integer :job_id -- cgit v1.2.3 From 8a92cdc8638d1f91aaa55fc47743a6210ad2181b Mon Sep 17 00:00:00 2001 From: Murray Steele Date: Fri, 19 Dec 2008 13:27:34 +0000 Subject: Add a repair_helper to repair changes to the validations inside validations_test.rb [#674 state:resolved] Many of the tests in validations_test would add a new validation to models. However, only Topic was being reset with a fairly aggressive clearing of all validations. None of the other models being used however were recieving the same treatment. Now we use repair_validations(Topic) for the whole test case because most test cases use Topic and then the block form of repair_validations() inside any tests that use other models. Signed-off-by: Pratik Naik --- activerecord/test/cases/helper.rb | 4 + activerecord/test/cases/validations_test.rb | 351 +++++++++++++++------------- 2 files changed, 195 insertions(+), 160 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/helper.rb b/activerecord/test/cases/helper.rb index afba715448..2043138ca3 100644 --- a/activerecord/test/cases/helper.rb +++ b/activerecord/test/cases/helper.rb @@ -9,6 +9,8 @@ require 'active_record/test_case' require 'active_record/fixtures' require 'connection' +require 'cases/repair_helper' + # Show backtraces for deprecated behavior for quicker cleanup. ActiveSupport::Deprecation.debug = true @@ -60,6 +62,8 @@ end class ActiveSupport::TestCase include ActiveRecord::TestFixtures + include ActiveRecord::Testing::RepairHelper + self.fixture_path = FIXTURES_ROOT self.use_instantiated_fixtures = false self.use_transactional_fixtures = true diff --git a/activerecord/test/cases/validations_test.rb b/activerecord/test/cases/validations_test.rb index c049659327..380d8ac260 100644 --- a/activerecord/test/cases/validations_test.rb +++ b/activerecord/test/cases/validations_test.rb @@ -6,6 +6,8 @@ require 'models/person' require 'models/developer' require 'models/warehouse_thing' require 'models/guid' +require 'models/owner' +require 'models/pet' # The following methods in Topic are used in test_conditional_validation_* class Topic @@ -31,10 +33,6 @@ class UniqueReply < Reply validates_uniqueness_of :content, :scope => 'parent_id' end -class PlagiarizedReply < Reply - validates_acceptance_of :author_name -end - class SillyUniqueReply < UniqueReply end @@ -58,11 +56,9 @@ end class ValidationsTest < ActiveRecord::TestCase fixtures :topics, :developers, 'warehouse-things' - def setup - Topic.instance_variable_set("@validate_callbacks", ActiveSupport::Callbacks::CallbackChain.new) - Topic.instance_variable_set("@validate_on_create_callbacks", ActiveSupport::Callbacks::CallbackChain.new) - Topic.instance_variable_set("@validate_on_update_callbacks", ActiveSupport::Callbacks::CallbackChain.new) - end + # Most of the tests mess with the validations of Topic, so lets repair it all the time. + # Other classes we mess with will be dealt with in the specific tests + repair_validations(Topic) def test_single_field_validation r = Reply.new @@ -134,7 +130,7 @@ class ValidationsTest < ActiveRecord::TestCase Reply.create!([ { "title" => "OK" }, { "title" => "Wrong Create" }]) end end - + def test_exception_on_create_bang_with_block assert_raises(ActiveRecord::RecordInvalid) do Reply.create!({ "title" => "OK" }) do |r| @@ -142,7 +138,7 @@ class ValidationsTest < ActiveRecord::TestCase end end end - + def test_exception_on_create_bang_many_with_block assert_raises(ActiveRecord::RecordInvalid) do Reply.create!([{ "title" => "OK" }, { "title" => "Wrong Create" }]) do |r| @@ -229,21 +225,16 @@ class ValidationsTest < ActiveRecord::TestCase end def test_validates_each - perform = true hits = 0 Topic.validates_each(:title, :content, [:title, :content]) do |record, attr| - if perform - record.errors.add attr, 'gotcha' - hits += 1 - end + record.errors.add attr, 'gotcha' + hits += 1 end t = Topic.new("title" => "valid", "content" => "whatever") assert !t.save assert_equal 4, hits assert_equal %w(gotcha gotcha), t.errors.on(:title) assert_equal %w(gotcha gotcha), t.errors.on(:content) - ensure - perform = false end def test_no_title_confirmation @@ -315,8 +306,12 @@ class ValidationsTest < ActiveRecord::TestCase end def test_validates_acceptance_of_as_database_column - reply = PlagiarizedReply.create("author_name" => "Dan Brown") - assert_equal "Dan Brown", reply["author_name"] + repair_validations(Reply) do + Reply.validates_acceptance_of(:author_name) + + reply = Reply.create("author_name" => "Dan Brown") + assert_equal "Dan Brown", reply["author_name"] + end end def test_validates_acceptance_of_with_non_existant_table @@ -372,22 +367,24 @@ class ValidationsTest < ActiveRecord::TestCase end def test_validate_uniqueness_with_scope - Reply.validates_uniqueness_of(:content, :scope => "parent_id") + repair_validations(Reply) do + Reply.validates_uniqueness_of(:content, :scope => "parent_id") - t = Topic.create("title" => "I'm unique!") + t = Topic.create("title" => "I'm unique!") - r1 = t.replies.create "title" => "r1", "content" => "hello world" - assert r1.valid?, "Saving r1" + r1 = t.replies.create "title" => "r1", "content" => "hello world" + assert r1.valid?, "Saving r1" - r2 = t.replies.create "title" => "r2", "content" => "hello world" - assert !r2.valid?, "Saving r2 first time" + r2 = t.replies.create "title" => "r2", "content" => "hello world" + assert !r2.valid?, "Saving r2 first time" - r2.content = "something else" - assert r2.save, "Saving r2 second time" + r2.content = "something else" + assert r2.save, "Saving r2 second time" - t2 = Topic.create("title" => "I'm unique too!") - r3 = t2.replies.create "title" => "r3", "content" => "hello world" - assert r3.valid?, "Saving r3" + t2 = Topic.create("title" => "I'm unique too!") + r3 = t2.replies.create "title" => "r3", "content" => "hello world" + assert r3.valid?, "Saving r3" + end end def test_validate_uniqueness_scoped_to_defining_class @@ -406,27 +403,29 @@ class ValidationsTest < ActiveRecord::TestCase end def test_validate_uniqueness_with_scope_array - Reply.validates_uniqueness_of(:author_name, :scope => [:author_email_address, :parent_id]) + repair_validations(Reply) do + Reply.validates_uniqueness_of(:author_name, :scope => [:author_email_address, :parent_id]) - t = Topic.create("title" => "The earth is actually flat!") + t = Topic.create("title" => "The earth is actually flat!") - r1 = t.replies.create "author_name" => "jeremy", "author_email_address" => "jeremy@rubyonrails.com", "title" => "You're crazy!", "content" => "Crazy reply" - assert r1.valid?, "Saving r1" + r1 = t.replies.create "author_name" => "jeremy", "author_email_address" => "jeremy@rubyonrails.com", "title" => "You're crazy!", "content" => "Crazy reply" + assert r1.valid?, "Saving r1" - r2 = t.replies.create "author_name" => "jeremy", "author_email_address" => "jeremy@rubyonrails.com", "title" => "You're crazy!", "content" => "Crazy reply again..." - assert !r2.valid?, "Saving r2. Double reply by same author." + r2 = t.replies.create "author_name" => "jeremy", "author_email_address" => "jeremy@rubyonrails.com", "title" => "You're crazy!", "content" => "Crazy reply again..." + assert !r2.valid?, "Saving r2. Double reply by same author." - r2.author_email_address = "jeremy_alt_email@rubyonrails.com" - assert r2.save, "Saving r2 the second time." + r2.author_email_address = "jeremy_alt_email@rubyonrails.com" + assert r2.save, "Saving r2 the second time." - r3 = t.replies.create "author_name" => "jeremy", "author_email_address" => "jeremy_alt_email@rubyonrails.com", "title" => "You're wrong", "content" => "It's cubic" - assert !r3.valid?, "Saving r3" + r3 = t.replies.create "author_name" => "jeremy", "author_email_address" => "jeremy_alt_email@rubyonrails.com", "title" => "You're wrong", "content" => "It's cubic" + assert !r3.valid?, "Saving r3" - r3.author_name = "jj" - assert r3.save, "Saving r3 the second time." + r3.author_name = "jj" + assert r3.save, "Saving r3 the second time." - r3.author_name = "jeremy" - assert !r3.save, "Saving r3 the third time." + r3.author_name = "jeremy" + assert !r3.save, "Saving r3 the third time." + end end def test_validate_case_insensitive_uniqueness @@ -523,10 +522,12 @@ class ValidationsTest < ActiveRecord::TestCase end def test_validate_uniqueness_with_columns_which_are_sql_keywords - Guid.validates_uniqueness_of :key - g = Guid.new - g.key = "foo" - assert_nothing_raised { !g.valid? } + repair_validations(Guid) do + Guid.validates_uniqueness_of :key + g = Guid.new + g.key = "foo" + assert_nothing_raised { !g.valid? } + end end def test_validate_straight_inheritance_uniqueness @@ -648,10 +649,12 @@ class ValidationsTest < ActiveRecord::TestCase end def test_numericality_with_getter_method - Developer.validates_numericality_of( :salary ) - developer = Developer.new("name" => "michael", "salary" => nil) - developer.instance_eval("def salary; read_attribute('salary') ? read_attribute('salary') : 100000; end") - assert developer.valid? + repair_validations(Developer) do + Developer.validates_numericality_of( :salary ) + developer = Developer.new("name" => "michael", "salary" => nil) + developer.instance_eval("def salary; read_attribute('salary') ? read_attribute('salary') : 100000; end") + assert developer.valid? + end end def test_validates_length_of_with_allow_nil @@ -684,10 +687,12 @@ class ValidationsTest < ActiveRecord::TestCase end def test_numericality_with_allow_nil_and_getter_method - Developer.validates_numericality_of( :salary, :allow_nil => true) - developer = Developer.new("name" => "michael", "salary" => nil) - developer.instance_eval("def salary; read_attribute('salary') ? read_attribute('salary') : 100000; end") - assert developer.valid? + repair_validations(Developer) do + Developer.validates_numericality_of( :salary, :allow_nil => true) + developer = Developer.new("name" => "michael", "salary" => nil) + developer.instance_eval("def salary; read_attribute('salary') ? read_attribute('salary') : 100000; end") + assert developer.valid? + end end def test_validates_exclusion_of @@ -892,26 +897,30 @@ class ValidationsTest < ActiveRecord::TestCase end def test_validates_size_of_association - assert_nothing_raised { Topic.validates_size_of :replies, :minimum => 1 } - t = Topic.new('title' => 'noreplies', 'content' => 'whatever') - assert !t.save - assert t.errors.on(:replies) - reply = t.replies.build('title' => 'areply', 'content' => 'whateveragain') - assert t.valid? + repair_validations(Owner) do + assert_nothing_raised { Owner.validates_size_of :pets, :minimum => 1 } + o = Owner.new('name' => 'nopets') + assert !o.save + assert o.errors.on(:pets) + pet = o.pets.build('name' => 'apet') + assert o.valid? + end end def test_validates_size_of_association_using_within - assert_nothing_raised { Topic.validates_size_of :replies, :within => 1..2 } - t = Topic.new('title' => 'noreplies', 'content' => 'whatever') - assert !t.save - assert t.errors.on(:replies) - - reply = t.replies.build('title' => 'areply', 'content' => 'whateveragain') - assert t.valid? - - 2.times { t.replies.build('title' => 'areply', 'content' => 'whateveragain') } - assert !t.save - assert t.errors.on(:replies) + repair_validations(Owner) do + assert_nothing_raised { Owner.validates_size_of :pets, :within => 1..2 } + o = Owner.new('name' => 'nopets') + assert !o.save + assert o.errors.on(:pets) + + pet = o.pets.build('name' => 'apet') + assert o.valid? + + 2.times { o.pets.build('name' => 'apet') } + assert !o.save + assert o.errors.on(:pets) + end end def test_validates_length_of_nasty_params @@ -1102,13 +1111,15 @@ class ValidationsTest < ActiveRecord::TestCase end def test_validates_size_of_association_utf8 - with_kcode('UTF8') do - assert_nothing_raised { Topic.validates_size_of :replies, :minimum => 1 } - t = Topic.new('title' => 'あいうえお', 'content' => 'かきくけこ') - assert !t.save - assert t.errors.on(:replies) - t.replies.build('title' => 'あいうえお', 'content' => 'かきくけこ') - assert t.valid? + repair_validations(Owner) do + with_kcode('UTF8') do + assert_nothing_raised { Owner.validates_size_of :pets, :minimum => 1 } + o = Owner.new('name' => 'あいうえおかきくけこ') + assert !o.save + assert o.errors.on(:pets) + o.pets.build('name' => 'あいうえおかきくけこ') + assert o.valid? + end end end @@ -1127,14 +1138,16 @@ class ValidationsTest < ActiveRecord::TestCase end def test_validates_associated_one - Reply.validates_associated( :topic ) - Topic.validates_presence_of( :content ) - r = Reply.new("title" => "A reply", "content" => "with content!") - r.topic = Topic.create("title" => "uhohuhoh") - assert !r.valid? - assert r.errors.on(:topic) - r.topic.content = "non-empty" - assert r.valid? + repair_validations(Reply) do + Reply.validates_associated( :topic ) + Topic.validates_presence_of( :content ) + r = Reply.new("title" => "A reply", "content" => "with content!") + r.topic = Topic.create("title" => "uhohuhoh") + assert !r.valid? + assert r.errors.on(:topic) + r.topic.content = "non-empty" + assert r.valid? + end end def test_validate_block @@ -1158,85 +1171,105 @@ class ValidationsTest < ActiveRecord::TestCase end def test_validates_acceptance_of_with_custom_error_using_quotes - Developer.validates_acceptance_of :salary, :message=> "This string contains 'single' and \"double\" quotes" - d = Developer.new - d.salary = "0" - assert !d.valid? - assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:salary).last + repair_validations(Developer) do + Developer.validates_acceptance_of :salary, :message=> "This string contains 'single' and \"double\" quotes" + d = Developer.new + d.salary = "0" + assert !d.valid? + assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:salary).last + end end def test_validates_confirmation_of_with_custom_error_using_quotes - Developer.validates_confirmation_of :name, :message=> "confirm 'single' and \"double\" quotes" - d = Developer.new - d.name = "John" - d.name_confirmation = "Johnny" - assert !d.valid? - assert_equal "confirm 'single' and \"double\" quotes", d.errors.on(:name) + repair_validations(Developer) do + Developer.validates_confirmation_of :name, :message=> "confirm 'single' and \"double\" quotes" + d = Developer.new + d.name = "John" + d.name_confirmation = "Johnny" + assert !d.valid? + assert_equal "confirm 'single' and \"double\" quotes", d.errors.on(:name) + end end def test_validates_format_of_with_custom_error_using_quotes - Developer.validates_format_of :name, :with => /^(A-Z*)$/, :message=> "format 'single' and \"double\" quotes" - d = Developer.new - d.name = d.name_confirmation = "John 32" - assert !d.valid? - assert_equal "format 'single' and \"double\" quotes", d.errors.on(:name) + repair_validations(Developer) do + Developer.validates_format_of :name, :with => /^(A-Z*)$/, :message=> "format 'single' and \"double\" quotes" + d = Developer.new + d.name = d.name_confirmation = "John 32" + assert !d.valid? + assert_equal "format 'single' and \"double\" quotes", d.errors.on(:name) + end end def test_validates_inclusion_of_with_custom_error_using_quotes - Developer.validates_inclusion_of :salary, :in => 1000..80000, :message=> "This string contains 'single' and \"double\" quotes" - d = Developer.new - d.salary = "90,000" - assert !d.valid? - assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:salary).last + repair_validations(Developer) do + Developer.validates_inclusion_of :salary, :in => 1000..80000, :message=> "This string contains 'single' and \"double\" quotes" + d = Developer.new + d.salary = "90,000" + assert !d.valid? + assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:salary).last + end end def test_validates_length_of_with_custom_too_long_using_quotes - Developer.validates_length_of :name, :maximum => 4, :too_long=> "This string contains 'single' and \"double\" quotes" - d = Developer.new - d.name = "Jeffrey" - assert !d.valid? - assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:name).last + repair_validations(Developer) do + Developer.validates_length_of :name, :maximum => 4, :too_long=> "This string contains 'single' and \"double\" quotes" + d = Developer.new + d.name = "Jeffrey" + assert !d.valid? + assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:name) + end end def test_validates_length_of_with_custom_too_short_using_quotes - Developer.validates_length_of :name, :minimum => 4, :too_short=> "This string contains 'single' and \"double\" quotes" - d = Developer.new - d.name = "Joe" - assert !d.valid? - assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:name).last + repair_validations(Developer) do + Developer.validates_length_of :name, :minimum => 4, :too_short=> "This string contains 'single' and \"double\" quotes" + d = Developer.new + d.name = "Joe" + assert !d.valid? + assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:name) + end end def test_validates_length_of_with_custom_message_using_quotes - Developer.validates_length_of :name, :minimum => 4, :message=> "This string contains 'single' and \"double\" quotes" - d = Developer.new - d.name = "Joe" - assert !d.valid? - assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:name).last + repair_validations(Developer) do + Developer.validates_length_of :name, :minimum => 4, :message=> "This string contains 'single' and \"double\" quotes" + d = Developer.new + d.name = "Joe" + assert !d.valid? + assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:name) + end end def test_validates_presence_of_with_custom_message_using_quotes - Developer.validates_presence_of :non_existent, :message=> "This string contains 'single' and \"double\" quotes" - d = Developer.new - d.name = "Joe" - assert !d.valid? - assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:non_existent) + repair_validations(Developer) do + Developer.validates_presence_of :non_existent, :message=> "This string contains 'single' and \"double\" quotes" + d = Developer.new + d.name = "Joe" + assert !d.valid? + assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:non_existent) + end end def test_validates_uniqueness_of_with_custom_message_using_quotes - Developer.validates_uniqueness_of :name, :message=> "This string contains 'single' and \"double\" quotes" - d = Developer.new - d.name = "David" - assert !d.valid? - assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:name).last + repair_validations(Developer) do + Developer.validates_uniqueness_of :name, :message=> "This string contains 'single' and \"double\" quotes" + d = Developer.new + d.name = "David" + assert !d.valid? + assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:name) + end end def test_validates_associated_with_custom_message_using_quotes - Reply.validates_associated :topic, :message=> "This string contains 'single' and \"double\" quotes" - Topic.validates_presence_of :content - r = Reply.create("title" => "A reply", "content" => "with content!") - r.topic = Topic.create("title" => "uhohuhoh") - assert !r.valid? - assert_equal "This string contains 'single' and \"double\" quotes", r.errors.on(:topic).last + repair_validations(Reply) do + Reply.validates_associated :topic, :message=> "This string contains 'single' and \"double\" quotes" + Topic.validates_presence_of :content + r = Reply.create("title" => "A reply", "content" => "with content!") + r.topic = Topic.create("title" => "uhohuhoh") + assert !r.valid? + assert_equal "This string contains 'single' and \"double\" quotes", r.errors.on(:topic) + end end def test_if_validation_using_method_true @@ -1346,13 +1379,15 @@ class ValidationsTest < ActiveRecord::TestCase end def test_validates_associated_missing - Reply.validates_presence_of(:topic) - r = Reply.create("title" => "A reply", "content" => "with content!") - assert !r.valid? - assert r.errors.on(:topic) - - r.topic = Topic.find :first - assert r.valid? + repair_validations(Reply) do + Reply.validates_presence_of(:topic) + r = Reply.create("title" => "A reply", "content" => "with content!") + assert !r.valid? + assert r.errors.on(:topic) + + r.topic = Topic.find :first + assert r.valid? + end end def test_errors_to_xml @@ -1364,14 +1399,14 @@ class ValidationsTest < ActiveRecord::TestCase assert xml.include?("Content Empty") end - def test_validation_order - Topic.validates_presence_of :title - Topic.validates_length_of :title, :minimum => 2 + def test_validation_order + Topic.validates_presence_of :title + Topic.validates_length_of :title, :minimum => 2 - t = Topic.new("title" => "") - assert !t.valid? - assert_equal "can't be blank", t.errors.on("title").first - end + t = Topic.new("title" => "") + assert !t.valid? + assert_equal "can't be blank", t.errors.on("title").first + end # previous implementation of validates_presence_of eval'd the # string with the wrong binding, this regression test is to @@ -1423,11 +1458,7 @@ class ValidatesNumericalityTest < ActiveRecord::TestCase JUNK = ["not a number", "42 not a number", "0xdeadbeef", "00-1", "--3", "+-3", "+3-1", "-+019.0", "12.12.13.12", "123\nnot a number"] INFINITY = [1.0/0.0] - def setup - Topic.instance_variable_set("@validate_callbacks", ActiveSupport::Callbacks::CallbackChain.new) - Topic.instance_variable_set("@validate_on_create_callbacks", ActiveSupport::Callbacks::CallbackChain.new) - Topic.instance_variable_set("@validate_on_update_callbacks", ActiveSupport::Callbacks::CallbackChain.new) - end + repair_validations(Topic) def test_default_validates_numericality_of Topic.validates_numericality_of :approved -- cgit v1.2.3 From 89b75814045e811d52b19278ae27b5f45c6d9dd6 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Fri, 19 Dec 2008 13:52:21 +0000 Subject: Add repair_helper.rb file I forgot in previous commit 8a92cd --- activerecord/test/cases/repair_helper.rb | 50 ++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 activerecord/test/cases/repair_helper.rb (limited to 'activerecord/test') diff --git a/activerecord/test/cases/repair_helper.rb b/activerecord/test/cases/repair_helper.rb new file mode 100644 index 0000000000..0155668811 --- /dev/null +++ b/activerecord/test/cases/repair_helper.rb @@ -0,0 +1,50 @@ +module ActiveRecord + module Testing + module RepairHelper + def self.included(base) + base.class_eval do + extend ClassMethods + end + end + + module Toolbox + def self.record_validations(*model_classes) + model_classes.inject({}) do |repair, klass| + repair[klass] ||= {} + [:validate, :validate_on_create, :validate_on_update].each do |callback| + the_callback = klass.instance_variable_get("@#{callback.to_s}_callbacks") + repair[klass][callback] = (the_callback.nil? ? nil : the_callback.dup) + end + repair + end + end + + def self.reset_validations(recorded) + recorded.each do |klass, repairs| + [:validate, :validate_on_create, :validate_on_update].each do |callback| + klass.instance_variable_set("@#{callback.to_s}_callbacks", repairs[callback]) + end + end + end + end + + module ClassMethods + def repair_validations(*model_classes) + setup do + @validation_repairs = ActiveRecord::Testing::RepairHelper::Toolbox.record_validations(*model_classes) + end + teardown do + ActiveRecord::Testing::RepairHelper::Toolbox.reset_validations(@validation_repairs) + end + end + end + + def repair_validations(*model_classes, &block) + validation_repairs = ActiveRecord::Testing::RepairHelper::Toolbox.record_validations(*model_classes) + return block.call + ensure + ActiveRecord::Testing::RepairHelper::Toolbox.reset_validations(validation_repairs) + end + end + end +end -- cgit v1.2.3 From c092dbef50ba207174a94b7e0beb7782a3d86649 Mon Sep 17 00:00:00 2001 From: Emilio Tagua Date: Sat, 20 Dec 2008 17:05:53 -0300 Subject: Add missing fixture to allow reload models test to run isolated [#1609 state:resolved] Signed-off-by: Pratik Naik --- activerecord/test/cases/reload_models_test.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/reload_models_test.rb b/activerecord/test/cases/reload_models_test.rb index 411b5f6afa..0d16a3526f 100644 --- a/activerecord/test/cases/reload_models_test.rb +++ b/activerecord/test/cases/reload_models_test.rb @@ -3,6 +3,8 @@ require 'models/owner' require 'models/pet' class ReloadModelsTest < ActiveRecord::TestCase + fixtures :pets + def test_has_one_with_reload pet = Pet.find_by_name('parrot') pet.owner = Owner.find_by_name('ashley') @@ -17,4 +19,4 @@ class ReloadModelsTest < ActiveRecord::TestCase pet.owner = Owner.find_by_name('ashley') assert_equal pet.owner, Owner.find_by_name('ashley') end -end \ No newline at end of file +end -- cgit v1.2.3 From 75a133f92ff7e27b83032babf829d8a58803bb3c Mon Sep 17 00:00:00 2001 From: Karthik Krishnan Date: Sat, 20 Dec 2008 20:52:48 +0000 Subject: Fix has many through not quoting table names [#1163 state:resolved] Signed-off-by: Frederick Cheung --- .../test/cases/associations/has_many_through_associations_test.rb | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index 0bfda337b2..ad6a5d6840 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -204,6 +204,10 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase assert_equal 2, people(:michael).posts.count(:include => :readers) end + def test_inner_join_with_quoted_table_name + assert_equal 2, people(:michael).jobs.size + end + def test_get_ids assert_equal [posts(:welcome).id, posts(:authorless).id].sort, people(:michael).post_ids.sort end -- cgit v1.2.3 From 6f4b2469fb19bb01fa0f53192eb49f8f2d95db1b Mon Sep 17 00:00:00 2001 From: Frederick Cheung Date: Sun, 21 Dec 2008 12:37:29 +0000 Subject: Use explicit order to stop test failing randomly --- activerecord/test/cases/associations/eager_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index 42063d18a3..a2d0efab92 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -729,12 +729,12 @@ class EagerAssociationTest < ActiveRecord::TestCase assert_equal authors(:david), assert_no_queries { posts[0].author} posts = assert_queries(2) do - Post.find(:all, :include => :author, :joins => {:taggings => :tag}, :conditions => "tags.name = 'General'") + Post.find(:all, :include => :author, :joins => {:taggings => :tag}, :conditions => "tags.name = 'General'", :order => 'posts.id') end assert_equal posts(:welcome, :thinking), posts posts = assert_queries(2) do - Post.find(:all, :include => :author, :joins => {:taggings => {:tag => :taggings}}, :conditions => "taggings_tags.super_tag_id=2") + Post.find(:all, :include => :author, :joins => {:taggings => {:tag => :taggings}}, :conditions => "taggings_tags.super_tag_id=2", :order => 'posts.id') end assert_equal posts(:welcome, :thinking), posts -- cgit v1.2.3 From b17b9371c6a26484eb1984d45acffcdcd91b1ae1 Mon Sep 17 00:00:00 2001 From: Frederick Cheung Date: Sun, 21 Dec 2008 15:38:40 +0000 Subject: Fix configure_dependency_for_has_many not quoting conditions properly [#1461 state:resolved] --- .../test/cases/associations/has_many_associations_test.rb | 13 +++++++++++++ activerecord/test/models/company.rb | 1 + 2 files changed, 14 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 816ceb6855..20b9acda44 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -665,6 +665,19 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal 1, Client.find_all_by_client_of(firm.id).size end + def test_dependent_association_respects_optional_hash_conditions_on_delete + firm = companies(:odegy) + Client.create(:client_of => firm.id, :name => "BigShot Inc.") + Client.create(:client_of => firm.id, :name => "SmallTime Inc.") + # only one of two clients is included in the association due to the :conditions key + assert_equal 2, Client.find_all_by_client_of(firm.id).size + assert_equal 1, firm.dependent_sanitized_conditional_clients_of_firm.size + firm.destroy + # only the correctly associated client should have been deleted + assert_equal 1, Client.find_all_by_client_of(firm.id).size + end + + def test_creation_respects_hash_condition ms_client = companies(:first_firm).clients_like_ms_with_hash_conditions.build diff --git a/activerecord/test/models/company.rb b/activerecord/test/models/company.rb index 0e3fafa37c..3b27a9e272 100644 --- a/activerecord/test/models/company.rb +++ b/activerecord/test/models/company.rb @@ -80,6 +80,7 @@ class ExclusivelyDependentFirm < Company has_one :account, :foreign_key => "firm_id", :dependent => :delete has_many :dependent_sanitized_conditional_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id", :dependent => :delete_all, :conditions => "name = 'BigShot Inc.'" has_many :dependent_conditional_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id", :dependent => :delete_all, :conditions => ["name = ?", 'BigShot Inc.'] + has_many :dependent_hash_conditional_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id", :dependent => :delete_all, :conditions => {:name => 'BigShot Inc.'} end class Client < Company -- cgit v1.2.3 From f7bd0beb67c5d9d50e37aa596605b91e61197fbe Mon Sep 17 00:00:00 2001 From: Daniel Luz Date: Sun, 21 Dec 2008 22:38:12 +0000 Subject: Ensure Model#last doesn't affects order for another finders inside the same scope [#1499 state:resolved] Signed-off-by: Pratik Naik --- activerecord/test/cases/method_scoping_test.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/method_scoping_test.rb b/activerecord/test/cases/method_scoping_test.rb index 6372b4f6aa..80a06116ad 100644 --- a/activerecord/test/cases/method_scoping_test.rb +++ b/activerecord/test/cases/method_scoping_test.rb @@ -27,6 +27,24 @@ class MethodScopingTest < ActiveRecord::TestCase end end + def test_scoped_find_last + highest_salary = Developer.find(:first, :order => "salary DESC") + + Developer.with_scope(:find => { :order => "salary" }) do + assert_equal highest_salary, Developer.last + end + end + + def test_scoped_find_last_preserves_scope + lowest_salary = Developer.find(:first, :order => "salary ASC") + highest_salary = Developer.find(:first, :order => "salary DESC") + + Developer.with_scope(:find => { :order => "salary" }) do + assert_equal highest_salary, Developer.last + assert_equal lowest_salary, Developer.first + end + end + def test_scoped_find_combines_conditions Developer.with_scope(:find => { :conditions => "salary = 9000" }) do assert_equal developers(:poor_jamis), Developer.find(:first, :conditions => "name = 'Jamis'") -- cgit v1.2.3 From 63aac338332a06d3c9e28dde7954679703ec7620 Mon Sep 17 00:00:00 2001 From: Luis Hurtado Date: Mon, 22 Dec 2008 15:18:43 +0000 Subject: Ensure of Model#create support custom updated_at and updated_on attributes [#1612 state:resolved] Signed-off-by: Pratik Naik --- activerecord/test/cases/base_test.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 5f54931d00..ce77ba4dbf 100755 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -16,6 +16,7 @@ require 'models/post' require 'models/comment' require 'models/minimalistic' require 'models/warehouse_thing' +require 'models/parrot' require 'rexml/document' class Category < ActiveRecord::Base; end @@ -2071,6 +2072,15 @@ class BasicsTest < ActiveRecord::TestCase ActiveRecord::Base.logger = original_logger end + def test_create_with_custom_timestamps + custom_datetime = 1.hour.ago.beginning_of_day + + %w(created_at created_on updated_at updated_on).each do |attribute| + parrot = LiveParrot.create(:name => "colombian", attribute => custom_datetime) + assert_equal custom_datetime, parrot[attribute] + end + end + private def with_kcode(kcode) if RUBY_VERSION < '1.9' -- cgit v1.2.3 From eb457ceee13779ade67e1bdebd2919d476148277 Mon Sep 17 00:00:00 2001 From: Pivotal Labs Date: Wed, 24 Dec 2008 14:57:09 -0800 Subject: Association preloading no longer stops if it hits a nil object [#1630 state:resolved] Signed-off-by: Frederick Cheung --- .../test/cases/associations/cascaded_eager_loading_test.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/associations/cascaded_eager_loading_test.rb b/activerecord/test/cases/associations/cascaded_eager_loading_test.rb index 8c9ae8a031..45e74ea024 100644 --- a/activerecord/test/cases/associations/cascaded_eager_loading_test.rb +++ b/activerecord/test/cases/associations/cascaded_eager_loading_test.rb @@ -104,6 +104,14 @@ class CascadedEagerLoadingTest < ActiveRecord::TestCase authors.first.posts.first.special_comments.first.post.very_special_comment end end + + def test_eager_association_loading_where_first_level_returns_nil + authors = Author.find(:all, :include => {:post_about_thinking => :comments}, :order => 'authors.id DESC') + assert_equal [authors(:mary), authors(:david)], authors + assert_no_queries do + authors[1].post_about_thinking.comments.first + end + end end require 'models/vertex' -- cgit v1.2.3 From 5cebe69e74d411c3c9e5f6ab9d4b2b16ee36177c Mon Sep 17 00:00:00 2001 From: Frederick Cheung Date: Fri, 19 Dec 2008 01:02:21 +0000 Subject: Preload uses exclusive scope [#643 state:resolved] With self referential associations, the scope for the the top level should not affect fetching of associations, for example when doing Person.male.find :all, :include => :friends we should load all of the friends for each male, not just the male friends. --- activerecord/test/cases/associations/eager_test.rb | 15 +++++++++++++++ activerecord/test/fixtures/people.yml | 11 ++++++++++- activerecord/test/models/person.rb | 6 ++++++ activerecord/test/schema/schema.rb | 6 ++++-- 4 files changed, 35 insertions(+), 3 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index a2d0efab92..afbd9fddf9 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -771,4 +771,19 @@ class EagerAssociationTest < ActiveRecord::TestCase assert_equal author_addresses(:david_address), authors[0].author_address end + def test_preload_belongs_to_uses_exclusive_scope + people = Person.males.find(:all, :include => :primary_contact) + assert_not_equal people.length, 0 + people.each do |person| + assert_no_queries {assert_not_nil person.primary_contact} + assert_equal Person.find(person.id).primary_contact, person.primary_contact + end + end + + def test_preload_has_many_uses_exclusive_scope + people = Person.males.find :all, :include => :agents + people.each do |person| + assert_equal Person.find(person.id).agents, person.agents + end + end end diff --git a/activerecord/test/fixtures/people.yml b/activerecord/test/fixtures/people.yml index d5a69e561d..3babb1fe59 100644 --- a/activerecord/test/fixtures/people.yml +++ b/activerecord/test/fixtures/people.yml @@ -1,6 +1,15 @@ michael: id: 1 first_name: Michael + primary_contact_id: 2 + gender: M david: id: 2 - first_name: David \ No newline at end of file + first_name: David + primary_contact_id: 3 + gender: M +susan: + id: 3 + first_name: Susan + primary_contact_id: 2 + gender: F \ No newline at end of file diff --git a/activerecord/test/models/person.rb b/activerecord/test/models/person.rb index 430d0b38f7..ec2f684a6e 100644 --- a/activerecord/test/models/person.rb +++ b/activerecord/test/models/person.rb @@ -7,4 +7,10 @@ class Person < ActiveRecord::Base has_many :jobs, :through => :references has_one :favourite_reference, :class_name => 'Reference', :conditions => ['favourite=?', true] has_many :posts_with_comments_sorted_by_comment_id, :through => :readers, :source => :post, :include => :comments, :order => 'comments.id' + + belongs_to :primary_contact, :class_name => 'Person' + has_many :agents, :class_name => 'Person', :foreign_key => 'primary_contact_id' + + named_scope :males, :conditions => { :gender => 'M' } + named_scope :females, :conditions => { :gender => 'F' } end diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index fbacc692b4..8199cb8fc7 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -298,8 +298,10 @@ ActiveRecord::Schema.define do end create_table :people, :force => true do |t| - t.string :first_name, :null => false - t.integer :lock_version, :null => false, :default => 0 + t.string :first_name, :null => false + t.references :primary_contact + t.string :gender, :limit => 1 + t.integer :lock_version, :null => false, :default => 0 end create_table :pets, :primary_key => :pet_id ,:force => true do |t| -- cgit v1.2.3 From 7db1704068b86fb2212388b14b4963526bacfa5d Mon Sep 17 00:00:00 2001 From: Frederick Cheung Date: Fri, 26 Dec 2008 22:53:07 +0000 Subject: Fix :include of has_many associations with :primary_key option --- activerecord/test/cases/associations/eager_test.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index afbd9fddf9..ff3d6ece05 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -786,4 +786,21 @@ class EagerAssociationTest < ActiveRecord::TestCase assert_equal Person.find(person.id).agents, person.agents end end + + def test_preload_has_many_using_primary_key + expected = Firm.find(:first).clients_using_primary_key.to_a + firm = Firm.find :first, :include => :clients_using_primary_key + assert_no_queries do + assert_equal expected, firm.clients_using_primary_key + end + end + + def test_include_has_many_using_primary_key + expected = Firm.find(1).clients_using_primary_key.sort_by &:name + firm = Firm.find 1, :include => :clients_using_primary_key, :order => 'clients_using_primary_keys_companies.name' + assert_no_queries do + assert_equal expected, firm.clients_using_primary_key + end + end + end -- cgit v1.2.3 From f9cab0e503a4721c9d0369f89bb85c6e658f778c Mon Sep 17 00:00:00 2001 From: Frederick Cheung Date: Fri, 26 Dec 2008 23:26:37 +0000 Subject: Fix :include of has_one with :primary_key option --- activerecord/test/cases/associations/eager_test.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index ff3d6ece05..14099d4176 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -803,4 +803,20 @@ class EagerAssociationTest < ActiveRecord::TestCase end end + def test_preload_has_one_using_primary_key + expected = Firm.find(:first).account_using_primary_key + firm = Firm.find :first, :include => :account_using_primary_key + assert_no_queries do + assert_equal expected, firm.account_using_primary_key + end + end + + def test_include_has_one_using_primary_key + expected = Firm.find(1).account_using_primary_key + firm = Firm.find(:all, :include => :account_using_primary_key, :order => 'accounts.id').detect {|f| f.id == 1} + assert_no_queries do + assert_equal expected, firm.account_using_primary_key + end + end + end -- cgit v1.2.3 From 21efba464afa2ae6e5dfd938ac8a3ce446faf7e7 Mon Sep 17 00:00:00 2001 From: Roman Shterenzon Date: Sat, 27 Dec 2008 01:10:29 +0000 Subject: Fix HasManyAssociation#create ignoring the :primary_key option [#1633 state:resolved] Signed-off-by: Frederick Cheung --- activerecord/test/cases/associations/has_many_associations_test.rb | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 20b9acda44..a5ae5cd8ec 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -1115,5 +1115,11 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert !client_association.respond_to?(:private_method) assert client_association.respond_to?(:private_method, true) end + + def test_creating_using_primary_key + firm = Firm.find(:first) + client = firm.clients_using_primary_key.create!(:name => 'test') + assert_equal firm.name, client.firm_name + end end -- cgit v1.2.3 From 6e98adfc8e19a39fa45d4acd94145d318d151964 Mon Sep 17 00:00:00 2001 From: Yaroslav Markin Date: Sat, 27 Dec 2008 16:26:13 +0300 Subject: ActiveRecord::Base#new_record? now returns false for existing records (was nil) [#1219 state:committed] Signed-off-by: David Heinemeier Hansson --- activerecord/test/cases/base_test.rb | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index ce77ba4dbf..0f03dae829 100755 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -1198,6 +1198,11 @@ class BasicsTest < ActiveRecord::TestCase assert b_true.value? end + def test_new_record_returns_boolean + assert_equal Topic.new.new_record?, true + assert_equal Topic.find(1).new_record?, false + end + def test_clone topic = Topic.find(1) cloned_topic = nil -- cgit v1.2.3 From 66ee5890c5f21995b7fe0c486547f1287afe2b55 Mon Sep 17 00:00:00 2001 From: Yaroslav Markin Date: Sun, 28 Dec 2008 22:25:55 +0300 Subject: Introduce dynamic scopes for ActiveRecord: you can now use class methods like scoped_by_user_name(user_name) and scoped_by_user_name_and_password(user_name, password) that will use the scoped method with attributes you supply. [#1648 state:committed] Signed-off-by: David Heinemeier Hansson --- activerecord/test/cases/named_scope_test.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/named_scope_test.rb b/activerecord/test/cases/named_scope_test.rb index 64e899780c..b152f95a15 100644 --- a/activerecord/test/cases/named_scope_test.rb +++ b/activerecord/test/cases/named_scope_test.rb @@ -278,3 +278,23 @@ class NamedScopeTest < ActiveRecord::TestCase assert_equal post.comments.size, Post.scoped(:joins => join).scoped(:joins => join, :conditions => "posts.id = #{post.id}").size end end + +class DynamicScopeMatchTest < ActiveRecord::TestCase + def test_scoped_by_no_match + assert_nil ActiveRecord::DynamicScopeMatch.match("not_scoped_at_all") + end + + def test_scoped_by + match = ActiveRecord::DynamicScopeMatch.match("scoped_by_age_and_sex_and_location") + assert_not_nil match + assert match.scope? + assert_equal %w(age sex location), match.attribute_names + end +end + +class DynamicScopeTest < ActiveRecord::TestCase + def test_dynamic_scope + assert_equal Post.scoped_by_author_id(1).find(1), Post.find(1) + assert_equal Post.scoped_by_author_id_and_title(1, "Welcome to the weblog").first, Post.find(:first, :conditions => { :author_id => 1, :title => "Welcome to the weblog"}) + end +end -- cgit v1.2.3 From a29369ae4ac705fbbd4ac0c0325468e50e4eeca0 Mon Sep 17 00:00:00 2001 From: Mike Gunderloy Date: Mon, 29 Dec 2008 19:14:30 -0600 Subject: Fix named scope tests for sqlite3 [#1667 state:resolved] Signed-off-by: Pratik Naik --- activerecord/test/cases/named_scope_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/named_scope_test.rb b/activerecord/test/cases/named_scope_test.rb index b152f95a15..bab842cf66 100644 --- a/activerecord/test/cases/named_scope_test.rb +++ b/activerecord/test/cases/named_scope_test.rb @@ -254,7 +254,7 @@ class NamedScopeTest < ActiveRecord::TestCase end def test_should_use_where_in_query_for_named_scope - assert_equal Developer.find_all_by_name('Jamis'), Developer.find_all_by_id(Developer.jamises) + assert_equal Developer.find_all_by_name('Jamis').to_set, Developer.find_all_by_id(Developer.jamises).to_set end def test_size_should_use_count_when_results_are_not_loaded -- cgit v1.2.3