0
+require Pathname(__FILE__).dirname.expand_path + 'spec_helper'
0
+ describe "Automatic Validation from Property Definition" do
0
+ include DataMapper::Resource
0
+ include DataMapper::Validate
0
+ property :name, String, :nullable => false , :validates => :presence_test
0
+ property :description, String, :length => 10, :validates => :length_test_1
0
+ property :notes, String, :length => 2..10, :validates => :length_test_2
0
+ property :no_validation, String, :auto_validation => false
0
+ property :salesman, String, :nullable => false, :validates => [:multi_context_1, :multi_context_2]
0
+ property :code, String, :format => Proc.new { |code| code =~ /A\d{4}/}, :validates => :format_test
0
+ property :allow_nil, String, :size => 5..10, :nullable => true, :validates => :nil_test
0
+ it "should have a hook for adding auto validations called from DataMapper::Property#new" do
0
+ SailBoat.should respond_to(:auto_generate_validations)
0
+ it "should auto add a validates_presence_of when property has option :nullable => false" do
0
+ validator = SailBoat.validators.context(:presence_test).first
0
+ validator.is_a?(DataMapper::Validate::RequiredFieldValidator).should == true
0
+ validator.field_name.should == :name
0
+ boat.valid_for_presence_test?.should == false
0
+ boat.valid_for_presence_test?.should == true
0
+ it "should auto add a validates_length_of for maximum size on String properties" do
0
+ # max length test max=10
0
+ boat.valid_for_length_test_1?.should == true #no minimum length
0
+ boat.description = 'ABCDEFGHIJK' #11
0
+ boat.valid_for_length_test_1?.should == false
0
+ boat.description = 'ABCDEFGHIJ' #10
0
+ boat.valid_for_length_test_1?.should == true
0
+ it "should auto add validates_length_of within a range when option :length or :size is a range" do
0
+ # Range test notes = 2..10
0
+ boat.valid_for_length_test_2?.should == false
0
+ boat.valid_for_length_test_2?.should == true
0
+ boat.notes = 'ABCDEFGHIJK' #11
0
+ boat.valid_for_length_test_2?.should == false
0
+ boat.notes = 'ABCDEFGHIJ' #10
0
+ boat.valid_for_length_test_2?.should == true
0
+ it "should auto add a validates_format_of if the :format option is given" do
0
+ # format test - format = /A\d{4}/ on code
0
+ boat.valid_for_format_test?.should == false
0
+ boat.valid_for_format_test?.should == true
0
+ boat.code = 'BAD CODE'
0
+ boat.valid_for_format_test?.should == false
0
+ it "should auto validate all strings for max length" do
0
+ include DataMapper::Resource
0
+ include DataMapper::Validate
0
+ property :name, String
0
+ Test.new().valid?().should == true
0
+ t.name = 'Lipsmackinthirstquenchinacetastinmotivatingoodbuzzincooltalkinhighwalkinfastlivinevergivincoolfizzin'
0
+ t.valid?().should == false
0
+ t.errors.full_messages.should include('Name must be less than 50 characters long')
0
+ it "should not auto add any validators if the option :auto_validation => false was given" do
0
+ include DataMapper::Resource
0
+ include DataMapper::Validate
0
+ property :name, String, :nullable => false, :auto_validation => false
0
+ Test.new().valid?().should == true
0
+ it 'It should auto add range checking the length of a string while still allowing null values' do
0
+ boat.allow_nil = 'ABC'
0
+ boat.should_not be_valid_for_nil_test
0
+ boat.errors.on(:allow_nil).should include('Allow nil must be between 5 and 10 characters long')
0
+ boat.allow_nil = 'ABCDEFG'
0
+ boat.should be_valid_for_nil_test
0
+ boat.allow_nil = 'ABCDEFGHIJKLMNOP'
0
+ boat.should_not be_valid_for_nil_test
0
+ boat.errors.on(:allow_nil).should include('Allow nil must be between 5 and 10 characters long')
0
+ boat.should be_valid_for_nil_test