From ba22cf90377f1593028abba8ec40de49651d3e14 Mon Sep 17 00:00:00 2001 From: Nacho Caballero Date: Sat, 7 Mar 2009 18:43:52 +0100 Subject: [PATCH] Added validates_uniqueness_of error --- activerecord/lib/active_record/validations.rb | 1 + activerecord/test/cases/validations_test.rb | 121 ++++++++++++++----------- 2 files changed, 68 insertions(+), 54 deletions(-) diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index 4453047..c909e03 100644 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -721,6 +721,7 @@ module ActiveRecord finder_class = class_hierarchy.detect { |klass| !klass.abstract_class? } column = finder_class.columns_hash[attr_name.to_s] + raise ArgumentError, "No column exists with name: #{attr_name.to_s}" unless column if value.nil? comparison_operator = "IS ?" diff --git a/activerecord/test/cases/validations_test.rb b/activerecord/test/cases/validations_test.rb index f871688..bd9f2b3 100644 --- a/activerecord/test/cases/validations_test.rb +++ b/activerecord/test/cases/validations_test.rb @@ -15,6 +15,8 @@ class Topic has_many :unique_replies, :dependent => :destroy, :foreign_key => "parent_id" has_many :silly_unique_replies, :dependent => :destroy, :foreign_key => "parent_id" + has_many :users, :through => :subscribers + def condition_is_true true end @@ -53,6 +55,13 @@ end class Thaumaturgist < IneptWizard end +class Subscriber < ActiveRecord::Base + belongs_to :topic +end + +class User +end + class ValidationsTest < ActiveRecord::TestCase fixtures :topics, :developers, 'warehouse-things' @@ -170,7 +179,7 @@ class ValidationsTest < ActiveRecord::TestCase assert_equal person.first_name, "Mary", "should be ok when no attributes are passed to create!" end end - end + end def test_single_error_per_attr_iteration r = Reply.new @@ -283,7 +292,6 @@ class ValidationsTest < ActiveRecord::TestCase assert t.save end - def test_eula Topic.validates_acceptance_of(:eula, :message => "must be abided", :on => :create) @@ -323,7 +331,7 @@ class ValidationsTest < ActiveRecord::TestCase end end - def test_validate_presences + def test_validates_presences Topic.validates_presence_of(:title, :content) t = Topic.create @@ -342,7 +350,7 @@ class ValidationsTest < ActiveRecord::TestCase assert t.save end - def test_validate_uniqueness + def test_validates_uniqueness Topic.validates_uniqueness_of(:title) t = Topic.new("title" => "I'm unique!") @@ -360,14 +368,19 @@ class ValidationsTest < ActiveRecord::TestCase assert t2.save, "Should now save t2 as unique" end - def test_validates_uniquness_with_newline_chars + def test_validates_uniqueness_on_wrong_column + Subscriber.validates_uniqueness_of(:topic) # :topic isn't a column, it should be :topic_id + assert_raise(ArgumentError) { Subscriber.create(:topic => Topic.new) } + end + + def test_validates_uniqueness_with_newline_chars Topic.validates_uniqueness_of(:title, :case_sensitive => false) t = Topic.new("title" => "new\nline") assert t.save, "Should save t as unique" end - def test_validate_uniqueness_with_scope + def test_validates_uniqueness_with_scope repair_validations(Reply) do Reply.validates_uniqueness_of(:content, :scope => "parent_id") @@ -388,7 +401,7 @@ class ValidationsTest < ActiveRecord::TestCase end end - def test_validate_uniqueness_scoped_to_defining_class + def test_validates_uniqueness_scoped_to_defining_class t = Topic.create("title" => "What, me worry?") r1 = t.unique_replies.create "title" => "r1", "content" => "a barrel of fun" @@ -403,7 +416,7 @@ class ValidationsTest < ActiveRecord::TestCase assert r3.valid?, "Saving r3" end - def test_validate_uniqueness_with_scope_array + def test_validates_uniqueness_with_scope_array repair_validations(Reply) do Reply.validates_uniqueness_of(:author_name, :scope => [:author_email_address, :parent_id]) @@ -429,7 +442,7 @@ class ValidationsTest < ActiveRecord::TestCase end end - def test_validate_case_insensitive_uniqueness + def test_validates_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) @@ -464,15 +477,15 @@ class ValidationsTest < ActiveRecord::TestCase assert t_utf8.save, "Should save t_utf8 as unique" # If database hasn't UTF-8 character set, this test fails - if Topic.find(t_utf8, :select => 'LOWER(title) AS title').title == "я тоже уникальный!" - t2_utf8 = Topic.new("title" => "я тоже УНИКАЛЬНЫЙ!") + if Topic.find(t_utf8, :select => 'LOWER(title) AS title').title == "? тоже уникальный!" + t2_utf8 = Topic.new("title" => "? тоже У?ИК?ЛЬ?ЫЙ!") assert !t2_utf8.valid?, "Shouldn't be valid" assert !t2_utf8.save, "Shouldn't save t2_utf8 as unique" end end end - def test_validate_case_sensitive_uniqueness + def test_validates_case_sensitive_uniqueness Topic.validates_uniqueness_of(:title, :case_sensitive => true, :allow_nil => true) t = Topic.new("title" => "I'm unique!") @@ -496,7 +509,7 @@ class ValidationsTest < ActiveRecord::TestCase assert_not_equal "has already been taken", t3.errors.on(:title) end - def test_validate_case_sensitive_uniqueness_with_attribute_passed_as_integer + def test_validates_case_sensitive_uniqueness_with_attribute_passed_as_integer Topic.validates_uniqueness_of(:title, :case_sensitve => true) t = Topic.create!('title' => 101) @@ -505,7 +518,7 @@ class ValidationsTest < ActiveRecord::TestCase assert t2.errors.on(:title) end - def test_validate_uniqueness_with_non_standard_table_names + def test_validates_uniqueness_with_non_standard_table_names i1 = WarehouseThing.create(:value => 1000) assert !i1.valid?, "i1 should not be valid" assert i1.errors.on(:value), "Should not be empty" @@ -522,7 +535,7 @@ class ValidationsTest < ActiveRecord::TestCase end end - def test_validate_uniqueness_with_columns_which_are_sql_keywords + def test_validates_uniqueness_with_columns_which_are_sql_keywords repair_validations(Guid) do Guid.validates_uniqueness_of :key g = Guid.new @@ -531,7 +544,7 @@ class ValidationsTest < ActiveRecord::TestCase end end - def test_validate_uniqueness_with_limit + def test_validates_uniqueness_with_limit # Event.title is limited to 5 characters e1 = Event.create(:title => "abcde") assert e1.valid?, "Could not create an event with a unique, 5 character title" @@ -539,7 +552,7 @@ class ValidationsTest < ActiveRecord::TestCase assert !e2.valid?, "Created an event whose title, with limit taken into account, is not unique" end - def test_validate_straight_inheritance_uniqueness + def test_validates_straight_inheritance_uniqueness w1 = IneptWizard.create(:name => "Rincewind", :city => "Ankh-Morpork") assert w1.valid?, "Saving w1" @@ -568,7 +581,7 @@ class ValidationsTest < ActiveRecord::TestCase assert_equal "has already been taken", w6.errors.on(:city), "Should have uniqueness message for city" end - def test_validate_format + def test_validates_format Topic.validates_format_of(:title, :content, :with => /^Validation\smacros \w+!$/, :message => "is bad data") t = Topic.create("title" => "i'm incorrect", "content" => "Validation macros rule!") @@ -585,7 +598,7 @@ class ValidationsTest < ActiveRecord::TestCase assert_raise(ArgumentError) { Topic.validates_format_of(:title, :content) } end - def test_validate_format_with_allow_blank + def test_validates_format_with_allow_blank Topic.validates_format_of(:title, :with => /^Validation\smacros \w+!$/, :allow_blank=>true) assert !Topic.create("title" => "Shouldn't be valid").valid? assert Topic.create("title" => "").valid? @@ -594,7 +607,7 @@ class ValidationsTest < ActiveRecord::TestCase end # testing ticket #3142 - def test_validate_format_numeric + def test_validates_format_numeric Topic.validates_format_of(:title, :content, :with => /^[1-9][0-9]*$/, :message => "is bad data") t = Topic.create("title" => "72x", "content" => "6789") @@ -621,7 +634,7 @@ class ValidationsTest < ActiveRecord::TestCase assert_nil t.errors.on(:title) end - def test_validate_format_with_formatted_message + def test_validates_format_with_formatted_message Topic.validates_format_of(:title, :with => /^Valid Title$/, :message => "can't be {{value}}") t = Topic.create(:title => 'Invalid title') assert_equal "can't be Invalid title", t.errors.on(:title) @@ -1063,20 +1076,20 @@ class ValidationsTest < ActiveRecord::TestCase def test_optionally_validates_length_of_using_within_on_create_utf8 with_kcode('UTF8') do - Topic.validates_length_of :title, :within => 5..10, :on => :create, :too_long => "長すぎます: {{count}}" + Topic.validates_length_of :title, :within => 5..10, :on => :create, :too_long => "長????: {{count}}" - t = Topic.create("title" => "一二三四五六七八九十A", "content" => "whatever") + t = Topic.create("title" => "一二三四五六七八???A", "content" => "whatever") assert !t.save assert t.errors.on(:title) - assert_equal "長すぎます: 10", t.errors[:title] + assert_equal "長????: 10", t.errors[:title] - t.title = "一二三四五六七八九" + t.title = "一二三四五六七八?" assert t.save t.title = "一二3" assert t.save - t.content = "一二三四五六七八九十" + t.content = "一二三四五六七八???" assert t.save t.content = t.title = "一二三四五六" @@ -1086,7 +1099,7 @@ class ValidationsTest < ActiveRecord::TestCase def test_optionally_validates_length_of_using_within_on_update_utf8 with_kcode('UTF8') do - Topic.validates_length_of :title, :within => 5..10, :on => :update, :too_short => "短すぎます: {{count}}" + Topic.validates_length_of :title, :within => 5..10, :on => :update, :too_short => "短????: {{count}}" t = Topic.create("title" => "一二三4", "content" => "whatever") assert !t.save @@ -1095,9 +1108,9 @@ class ValidationsTest < ActiveRecord::TestCase t.title = "1二三4" assert !t.save assert t.errors.on(:title) - assert_equal "短すぎます: 5", t.errors[:title] + assert_equal "短????: 5", t.errors[:title] - t.title = "一二三四五六七八九十A" + t.title = "一二三四五六七八???A" assert !t.save assert t.errors.on(:title) @@ -1122,7 +1135,7 @@ class ValidationsTest < ActiveRecord::TestCase def test_validates_length_of_with_block Topic.validates_length_of :content, :minimum => 5, :too_short=>"Your essay must be at least {{count}} words.", - :tokenizer => lambda {|str| str.scan(/\w+/) } + :tokenizer => lambda {|str| str.scan(/\w+/) } t = Topic.create!(:content => "this content should be long enough") assert t.valid? @@ -1136,10 +1149,10 @@ class ValidationsTest < ActiveRecord::TestCase repair_validations(Owner) do with_kcode('UTF8') do assert_nothing_raised { Owner.validates_size_of :pets, :minimum => 1 } - o = Owner.new('name' => 'あいうえおかきくけこ') + o = Owner.new('name' => '????????????') assert !o.save assert o.errors.on(:pets) - o.pets.build('name' => 'あいうえおかきくけこ') + o.pets.build('name' => '????????????') assert o.valid? end end @@ -1172,7 +1185,7 @@ class ValidationsTest < ActiveRecord::TestCase end end - def test_validate_block + def test_validates_block Topic.validate { |topic| topic.errors.add("title", "will never be valid") } t = Topic.create("title" => "Title", "content" => "whatever") assert !t.valid? @@ -1422,12 +1435,12 @@ class ValidationsTest < ActiveRecord::TestCase end def test_validation_order - Topic.validates_presence_of :title - Topic.validates_length_of :title, :minimum => 2 + Topic.validates_presence_of :title + Topic.validates_length_of :title, :minimum => 2 - t = Topic.new("title" => "") - assert !t.valid? - assert_equal "can't be blank", t.errors.on("title").first + t = Topic.new("title" => "") + assert !t.valid? + assert_equal "can't be blank", t.errors.on("title").first end # previous implementation of validates_presence_of eval'd the @@ -1567,25 +1580,25 @@ class ValidatesNumericalityTest < ActiveRecord::TestCase end private - def invalid!(values, error=nil) - with_each_topic_approved_value(values) do |topic, value| - assert !topic.valid?, "#{value.inspect} not rejected as a number" - assert topic.errors.on(:approved) - assert_equal error, topic.errors.on(:approved) if error - end + def invalid!(values, error=nil) + with_each_topic_approved_value(values) do |topic, value| + assert !topic.valid?, "#{value.inspect} not rejected as a number" + assert topic.errors.on(:approved) + assert_equal error, topic.errors.on(:approved) if error end + end - def valid!(values) - with_each_topic_approved_value(values) do |topic, value| - assert topic.valid?, "#{value.inspect} not accepted as a number" - end + def valid!(values) + with_each_topic_approved_value(values) do |topic, value| + assert topic.valid?, "#{value.inspect} not accepted as a number" end + end - def with_each_topic_approved_value(values) - topic = Topic.new("title" => "numeric test", "content" => "whatever") - values.each do |value| - topic.approved = value - yield topic, value - end + def with_each_topic_approved_value(values) + topic = Topic.new("title" => "numeric test", "content" => "whatever") + values.each do |value| + topic.approved = value + yield topic, value end + end end -- 1.6.1.9.g97c34