aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2004-12-16 17:00:22 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2004-12-16 17:00:22 +0000
commitd2b75a083af8db6704d195e1d3a3035cdcb65c99 (patch)
tree256e1e31e79dd4424952601aaf6e4b60e8d24166 /activerecord/test
parentf033833fb9f48927995e8bc521ae032888813989 (diff)
downloadrails-d2b75a083af8db6704d195e1d3a3035cdcb65c99.tar.gz
rails-d2b75a083af8db6704d195e1d3a3035cdcb65c99.tar.bz2
rails-d2b75a083af8db6704d195e1d3a3035cdcb65c99.zip
Added Base.validates_inclusion_of
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@192 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/test')
-rwxr-xr-xactiverecord/test/validations_test.rb133
1 files changed, 119 insertions, 14 deletions
diff --git a/activerecord/test/validations_test.rb b/activerecord/test/validations_test.rb
index 92c0a71c35..b6c9cfed7d 100755
--- a/activerecord/test/validations_test.rb
+++ b/activerecord/test/validations_test.rb
@@ -189,20 +189,6 @@ class ValidationsTest < Test::Unit::TestCase
t2.title = "Now Im really also unique"
assert t2.save, "Should now save t2 as unique"
end
-
- def test_validate_boundaries
- Topic.validates_boundaries_of(:title, :content, :within => 3..5)
-
- t = Topic.create("title" => "a!", "content" => "I'm ooooooooh so very long")
- assert !t.save
- assert_equal "is too short (min is 3 characters)", t.errors.on(:title)
- assert_equal "is too long (max is 5 characters)", t.errors.on(:content)
-
- t.title = "abe"
- t.content = "mad"
-
- assert t.save
- end
def test_validate_format
Topic.validates_format_of(:title, :content, :with => /^Validation macros rule!$/, :message => "is bad data")
@@ -243,4 +229,123 @@ class ValidationsTest < Test::Unit::TestCase
assert_nothing_raised(ArgumentError) { Topic.validates_inclusion_of( :title, :in => {} ) }
assert_nothing_raised(ArgumentError) { Topic.validates_inclusion_of( :title, :in => [] ) }
end
+
+ def test_validates_length_of_using_minimum
+ Topic.validates_length_of( :title, :minimum=>5 )
+ t = Topic.create("title" => "valid", "content" => "whatever")
+ assert t.valid?
+ t.title = "not"
+ assert !t.valid?
+ assert t.errors.on(:title)
+ assert_equal "is too short (min is 5 characters)", t.errors["title"]
+ t.title = ""
+ assert !t.valid?
+ assert t.errors.on(:title)
+ t.title = nil
+ assert !t.valid?
+ assert_equal "is too short (min is 5 characters)", t.errors["title"]
+ assert t.errors.on(:title)
+ end
+
+ def test_validates_length_of_using_maximum
+ Topic.validates_length_of( :title, :maximum=>5 )
+ t = Topic.create("title" => "valid", "content" => "whatever")
+ assert t.valid?
+ t.title = "notvalid"
+ assert !t.valid?
+ assert t.errors.on(:title)
+ assert_equal "is too long (max is 5 characters)", t.errors["title"]
+ t.title = ""
+ assert t.valid?
+ t.title = nil
+ assert t.valid?
+ end
+
+ def test_validates_length_of_using_within
+ Topic.validates_length_of(:title, :content, :within => 3..5)
+
+ t = Topic.create("title" => "a!", "content" => "I'm ooooooooh so very long")
+ assert !t.save
+ assert_equal "is too short (min is 3 characters)", t.errors.on(:title)
+ assert_equal "is too long (max is 5 characters)", t.errors.on(:content)
+
+ t.title = "abe"
+ t.content = "mad"
+
+ assert t.save
+ end
+
+ def test_validates_length_of_using_is
+ Topic.validates_length_of( :title, :is=>5 )
+ t = Topic.create("title" => "valid", "content" => "whatever")
+ assert t.valid?
+ t.title = "notvalid"
+ assert !t.valid?
+ assert t.errors.on(:title)
+ assert_equal "is the wrong length (should be 5 characters)", t.errors["title"]
+ t.title = ""
+ assert !t.valid?
+ t.title = nil
+ assert !t.valid?
+ 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, :minimum=>"a") }
+ assert_raise(ArgumentError) { Topic.validates_length_of(:title, :maximum=>"a") }
+ assert_raise(ArgumentError) { Topic.validates_length_of(:title, :within=>"a") }
+ assert_raise(ArgumentError) { Topic.validates_length_of(:title, :is=>"a") }
+ end
+
+ def test_validates_length_of_custom_errors_for_minimum_with_message
+ Topic.validates_length_of( :title, :minimum=>5, :message=>"boo %d" )
+ t = Topic.create("title" => "uhoh", "content" => "whatever")
+ assert !t.valid?
+ assert t.errors.on(:title)
+ assert_equal "boo 5", t.errors["title"]
+ end
+
+ def test_validates_length_of_custom_errors_for_minimum_with_too_short
+ Topic.validates_length_of( :title, :minimum=>5, :too_short=>"hoo %d" )
+ t = Topic.create("title" => "uhoh", "content" => "whatever")
+ assert !t.valid?
+ assert t.errors.on(:title)
+ assert_equal "hoo 5", t.errors["title"]
+ end
+
+ def test_validates_length_of_custom_errors_for_maximum_with_message
+ Topic.validates_length_of( :title, :maximum=>5, :message=>"boo %d" )
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
+ assert !t.valid?
+ assert t.errors.on(:title)
+ assert_equal "boo 5", t.errors["title"]
+ end
+
+ def test_validates_length_of_custom_errors_for_maximum_with_too_long
+ Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d" )
+ 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_validates_length_of_custom_errors_for_is_with_message
+ Topic.validates_length_of( :title, :is=>5, :message=>"boo %d" )
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
+ assert !t.valid?
+ assert t.errors.on(:title)
+ assert_equal "boo 5", t.errors["title"]
+ end
+
+ def test_validates_length_of_custom_errors_for_is_with_wrong_length
+ Topic.validates_length_of( :title, :is=>5, :wrong_length=>"hoo %d" )
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
+ assert !t.valid?
+ assert t.errors.on(:title)
+ assert_equal "hoo 5", t.errors["title"]
+ end
+
end