diff options
12 files changed, 169 insertions, 34 deletions
diff --git a/activemodel/test/cases/validations/acceptance_validation_test.rb b/activemodel/test/cases/validations/acceptance_validation_test.rb index b24da3b334..88e5fdb358 100644 --- a/activemodel/test/cases/validations/acceptance_validation_test.rb +++ b/activemodel/test/cases/validations/acceptance_validation_test.rb @@ -5,6 +5,7 @@ require 'cases/tests_database' require 'models/topic' require 'models/reply' require 'models/developer' +require 'models/person' class AcceptanceValidationTest < ActiveModel::TestCase include ActiveModel::TestsDatabase @@ -52,15 +53,6 @@ class AcceptanceValidationTest < ActiveModel::TestCase assert t.save end - def test_validates_acceptance_of_as_database_column - 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_custom_error_using_quotes repair_validations(Developer) do Developer.validates_acceptance_of :salary, :message=> "This string contains 'single' and \"double\" quotes" @@ -70,4 +62,19 @@ class AcceptanceValidationTest < ActiveModel::TestCase assert_equal "This string contains 'single' and \"double\" quotes", d.errors[:salary].last end end + + def test_validates_acceptance_of_for_ruby_class + repair_validations(Person) do + Person.validates_acceptance_of :karma + + p = Person.new + p.karma = "" + + assert p.invalid? + assert_equal ["must be accepted"], p.errors[:karma] + + p.karma = "1" + assert p.valid? + end + end end diff --git a/activemodel/test/cases/validations/confirmation_validation_test.rb b/activemodel/test/cases/validations/confirmation_validation_test.rb index 3b4b27df39..1d6f2a6ec5 100644 --- a/activemodel/test/cases/validations/confirmation_validation_test.rb +++ b/activemodel/test/cases/validations/confirmation_validation_test.rb @@ -4,6 +4,7 @@ require 'cases/tests_database' require 'models/topic' require 'models/developer' +require 'models/person' class ConfirmationValidationTest < ActiveModel::TestCase include ActiveModel::TestsDatabase @@ -48,4 +49,20 @@ class ConfirmationValidationTest < ActiveModel::TestCase assert_equal ["confirm 'single' and \"double\" quotes"], d.errors[:name] end end + + def test_validates_confirmation_of_for_ruby_class + repair_validations(Person) do + Person.validates_confirmation_of :karma + + p = Person.new + p.karma_confirmation = "None" + assert p.invalid? + + assert_equal ["doesn't match confirmation"], p.errors[:karma] + + p.karma = "None" + assert p.valid? + end + end + end diff --git a/activemodel/test/cases/validations/exclusion_validation_test.rb b/activemodel/test/cases/validations/exclusion_validation_test.rb index ad3123c1e4..584f009e84 100644 --- a/activemodel/test/cases/validations/exclusion_validation_test.rb +++ b/activemodel/test/cases/validations/exclusion_validation_test.rb @@ -3,6 +3,7 @@ require 'cases/helper' require 'cases/tests_database' require 'models/topic' +require 'models/person' class ExclusionValidationTest < ActiveModel::TestCase include ActiveModel::TestsDatabase @@ -27,4 +28,19 @@ class ExclusionValidationTest < ActiveModel::TestCase assert t.errors[:title].any? assert_equal ["option monkey is restricted"], t.errors[:title] end + + def test_validates_exclusion_of_for_ruby_class + repair_validations(Person) do + Person.validates_exclusion_of :karma, :in => %w( abe monkey ) + + p = Person.new + p.karma = "abe" + assert p.invalid? + + assert_equal ["is reserved"], p.errors[:karma] + + p.karma = "Lifo" + assert p.valid? + end + end end diff --git a/activemodel/test/cases/validations/format_validation_test.rb b/activemodel/test/cases/validations/format_validation_test.rb index 301cb36e61..2c06a9dd02 100644 --- a/activemodel/test/cases/validations/format_validation_test.rb +++ b/activemodel/test/cases/validations/format_validation_test.rb @@ -3,6 +3,8 @@ require 'cases/helper' require 'cases/tests_database' require 'models/topic' +require 'models/developer' +require 'models/person' class PresenceValidationTest < ActiveModel::TestCase include ActiveModel::TestsDatabase @@ -78,4 +80,19 @@ class PresenceValidationTest < ActiveModel::TestCase assert_equal ["format 'single' and \"double\" quotes"], d.errors[:name] end end + + def test_validates_format_of_for_ruby_class + repair_validations(Person) do + Person.validates_format_of :karma, :with => /\A\d+\Z/ + + p = Person.new + p.karma = "Pixies" + assert p.invalid? + + assert_equal ["is invalid"], p.errors[:karma] + + p.karma = "1234" + assert p.valid? + end + end end diff --git a/activemodel/test/cases/validations/inclusion_validation_test.rb b/activemodel/test/cases/validations/inclusion_validation_test.rb index 678461a037..bc1b0365d2 100644 --- a/activemodel/test/cases/validations/inclusion_validation_test.rb +++ b/activemodel/test/cases/validations/inclusion_validation_test.rb @@ -3,6 +3,8 @@ require 'cases/helper' require 'cases/tests_database' require 'models/topic' +require 'models/developer' +require 'models/person' class InclusionValidationTest < ActiveModel::TestCase include ActiveModel::TestsDatabase @@ -60,4 +62,19 @@ class InclusionValidationTest < ActiveModel::TestCase assert_equal "This string contains 'single' and \"double\" quotes", d.errors[:salary].last end end + + def test_validates_inclusion_of_for_ruby_class + repair_validations(Person) do + Person.validates_inclusion_of :karma, :in => %w( abe monkey ) + + p = Person.new + p.karma = "Lifo" + assert p.invalid? + + assert_equal ["is not included in the list"], p.errors[:karma] + + p.karma = "monkey" + assert p.valid? + end + end end diff --git a/activemodel/test/cases/validations/length_validation_test.rb b/activemodel/test/cases/validations/length_validation_test.rb index ab725d9e1f..4a2f72feab 100644 --- a/activemodel/test/cases/validations/length_validation_test.rb +++ b/activemodel/test/cases/validations/length_validation_test.rb @@ -3,6 +3,8 @@ require 'cases/helper' require 'cases/tests_database' require 'models/topic' +require 'models/developer' +require 'models/person' class LengthValidationTest < ActiveModel::TestCase include ActiveModel::TestsDatabase @@ -429,4 +431,19 @@ class LengthValidationTest < ActiveModel::TestCase assert_equal ["This string contains 'single' and \"double\" quotes"], d.errors[:name] end end + + def test_validates_length_of_for_ruby_class + repair_validations(Person) do + Person.validates_length_of :karma, :minimum => 5 + + p = Person.new + p.karma = "Pix" + assert p.invalid? + + assert_equal ["is too short (minimum is 5 characters)"], p.errors[:karma] + + p.karma = "The Smiths" + assert p.valid? + end + end end diff --git a/activemodel/test/cases/validations/numericality_validation_test.rb b/activemodel/test/cases/validations/numericality_validation_test.rb index 53d3621149..0af6eb69ce 100644 --- a/activemodel/test/cases/validations/numericality_validation_test.rb +++ b/activemodel/test/cases/validations/numericality_validation_test.rb @@ -4,6 +4,7 @@ require 'cases/tests_database' require 'models/topic' require 'models/developer' +require 'models/person' class NumericalityValidationTest < ActiveModel::TestCase include ActiveModel::TestsDatabase @@ -119,6 +120,39 @@ class NumericalityValidationTest < ActiveModel::TestCase assert_equal ["greater than 4"], topic.errors[:approved] end + def test_numericality_with_getter_method + 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_numericality_with_allow_nil_and_getter_method + 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_numericality_of_for_ruby_class + repair_validations(Person) do + Person.validates_numericality_of :karma, :allow_nil => false + + p = Person.new + p.karma = "Pix" + assert p.invalid? + + assert_equal ["is not a number"], p.errors[:karma] + + p.karma = "1234" + assert p.valid? + end + end + private def invalid!(values, error = nil) @@ -142,22 +176,4 @@ class NumericalityValidationTest < ActiveModel::TestCase yield topic, value end end - - def test_numericality_with_getter_method - 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_numericality_with_allow_nil_and_getter_method - 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 end diff --git a/activemodel/test/cases/validations/presence_validation_test.rb b/activemodel/test/cases/validations/presence_validation_test.rb index 994066095b..f6bed4903a 100644 --- a/activemodel/test/cases/validations/presence_validation_test.rb +++ b/activemodel/test/cases/validations/presence_validation_test.rb @@ -3,6 +3,8 @@ require 'cases/helper' require 'cases/tests_database' require 'models/topic' +require 'models/developer' +require 'models/person' class PresenceValidationTest < ActiveModel::TestCase include ActiveModel::TestsDatabase @@ -38,4 +40,18 @@ class PresenceValidationTest < ActiveModel::TestCase assert_equal ["This string contains 'single' and \"double\" quotes"], d.errors[:non_existent] end end + + def test_validates_presence_of_for_ruby_class + repair_validations(Person) do + Person.validates_presence_of :karma + + p = Person.new + assert p.invalid? + + assert_equal ["can't be blank"], p.errors[:karma] + + p.karma = "Cold" + assert p.valid? + end + end end diff --git a/activemodel/test/models/developer.rb b/activemodel/test/models/developer.rb index a839fe1135..5e6eefeed1 100644 --- a/activemodel/test/models/developer.rb +++ b/activemodel/test/models/developer.rb @@ -1,4 +1,6 @@ class Developer < ActiveRecord::Base validates_inclusion_of :salary, :in => 50000..200000 validates_length_of :name, :within => 3..20 + + attr_accessor :name_confirmation end diff --git a/activemodel/test/models/person.rb b/activemodel/test/models/person.rb index 6f787d9d3d..d98420f900 100644 --- a/activemodel/test/models/person.rb +++ b/activemodel/test/models/person.rb @@ -1,5 +1,5 @@ class Person include ActiveModel::Validations - attr_accessor :title + attr_accessor :title, :karma end diff --git a/activemodel/test/schema.rb b/activemodel/test/schema.rb index f7fe31dc4e..56b824d445 100644 --- a/activemodel/test/schema.rb +++ b/activemodel/test/schema.rb @@ -1,13 +1,14 @@ ActiveRecord::Schema.define do create_table :topics, :force => true do |t| - t.string :title - t.string :author_name - t.text :content - t.boolean :approved, :default => true - t.string :type + t.string :title + t.string :author_name + t.text :content + t.boolean :approved, :default => true + t.string :type end create_table :developers, :force => true do |t| - t.string :name + t.string :name + t.float :salary end end diff --git a/activerecord/test/cases/validations_test.rb b/activerecord/test/cases/validations_test.rb index 27a42bcd03..69691aad02 100644 --- a/activerecord/test/cases/validations_test.rb +++ b/activerecord/test/cases/validations_test.rb @@ -141,4 +141,13 @@ class ValidationsTest < ActiveRecord::TestCase assert_equal ['tu est trops petit hombre 10'], t.errors[:title] end + + def test_validates_acceptance_of_as_database_column + 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 end |