diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2010-01-17 03:20:30 +0530 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2010-01-17 03:20:30 +0530 |
commit | b04230e3bbf912d60601e9e7b797c4cd43581d51 (patch) | |
tree | 97a2f784a2ec2bfae4f960af56a9280dad6f7774 /activemodel/test | |
parent | 867829b187969607aa12f2b0457f25da9c204db0 (diff) | |
parent | 6e3bee6cf1f0d2684152292db0a8b757249824fd (diff) | |
download | rails-b04230e3bbf912d60601e9e7b797c4cd43581d51.tar.gz rails-b04230e3bbf912d60601e9e7b797c4cd43581d51.tar.bz2 rails-b04230e3bbf912d60601e9e7b797c4cd43581d51.zip |
Merge remote branch 'mainstream/master'
Conflicts:
actionpack/lib/action_controller/metal/flash.rb
Diffstat (limited to 'activemodel/test')
13 files changed, 218 insertions, 91 deletions
diff --git a/activemodel/test/cases/helper.rb b/activemodel/test/cases/helper.rb index 30193956ea..917bb720d0 100644 --- a/activemodel/test/cases/helper.rb +++ b/activemodel/test/cases/helper.rb @@ -8,7 +8,6 @@ $:.unshift(lib) unless $:.include?('lib') || $:.include?(lib) require 'config' require 'active_model' -require 'active_model/test_case' # Show backtraces for deprecated behavior for quicker cleanup. ActiveSupport::Deprecation.debug = true diff --git a/activemodel/test/cases/tests_database.rb b/activemodel/test/cases/tests_database.rb index 79668dd941..8ca54d2678 100644 --- a/activemodel/test/cases/tests_database.rb +++ b/activemodel/test/cases/tests_database.rb @@ -2,8 +2,6 @@ require 'logger' $:.unshift(File.dirname(__FILE__) + '/../../../activerecord/lib') require 'active_record' -require 'active_record/test_case' -require 'active_record/fixtures' module ActiveModel module TestsDatabase diff --git a/activemodel/test/cases/translation_test.rb b/activemodel/test/cases/translation_test.rb index bfc1ca12e6..e25d308ca1 100644 --- a/activemodel/test/cases/translation_test.rb +++ b/activemodel/test/cases/translation_test.rb @@ -11,6 +11,11 @@ class ActiveModelI18nTests < ActiveModel::TestCase I18n.backend.store_translations 'en', :activemodel => {:attributes => {:person => {:name => 'person name attribute'} } } assert_equal 'person name attribute', Person.human_attribute_name('name') end + + def test_translated_model_attributes_with_default + I18n.backend.store_translations 'en', :attributes => { :name => 'name default attribute' } + assert_equal 'name default attribute', Person.human_attribute_name('name') + end def test_translated_model_attributes_with_symbols I18n.backend.store_translations 'en', :activemodel => {:attributes => {:person => {:name => 'person name attribute'} } } diff --git a/activemodel/test/cases/validations/i18n_generate_message_validation_test.rb b/activemodel/test/cases/validations/i18n_generate_message_validation_test.rb index 54b2405c92..6116ef71d4 100644 --- a/activemodel/test/cases/validations/i18n_generate_message_validation_test.rb +++ b/activemodel/test/cases/validations/i18n_generate_message_validation_test.rb @@ -7,42 +7,6 @@ class I18nGenerateMessageValidationTest < ActiveModel::TestCase def setup Person.reset_callbacks(:validate) @person = Person.new - - @old_load_path, @old_backend = I18n.load_path, I18n.backend - I18n.load_path.clear - I18n.backend = I18n::Backend::Simple.new - - I18n.backend.store_translations :'en', { - :activemodel => { - :errors => { - :messages => { - :inclusion => "is not included in the list", - :exclusion => "is reserved", - :invalid => "is invalid", - :confirmation => "doesn't match confirmation", - :accepted => "must be accepted", - :empty => "can't be empty", - :blank => "can't be blank", - :too_long => "is too long (maximum is {{count}} characters)", - :too_short => "is too short (minimum is {{count}} characters)", - :wrong_length => "is the wrong length (should be {{count}} characters)", - :not_a_number => "is not a number", - :greater_than => "must be greater than {{count}}", - :greater_than_or_equal_to => "must be greater than or equal to {{count}}", - :equal_to => "must be equal to {{count}}", - :less_than => "must be less than {{count}}", - :less_than_or_equal_to => "must be less than or equal to {{count}}", - :odd => "must be odd", - :even => "must be even" - } - } - } - } - end - - def teardown - I18n.load_path.replace @old_load_path - I18n.backend = @old_backend end # validates_inclusion_of: generate_message(attr_name, :inclusion, :default => configuration[:message], :value => value) diff --git a/activemodel/test/cases/validations/i18n_validation_test.rb b/activemodel/test/cases/validations/i18n_validation_test.rb index a7656fe219..7d33fcea98 100644 --- a/activemodel/test/cases/validations/i18n_validation_test.rb +++ b/activemodel/test/cases/validations/i18n_validation_test.rb @@ -9,10 +9,10 @@ class I18nValidationTest < ActiveModel::TestCase Person.reset_callbacks(:validate) @person = Person.new - @old_load_path, @old_backend = I18n.load_path, I18n.backend + @old_load_path, @old_backend = I18n.load_path.dup, I18n.backend I18n.load_path.clear I18n.backend = I18n::Backend::Simple.new - I18n.backend.store_translations('en', :activemodel => {:errors => {:messages => {:custom => nil}}}) + I18n.backend.store_translations('en', :errors => {:messages => {:custom => nil}}) end def teardown @@ -42,13 +42,13 @@ class I18nValidationTest < ActiveModel::TestCase end def test_errors_full_messages_translates_human_attribute_name_for_model_attributes - @person.errors.add('name', 'empty') - I18n.expects(:translate).with(:"person.name", :default => ['Name', 'Name'], :scope => [:activemodel, :attributes], :count => 1).returns('Name') - @person.errors.full_messages + @person.errors.add(:name, 'not found') + Person.expects(:human_attribute_name).with(:name, :default => 'Name').returns("Person's name") + assert_equal ["Person's name not found"], @person.errors.full_messages end def test_errors_full_messages_uses_format - I18n.backend.store_translations('en', :activemodel => {:errors => {:format => "Field {{attribute}} {{message}}"}}) + I18n.backend.store_translations('en', :errors => {:format => "Field {{attribute}} {{message}}"}) @person.errors.add('name', 'empty') assert_equal ["Field Name empty"], @person.errors.full_messages end @@ -254,8 +254,8 @@ class I18nValidationTest < ActiveModel::TestCase # validates_confirmation_of w/o mocha def test_validates_confirmation_of_finds_custom_model_key_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:confirmation => 'custom message'}}}}}} - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:confirmation => 'global message'}}} + I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:confirmation => 'custom message'}}}}} + I18n.backend.store_translations 'en', :errors => {:messages => {:confirmation => 'global message'}} Person.validates_confirmation_of :title @person.title_confirmation = 'foo' @@ -264,7 +264,7 @@ class I18nValidationTest < ActiveModel::TestCase end def test_validates_confirmation_of_finds_global_default_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:confirmation => 'global message'}}} + I18n.backend.store_translations 'en', :errors => {:messages => {:confirmation => 'global message'}} Person.validates_confirmation_of :title @person.title_confirmation = 'foo' @@ -275,8 +275,8 @@ class I18nValidationTest < ActiveModel::TestCase # validates_acceptance_of w/o mocha def test_validates_acceptance_of_finds_custom_model_key_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:accepted => 'custom message'}}}}}} - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:accepted => 'global message'}}} + I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:accepted => 'custom message'}}}}} + I18n.backend.store_translations 'en', :errors => {:messages => {:accepted => 'global message'}} Person.validates_acceptance_of :title, :allow_nil => false @person.valid? @@ -284,7 +284,7 @@ class I18nValidationTest < ActiveModel::TestCase end def test_validates_acceptance_of_finds_global_default_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:accepted => 'global message'}}} + I18n.backend.store_translations 'en', :errors => {:messages => {:accepted => 'global message'}} Person.validates_acceptance_of :title, :allow_nil => false @person.valid? @@ -294,8 +294,8 @@ class I18nValidationTest < ActiveModel::TestCase # validates_presence_of w/o mocha def test_validates_presence_of_finds_custom_model_key_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:blank => 'custom message'}}}}}} - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:blank => 'global message'}}} + I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:blank => 'custom message'}}}}} + I18n.backend.store_translations 'en', :errors => {:messages => {:blank => 'global message'}} Person.validates_presence_of :title @person.valid? @@ -303,7 +303,7 @@ class I18nValidationTest < ActiveModel::TestCase end def test_validates_presence_of_finds_global_default_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:blank => 'global message'}}} + I18n.backend.store_translations 'en', :errors => {:messages => {:blank => 'global message'}} Person.validates_presence_of :title @person.valid? @@ -313,8 +313,8 @@ class I18nValidationTest < ActiveModel::TestCase # validates_length_of :within w/o mocha def test_validates_length_of_within_finds_custom_model_key_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:too_short => 'custom message'}}}}}} - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:too_short => 'global message'}}} + I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:too_short => 'custom message'}}}}} + I18n.backend.store_translations 'en', :errors => {:messages => {:too_short => 'global message'}} Person.validates_length_of :title, :within => 3..5 @person.valid? @@ -322,7 +322,7 @@ class I18nValidationTest < ActiveModel::TestCase end def test_validates_length_of_within_finds_global_default_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:too_short => 'global message'}}} + I18n.backend.store_translations 'en', :errors => {:messages => {:too_short => 'global message'}} Person.validates_length_of :title, :within => 3..5 @person.valid? @@ -332,8 +332,8 @@ class I18nValidationTest < ActiveModel::TestCase # validates_length_of :is w/o mocha def test_validates_length_of_is_finds_custom_model_key_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:wrong_length => 'custom message'}}}}}} - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:wrong_length => 'global message'}}} + I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:wrong_length => 'custom message'}}}}} + I18n.backend.store_translations 'en', :errors => {:messages => {:wrong_length => 'global message'}} Person.validates_length_of :title, :is => 5 @person.valid? @@ -341,7 +341,7 @@ class I18nValidationTest < ActiveModel::TestCase end def test_validates_length_of_is_finds_global_default_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:wrong_length => 'global message'}}} + I18n.backend.store_translations 'en', :errors => {:messages => {:wrong_length => 'global message'}} Person.validates_length_of :title, :is => 5 @person.valid? @@ -351,8 +351,8 @@ class I18nValidationTest < ActiveModel::TestCase # validates_format_of w/o mocha def test_validates_format_of_finds_custom_model_key_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:invalid => 'custom message'}}}}}} - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:invalid => 'global message'}}} + I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:invalid => 'custom message'}}}}} + I18n.backend.store_translations 'en', :errors => {:messages => {:invalid => 'global message'}} Person.validates_format_of :title, :with => /^[1-9][0-9]*$/ @person.valid? @@ -360,7 +360,7 @@ class I18nValidationTest < ActiveModel::TestCase end def test_validates_format_of_finds_global_default_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:invalid => 'global message'}}} + I18n.backend.store_translations 'en', :errors => {:messages => {:invalid => 'global message'}} Person.validates_format_of :title, :with => /^[1-9][0-9]*$/ @person.valid? @@ -370,8 +370,8 @@ class I18nValidationTest < ActiveModel::TestCase # validates_inclusion_of w/o mocha def test_validates_inclusion_of_finds_custom_model_key_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:inclusion => 'custom message'}}}}}} - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:inclusion => 'global message'}}} + I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:inclusion => 'custom message'}}}}} + I18n.backend.store_translations 'en', :errors => {:messages => {:inclusion => 'global message'}} Person.validates_inclusion_of :title, :in => %w(a b c) @person.valid? @@ -379,7 +379,7 @@ class I18nValidationTest < ActiveModel::TestCase end def test_validates_inclusion_of_finds_global_default_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:inclusion => 'global message'}}} + I18n.backend.store_translations 'en', :errors => {:messages => {:inclusion => 'global message'}} Person.validates_inclusion_of :title, :in => %w(a b c) @person.valid? @@ -389,8 +389,8 @@ class I18nValidationTest < ActiveModel::TestCase # validates_exclusion_of w/o mocha def test_validates_exclusion_of_finds_custom_model_key_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:exclusion => 'custom message'}}}}}} - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:exclusion => 'global message'}}} + I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:exclusion => 'custom message'}}}}} + I18n.backend.store_translations 'en', :errors => {:messages => {:exclusion => 'global message'}} Person.validates_exclusion_of :title, :in => %w(a b c) @person.title = 'a' @@ -399,7 +399,7 @@ class I18nValidationTest < ActiveModel::TestCase end def test_validates_exclusion_of_finds_global_default_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:exclusion => 'global message'}}} + I18n.backend.store_translations 'en', :errors => {:messages => {:exclusion => 'global message'}} Person.validates_exclusion_of :title, :in => %w(a b c) @person.title = 'a' @@ -410,8 +410,8 @@ class I18nValidationTest < ActiveModel::TestCase # validates_numericality_of without :only_integer w/o mocha def test_validates_numericality_of_finds_custom_model_key_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:not_a_number => 'custom message'}}}}}} - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:not_a_number => 'global message'}}} + I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:not_a_number => 'custom message'}}}}} + I18n.backend.store_translations 'en', :errors => {:messages => {:not_a_number => 'global message'}} Person.validates_numericality_of :title @person.title = 'a' @@ -420,7 +420,7 @@ class I18nValidationTest < ActiveModel::TestCase end def test_validates_numericality_of_finds_global_default_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:not_a_number => 'global message'}}} + I18n.backend.store_translations 'en', :errors => {:messages => {:not_a_number => 'global message'}} Person.validates_numericality_of :title, :only_integer => true @person.title = 'a' @@ -431,8 +431,8 @@ class I18nValidationTest < ActiveModel::TestCase # validates_numericality_of with :only_integer w/o mocha def test_validates_numericality_of_only_integer_finds_custom_model_key_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:not_a_number => 'custom message'}}}}}} - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:not_a_number => 'global message'}}} + I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:not_a_number => 'custom message'}}}}} + I18n.backend.store_translations 'en', :errors => {:messages => {:not_a_number => 'global message'}} Person.validates_numericality_of :title, :only_integer => true @person.title = 'a' @@ -441,7 +441,7 @@ class I18nValidationTest < ActiveModel::TestCase end def test_validates_numericality_of_only_integer_finds_global_default_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:not_a_number => 'global message'}}} + I18n.backend.store_translations 'en', :errors => {:messages => {:not_a_number => 'global message'}} Person.validates_numericality_of :title, :only_integer => true @person.title = 'a' @@ -452,8 +452,8 @@ class I18nValidationTest < ActiveModel::TestCase # validates_numericality_of :odd w/o mocha def test_validates_numericality_of_odd_finds_custom_model_key_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:odd => 'custom message'}}}}}} - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:odd => 'global message'}}} + I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:odd => 'custom message'}}}}} + I18n.backend.store_translations 'en', :errors => {:messages => {:odd => 'global message'}} Person.validates_numericality_of :title, :only_integer => true, :odd => true @person.title = 0 @@ -462,7 +462,7 @@ class I18nValidationTest < ActiveModel::TestCase end def test_validates_numericality_of_odd_finds_global_default_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:odd => 'global message'}}} + I18n.backend.store_translations 'en', :errors => {:messages => {:odd => 'global message'}} Person.validates_numericality_of :title, :only_integer => true, :odd => true @person.title = 0 @@ -473,8 +473,8 @@ class I18nValidationTest < ActiveModel::TestCase # validates_numericality_of :less_than w/o mocha def test_validates_numericality_of_less_than_finds_custom_model_key_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:less_than => 'custom message'}}}}}} - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:less_than => 'global message'}}} + I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:less_than => 'custom message'}}}}} + I18n.backend.store_translations 'en', :errors => {:messages => {:less_than => 'global message'}} Person.validates_numericality_of :title, :only_integer => true, :less_than => 0 @person.title = 1 @@ -483,7 +483,7 @@ class I18nValidationTest < ActiveModel::TestCase end def test_validates_numericality_of_less_than_finds_global_default_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:less_than => 'global message'}}} + I18n.backend.store_translations 'en', :errors => {:messages => {:less_than => 'global message'}} Person.validates_numericality_of :title, :only_integer => true, :less_than => 0 @person.title = 1 @@ -494,7 +494,7 @@ class I18nValidationTest < ActiveModel::TestCase # test with validates_with def test_validations_with_message_symbol_must_translate - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:custom_error => "I am a custom error"}}} + I18n.backend.store_translations 'en', :errors => {:messages => {:custom_error => "I am a custom error"}} Person.validates_presence_of :title, :message => :custom_error @person.title = nil @person.valid? @@ -502,7 +502,7 @@ class I18nValidationTest < ActiveModel::TestCase end def test_validates_with_message_symbol_must_translate_per_attribute - I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:custom_error => "I am a custom error"}}}}}} + I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:custom_error => "I am a custom error"}}}}} Person.validates_presence_of :title, :message => :custom_error @person.title = nil @person.valid? @@ -510,7 +510,7 @@ class I18nValidationTest < ActiveModel::TestCase end def test_validates_with_message_symbol_must_translate_per_model - I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:custom_error => "I am a custom error"}}}} + I18n.backend.store_translations 'en', :errors => {:models => {:person => {:custom_error => "I am a custom error"}}} Person.validates_presence_of :title, :message => :custom_error @person.title = nil @person.valid? diff --git a/activemodel/test/cases/validations/length_validation_test.rb b/activemodel/test/cases/validations/length_validation_test.rb index f3ef5e648a..99d0268b67 100644 --- a/activemodel/test/cases/validations/length_validation_test.rb +++ b/activemodel/test/cases/validations/length_validation_test.rb @@ -221,10 +221,8 @@ class LengthValidationTest < ActiveModel::TestCase end def test_validates_length_of_nasty_params - assert_raise(ArgumentError) { Topic.validates_length_of(:title, :minimum=>6, :maximum=>9) } - assert_raise(ArgumentError) { Topic.validates_length_of(:title, :within=>6, :maximum=>9) } - assert_raise(ArgumentError) { Topic.validates_length_of(:title, :within=>6, :minimum=>9) } - assert_raise(ArgumentError) { Topic.validates_length_of(:title, :within=>6, :is=>9) } + assert_raise(ArgumentError) { Topic.validates_length_of(:title, :is=>-6) } + assert_raise(ArgumentError) { Topic.validates_length_of(:title, :within=>6) } assert_raise(ArgumentError) { Topic.validates_length_of(:title, :minimum=>"a") } assert_raise(ArgumentError) { Topic.validates_length_of(:title, :maximum=>"a") } assert_raise(ArgumentError) { Topic.validates_length_of(:title, :within=>"a") } diff --git a/activemodel/test/cases/validations/numericality_validation_test.rb b/activemodel/test/cases/validations/numericality_validation_test.rb index 75cd654f98..38b3f87e93 100644 --- a/activemodel/test/cases/validations/numericality_validation_test.rb +++ b/activemodel/test/cases/validations/numericality_validation_test.rb @@ -154,6 +154,14 @@ class NumericalityValidationTest < ActiveModel::TestCase Person.reset_callbacks(:validate) end + def test_validates_numericality_with_invalid_args + assert_raise(ArgumentError){ Topic.validates_numericality_of :approved, :greater_than_or_equal_to => "foo" } + assert_raise(ArgumentError){ Topic.validates_numericality_of :approved, :less_than_or_equal_to => "foo" } + assert_raise(ArgumentError){ Topic.validates_numericality_of :approved, :greater_than => "foo" } + assert_raise(ArgumentError){ Topic.validates_numericality_of :approved, :less_than => "foo" } + assert_raise(ArgumentError){ Topic.validates_numericality_of :approved, :equal_to => "foo" } + end + private def invalid!(values, error = nil) diff --git a/activemodel/test/cases/validations/validates_test.rb b/activemodel/test/cases/validations/validates_test.rb new file mode 100644 index 0000000000..d15fb4a524 --- /dev/null +++ b/activemodel/test/cases/validations/validates_test.rb @@ -0,0 +1,114 @@ +# encoding: utf-8 +require 'cases/helper' +require 'models/person' +require 'models/person_with_validator' +require 'validators/email_validator' + +class ValidatesTest < ActiveModel::TestCase + setup :reset_callbacks + teardown :reset_callbacks + + def reset_callbacks + Person.reset_callbacks(:validate) + PersonWithValidator.reset_callbacks(:validate) + end + + def test_validates_with_built_in_validation + Person.validates :title, :numericality => true + person = Person.new + person.valid? + assert_equal ['is not a number'], person.errors[:title] + end + + def test_validates_with_built_in_validation_and_options + Person.validates :salary, :numericality => { :message => 'my custom message' } + person = Person.new + person.valid? + assert_equal ['my custom message'], person.errors[:salary] + end + + def test_validates_with_validator_class + Person.validates :karma, :email => true + person = Person.new + person.valid? + assert_equal ['is not an email'], person.errors[:karma] + end + + def test_validates_with_if_as_local_conditions + Person.validates :karma, :presence => true, :email => { :unless => :condition_is_true } + person = Person.new + person.valid? + assert_equal ["can't be blank"], person.errors[:karma] + end + + def test_validates_with_if_as_shared_conditions + Person.validates :karma, :presence => true, :email => true, :if => :condition_is_true + person = Person.new + person.valid? + assert ["can't be blank", "is not an email"], person.errors[:karma].sort + end + + def test_validates_with_unless_shared_conditions + Person.validates :karma, :presence => true, :email => true, :unless => :condition_is_true + person = Person.new + assert person.valid? + end + + def test_validates_with_allow_nil_shared_conditions + Person.validates :karma, :length => { :minimum => 20 }, :email => true, :allow_nil => true + person = Person.new + assert person.valid? + end + + def test_validates_with_regexp + Person.validates :karma, :format => /positive|negative/ + person = Person.new + assert person.invalid? + assert_equal ['is invalid'], person.errors[:karma] + person.karma = "positive" + assert person.valid? + end + + def test_validates_with_array + Person.validates :gender, :inclusion => %w(m f) + person = Person.new + assert person.invalid? + assert_equal ['is not included in the list'], person.errors[:gender] + person.gender = "m" + assert person.valid? + end + + def test_validates_with_range + Person.validates :karma, :length => 6..20 + person = Person.new + assert person.invalid? + assert_equal ['is too short (minimum is 6 characters)'], person.errors[:karma] + person.karma = 'something' + assert person.valid? + end + + def test_validates_with_validator_class_and_options + Person.validates :karma, :email => { :message => 'my custom message' } + person = Person.new + person.valid? + assert_equal ['my custom message'], person.errors[:karma] + end + + def test_validates_with_unknown_validator + assert_raise(ArgumentError) { Person.validates :karma, :unknown => true } + end + + def test_validates_with_included_validator + PersonWithValidator.validates :title, :presence => true + person = PersonWithValidator.new + person.valid? + assert_equal ['Local validator'], person.errors[:title] + end + + def test_validates_with_included_validator_and_options + PersonWithValidator.validates :title, :presence => { :custom => ' please' } + person = PersonWithValidator.new + person.valid? + assert_equal ['Local validator please'], person.errors[:title] + end +end
\ No newline at end of file diff --git a/activemodel/test/cases/validations/with_validation_test.rb b/activemodel/test/cases/validations/with_validation_test.rb index 7540ccb580..66b072ea38 100644 --- a/activemodel/test/cases/validations/with_validation_test.rb +++ b/activemodel/test/cases/validations/with_validation_test.rb @@ -120,6 +120,28 @@ class ValidatesWithTest < ActiveRecord::TestCase Topic.validates_with(validator, :if => "1 == 1", :foo => :bar) assert topic.valid? end + + test "calls setup method of validator passing in self when validator has setup method" do + topic = Topic.new + validator = stub_everything + validator.stubs(:new).returns(validator) + validator.stubs(:validate) + validator.stubs(:respond_to?).with(:setup).returns(true) + validator.expects(:setup).with(Topic).once + Topic.validates_with(validator) + assert topic.valid? + end + + test "doesn't call setup method of validator when validator has no setup method" do + topic = Topic.new + validator = stub_everything + validator.stubs(:new).returns(validator) + validator.stubs(:validate) + validator.stubs(:respond_to?).with(:setup).returns(false) + validator.expects(:setup).with(Topic).never + Topic.validates_with(validator) + assert topic.valid? + end test "validates_with with options" do Topic.validates_with(ValidatorThatValidatesOptions, :field => :first_name) diff --git a/activemodel/test/models/custom_reader.rb b/activemodel/test/models/custom_reader.rb index 7ac70e6167..14a8be9ebc 100644 --- a/activemodel/test/models/custom_reader.rb +++ b/activemodel/test/models/custom_reader.rb @@ -8,8 +8,6 @@ class CustomReader def []=(key, value) @data[key] = value end - - private def read_attribute_for_validation(key) @data[key] diff --git a/activemodel/test/models/person.rb b/activemodel/test/models/person.rb index c83d768379..cf16a38618 100644 --- a/activemodel/test/models/person.rb +++ b/activemodel/test/models/person.rb @@ -2,7 +2,11 @@ class Person include ActiveModel::Validations extend ActiveModel::Translation - attr_accessor :title, :karma, :salary + attr_accessor :title, :karma, :salary, :gender + + def condition_is_true + true + end end class Child < Person diff --git a/activemodel/test/models/person_with_validator.rb b/activemodel/test/models/person_with_validator.rb new file mode 100644 index 0000000000..f9763ea853 --- /dev/null +++ b/activemodel/test/models/person_with_validator.rb @@ -0,0 +1,11 @@ +class PersonWithValidator + include ActiveModel::Validations + + class PresenceValidator < ActiveModel::EachValidator + def validate_each(record, attribute, value) + record.errors[attribute] << "Local validator#{options[:custom]}" if value.blank? + end + end + + attr_accessor :title, :karma +end diff --git a/activemodel/test/validators/email_validator.rb b/activemodel/test/validators/email_validator.rb new file mode 100644 index 0000000000..cff47ac230 --- /dev/null +++ b/activemodel/test/validators/email_validator.rb @@ -0,0 +1,6 @@ +class EmailValidator < ActiveModel::EachValidator + def validate_each(record, attribute, value) + record.errors[attribute] << (options[:message] || "is not an email") unless + value =~ /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i + end +end
\ No newline at end of file |