test/unit/test_validations.rb in mongo_mapper-0.8.6 vs test/unit/test_validations.rb in mongo_mapper-0.9.0
- old
+ new
@@ -2,33 +2,13 @@
class ValidationsTest < Test::Unit::TestCase
context "Validations" do
context "on a Document" do
setup do
- @document = Doc()
+ @document = Doc('John')
end
- context "Validating uniquness of" do
- should "not validate nil when allow_nil false" do
- @document.key :name, String
- @document.validates_uniqueness_of :name, :allow_nil => false
- doc = @document.new(:name => nil)
- doc.should have_error_on(:name)
- doc.name = "Ryan"
- doc.should_not have_error_on(:name)
- end
-
- should "not validate blank when allow_blank false" do
- @document.key :name, String
- @document.validates_uniqueness_of :name, :allow_blank => false
- doc = @document.new(:name => "")
- doc.should have_error_on(:name)
- doc.name = "Ryan"
- doc.should_not have_error_on(:name)
- end
- end
-
context "Validating acceptance of" do
should "work with validates_acceptance_of macro" do
@document.key :terms, String
@document.validates_acceptance_of :terms
doc = @document.new(:terms => '')
@@ -40,13 +20,17 @@
context "validating confirmation of" do
should "work with validates_confirmation_of macro" do
@document.key :password, String
@document.validates_confirmation_of :password
+
+ # NOTE: Api change as ActiveModel passes if password_confirmation is nil
doc = @document.new
doc.password = 'foobar'
+ doc.password_confirmation = 'foobar1'
doc.should have_error_on(:password)
+
doc.password_confirmation = 'foobar'
doc.should_not have_error_on(:password)
end
end
@@ -158,23 +142,23 @@
end
end
end # numericality of
context "validating presence of" do
- should "work with validates_presence_of macro" do
- @document.key :name, String
- @document.validates_presence_of :name
- doc = @document.new
- doc.should have_error_on(:name)
- end
+ should "work with validates_presence_of macro" do
+ @document.key :name, String
+ @document.validates_presence_of :name
+ doc = @document.new
+ doc.should have_error_on(:name)
+ end
- should "work with :required shortcut on key definition" do
- @document.key :name, String, :required => true
- doc = @document.new
- doc.should have_error_on(:name)
- end
- end
+ should "work with :required shortcut on key definition" do
+ @document.key :name, String, :required => true
+ doc = @document.new
+ doc.should have_error_on(:name)
+ end
+ end
context "validating exclusion of" do
should "throw error if enumerator not provided" do
@document.key :action, String
lambda {
@@ -182,11 +166,11 @@
}.should raise_error(ArgumentError)
end
should "work with validates_exclusion_of macro" do
@document.key :action, String
- @document.validates_exclusion_of :action, :within => %w(kick run)
+ @document.validates_exclusion_of :action, :in => %w(kick run)
doc = @document.new
doc.should_not have_error_on(:action)
doc.action = 'fart'
@@ -209,19 +193,19 @@
doc.should have_error_on(:action, 'is reserved')
end
should "not have error if allow nil is true and value is nil" do
@document.key :action, String
- @document.validates_exclusion_of :action, :within => %w(kick run), :allow_nil => true
+ @document.validates_exclusion_of :action, :in => %w(kick run), :allow_nil => true
doc = @document.new
doc.should_not have_error_on(:action)
end
should "not have error if allow blank is true and value is blank" do
@document.key :action, String
- @document.validates_exclusion_of :action, :within => %w(kick run), :allow_nil => true
+ @document.validates_exclusion_of :action, :in => %w(kick run), :allow_nil => true
doc = @document.new(:action => '')
doc.should_not have_error_on(:action)
end
end
@@ -234,46 +218,46 @@
}.should raise_error(ArgumentError)
end
should "work with validates_inclusion_of macro" do
@document.key :action, String
- @document.validates_inclusion_of :action, :within => %w(kick run)
+ @document.validates_inclusion_of :action, :in => %w(kick run)
doc = @document.new
- doc.should have_error_on(:action, 'is not in the list')
+ doc.should have_error_on(:action, 'is not included in the list')
doc.action = 'fart'
- doc.should have_error_on(:action, 'is not in the list')
+ doc.should have_error_on(:action, 'is not included in the list')
doc.action = 'kick'
doc.should_not have_error_on(:action)
end
should "work with :in shortcut on key definition" do
@document.key :action, String, :in => %w(kick run)
doc = @document.new
- doc.should have_error_on(:action, 'is not in the list')
+ doc.should have_error_on(:action, 'is not included in the list')
doc.action = 'fart'
- doc.should have_error_on(:action, 'is not in the list')
+ doc.should have_error_on(:action, 'is not included in the list')
doc.action = 'kick'
doc.should_not have_error_on(:action)
end
should "not have error if allow nil is true and value is nil" do
@document.key :action, String
- @document.validates_inclusion_of :action, :within => %w(kick run), :allow_nil => true
+ @document.validates_inclusion_of :action, :in => %w(kick run), :allow_nil => true
doc = @document.new
doc.should_not have_error_on(:action)
end
should "not have error if allow blank is true and value is blank" do
@document.key :action, String
- @document.validates_inclusion_of :action, :within => %w(kick run), :allow_blank => true
+ @document.validates_inclusion_of :action, :in => %w(kick run), :allow_blank => true
doc = @document.new(:action => '')
doc.should_not have_error_on(:action)
end
end
@@ -300,10 +284,11 @@
should "work with validates_confirmation_of macro" do
@embedded_doc.key :password, String
@embedded_doc.validates_confirmation_of :password
doc = @embedded_doc.new
doc.password = 'foobar'
+ doc.password_confirmation = 'foobar1'
doc.should have_error_on(:password)
doc.password_confirmation = 'foobar'
doc.should_not have_error_on(:password)
end
end
@@ -416,23 +401,23 @@
end
end
end # numericality of
context "validating presence of" do
- should "work with validates_presence_of macro" do
- @embedded_doc.key :name, String
- @embedded_doc.validates_presence_of :name
- doc = @embedded_doc.new
- doc.should have_error_on(:name)
- end
+ should "work with validates_presence_of macro" do
+ @embedded_doc.key :name, String
+ @embedded_doc.validates_presence_of :name
+ doc = @embedded_doc.new
+ doc.should have_error_on(:name)
+ end
- should "work with :required shortcut on key definition" do
- @embedded_doc.key :name, String, :required => true
- doc = @embedded_doc.new
- doc.should have_error_on(:name)
- end
- end
+ should "work with :required shortcut on key definition" do
+ @embedded_doc.key :name, String, :required => true
+ doc = @embedded_doc.new
+ doc.should have_error_on(:name)
+ end
+ end
context "validating exclusion of" do
should "throw error if enumerator not provided" do
@embedded_doc.key :action, String
lambda {
@@ -440,11 +425,11 @@
}.should raise_error(ArgumentError)
end
should "work with validates_exclusion_of macro" do
@embedded_doc.key :action, String
- @embedded_doc.validates_exclusion_of :action, :within => %w(kick run)
+ @embedded_doc.validates_exclusion_of :action, :in => %w(kick run)
doc = @embedded_doc.new
doc.should_not have_error_on(:action)
doc.action = 'fart'
@@ -467,19 +452,19 @@
doc.should have_error_on(:action, 'is reserved')
end
should "not have error if allow nil is true and value is nil" do
@embedded_doc.key :action, String
- @embedded_doc.validates_exclusion_of :action, :within => %w(kick run), :allow_nil => true
+ @embedded_doc.validates_exclusion_of :action, :in => %w(kick run), :allow_nil => true
doc = @embedded_doc.new
doc.should_not have_error_on(:action)
end
should "not have error if allow blank is true and value is blank" do
@embedded_doc.key :action, String
- @embedded_doc.validates_exclusion_of :action, :within => %w(kick run), :allow_nil => true
+ @embedded_doc.validates_exclusion_of :action, :in => %w(kick run), :allow_nil => true
doc = @embedded_doc.new(:action => '')
doc.should_not have_error_on(:action)
end
end
@@ -492,46 +477,46 @@
}.should raise_error(ArgumentError)
end
should "work with validates_inclusion_of macro" do
@embedded_doc.key :action, String
- @embedded_doc.validates_inclusion_of :action, :within => %w(kick run)
+ @embedded_doc.validates_inclusion_of :action, :in => %w(kick run)
doc = @embedded_doc.new
- doc.should have_error_on(:action, 'is not in the list')
+ doc.should have_error_on(:action, 'is not included in the list')
doc.action = 'fart'
- doc.should have_error_on(:action, 'is not in the list')
+ doc.should have_error_on(:action, 'is not included in the list')
doc.action = 'kick'
doc.should_not have_error_on(:action)
end
should "work with :in shortcut on key definition" do
@embedded_doc.key :action, String, :in => %w(kick run)
doc = @embedded_doc.new
- doc.should have_error_on(:action, 'is not in the list')
+ doc.should have_error_on(:action, 'is not included in the list')
doc.action = 'fart'
- doc.should have_error_on(:action, 'is not in the list')
+ doc.should have_error_on(:action, 'is not included in the list')
doc.action = 'kick'
doc.should_not have_error_on(:action)
end
should "not have error if allow nil is true and value is nil" do
@embedded_doc.key :action, String
- @embedded_doc.validates_inclusion_of :action, :within => %w(kick run), :allow_nil => true
+ @embedded_doc.validates_inclusion_of :action, :in => %w(kick run), :allow_nil => true
doc = @embedded_doc.new
doc.should_not have_error_on(:action)
end
should "not have error if allow blank is true and value is blank" do
@embedded_doc.key :action, String
- @embedded_doc.validates_inclusion_of :action, :within => %w(kick run), :allow_blank => true
+ @embedded_doc.validates_inclusion_of :action, :in => %w(kick run), :allow_blank => true
doc = @embedded_doc.new(:action => '')
doc.should_not have_error_on(:action)
end
end
@@ -539,26 +524,26 @@
end # End on an EmbeddedDocument
end # Validations
context "Adding validation errors" do
- setup do
- @document = Doc do
- key :action, String
- def action_present
- errors.add(:action, 'is invalid') if action.blank?
+ setup do
+ @document = Doc do
+ key :action, String
+ def action_present
+ errors.add(:action, 'is invalid') if action.blank?
+ end
end
end
- end
- should "work with validate callback" do
- @document.validate :action_present
+ should "work with validate callback" do
+ @document.validate :action_present
- doc = @document.new
- doc.action = nil
- doc.should have_error_on(:action)
+ doc = @document.new
+ doc.action = nil
+ doc.should have_error_on(:action)
- doc.action = 'kick'
- doc.should_not have_error_on(:action)
+ doc.action = 'kick'
+ doc.should_not have_error_on(:action)
+ end
end
- end
end