spec/validator_spec.rb in validates_timeliness-2.2.2 vs spec/validator_spec.rb in validates_timeliness-2.3.0
- old
+ new
@@ -10,17 +10,21 @@
after :all do
Time.now = nil
end
- before :each do
- @person = Person.new
+ def person
+ @person ||= Person.new
end
describe "option keys validation" do
+ before(:all) do
+ ActiveSupport::Deprecation.silenced = true
+ end
+
before do
- keys = ValidatesTimeliness::Validator::VALID_OPTIONS - [:invalid_date_message, :invalid_time_message, :with_date, :with_time]
+ keys = ValidatesTimeliness::Validator::VALID_OPTION_KEYS - [: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')
@@ -28,10 +32,19 @@
end
it "should not raise error if option keys are valid" do
lambda { Person.validates_datetime(@valid_options) }.should_not raise_error(ArgumentError)
end
+
+ it "should display deprecation notice for :equal_to" do
+ ::ActiveSupport::Deprecation.should_receive(:warn)
+ Person.validates_datetime :equal_to => Time.now
+ end
+
+ after(:all) do
+ ActiveSupport::Deprecation.silenced = false
+ end
end
describe "evaluate_option_value" do
it "should return Time object when restriction is Time object" do
evaluate_option_value(Time.now, :datetime).should be_kind_of(Time)
@@ -303,11 +316,11 @@
it "should be valid when value is equal to latest :between restriction" do
validate_with(:birth_date, 1.day.from_now.to_date)
should_have_no_error(:birth_date, :between)
end
-
+
it "should allow a range for between restriction" do
configure_validator(:type => :date, :between => (1.day.ago.to_date)..(1.day.from_now.to_date))
validate_with(:birth_date, 1.day.from_now.to_date)
should_have_no_error(:birth_date, :between)
end
@@ -344,67 +357,67 @@
should_have_no_error(:birth_time, :between)
end
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
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
describe "instance with :with_time option" do
@@ -420,13 +433,13 @@
validate_with(:birth_date, "2000-01-01")
should_have_no_error(:birth_date, :on_or_before)
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
describe "instance with :with_date option" do
@@ -494,10 +507,42 @@
validate_with(:birth_time, "12:00")
should_have_error(:birth_time, :invalid_time)
end
end
+ if defined?(I18n)
+
+ describe "localized error messages" do
+ before(:all) do
+ translations = {
+ :activerecord => {:errors => {:messages => { :after => 'retfa {{restriction}}' }}},
+ :validates_timeliness => {:error_value_formats => {}}
+ }
+ I18n.backend.store_translations 'zz', translations
+ I18n.locale = :zz
+ end
+
+ it "should be used if defined" do
+ configure_validator(:type => :date, :after => Date.today)
+ validate_with(:birth_date, 1.day.ago)
+ person.errors.on(:birth_date).should match(/retfa/)
+ end
+
+ it "should use I18n translation missing message when not defined" do
+ configure_validator(:type => :date, :on_or_after => Date.today)
+ validate_with(:birth_date, 1.day.ago)
+ person.errors.on(:birth_date).should match(/translation missing/)
+ end
+
+ after(:all) do
+ I18n.locale = :en
+ end
+
+ end
+
+ end
+
describe "custom_error_messages" do
it "should return hash of custom error messages from configuration with _message truncated from keys" do
configure_validator(:type => :date, :invalid_date_message => 'thats no date')
validator.send(:custom_error_messages)[:invalid_date].should == 'thats no date'
end
@@ -564,10 +609,28 @@
it "should format time value of restriction" do
configure_validator(:type => :time, :after => '12:00')
validate_with(:birth_time, '11:59')
person.errors.on(:birth_time).should match(/after \d{2}:\d{2}:\d{2}\Z/)
end
+
+ if defined?(I18n)
+
+ describe "I18n" do
+ it "should use global default if locale format missing" do
+ I18n.backend.store_translations 'zz', :activerecord => {:errors => {:messages => { :after => 'after {{restriction}}' }}}
+ I18n.locale = :zz
+ configure_validator(:type => :datetime, :after => 1.day.from_now)
+ validate_with(:birth_date_and_time, Time.now)
+ person.errors.on(:birth_date_and_time).should match(/after \d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\Z/)
+ end
+
+ after do
+ I18n.locale = :en
+ end
+ end
+
+ end
end
describe "custom formats" do
before :all do
@@ -642,9 +705,9 @@
end
end
def error_messages
return @error_messages if defined?(@error_messages)
- messages = validator.send(:error_messages)
+ messages = defined?(I18n) ? I18n.t('activerecord.errors.messages') : validator.send(:error_messages)
@error_messages = messages.inject({}) {|h, (k, v)| h[k] = v.sub(/ (\%s|\{\{\w*\}\}).*/, ''); h }
end
end