From c224db7af8436e853e50474786f015ab3da50c63 Mon Sep 17 00:00:00 2001 From: Adam Meehan Date: Fri, 5 Dec 2008 18:24:31 +1100 Subject: [PATCH] moved specs to validator --- spec/validation_methods_spec.rb | 416 +------------------------------- 1 file changed, 6 insertions(+), 410 deletions(-) diff --git a/spec/validation_methods_spec.rb b/spec/validation_methods_spec.rb index 7c64e32..15997e1 100644 --- a/spec/validation_methods_spec.rb +++ b/spec/validation_methods_spec.rb @@ -46,415 +46,11 @@ describe ValidatesTimeliness::ValidationMethods do end end + # it "should have same value for before_type_cast after failed validation" do + # person.birth_date_and_time = "2000-01-01 25:02:03" + # person.should_not be_valid + # person.birth_date_and_time_before_type_cast.should == "2000-01-01 25:02:03" + # end + # - describe "with no restrictions" do - class BasicValidation < Person - validates_datetime :birth_date_and_time, :allow_blank => true - validates_date :birth_date, :allow_blank => true - validates_time :birth_time, :allow_blank => true - end - - before :each do - @person = BasicValidation.new - end - - it "should have error for invalid date component for datetime column" do - person.birth_date_and_time = "2000-01-32 01:02:03" - person.should_not be_valid - person.errors.on(:birth_date_and_time).should == "is not a valid datetime" - end - - it "should have error for invalid time component for datetime column" do - person.birth_date_and_time = "2000-01-01 25:02:03" - person.should_not be_valid - person.errors.on(:birth_date_and_time).should == "is not a valid datetime" - end - - it "should have error for invalid date value for date column" do - person.birth_date = "2000-01-32" - person.should_not be_valid - person.errors.on(:birth_date).should == "is not a valid date" - end - - it "should have error for invalid time value for time column" do - person.birth_time = "25:00" - person.should_not be_valid - person.errors.on(:birth_time).should == "is not a valid time" - end - - it "should have same value for before_type_cast after failed validation" do - person.birth_date_and_time = "2000-01-01 25:02:03" - person.should_not be_valid - person.birth_date_and_time_before_type_cast.should == "2000-01-01 25:02:03" - end - - it "should be valid with valid values" do - person.birth_date_and_time = "2000-01-01 12:12:12" - person.birth_date = "2000-01-31" - person.should be_valid - end - - it "should be valid with value out of range for Time class" do - person.birth_date_and_time = "1890-01-01 12:12:12" - person.should be_valid - end - - it "should be valid with nil values when allow_blank is true" do - person.birth_date_and_time = nil - person.birth_date = nil - person.birth_time = nil - person.should be_valid - end - end - - describe "for datetime type" do - - describe "with before and after restrictions" do - class DateTimeBeforeAfter < Person - validates_datetime :birth_date_and_time, - :before => lambda { Time.now }, :after => lambda { 1.day.ago} - end - - before :each do - @person = DateTimeBeforeAfter.new - end - - it "should have error when past :before restriction" do - person.birth_date_and_time = 1.minute.from_now - person.should_not be_valid - person.errors.on(:birth_date_and_time).should match(/must be before/) - end - - it "should have error when before :after restriction" do - person.birth_date_and_time = 2.days.ago - person.should_not be_valid - person.errors.on(:birth_date_and_time).should match(/must be after/) - end - - it "should have error when on boundary of :before restriction" do - person.birth_date_and_time = Time.now - person.should_not be_valid - person.errors.on(:birth_date_and_time).should match(/must be before/) - end - - it "should have error when on boundary of :after restriction" do - person.birth_date_and_time = 1.day.ago - person.should_not be_valid - person.errors.on(:birth_date_and_time).should match(/must be after/) - end - end - - describe "with on_or_before and on_or_after restrictions" do - class DateTimeOnOrBeforeAndAfter < Person - validates_datetime :birth_date_and_time, :type => :datetime, - :on_or_before => lambda { Time.now.at_midnight }, - :on_or_after => lambda { 1.day.ago } - end - - before do - @person = DateTimeOnOrBeforeAndAfter.new - end - - it "should have error when past :on_or_before restriction" do - person.birth_date_and_time = Time.now.at_midnight + 1 - person.should_not be_valid - person.errors.on(:birth_date_and_time).should match(/must be on or before/) - end - - it "should have error when before :on_or_after restriction" do - person.birth_date_and_time = 1.days.ago - 1 - person.should_not be_valid - person.errors.on(:birth_date_and_time).should match(/must be on or after/) - end - - it "should be valid when value equal to :on_or_before restriction" do - person.birth_date_and_time = Time.now.at_midnight - person.should be_valid - end - - it "should be valid when value equal to :on_or_after restriction" do - person.birth_date_and_time = 1.day.ago - person.should be_valid - end - end - - end - - describe "for date type" do - - describe "with before and after restrictions" do - before :all do - class DateBeforeAfter < Person - validates_date :birth_date, - :before => 1.day.from_now, - :after => 1.day.ago - end - end - - before :each do - @person = DateBeforeAfter.new - end - - it "should have error when past :before restriction" do - person.birth_date = 2.days.from_now - person.should_not be_valid - person.errors.on(:birth_date).should match(/must be before/) - end - - it "should have error when before :after restriction" do - person.birth_date = 2.days.ago - person.should_not be_valid - person.errors.on(:birth_date).should match(/must be after/) - end - end - - describe "with on_or_before and on_or_after restrictions" do - before :all do - class DateOnOrBeforeAndAfter < Person - validates_date :birth_date, - :on_or_before => 1.day.from_now, - :on_or_after => 1.day.ago - end - end - - before :each do - @person = DateOnOrBeforeAndAfter.new - end - - it "should have error when past :on_or_before restriction" do - person.birth_date = 2.days.from_now - person.should_not be_valid - person.errors.on(:birth_date).should match(/must be on or before/) - end - - it "should have error when before :on_or_after restriction" do - person.birth_date = 2.days.ago - person.should_not be_valid - person.errors.on(:birth_date).should match(/must be on or after/) - end - - it "should be valid when value equal to :on_or_before restriction" do - person.birth_date = 1.day.from_now - person.should be_valid - end - - it "should be valid when value equal to :on_or_after restriction" do - person.birth_date = 1.day.ago - person.should be_valid - end - end - end - - describe "for time type" do - - describe "with before and after restrictions" do - class TimeBeforeAfter < Person - validates_time :birth_time, - :before => "23:00", - :after => "06:00" - end - - before :each do - @person = TimeBeforeAfter.new - end - - it "should have error when on boundary of :before restriction" do - person.birth_time = "23:00" - person.should_not be_valid - person.errors.on(:birth_time).should match(/must be before/) - end - - it "should have error when on boundary of :after restriction" do - person.birth_time = "06:00am" - person.should_not be_valid - person.errors.on(:birth_time).should match(/must be after/) - end - - it "should have error when past :before restriction" do - person.birth_time = "23:01" - person.should_not be_valid - person.errors.on(:birth_time).should match(/must be before/) - end - - it "should have error when before :after restriction" do - person.birth_time = "05:59" - person.should_not be_valid - person.errors.on(:birth_time).should match(/must be after/) - end - - it "should not have error when before :before restriction" do - person.birth_time = "22:59" - person.should be_valid - end - - it "should have error when before :after restriction" do - person.birth_time = "06:01" - person.should be_valid - end - end - - describe "with on_or_before and on_or_after restrictions" do - class TimeOnOrBeforeAndAfter < Person - validates_time :birth_time, - :on_or_before => "23:00", - :on_or_after => "06:00" - end - - before :each do - @person = TimeOnOrBeforeAndAfter.new - end - - it "should have error when past :on_or_before restriction" do - person.birth_time = "23:01" - person.should_not be_valid - person.errors.on(:birth_time).should match(/must be on or before/) - end - - it "should have error when before :on_or_after restriction" do - person.birth_time = "05:59" - person.should_not be_valid - person.errors.on(:birth_time).should match(/must be on or after/) - end - - it "should be valid when on boundary of :on_or_before restriction" do - person.birth_time = "23:00" - person.should be_valid - end - - it "should be valid when on boundary of :on_or_after restriction" do - person.birth_time = "06:00" - person.should be_valid - end - end - end - - describe "with mixed value and restriction types" do - class MixedBeforeAndAfter < Person - validates_datetime :birth_date_and_time, - :before => Date.new(2000,1,2), - :after => lambda { "2000-01-01" } - validates_date :birth_date, - :on_or_before => lambda { "2000-01-01" }, - :on_or_after => :birth_date_and_time - end - - before :each do - @person = MixedBeforeAndAfter.new - end - - it "should correctly validate time attribute with Date restriction" do - person.birth_date_and_time = "2000-01-03 00:00:00" - person.should_not be_valid - person.errors.on(:birth_date_and_time).should match(/must be before/) - end - - it "should correctly validate with proc restriction" do - person.birth_date_and_time = "2000-01-01 00:00:00" - person.should_not be_valid - person.errors.on(:birth_date_and_time).should match(/must be after/) - end - - it "should correctly validate date attribute with DateTime restriction" do - person.birth_date = "2000-01-03" - person.birth_date_and_time = "1890-01-01 00:00:00" - person.should_not be_valid - person.errors.on(:birth_date).should match(/must be on or before/) - end - - it "should correctly validate date attribute with symbol restriction" do - person.birth_date = "2000-01-01" - person.birth_date_and_time = "2000-01-02 12:00:00" - person.should_not be_valid - person.errors.on(:birth_date).should match(/must be on or after/) - end - - end - - describe "ignoring restriction errors" do - class BadRestriction < Person - validates_date :birth_date, :before => Proc.new { raise } - end - - before :all do - ValidatesTimeliness.ignore_restriction_errors = true - end - - after :all do - ValidatesTimeliness.ignore_restriction_errors = false - end - - before :each do - @person = BadRestriction.new - end - - it "should have no errors when restriction is invalid" do - person.birth_date = '2000-01-01' - person.should be_valid - end - end - - describe "restriction value error message" do - class ValueFormats < Person - validates_datetime :birth_date_and_time, :allow_blank => true, :after => 1.day.from_now - validates_date :birth_date, :allow_blank => true, :after => 1.day.from_now - validates_time :birth_time, :allow_blank => true, :after => '23:59:59' - end - - describe "default formats" do - before :each do - @person = ValueFormats.new - end - - it "should format datetime value of restriction" do - person.birth_date_and_time = Time.now - person.save - person.errors.on(:birth_date_and_time).should match(/after \d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\Z/) - end - - it "should format date value of restriction" do - person.birth_date = Time.now - person.save - person.errors.on(:birth_date).should match(/after \d{4}-\d{2}-\d{2}\Z/) - end - - it "should format time value of restriction" do - person.birth_time = '12:00:00' - person.save - person.errors.on(:birth_time).should match(/after \d{2}:\d{2}:\d{2}\Z/) - end - end - - describe "custom formats" do - before :all do - ValidatesTimeliness.error_value_formats = { - :time => '%H:%M %p', - :date => '%d-%m-%Y', - :datetime => '%d-%m-%Y %H:%M %p' - } - end - - before :each do - @person = ValueFormats.new - end - - it "should format datetime value of restriction" do - person.birth_date_and_time = Time.now - person.save - person.errors.on(:birth_date_and_time).should match(/after \d{2}-\d{2}-\d{4} \d{2}:\d{2} (AM|PM)\Z/) - end - - it "should format date value of restriction" do - person.birth_date = Time.now - person.save - person.errors.on(:birth_date).should match(/after \d{2}-\d{2}-\d{4}\Z/) - end - - it "should format time value of restriction" do - person.birth_time = '12:00:00' - person.save - person.errors.on(:birth_time).should match(/after \d{2}:\d{2} (AM|PM)\Z/) - end - end - - end end