change equal_to to is_at to fix overlap with default rails message

This commit is contained in:
Adam Meehan
2009-12-11 15:20:34 +11:00
parent 3bfc7b748f
commit f3c119e191
7 changed files with 109 additions and 108 deletions

View File

@@ -40,7 +40,7 @@ describe ValidatesTimeliness::ActiveRecord::AttributeMethods do
@person.birth_date_and_time = time_string
@person.birth_date_and_time_before_type_cast.should == time_string
end
it "should return Time object for attribute_before_type_cast when written as Time" do
@person.birth_date_and_time = Time.mktime(2000, 1, 1, 2, 3, 4)
@person.birth_date_and_time_before_type_cast.should be_kind_of(Time)
@@ -59,24 +59,24 @@ describe ValidatesTimeliness::ActiveRecord::AttributeMethods do
it "should return Time object for datetime attribute read method when assigned string" do
@person.birth_date_and_time = "2000-01-01 02:03:04"
@person.birth_date_and_time.should be_kind_of(Time)
end
end
it "should return Date object for date attribute read method when assigned Date object" do
@person.birth_date = Date.today
@person.birth_date.should be_kind_of(Date)
end
end
it "should return Date object for date attribute read method when assigned string" do
@person.birth_date = '2000-01-01'
@person.birth_date.should be_kind_of(Date)
end
end
it "should return nil when time is invalid" do
@person.birth_date_and_time = "2000-01-32 02:03:04"
@person.birth_date_and_time.should be_nil
end
it "should not save invalid date value to database" do
it "should not save invalid date value to database" do
time_string = "2000-01-32 02:03:04"
@person = Person.new
@person.birth_date_and_time = time_string
@@ -84,7 +84,7 @@ describe ValidatesTimeliness::ActiveRecord::AttributeMethods do
@person.reload
@person.birth_date_and_time_before_type_cast.should be_nil
end
if RAILS_VER < '2.1'
it "should return time object from database in default timezone" do
@@ -106,7 +106,7 @@ describe ValidatesTimeliness::ActiveRecord::AttributeMethods do
@person.birth_date_and_time.strftime('%Y-%m-%d %H:%M:%S %Z %z').should == time_string + ' EST +1000'
end
it "should return time object from database in correct timezone" do
it "should return time object from database in correct timezone" do
Time.zone = 'Melbourne'
time_string = "2000-06-01 09:00:00"
@person = Person.new
@@ -115,19 +115,19 @@ describe ValidatesTimeliness::ActiveRecord::AttributeMethods do
@person.reload
@person.birth_date_and_time.strftime('%Y-%m-%d %H:%M:%S %Z %z').should == time_string + ' EST +1000'
end
end
it "should return correct date value after new value assigned" do
today = Date.today
tomorrow = Date.today + 1.day
tomorrow = Date.today + 1.day
@person = Person.new
@person.birth_date = today
@person.birth_date.should == today
@person.birth_date = tomorrow
@person.birth_date.should == tomorrow
end
it "should update date attribute on existing object" do
today = Date.today
tomorrow = Date.today + 1.day

View File

@@ -5,24 +5,24 @@ class NoValidation < Person
end
class WithValidation < Person
validates_date :birth_date,
:equal_to => '2000-01-01',
validates_date :birth_date,
:is_at => '2000-01-01',
:before => '2000-01-10',
:after => '2000-01-01',
:on_or_before => '2000-01-09',
:on_or_after => '2000-01-02',
:between => ['2000-01-01', '2000-01-03']
validates_time :birth_time,
:equal_to => '09:00',
validates_time :birth_time,
:is_at => '09:00',
:before => '23:00',
:after => '09:00',
:on_or_before => '22:00',
:on_or_after => '10:00',
:between => ['09:00', '17:00']
validates_datetime :birth_date_and_time,
:equal_to => '2000-01-01 09:00',
validates_datetime :birth_date_and_time,
:is_at => '2000-01-01 09:00',
:before => '2000-01-10 23:00',
:after => '2000-01-01 09:00',
:on_or_before => '2000-01-09 23:00',
@@ -46,44 +46,44 @@ end
describe "ValidateTimeliness matcher" do
attr_accessor :no_validation, :with_validation
@@attribute_for_type = { :date => :birth_date, :time => :birth_time, :datetime => :birth_date_and_time }
before do
@no_validation = NoValidation.new
@with_validation = WithValidation.new
end
[:date, :time, :datetime].each do |type|
it "should report that #{type} is validated" do
with_validation.should self.send("validate_#{type}", attribute_for_type(type))
end
it "should report that #{type} is not validated" do
no_validation.should_not self.send("validate_#{type}", attribute_for_type(type))
end
end
end
describe "with equal_to option" do
describe "with is_at option" do
test_values = {
:date => ['2000-01-01', '2000-01-02'],
:time => ['09:00', '09:01'],
:datetime => ['2000-01-01 09:00', '2000-01-01 09:01']
}
[:date, :time, :datetime].each do |type|
it "should report that #{type} is validated" do
with_validation.should self.send("validate_#{type}", attribute_for_type(type), :equal_to => test_values[type][0])
with_validation.should self.send("validate_#{type}", attribute_for_type(type), :is_at => test_values[type][0])
end
it "should report that #{type} is not validated when option value is incorrect" do
with_validation.should_not self.send("validate_#{type}", attribute_for_type(type), :equal_to => test_values[type][1])
with_validation.should_not self.send("validate_#{type}", attribute_for_type(type), :is_at => test_values[type][1])
end
it "should report that #{type} is not validated with option" do
no_validation.should_not self.send("validate_#{type}", attribute_for_type(type), :equal_to => test_values[type][0])
no_validation.should_not self.send("validate_#{type}", attribute_for_type(type), :is_at => test_values[type][0])
end
end
end
@@ -92,111 +92,111 @@ describe "ValidateTimeliness matcher" do
test_values = {
:date => ['2000-01-10', '2000-01-11'],
:time => ['23:00', '22:59'],
:datetime => ['2000-01-10 23:00', '2000-01-10 22:59']
:datetime => ['2000-01-10 23:00', '2000-01-10 22:59']
}
[:date, :time, :datetime].each do |type|
it "should report that #{type} is validated" do
with_validation.should self.send("validate_#{type}", attribute_for_type(type), :before => test_values[type][0])
end
it "should report that #{type} is not validated when option value is incorrect" do
with_validation.should_not self.send("validate_#{type}", attribute_for_type(type), :before => test_values[type][1])
end
it "should report that #{type} is not validated with option" do
no_validation.should_not self.send("validate_#{type}", attribute_for_type(type), :before => test_values[type][0])
end
end
end
describe "with after option" do
test_values = {
:date => ['2000-01-01', '2000-01-02'],
:time => ['09:00', '09:01'],
:datetime => ['2000-01-01 09:00', '2000-01-01 09:01']
:datetime => ['2000-01-01 09:00', '2000-01-01 09:01']
}
[:date, :time, :datetime].each do |type|
it "should report that #{type} is validated" do
with_validation.should self.send("validate_#{type}", attribute_for_type(type), :after => test_values[type][0])
end
it "should report that #{type} is not validated when option value is incorrect" do
with_validation.should_not self.send("validate_#{type}", attribute_for_type(type), :after => test_values[type][1])
end
it "should report that #{type} is not validated with option" do
no_validation.should_not self.send("validate_#{type}", attribute_for_type(type), :after => test_values[type][0])
end
end
end
describe "with on_or_before option" do
test_values = {
:date => ['2000-01-09', '2000-01-08'],
:time => ['22:00', '21:59'],
:datetime => ['2000-01-09 23:00', '2000-01-09 22:59']
:datetime => ['2000-01-09 23:00', '2000-01-09 22:59']
}
[:date, :time, :datetime].each do |type|
it "should report that #{type} is validated" do
with_validation.should self.send("validate_#{type}", attribute_for_type(type), :on_or_before => test_values[type][0])
end
it "should report that #{type} is not validated when option value is incorrect" do
with_validation.should_not self.send("validate_#{type}", attribute_for_type(type), :on_or_before => test_values[type][1])
end
it "should report that #{type} is not validated with option" do
no_validation.should_not self.send("validate_#{type}", attribute_for_type(type), :on_or_before => test_values[type][0])
end
end
end
describe "with on_or_after option" do
test_values = {
:date => ['2000-01-02', '2000-01-03'],
:time => ['10:00', '10:01'],
:datetime => ['2000-01-02 09:00', '2000-01-02 09:01']
:datetime => ['2000-01-02 09:00', '2000-01-02 09:01']
}
[:date, :time, :datetime].each do |type|
it "should report that #{type} is validated" do
with_validation.should self.send("validate_#{type}", attribute_for_type(type), :on_or_after => test_values[type][0])
end
it "should report that #{type} is not validated when option value is incorrect" do
with_validation.should_not self.send("validate_#{type}", attribute_for_type(type), :on_or_after => test_values[type][1])
end
it "should report that #{type} is not validated with option" do
no_validation.should_not self.send("validate_#{type}", attribute_for_type(type), :on_or_after => test_values[type][0])
end
end
end
describe "between option" do
test_values = {
:date => [ ['2000-01-01', '2000-01-03'], ['2000-01-01', '2000-01-04'] ],
:time => [ ['09:00', '17:00'], ['09:00', '17:01'] ],
:datetime => [ ['2000-01-01 09:00', '2000-01-01 17:00'], ['2000-01-01 09:00', '2000-01-01 17:01'] ]
:time => [ ['09:00', '17:00'], ['09:00', '17:01'] ],
:datetime => [ ['2000-01-01 09:00', '2000-01-01 17:00'], ['2000-01-01 09:00', '2000-01-01 17:01'] ]
}
[:date, :time, :datetime].each do |type|
it "should report that #{type} is validated" do
with_validation.should self.send("validate_#{type}", attribute_for_type(type), :between => test_values[type][0])
end
it "should report that #{type} is not validated when option value is incorrect" do
with_validation.should_not self.send("validate_#{type}", attribute_for_type(type), :between => test_values[type][1])
end
it "should report that #{type} is not validated with option" do
no_validation.should_not self.send("validate_#{type}", attribute_for_type(type), :between => test_values[type][0])
end
@@ -208,35 +208,35 @@ describe "ValidateTimeliness matcher" do
before do
@person = CustomMessages.new
end
it "should match error message for invalid" do
@person.should validate_date(:birth_date, :invalid_date_message => 'is not really a date')
end
it "should match error message for before option" do
@person.should validate_date(:birth_date, :before => '2000-01-10',
@person.should validate_date(:birth_date, :before => '2000-01-10',
:invalid_date_message => 'is not really a date',
:before_message => 'is too late')
end
it "should match error message for after option" do
@person.should validate_date(:birth_date, :after => '2000-01-01',
@person.should validate_date(:birth_date, :after => '2000-01-01',
:invalid_date_message => 'is not really a date',
:after_message => 'is too early')
end
end
it "should match error message for on_or_before option" do
@person.should validate_date(:birth_date, :on_or_before => '2000-01-09',
:invalid_date_message => 'is not really a date',
:on_or_before_message => 'is just too late')
end
it "should match error message for on_or_after option" do
@person.should validate_date(:birth_date, :on_or_after => '2000-01-02',
:invalid_date_message => 'is not really a date',
:on_or_after_message => 'is just too early')
end
end
def attribute_for_type(type)

View File

@@ -346,53 +346,53 @@ describe ValidatesTimeliness::Validator do
end
end
describe "instance with :equal_to restriction" do
describe "instance with :is_at restriction" do
describe "for datetime type" do
before do
configure_validator(:equal_to => Time.now)
configure_validator(:is_at => Time.now)
end
it "should have error when value not equal to :equal_to restriction" do
it "should have error when value not equal to :is_at restriction" do
validate_with(:birth_date_and_time, Time.now + 1)
should_have_error(:birth_date_and_time, :equal_to)
should_have_error(:birth_date_and_time, :is_at)
end
it "should be valid when value is equal to :equal_to restriction" do
it "should be valid when value is equal to :is_at restriction" do
validate_with(:birth_date_and_time, Time.now)
should_have_no_error(:birth_date_and_time, :equal_to)
should_have_no_error(:birth_date_and_time, :is_at)
end
end
describe "for date type" do
before do
configure_validator(:type => :date, :equal_to => Date.today)
configure_validator(:type => :date, :is_at => Date.today)
end
it "should have error when value is not equal to :equal_to restriction" do
it "should have error when value is not equal to :is_at restriction" do
validate_with(:birth_date, Date.today + 1)
should_have_error(:birth_date, :equal_to)
should_have_error(:birth_date, :is_at)
end
it "should be valid when value is equal to :equal_to restriction" do
it "should be valid when value is equal to :is_at restriction" do
validate_with(:birth_date, Date.today)
should_have_no_error(:birth_date, :equal_to)
should_have_no_error(:birth_date, :is_at)
end
end
describe "for time type" do
before do
configure_validator(:type => :time, :equal_to => "09:00:00")
configure_validator(:type => :time, :is_at => "09:00:00")
end
it "should have error when value is not equal to :equal_to restriction" do
it "should have error when value is not equal to :is_at restriction" do
validate_with(:birth_time, "09:00:01")
should_have_error(:birth_time, :equal_to)
should_have_error(:birth_time, :is_at)
end
it "should be valid when value is equal to :equal_to restriction" do
it "should be valid when value is equal to :is_at restriction" do
validate_with(:birth_time, "09:00:00")
should_have_no_error(:birth_time, :equal_to)
should_have_no_error(:birth_time, :is_at)
end
end
end
@@ -400,9 +400,9 @@ describe ValidatesTimeliness::Validator do
describe "instance with :ignore_usec option" do
it "should ignore usec on time values when evaluated" do
configure_validator(:equal_to => Time.utc(2000, 1, 1, 0, 0, 0, 0), :ignore_usec => true)
configure_validator(:is_at => 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)
should_have_no_error(:birth_date_and_time, :is_at)
end
end
@@ -422,9 +422,9 @@ describe ValidatesTimeliness::Validator do
end
it "should should ignore usec value on combined value if :ignore_usec option is true" do
configure_validator(:type => :date, :with_time => Time.mktime(2000,1,1,12,30,0,500), :equal_to => Time.mktime(2000,1,1,12,30), :ignore_usec => true)
configure_validator(:type => :date, :with_time => Time.mktime(2000,1,1,12,30,0,500), :is_at => Time.mktime(2000,1,1,12,30), :ignore_usec => true)
validate_with(:birth_date, "2000-01-01")
should_have_no_error(:birth_date, :equal_to)
should_have_no_error(:birth_date, :is_at)
end
end