changed to using accessor for model instance and using specific validation method for each type

This commit is contained in:
Adam Meehan 2008-07-25 19:17:55 +10:00
parent 5567f920f7
commit 24db8ab774

View File

@ -1,6 +1,8 @@
require File.dirname(__FILE__) + '/spec_helper' require File.dirname(__FILE__) + '/spec_helper'
describe ValidatesTimeliness::Validations do describe ValidatesTimeliness::Validations do
attr_accessor :person
before :all do before :all do
# freezes time using time_travel plugin # freezes time using time_travel plugin
Time.now = Time.utc(2000, 1, 1, 0, 0, 0) Time.now = Time.utc(2000, 1, 1, 0, 0, 0)
@ -10,7 +12,7 @@ describe ValidatesTimeliness::Validations do
Time.now = nil Time.now = nil
end end
describe "timeliness_date_time_parse" do describe "parse_date_time" do
it "should return time object for valid time string" do it "should return time object for valid time string" do
parse_method("2000-01-01 12:13:14", :datetime).should be_kind_of(Time) parse_method("2000-01-01 12:13:14", :datetime).should be_kind_of(Time)
end end
@ -47,9 +49,9 @@ describe ValidatesTimeliness::Validations do
describe "with no restrictions" do describe "with no restrictions" do
before :all do before :all do
class BasicValidation < Person class BasicValidation < Person
validates_timeliness_of :birth_date_and_time, :allow_blank => true, :type => :datetime validates_datetime :birth_date_and_time, :allow_blank => true
validates_timeliness_of :birth_date, :allow_blank => true, :type => :date validates_date :birth_date, :allow_blank => true
validates_timeliness_of :birth_time, :allow_blank => true, :type => :time validates_time :birth_time, :allow_blank => true
end end
end end
@ -58,47 +60,47 @@ describe ValidatesTimeliness::Validations do
end end
it "should have error for invalid date component for datetime column" do it "should have error for invalid date component for datetime column" do
@person.birth_date_and_time = "2000-01-32 01:02:03" person.birth_date_and_time = "2000-01-32 01:02:03"
@person.should_not be_valid person.should_not be_valid
@person.errors.on(:birth_date_and_time).should == "is not a valid datetime" person.errors.on(:birth_date_and_time).should == "is not a valid datetime"
end end
it "should have error for invalid time component for datetime column" do it "should have error for invalid time component for datetime column" do
@person.birth_date_and_time = "2000-01-01 25:02:03" person.birth_date_and_time = "2000-01-01 25:02:03"
@person.should_not be_valid person.should_not be_valid
@person.errors.on(:birth_date_and_time).should == "is not a valid datetime" person.errors.on(:birth_date_and_time).should == "is not a valid datetime"
end end
it "should have error for invalid date value for date column" do it "should have error for invalid date value for date column" do
@person.birth_date = "2000-01-32" person.birth_date = "2000-01-32"
@person.should_not be_valid person.should_not be_valid
@person.errors.on(:birth_date).should == "is not a valid date" person.errors.on(:birth_date).should == "is not a valid date"
end end
it "should have error for invalid time value for time column" do it "should have error for invalid time value for time column" do
@person.birth_time = "25:00" person.birth_time = "25:00"
@person.should_not be_valid person.should_not be_valid
@person.errors.on(:birth_time).should == "is not a valid time" person.errors.on(:birth_time).should == "is not a valid time"
end end
it "should be valid with valid values" do it "should be valid with valid values" do
@person.birth_date_and_time = "2000-01-01 12:12:12" person.birth_date_and_time = "2000-01-01 12:12:12"
@person.birth_date = "2000-01-31" person.birth_date = "2000-01-31"
@person.should be_valid person.should be_valid
end end
it "should be valid with values before out of Time range" do it "should be valid with values before out of Time range" do
@person.birth_date_and_time = "1890-01-01 12:12:12" person.birth_date_and_time = "1890-01-01 12:12:12"
@person.birth_date = "1890-01-31" person.birth_date = "1890-01-31"
@person.birth_time = "23:59:59" person.birth_time = "23:59:59"
@person.should be_valid person.should be_valid
end end
it "should be valid with nil values when allow_blank si true" do it "should be valid with nil values when allow_blank is true" do
@person.birth_date_and_time = nil person.birth_date_and_time = nil
@person.birth_date = nil person.birth_date = nil
@person.birth_time = nil person.birth_time = nil
@person.should be_valid person.should be_valid
end end
end end
@ -107,7 +109,7 @@ describe ValidatesTimeliness::Validations do
describe "with before and after restrictions" do describe "with before and after restrictions" do
before :all do before :all do
class DateTimeBeforeAfter < Person class DateTimeBeforeAfter < Person
validates_timeliness_of :birth_date_and_time, :type => :datetime, validates_datetime :birth_date_and_time,
:before => lambda { Time.now }, :after => lambda { 1.day.ago} :before => lambda { Time.now }, :after => lambda { 1.day.ago}
end end
end end
@ -117,34 +119,34 @@ describe ValidatesTimeliness::Validations do
end end
it "should have error when past :before restriction" do it "should have error when past :before restriction" do
@person.birth_date_and_time = 1.minute.from_now person.birth_date_and_time = 1.minute.from_now
@person.should_not be_valid person.should_not be_valid
@person.errors.on(:birth_date_and_time).should match(/must be before/) person.errors.on(:birth_date_and_time).should match(/must be before/)
end end
it "should have error when before :after restriction" do it "should have error when before :after restriction" do
@person.birth_date_and_time = 2.days.ago person.birth_date_and_time = 2.days.ago
@person.should_not be_valid person.should_not be_valid
@person.errors.on(:birth_date_and_time).should match(/must be after/) person.errors.on(:birth_date_and_time).should match(/must be after/)
end end
it "should have error when on boundary of :before restriction" do it "should have error when on boundary of :before restriction" do
@person.birth_date_and_time = Time.now person.birth_date_and_time = Time.now
@person.should_not be_valid person.should_not be_valid
@person.errors.on(:birth_date_and_time).should match(/must be before/) person.errors.on(:birth_date_and_time).should match(/must be before/)
end end
it "should have error when on boundary of :after restriction" do it "should have error when on boundary of :after restriction" do
@person.birth_date_and_time = 1.day.ago person.birth_date_and_time = 1.day.ago
@person.should_not be_valid person.should_not be_valid
@person.errors.on(:birth_date_and_time).should match(/must be after/) person.errors.on(:birth_date_and_time).should match(/must be after/)
end end
end end
describe "with on_or_before and on_or_after restrictions" do describe "with on_or_before and on_or_after restrictions" do
before :all do before :all do
class DateTimeOnOrBeforeAndAfter < Person class DateTimeOnOrBeforeAndAfter < Person
validates_timeliness_of :birth_date_and_time, :type => :datetime, validates_datetime :birth_date_and_time, :type => :datetime,
:on_or_before => lambda { Time.now.at_midnight }, :on_or_before => lambda { Time.now.at_midnight },
:on_or_after => lambda { 1.day.ago } :on_or_after => lambda { 1.day.ago }
end end
@ -155,25 +157,25 @@ describe ValidatesTimeliness::Validations do
end end
it "should have error when past :on_or_before restriction" do it "should have error when past :on_or_before restriction" do
@person.birth_date_and_time = Time.now.at_midnight + 1 person.birth_date_and_time = Time.now.at_midnight + 1
@person.should_not be_valid person.should_not be_valid
@person.errors.on(:birth_date_and_time).should match(/must be on or before/) person.errors.on(:birth_date_and_time).should match(/must be on or before/)
end end
it "should have error when before :on_or_after restriction" do it "should have error when before :on_or_after restriction" do
@person.birth_date_and_time = 1.days.ago - 1 person.birth_date_and_time = 1.days.ago - 1
@person.should_not be_valid person.should_not be_valid
@person.errors.on(:birth_date_and_time).should match(/must be on or after/) person.errors.on(:birth_date_and_time).should match(/must be on or after/)
end end
it "should be valid when value equal to :on_or_before restriction" do it "should be valid when value equal to :on_or_before restriction" do
@person.birth_date_and_time = Time.now.at_midnight person.birth_date_and_time = Time.now.at_midnight
@person.should be_valid person.should be_valid
end end
it "should be valid when value equal to :on_or_after restriction" do it "should be valid when value equal to :on_or_after restriction" do
@person.birth_date_and_time = 1.day.ago person.birth_date_and_time = 1.day.ago
@person.should be_valid person.should be_valid
end end
end end
@ -184,7 +186,7 @@ describe ValidatesTimeliness::Validations do
describe "with before and after restrictions" do describe "with before and after restrictions" do
before :all do before :all do
class DateBeforeAfter < Person class DateBeforeAfter < Person
validates_timeliness_of :birth_date, :type => :date, validates_date :birth_date,
:before => 1.day.from_now, :before => 1.day.from_now,
:after => 1.day.ago :after => 1.day.ago
end end
@ -195,22 +197,22 @@ describe ValidatesTimeliness::Validations do
end end
it "should have error when past :before restriction" do it "should have error when past :before restriction" do
@person.birth_date = 2.days.from_now person.birth_date = 2.days.from_now
@person.should_not be_valid person.should_not be_valid
@person.errors.on(:birth_date).should match(/must be before/) person.errors.on(:birth_date).should match(/must be before/)
end end
it "should have error when before :after restriction" do it "should have error when before :after restriction" do
@person.birth_date = 2.days.ago person.birth_date = 2.days.ago
@person.should_not be_valid person.should_not be_valid
@person.errors.on(:birth_date).should match(/must be after/) person.errors.on(:birth_date).should match(/must be after/)
end end
end end
describe "with on_or_before and on_or_after restrictions" do describe "with on_or_before and on_or_after restrictions" do
before :all do before :all do
class DateOnOrBeforeAndAfter < Person class DateOnOrBeforeAndAfter < Person
validates_timeliness_of :birth_date, :type => :date, validates_date :birth_date,
:on_or_before => 1.day.from_now, :on_or_before => 1.day.from_now,
:on_or_after => 1.day.ago :on_or_after => 1.day.ago
end end
@ -221,25 +223,25 @@ describe ValidatesTimeliness::Validations do
end end
it "should have error when past :on_or_before restriction" do it "should have error when past :on_or_before restriction" do
@person.birth_date = 2.days.from_now person.birth_date = 2.days.from_now
@person.should_not be_valid person.should_not be_valid
@person.errors.on(:birth_date).should match(/must be on or before/) person.errors.on(:birth_date).should match(/must be on or before/)
end end
it "should have error when before :on_or_after restriction" do it "should have error when before :on_or_after restriction" do
@person.birth_date = 2.days.ago person.birth_date = 2.days.ago
@person.should_not be_valid person.should_not be_valid
@person.errors.on(:birth_date).should match(/must be on or after/) person.errors.on(:birth_date).should match(/must be on or after/)
end end
it "should be valid when value equal to :on_or_before restriction" do it "should be valid when value equal to :on_or_before restriction" do
@person.birth_date = 1.day.from_now person.birth_date = 1.day.from_now
@person.should be_valid person.should be_valid
end end
it "should be valid when value equal to :on_or_after restriction" do it "should be valid when value equal to :on_or_after restriction" do
@person.birth_date = 1.day.ago person.birth_date = 1.day.ago
@person.should be_valid person.should be_valid
end end
end end
end end
@ -249,7 +251,7 @@ describe ValidatesTimeliness::Validations do
describe "with before and after restrictions" do describe "with before and after restrictions" do
before :all do before :all do
class TimeBeforeAfter < Person class TimeBeforeAfter < Person
validates_timeliness_of :birth_time, :type => :time, validates_time :birth_time,
:before => "23:00", :before => "23:00",
:after => "06:00" :after => "06:00"
end end
@ -260,44 +262,44 @@ describe ValidatesTimeliness::Validations do
end end
it "should have error when on boundary of :before restriction" do it "should have error when on boundary of :before restriction" do
@person.birth_time = "23:00" person.birth_time = "23:00"
@person.should_not be_valid person.should_not be_valid
@person.errors.on(:birth_time).should match(/must be before/) person.errors.on(:birth_time).should match(/must be before/)
end end
it "should have error when on boundary of :after restriction" do it "should have error when on boundary of :after restriction" do
@person.birth_time = "06:00am" person.birth_time = "06:00am"
@person.should_not be_valid person.should_not be_valid
@person.errors.on(:birth_time).should match(/must be after/) person.errors.on(:birth_time).should match(/must be after/)
end end
it "should have error when past :before restriction" do it "should have error when past :before restriction" do
@person.birth_time = "23:01" person.birth_time = "23:01"
@person.should_not be_valid person.should_not be_valid
@person.errors.on(:birth_time).should match(/must be before/) person.errors.on(:birth_time).should match(/must be before/)
end end
it "should have error when before :after restriction" do it "should have error when before :after restriction" do
@person.birth_time = "05:59" person.birth_time = "05:59"
@person.should_not be_valid person.should_not be_valid
@person.errors.on(:birth_time).should match(/must be after/) person.errors.on(:birth_time).should match(/must be after/)
end end
it "should not have error when before :before restriction" do it "should not have error when before :before restriction" do
@person.birth_time = "22:59" person.birth_time = "22:59"
@person.should be_valid person.should be_valid
end end
it "should have error when before :after restriction" do it "should have error when before :after restriction" do
@person.birth_time = "06:01" person.birth_time = "06:01"
@person.should be_valid person.should be_valid
end end
end end
describe "with on_or_before and on_or_after restrictions" do describe "with on_or_before and on_or_after restrictions" do
before :all do before :all do
class TimeOnOrBeforeAndAfter < Person class TimeOnOrBeforeAndAfter < Person
validates_timeliness_of :birth_time, :type => :time, validates_time :birth_time,
:on_or_before => "23:00", :on_or_before => "23:00",
:on_or_after => "06:00" :on_or_after => "06:00"
end end
@ -308,25 +310,25 @@ describe ValidatesTimeliness::Validations do
end end
it "should have error when past :on_or_before restriction" do it "should have error when past :on_or_before restriction" do
@person.birth_time = "23:01" person.birth_time = "23:01"
@person.should_not be_valid person.should_not be_valid
@person.errors.on(:birth_time).should match(/must be on or before/) person.errors.on(:birth_time).should match(/must be on or before/)
end end
it "should have error when before :on_or_after restriction" do it "should have error when before :on_or_after restriction" do
@person.birth_time = "05:59" person.birth_time = "05:59"
@person.should_not be_valid person.should_not be_valid
@person.errors.on(:birth_time).should match(/must be on or after/) person.errors.on(:birth_time).should match(/must be on or after/)
end end
it "should be valid when on boundary of :on_or_before restriction" do it "should be valid when on boundary of :on_or_before restriction" do
@person.birth_time = "23:00" person.birth_time = "23:00"
@person.should be_valid person.should be_valid
end end
it "should be valid when on boundary of :on_or_after restriction" do it "should be valid when on boundary of :on_or_after restriction" do
@person.birth_time = "06:00" person.birth_time = "06:00"
@person.should be_valid person.should be_valid
end end
end end
end end
@ -335,12 +337,12 @@ describe ValidatesTimeliness::Validations do
before :all do before :all do
class MixedBeforeAndAfter < Person class MixedBeforeAndAfter < Person
validates_timeliness_of :birth_date_and_time, validates_datetime :birth_date_and_time,
:before => Date.new(2000,1,2), :before => Date.new(2000,1,2),
:after => lambda { "2000-01-01" } :after => lambda { "2000-01-01" }
validates_timeliness_of :birth_date, :type => :date, validates_date :birth_date,
:on_or_before => lambda { "2000-01-01" }, :on_or_before => lambda { "2000-01-01" },
:on_or_after => :birth_date_and_time :on_or_after => :birth_date_and_time
end end
end end
@ -349,29 +351,29 @@ describe ValidatesTimeliness::Validations do
end end
it "should correctly validate time attribute with Date restriction" do it "should correctly validate time attribute with Date restriction" do
@person.birth_date_and_time = "2000-01-03 00:00:00" person.birth_date_and_time = "2000-01-03 00:00:00"
@person.should_not be_valid person.should_not be_valid
@person.errors.on(:birth_date_and_time).should match(/must be before/) person.errors.on(:birth_date_and_time).should match(/must be before/)
end end
it "should correctly validate with proc restriction" do it "should correctly validate with proc restriction" do
@person.birth_date_and_time = "2000-01-01 00:00:00" person.birth_date_and_time = "2000-01-01 00:00:00"
@person.should_not be_valid person.should_not be_valid
@person.errors.on(:birth_date_and_time).should match(/must be after/) person.errors.on(:birth_date_and_time).should match(/must be after/)
end end
it "should correctly validate date attribute with DateTime restriction" do it "should correctly validate date attribute with DateTime restriction" do
@person.birth_date = "2000-01-03" person.birth_date = "2000-01-03"
@person.birth_date_and_time = "1890-01-01 00:00:00" person.birth_date_and_time = "1890-01-01 00:00:00"
@person.should_not be_valid person.should_not be_valid
@person.errors.on(:birth_date).should match(/must be on or before/) person.errors.on(:birth_date).should match(/must be on or before/)
end end
it "should correctly validate date attribute with symbol restriction" do it "should correctly validate date attribute with symbol restriction" do
@person.birth_date = "2000-01-01" person.birth_date = "2000-01-01"
@person.birth_date_and_time = "2000-01-02 12:00:00" person.birth_date_and_time = "2000-01-02 12:00:00"
@person.should_not be_valid person.should_not be_valid
@person.errors.on(:birth_date).should match(/must be on or after/) person.errors.on(:birth_date).should match(/must be on or after/)
end end
end end
@ -389,8 +391,81 @@ describe ValidatesTimeliness::Validations do
end end
it "should have no errors when restriction is invalid" do it "should have no errors when restriction is invalid" do
@person.birth_date = '2000-01-01' person.birth_date = '2000-01-01'
@person.should be_valid person.should be_valid
end end
end end
describe "restriction value error message" do
describe "default formats" do
before :all do
class DefaultFormats < 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
end
before :each do
@person = DefaultFormats.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
class CustomFormats < 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
ActiveRecord::Errors.date_time_error_value_formats = {
:time => '%H:%M %p',
:date => '%d-%m-%Y',
:datetime => '%d-%m-%Y %H:%M %p'
}
end
before :each do
@person = CustomFormats.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 end