spec/validator_spec.rb in validates_timeliness-1.1.5 vs spec/validator_spec.rb in validates_timeliness-1.1.6

- old
+ new

@@ -14,59 +14,75 @@ before :each do @person = Person.new end - describe "restriction_value" do + describe "option keys validation" do + before do + keys = ValidatesTimeliness::Validator::VALID_OPTIONS - [:invalid_date_message, :invalid_time_message, :with_date, :with_time] + @valid_options = keys.inject({}) {|hash, opt| hash[opt] = nil; hash } + end + + it "should raise error if invalid option key passed" do + @valid_options.update(:invalid_key => 'will not open lock') + lambda { Person.validates_datetime(@valid_options) }.should raise_error(ArgumentError) + end + + it "should not raise error if option keys are valid" do + lambda { Person.validates_datetime(@valid_options) }.should_not raise_error(ArgumentError) + end + end + + describe "evaluate_option_value" do it "should return Time object when restriction is Time object" do - restriction_value(Time.now, :datetime).should be_kind_of(Time) + evaluate_option_value(Time.now, :datetime).should be_kind_of(Time) end it "should return Time object when restriction is string" do - restriction_value("2007-01-01 12:00", :datetime).should be_kind_of(Time) + evaluate_option_value("2007-01-01 12:00", :datetime).should be_kind_of(Time) end it "should return Time object when restriction is method and method returns Time object" do person.stub!(:datetime_attr).and_return(Time.now) - restriction_value(:datetime_attr, :datetime).should be_kind_of(Time) + evaluate_option_value(:datetime_attr, :datetime).should be_kind_of(Time) end it "should return Time object when restriction is method and method returns string" do person.stub!(:datetime_attr).and_return("2007-01-01 12:00") - restriction_value(:datetime_attr, :datetime).should be_kind_of(Time) + evaluate_option_value(:datetime_attr, :datetime).should be_kind_of(Time) end it "should return Time object when restriction is proc which returns Time object" do - restriction_value(lambda { Time.now }, :datetime).should be_kind_of(Time) + evaluate_option_value(lambda { Time.now }, :datetime).should be_kind_of(Time) end it "should return Time object when restriction is proc which returns string" do - restriction_value(lambda {"2007-01-01 12:00"}, :datetime).should be_kind_of(Time) + evaluate_option_value(lambda {"2007-01-01 12:00"}, :datetime).should be_kind_of(Time) end it "should return array of Time objects when restriction is array of Time objects" do time1, time2 = Time.now, 1.day.ago - restriction_value([time1, time2], :datetime).should == [time2, time1] + evaluate_option_value([time1, time2], :datetime).should == [time2, time1] end it "should return array of Time objects when restriction is array of strings" do time1, time2 = "2000-01-02", "2000-01-01" - restriction_value([time1, time2], :datetime).should == [Person.parse_date_time(time2, :datetime), Person.parse_date_time(time1, :datetime)] + evaluate_option_value([time1, time2], :datetime).should == [Person.parse_date_time(time2, :datetime), Person.parse_date_time(time1, :datetime)] end it "should return array of Time objects when restriction is Range of Time objects" do time1, time2 = Time.now, 1.day.ago - restriction_value(time1..time2, :datetime).should == [time2, time1] + evaluate_option_value(time1..time2, :datetime).should == [time2, time1] end it "should return array of Time objects when restriction is Range of time strings" do time1, time2 = "2000-01-02", "2000-01-01" - restriction_value(time1..time2, :datetime).should == [Person.parse_date_time(time2, :datetime), Person.parse_date_time(time1, :datetime)] + evaluate_option_value(time1..time2, :datetime).should == [Person.parse_date_time(time2, :datetime), Person.parse_date_time(time1, :datetime)] end - def restriction_value(restriction, type) + def evaluate_option_value(restriction, type) configure_validator(:type => type) - validator.send(:restriction_value, restriction, person) + validator.class.send(:evaluate_option_value, restriction, type, person) end end describe "instance with defaults" do @@ -328,10 +344,104 @@ should_have_no_error(:birth_time, :between) end end end + describe "instance with :equal_to restriction" do + + describe "for datetime type" do + before do + configure_validator(:equal_to => Time.now) + end + + it "should have error when value not equal to :equal_to restriction" do + validate_with(:birth_date_and_time, Time.now + 1) + should_have_error(:birth_date_and_time, :equal_to) + end + + it "should have error when value is equal to :equal_to restriction for all values except microscond, and microsecond is not ignored" do + configure_validator(:equal_to => Time.utc(2000, 1, 1, 0, 0, 0, 0), :ignore_usec => false) + validate_with(:birth_date_and_time, Time.utc(2000, 1, 1, 0, 0, 0, 500)) + should_have_error(:birth_date_and_time, :equal_to) + end + + it "should be valid when value is equal to :equal_to restriction for all values except microscond, and microsecond is ignored" do + configure_validator(:equal_to => Time.utc(2000, 1, 1, 0, 0, 0, 0), :ignore_usec => true) + validate_with(:birth_date_and_time, Time.utc(2000, 1, 1, 0, 0, 0, 500)) + should_have_no_error(:birth_date_and_time, :equal_to) + end + + it "should be valid when value is equal to :equal_to restriction" do + validate_with(:birth_date_and_time, Time.now) + should_have_no_error(:birth_date_and_time, :equal_to) + end + end + + describe "for date type" do + before do + configure_validator(:type => :date, :equal_to => Date.today) + end + + it "should have error when value is not equal to :equal_to restriction" do + validate_with(:birth_date, Date.today + 1) + should_have_error(:birth_date, :equal_to) + end + + it "should be valid when value is equal to :equal_to restriction" do + validate_with(:birth_date, Date.today) + should_have_no_error(:birth_date, :equal_to) + end + end + + describe "for time type" do + before do + configure_validator(:type => :time, :equal_to => "09:00:00") + end + + it "should have error when value is not equal to :equal_to restriction" do + validate_with(:birth_time, "09:00:01") + should_have_error(:birth_time, :equal_to) + end + + it "should be valid when value is equal to :equal_to restriction" do + validate_with(:birth_time, "09:00:00") + should_have_no_error(:birth_time, :equal_to) + end + end + end + + describe "instance with :with_time option" do + + it "should validate date attribute as datetime combining value of :with_time against restrictions " do + configure_validator(:type => :date, :with_time => '12:31', :on_or_before => Time.mktime(2000,1,1,12,30)) + validate_with(:birth_date, "2000-01-01") + should_have_error(:birth_date, :on_or_before) + end + + it "should skip restriction validation if :with_time value is nil" do + configure_validator(:type => :date, :with_time => nil, :on_or_before => Time.mktime(2000,1,1,12,30)) + validate_with(:birth_date, "2000-01-01") + should_have_no_error(:birth_date, :on_or_before) + end + + end + + describe "instance with :with_date option" do + + it "should validate time attribute as datetime combining value of :with_date against restrictions " do + configure_validator(:type => :time, :with_date => '2009-01-01', :on_or_before => Time.mktime(2000,1,1,12,30)) + validate_with(:birth_date, "12:30") + should_have_error(:birth_date, :on_or_before) + end + + it "should skip restriction validation if :with_date value is nil" do + configure_validator(:type => :time, :with_date => nil, :on_or_before => Time.mktime(2000,1,1,12,30)) + validate_with(:birth_date, "12:30") + should_have_no_error(:birth_date, :on_or_before) + end + end + describe "instance with mixed value and restriction types" do it "should validate datetime attribute with Date restriction" do configure_validator(:type => :datetime, :on_or_before => Date.new(2000,1,1)) validate_with(:birth_date_and_time, "2000-01-01 00:00:00") @@ -481,10 +591,10 @@ @validator = ValidatesTimeliness::Validator.new(options) end def validate_with(attr_name, value) person.send("#{attr_name}=", value) - validator.call(person, attr_name) + validator.call(person, attr_name, value) end def should_have_error(attr_name, error) message = error_messages[error] person.errors.on(attr_name).should match(/#{message}/)