Permalink
Browse files

Add ActiveModel::Validations tests for regular ruby classes

  • Loading branch information...
1 parent 08a99d0 commit 6173e5bfaec44729ecabc2e6e05aa2608a85981f @lifo lifo committed Mar 20, 2009
@@ -5,6 +5,7 @@
require 'models/topic'
require 'models/reply'
require 'models/developer'
+require 'models/person'
class AcceptanceValidationTest < ActiveModel::TestCase
include ActiveModel::TestsDatabase
@@ -52,15 +53,6 @@ def test_terms_of_service_agreement_with_accept_value
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 @@ def test_validates_acceptance_of_with_custom_error_using_quotes
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
@@ -4,6 +4,7 @@
require 'models/topic'
require 'models/developer'
+require 'models/person'
class ConfirmationValidationTest < ActiveModel::TestCase
include ActiveModel::TestsDatabase
@@ -48,4 +49,20 @@ def test_validates_confirmation_of_with_custom_error_using_quotes
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
@@ -3,6 +3,7 @@
require 'cases/tests_database'
require 'models/topic'
+require 'models/person'
class ExclusionValidationTest < ActiveModel::TestCase
include ActiveModel::TestsDatabase
@@ -27,4 +28,19 @@ def test_validates_exclusion_of_with_formatted_message
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
@@ -3,6 +3,8 @@
require 'cases/tests_database'
require 'models/topic'
+require 'models/developer'
+require 'models/person'
class PresenceValidationTest < ActiveModel::TestCase
include ActiveModel::TestsDatabase
@@ -78,4 +80,19 @@ def test_validates_format_of_with_custom_error_using_quotes
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
@@ -3,6 +3,8 @@
require 'cases/tests_database'
require 'models/topic'
+require 'models/developer'
+require 'models/person'
class InclusionValidationTest < ActiveModel::TestCase
include ActiveModel::TestsDatabase
@@ -60,4 +62,19 @@ def test_validates_inclusion_of_with_custom_error_using_quotes
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
@@ -3,6 +3,8 @@
require 'cases/tests_database'
require 'models/topic'
+require 'models/developer'
+require 'models/person'
class LengthValidationTest < ActiveModel::TestCase
include ActiveModel::TestsDatabase
@@ -429,4 +431,19 @@ def test_validates_length_of_with_custom_message_using_quotes
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
@@ -4,6 +4,7 @@
require 'models/topic'
require 'models/developer'
+require 'models/person'
class NumericalityValidationTest < ActiveModel::TestCase
include ActiveModel::TestsDatabase
@@ -119,6 +120,39 @@ def test_validates_numericality_with_numeric_message
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 @@ def with_each_topic_approved_value(values)
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
@@ -3,6 +3,8 @@
require 'cases/tests_database'
require 'models/topic'
+require 'models/developer'
+require 'models/person'
class PresenceValidationTest < ActiveModel::TestCase
include ActiveModel::TestsDatabase
@@ -38,4 +40,18 @@ def test_validates_presence_of_with_custom_message_using_quotes
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
@@ -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
@@ -1,5 +1,5 @@
class Person
include ActiveModel::Validations
- attr_accessor :title
+ attr_accessor :title, :karma
end
View
@@ -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
@@ -141,4 +141,13 @@ def test_validates_length_with_globally_modified_error_message
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

0 comments on commit 6173e5b

Please sign in to comment.