aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-05-21 10:57:18 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-05-21 10:57:18 +0000
commit10eb22cdce104326e319826af0ea4292940a9246 (patch)
tree3b57c0554d5639850754337d6f5ca29dd1ad85a7 /activerecord/test
parent2bccdba9d1403ce0010b0efa317376c01236440a (diff)
downloadrails-10eb22cdce104326e319826af0ea4292940a9246.tar.gz
rails-10eb22cdce104326e319826af0ea4292940a9246.tar.bz2
rails-10eb22cdce104326e319826af0ea4292940a9246.zip
Added the :if option to all validations that can either use a block or a method pointer to determine whether the validation should be run or not. #1324 [Duane Johnson/jhosteny]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1340 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/test')
-rwxr-xr-xactiverecord/test/validations_test.rb67
1 files changed, 65 insertions, 2 deletions
diff --git a/activerecord/test/validations_test.rb b/activerecord/test/validations_test.rb
index 4f5f5802ef..13c4a04b62 100755
--- a/activerecord/test/validations_test.rb
+++ b/activerecord/test/validations_test.rb
@@ -3,6 +3,17 @@ require 'fixtures/topic'
require 'fixtures/reply'
require 'fixtures/developer'
+# The following methods in Topic are used in test_conditional_validation_*
+class Topic
+ def condition_is_true
+ return true
+ end
+
+ def condition_is_true_but_its_not
+ return false
+ end
+end
+
class ValidationsTest < Test::Unit::TestCase
fixtures :topics, :developers
@@ -688,7 +699,7 @@ class ValidationsTest < Test::Unit::TestCase
#assert_in_delta v.to_f, t.approved, 0.0000001
end
end
-
+
def test_validates_numericality_of_int_with_string
Topic.validates_numericality_of( :approved, :only_integer => true )
["not a number","42 not a number","0xdeadbeef","0-1","--3","+-3","+3-1",nil].each do |v|
@@ -697,7 +708,7 @@ class ValidationsTest < Test::Unit::TestCase
assert t.errors.on(:approved)
end
end
-
+
def test_validates_numericality_of_int
Topic.validates_numericality_of( :approved, :only_integer => true, :allow_nil => true )
["42", "+42", "-42", "042", "0042", "-042", 42, nil,""].each do |v|
@@ -707,4 +718,56 @@ class ValidationsTest < Test::Unit::TestCase
end
end
+ def test_conditional_validation_using_method_true
+ # When the method returns true
+ Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", :if => :condition_is_true )
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
+ assert !t.valid?
+ assert t.errors.on(:title)
+ assert_equal "hoo 5", t.errors["title"]
+ end
+
+ def test_conditional_validation_using_method_false
+ # When the method returns false
+ Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", :if => :condition_is_true_but_its_not )
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
+ assert t.valid?
+ assert !t.errors.on(:title)
+ end
+
+ def test_conditional_validation_using_string_true
+ # When the evaluated string returns true
+ Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", :if => "a = 1; a == 1" )
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
+ assert !t.valid?
+ assert t.errors.on(:title)
+ assert_equal "hoo 5", t.errors["title"]
+ end
+
+ def test_conditional_validation_using_string_false
+ # When the evaluated string returns false
+ Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", :if => "false")
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
+ assert t.valid?
+ assert !t.errors.on(:title)
+ end
+
+ def test_conditional_validation_using_block_true
+ # When the block returns true
+ Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d",
+ :if => Proc.new { |r| r.content.size > 4 } )
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
+ assert !t.valid?
+ assert t.errors.on(:title)
+ assert_equal "hoo 5", t.errors["title"]
+ end
+
+ def test_conditional_validation_using_block_false
+ # When the block returns false
+ Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d",
+ :if => Proc.new { |r| r.title != "uhohuhoh"} )
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
+ assert t.valid?
+ assert !t.errors.on(:title)
+ end
end