From aaf9a45ca9ce5ceb67b7d4ae0dff350389b72c69 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 10 Dec 2004 16:02:11 +0000 Subject: Added Base.validate_uniqueness thatv alidates whether the value of the specified attributes are unique across the system. Useful for making sure that only one user can be named "davidhh". git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@108 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/test/finder_test.rb | 10 ++++++++++ activerecord/test/validations_test.rb | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/finder_test.rb b/activerecord/test/finder_test.rb index ff0ab56909..ca78fbe651 100755 --- a/activerecord/test/finder_test.rb +++ b/activerecord/test/finder_test.rb @@ -95,6 +95,16 @@ class FinderTest < Test::Unit::TestCase Company.find_first(["id=?", 2, 3, 4]) } end + + def test_bind_variables_with_quotes + Company.create("name" => "37signals' go'es agains") + assert Company.find_first(["name = ?", "37signals' go'es agains"]) + end + + def test_named_bind_variables_with_quotes + Company.create("name" => "37signals' go'es agains") + assert Company.find_first(["name = :name", {:name => "37signals' go'es agains"}]) + end def test_named_bind_variables assert_kind_of Firm, Company.find_first(["name = :name", { :name => "37signals" }]) diff --git a/activerecord/test/validations_test.rb b/activerecord/test/validations_test.rb index 2d6c795d92..1abdfa062d 100755 --- a/activerecord/test/validations_test.rb +++ b/activerecord/test/validations_test.rb @@ -172,4 +172,22 @@ class ValidationsTest < Test::Unit::TestCase assert t.save end + + def test_validate_uniqueness + Topic.validate_uniqueness(:title) + + t = Topic.new("title" => "I'm unique!") + 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!") + assert !t2.valid?, "Shouldn't be valid" + assert !t2.save, "Shouldn't save t2 as unique" + assert_equal "has already been taken", t2.errors.on(:title) + + t2.title = "Now Im really also unique" + assert t2.save, "Should now save t2 as unique" + end end \ No newline at end of file -- cgit v1.2.3