test/unit/test_validations.rb in mongo_mapper-0.6.10 vs test/unit/test_validations.rb in mongo_mapper-0.7.0

- old
+ new

@@ -1,17 +1,12 @@ require 'test_helper' class ValidationsTest < Test::Unit::TestCase context "Validations" do - context "on a Document" do - setup do - @document = Class.new do - include MongoMapper::Document - set_collection_name 'test' - end + @document = Doc() end context "Validating acceptance of" do should "work with validates_acceptance_of macro" do @document.key :terms, String @@ -178,11 +173,24 @@ doc.should_not have_error_on(:action) doc.action = 'kick' doc.should have_error_on(:action, 'is reserved') end - + + should "work with :not_in shortcut on key definition" do + @document.key :action, String, :not_in => %w(kick run) + + doc = @document.new + doc.should_not have_error_on(:action) + + doc.action = 'fart' + doc.should_not have_error_on(:action) + + doc.action = 'kick' + 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 doc = @document.new @@ -217,10 +225,23 @@ doc.should have_error_on(:action, 'is not 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.action = 'fart' + doc.should have_error_on(:action, 'is not 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 @@ -238,15 +259,12 @@ end end # End on a Document context "On an EmbeddedDocument" do - setup do - @embedded_doc = Class.new do - include MongoMapper::EmbeddedDocument - end + @embedded_doc = EDoc() end context "Validating acceptance of" do should "work with validates_acceptance_of macro" do @embedded_doc.key :terms, String @@ -413,10 +431,23 @@ doc.should_not have_error_on(:action) doc.action = 'kick' doc.should have_error_on(:action, 'is reserved') end + + should "work with :not_in shortcut on key definition" do + @embedded_doc.key :action, String, :not_in => %w(kick run) + + doc = @embedded_doc.new + doc.should_not have_error_on(:action) + + doc.action = 'fart' + doc.should_not have_error_on(:action) + + doc.action = 'kick' + 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 @@ -452,11 +483,24 @@ doc.should have_error_on(:action, 'is not 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.action = 'fart' + doc.should have_error_on(:action, 'is not 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 doc = @embedded_doc.new @@ -476,14 +520,11 @@ end # Validations context "Adding validation errors" do setup do - @document = Class.new do - include MongoMapper::Document - set_collection_name 'test' - + @document = Doc do key :action, String def action_present errors.add(:action, 'is invalid') if action.blank? end end @@ -498,6 +539,6 @@ doc.action = 'kick' doc.should_not have_error_on(:action) end end -end \ No newline at end of file +end