test/rails/macros/validates_uniqueness_test.rb in whyvalidationssuckin96-1.6.1 vs test/rails/macros/validates_uniqueness_test.rb in whyvalidationssuckin96-1.6.2
- old
+ new
@@ -1,43 +1,49 @@
require 'teststrap'
require 'rails/active_record_test_helper'
context "validates uniqueness" do
-
+
should "add a validation macro" do
- WhyValidationsSuckIn96::ValidationBuilder.instance_methods
+ WhyValidationsSuckIn96::ValidationBuilder.instance_methods.map {|im| im.to_s}
end.includes('validates_uniqueness_of')
-
+
context "with some default options" do
setup do
WhyValidationsSuckIn96::ValidatesUniqueness.new(Object.new, :attribute => :username)
end
-
+
should "have a message accessor with a default message" do
topic.message
end.equals("has already been taken")
-
+
should "default to being case insensitive" do
topic.options[:case_sensitive]
end.equals(false)
-
+
should "default :base_class_scope to true" do
topic.options[:base_class_scope]
end.equals(true)
end # with some default options
-
+
+ teardown do
+ next unless topic.kind_of?(Class)
+ Object.send(:remove_const, topic.name)
+ end
+
context "validating an object" do
-
+
context "with default options" do
setup do
- Class.new(VisualWork) do
+ class ::TestVisualWork < VisualWork
setup_validations do
validates_uniqueness_of :name
end
+ self
end # VisualWork
end # setup do
-
+
should "mark the second object with the same name as invalid" do
work = topic.new(:name => "example")
work.save!
other_work = topic.new(:name => "example")
!other_work.valid? && other_work.failed_validations.detect do |fv|
@@ -49,34 +55,35 @@
work = topic.new(:name => "example one")
work.save!
other = topic.new(:name => "example two")
other.valid?
end
-
+
should "not care about case sensitivity by default" do
work = topic.new(:name => "cAsE SeNsItiViTy")
work.save!
other_work = topic.new(:name => "case sensitivity")
!other_work.valid? && other_work.failed_validations.detect do |fv|
fv.kind_of?(WhyValidationsSuckIn96::ValidatesUniqueness) && fv.attribute == :name
end
end
-
+
should "allow the one object to be saved twice and not violate the constraint" do
work = topic.new(:name => "screenshot")
work.save
work.author = "gabe"
work.save
end
end # with default options
context "uniqueness of integers" do
setup do
- Class.new(Chapter) do
+ class ::TestChapter < Chapter
setup_validations do
validates_uniqueness_of :number, :scope_to => :book_id
end
+ self
end
end
should "not allow two objects with the same number" do
chapter = topic.new(:name => "A Dark and Stormy Night",
@@ -87,82 +94,83 @@
!other_chapter.valid? && other_chapter.failed_validations.detect do |fv|
fv.kind_of?(WhyValidationsSuckIn96::ValidatesUniqueness) && fv.attribute == :number
end
end
end
-
+
context "specifying case sensitivity" do
setup do
- Class.new(VisualWork) do
+ class ::TestVisualWork < VisualWork
setup_validations do
validates_uniqueness_of :name, :case_sensitive => true
end
+ self
end # VisualWork
end # setup do
-
+
should "allow objects with the same name and different case to be valid" do
work = topic.create!(:name => "CASE SENSITIVE IS TRUE")
other_work = topic.new(:name => "case sensitive is true")
other_work.valid?
end
-
+
should "mark the second object with the name in the same case as invalid" do
work = topic.create(:name => "SO WHAT")
other_work = topic.new(:name => "SO WHAT")
!other_work.valid? && other_work.failed_validations.detect do |fv|
fv.kind_of?(WhyValidationsSuckIn96::ValidatesUniqueness) && fv.attribute == :name
end
end
end # specifying case sensitivity
-
+
context "specifying base class scope" do
context "as false" do
should "allow records to be created in different subclasses that violate the uniqueness" do
Painting.create!(:name => "painting one", :author => "humbug")
Photograph.new(:name => "photo one", :author => "humbug").valid?
- end
-
+ end
+
should "not allow records to be created in the same class that violate the uniqueness" do
Photograph.create!(:name => "photo one", :author => "gus")
photo = Photograph.new(:name => "photo two", :author => "gus")
!photo.valid? && photo.failed_validations.detect do |fv|
fv.kind_of?(WhyValidationsSuckIn96::ValidatesUniqueness) && fv.attribute == :author
end
end
end # as false
-
+
context "as true" do
should "not allow records to be created that violate a uniqueness constraint based on the base class" do
VisualWork.create!(:name => "mspaint", :author => "dan")
work = Painting.new(:name => "the treachery of images", :author => "dan")
!work.valid? && work.failed_validations.detect do |fv|
fv.kind_of?(WhyValidationsSuckIn96::ValidatesUniqueness) && fv.attribute == :author
end
end
-
+
should "allow records to be created that don't violate the uniqueness constraint" do
VisualWork.create!(:name => "photoshop", :author => "alex")
Painting.create!(:name => "paintshop", :author => "evan")
end
end # as true
end # specifying base class scope
-
+
context "specifying scope options" do
should "pass when uniqueness is valid against the given scope column" do
Collage.create!(:name => "wired snippets", :author => "dan")
work = Collage.new(:name => "wired snippets", :author => "gabe")
work.valid?
end
-
+
should "fail when uniqueness is invalid against the given scope column" do
Collage.create!(:name => "rolling stone snippets", :author => "gabe")
work = Collage.new(:name => "rolling stone snippets", :author => "gabe")
!work.valid? && work.failed_validations.detect do |fv|
fv.kind_of?(WhyValidationsSuckIn96::ValidatesUniqueness) && fv.attribute == :name
end
end
end # specifying scope options
-
+
context "when using with_scope" do
should "basically just ignore with_scope" do
Painting.create!(:author => "fred", :name => "bbzzzt")
Painting.send(:with_scope, :find => {:conditions => {:author => "fred"}}) do
Painting.create!(:author => "myles", :name => "bitchesbrew")