diff options
author | Colin Curtin <colin@procore.com> | 2008-12-20 20:21:20 -0800 |
---|---|---|
committer | Colin Curtin <colin@procore.com> | 2008-12-20 20:21:20 -0800 |
commit | 0e121f9ed93ebb417cc63307aaaf47f8adfd220d (patch) | |
tree | 39530f289b38a5dd1b2e6b170dab2631c983aaf3 /activerecord/test | |
parent | b0187434bd7601a5b2e5f7afac99c964e566b9db (diff) | |
parent | a0d091a41604d709cf2b99c85dcf4ae3ee275b9c (diff) | |
download | rails-0e121f9ed93ebb417cc63307aaaf47f8adfd220d.tar.gz rails-0e121f9ed93ebb417cc63307aaaf47f8adfd220d.tar.bz2 rails-0e121f9ed93ebb417cc63307aaaf47f8adfd220d.zip |
Merge branch 'master' of git@github.com:lifo/docrails
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/cases/associations/eager_test.rb | 68 | ||||
-rw-r--r-- | activerecord/test/cases/associations/has_many_through_associations_test.rb | 3 | ||||
-rw-r--r-- | activerecord/test/cases/associations/has_one_through_associations_test.rb | 17 | ||||
-rw-r--r-- | activerecord/test/cases/helper.rb | 4 | ||||
-rw-r--r-- | activerecord/test/cases/repair_helper.rb | 50 | ||||
-rw-r--r-- | activerecord/test/cases/validations_test.rb | 351 | ||||
-rw-r--r-- | activerecord/test/fixtures/member_types.yml | 6 | ||||
-rw-r--r-- | activerecord/test/fixtures/members.yml | 4 | ||||
-rw-r--r-- | activerecord/test/models/member.rb | 1 | ||||
-rw-r--r-- | activerecord/test/models/member_detail.rb | 1 | ||||
-rw-r--r-- | activerecord/test/models/member_type.rb | 3 | ||||
-rw-r--r-- | activerecord/test/schema/schema.rb | 5 |
12 files changed, 350 insertions, 163 deletions
diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index 3c8408d14b..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' @@ -145,7 +146,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 +706,69 @@ 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 + + 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 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 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/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/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 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?("<error>Content Empty</error>") 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 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 |