From bdb2a2f1cb645a5dfde2de3b03f0f22b54b8b5d0 Mon Sep 17 00:00:00 2001 From: Rick Olson Date: Wed, 12 Apr 2006 20:42:13 +0000 Subject: Add :case_sensitive option to validates_uniqueness_of (closes #3090) [Rick] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4207 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/test/validations_test.rb | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'activerecord/test/validations_test.rb') diff --git a/activerecord/test/validations_test.rb b/activerecord/test/validations_test.rb index d7ba74597c..b73058a64b 100755 --- a/activerecord/test/validations_test.rb +++ b/activerecord/test/validations_test.rb @@ -304,6 +304,37 @@ class ValidationsTest < Test::Unit::TestCase assert !r3.save, "Saving r3 the third time." end + def test_validate_case_insensitive_uniqueness + Topic.validates_uniqueness_of(:title, :parent_id, :case_sensitive => false, :allow_nil => true) + + t = Topic.new("title" => "I'm unique!", :parent_id => 2) + assert t.save, "Should save t as unique" + + t.content = "Remaining unique" + assert t.save, "Should still save t as unique" + + t2 = Topic.new("title" => "I'm UNIQUE!", :parent_id => 1) + assert !t2.valid?, "Shouldn't be valid" + assert !t2.save, "Shouldn't save t2 as unique" + assert t2.errors.on(:title) + assert t2.errors.on(:parent_id) + assert_equal "has already been taken", t2.errors.on(:title) + + t2.title = "I'm truly UNIQUE!" + assert !t2.valid?, "Shouldn't be valid" + assert !t2.save, "Shouldn't save t2 as unique" + assert_nil t2.errors.on(:title) + assert t2.errors.on(:parent_id) + + t2.parent_id = 3 + assert t2.save, "Should now save t2 as unique" + + t2.parent_id = nil + t2.title = nil + assert t2.valid?, "should validate with nil" + assert t2.save, "should save with nil" + end + def test_validate_format Topic.validates_format_of(:title, :content, :with => /^Validation\smacros \w+!$/, :message => "is bad data") -- cgit v1.2.3