From b5661bacec69611d586b13e063e1708db5772280 Mon Sep 17 00:00:00 2001 From: Adam Meehan Date: Mon, 2 Aug 2010 19:26:41 +1000 Subject: [PATCH] remaining specs for validator options --- .../validator/after_spec.rb | 59 +++++++++++++++++++ .../validator/before_spec.rb | 59 +++++++++++++++++++ .../validator/is_at_spec.rb | 29 +++++---- .../validator/on_or_after_spec.rb | 59 +++++++++++++++++++ .../validator/on_or_before_spec.rb | 59 +++++++++++++++++++ 5 files changed, 252 insertions(+), 13 deletions(-) create mode 100644 spec/validates_timeliness/validator/after_spec.rb create mode 100644 spec/validates_timeliness/validator/before_spec.rb create mode 100644 spec/validates_timeliness/validator/on_or_after_spec.rb create mode 100644 spec/validates_timeliness/validator/on_or_before_spec.rb diff --git a/spec/validates_timeliness/validator/after_spec.rb b/spec/validates_timeliness/validator/after_spec.rb new file mode 100644 index 0000000..72de343 --- /dev/null +++ b/spec/validates_timeliness/validator/after_spec.rb @@ -0,0 +1,59 @@ +require 'spec_helper' + +describe ValidatesTimeliness::Validator, ":after option" do + include ModelHelpers + + describe "for date type" do + before do + Person.validates_date :birth_date, :after => Date.new(2010, 1, 1) + end + + it "should not be valid for same date value" do + invalid!(:birth_date, Date.new(2010, 1, 1), 'must be after 2010-01-01') + end + + it "should not be valid for date before restriction" do + invalid!(:birth_date, Date.new(2009, 12, 31), 'must be after 2010-01-01') + end + + it "should be valid for date after restriction" do + valid!(:birth_date, Date.new(2010, 1, 2)) + end + end + + describe "for time type" do + before do + Person.validates_time :birth_time, :after => Time.mktime(2000, 1, 1, 12, 0, 0) + end + + it "should not be valid for same time as restriction" do + invalid!(:birth_time, Time.local_time(2000, 1, 1, 12, 0, 0), 'must be after 12:00:00') + end + + it "should not be valid for time before restriction" do + invalid!(:birth_time, Time.local_time(2000, 1, 1, 11, 59, 59), 'must be after 12:00:00') + end + + it "should be valid for time after restriction" do + valid!(:birth_time, Time.local_time(2000, 1, 1, 12, 00, 01)) + end + end + + describe "for datetime type" do + before do + Person.validates_datetime :birth_datetime, :after => DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 0) + end + + it "should not be valid for same datetime as restriction" do + invalid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 0), 'must be after 2010-01-01 12:00:00') + end + + it "should be valid for datetime is before restriction" do + invalid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 11, 59, 59), 'must be after 2010-01-01 12:00:00') + end + + it "should be valid for datetime is after restriction" do + valid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 1)) + end + end +end diff --git a/spec/validates_timeliness/validator/before_spec.rb b/spec/validates_timeliness/validator/before_spec.rb new file mode 100644 index 0000000..d856cb3 --- /dev/null +++ b/spec/validates_timeliness/validator/before_spec.rb @@ -0,0 +1,59 @@ +require 'spec_helper' + +describe ValidatesTimeliness::Validator, ":before option" do + include ModelHelpers + + describe "for date type" do + before do + Person.validates_date :birth_date, :before => Date.new(2010, 1, 1) + end + + it "should not be valid for date after restriction" do + invalid!(:birth_date, Date.new(2010, 1, 2), 'must be before 2010-01-01') + end + + it "should not be valid for same date value" do + invalid!(:birth_date, Date.new(2010, 1, 1), 'must be before 2010-01-01') + end + + it "should be valid for date before restriction" do + valid!(:birth_date, Date.new(2009, 12, 31)) + end + end + + describe "for time type" do + before do + Person.validates_time :birth_time, :before => Time.mktime(2000, 1, 1, 12, 0, 0) + end + + it "should not be valid for time after restriction" do + invalid!(:birth_time, Time.local_time(2000, 1, 1, 12, 00, 01), 'must be before 12:00:00') + end + + it "should not be valid for same time as restriction" do + invalid!(:birth_time, Time.local_time(2000, 1, 1, 12, 0, 0), 'must be before 12:00:00') + end + + it "should be valid for time before restriction" do + valid!(:birth_time, Time.local_time(2000, 1, 1, 11, 59, 59)) + end + end + + describe "for datetime type" do + before do + Person.validates_datetime :birth_datetime, :before => DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 0) + end + + it "should not be valid for datetime after restriction" do + invalid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 1), 'must be before 2010-01-01 12:00:00') + end + + it "should not be valid for same datetime as restriction" do + invalid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 0), 'must be before 2010-01-01 12:00:00') + end + + it "should be valid for datetime before restriction" do + valid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 11, 59, 59)) + end + end +end diff --git a/spec/validates_timeliness/validator/is_at_spec.rb b/spec/validates_timeliness/validator/is_at_spec.rb index d09ab01..3451b3f 100644 --- a/spec/validates_timeliness/validator/is_at_spec.rb +++ b/spec/validates_timeliness/validator/is_at_spec.rb @@ -8,53 +8,56 @@ describe ValidatesTimeliness::Validator, ":is_at option" do end describe "for date type" do - it "should not be valid for day before restriction" do + before do Person.validates_date :birth_date, :is_at => Date.new(2010, 1, 1) + end + + it "should not be valid for date before restriction" do invalid!(:birth_date, Date.new(2009, 12, 31), 'must be at 2010-01-01') end it "should not be valid for date after restriction" do - Person.validates_date :birth_date, :is_at => Date.new(2010, 1, 1) invalid!(:birth_date, Date.new(2010, 1, 2), 'must be at 2010-01-01') end it "should be valid for same date value" do - Person.validates_date :birth_date, :is_at => Date.new(2010, 1, 1) valid!(:birth_date, Date.new(2010, 1, 1)) end end describe "for time type" do - it "should not be valid for time before restriction" do + before do Person.validates_time :birth_time, :is_at => Time.mktime(2000, 1, 1, 12, 0, 0) + end + + it "should not be valid for time before restriction" do invalid!(:birth_time, Time.local_time(2000, 1, 1, 11, 59, 59), 'must be at 12:00:00') end it "should not be valid for time after restriction" do - Person.validates_time :birth_time, :is_at => Time.mktime(2000, 1, 1, 12, 0, 0) invalid!(:birth_time, Time.local_time(2000, 1, 1, 12, 00, 01), 'must be at 12:00:00') end it "should be valid for same time as restriction" do - Person.validates_time :birth_time, :is_at => Time.local_time(2000, 1, 1, 0, 0, 0) - valid!(:birth_time, Time.local_time(2000, 1, 1, 0, 0, 0)) + valid!(:birth_time, Time.local_time(2000, 1, 1, 12, 0, 0)) end end describe "for datetime type" do - it "should not be valid for datetime where time part is before restriction" do + before do Person.validates_datetime :birth_datetime, :is_at => DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 0) + end + + it "should not be valid for datetime before restriction" do invalid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 11, 59, 59), 'must be at 2010-01-01 12:00:00') end - it "should not be valid for datetime where date is before restriction" do - Person.validates_datetime :birth_datetime, :is_at => DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 0) - invalid!(:birth_datetime, DateTime.civil_from_format(:local, 2009, 12, 31, 12, 0, 0), 'must be at 2010-01-01 12:00:00') + it "should not be valid for datetime after restriction" do + invalid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 1), 'must be at 2010-01-01 12:00:00') end it "should be valid for same datetime as restriction" do - Person.validates_datetime :birth_datetime, :is_at => DateTime.civil_from_format(:local, 2010, 1, 1, 0, 0, 0) - valid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 0, 0, 0)) + valid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 0)) end end end diff --git a/spec/validates_timeliness/validator/on_or_after_spec.rb b/spec/validates_timeliness/validator/on_or_after_spec.rb new file mode 100644 index 0000000..0457bea --- /dev/null +++ b/spec/validates_timeliness/validator/on_or_after_spec.rb @@ -0,0 +1,59 @@ +require 'spec_helper' + +describe ValidatesTimeliness::Validator, ":on_or_after option" do + include ModelHelpers + + describe "for date type" do + before do + Person.validates_date :birth_date, :on_or_after => Date.new(2010, 1, 1) + end + + it "should not be valid for date before restriction" do + invalid!(:birth_date, Date.new(2009, 12, 31), 'must be on or after 2010-01-01') + end + + it "should be valid for same date value" do + valid!(:birth_date, Date.new(2010, 1, 1)) + end + + it "should be valid for date after restriction" do + valid!(:birth_date, Date.new(2010, 1, 2)) + end + end + + describe "for time type" do + before do + Person.validates_time :birth_time, :on_or_after => Time.mktime(2000, 1, 1, 12, 0, 0) + end + + it "should not be valid for time before restriction" do + invalid!(:birth_time, Time.local_time(2000, 1, 1, 11, 59, 59), 'must be on or after 12:00:00') + end + + it "should be valid for time after restriction" do + valid!(:birth_time, Time.local_time(2000, 1, 1, 12, 00, 01)) + end + + it "should be valid for same time as restriction" do + valid!(:birth_time, Time.local_time(2000, 1, 1, 12, 0, 0)) + end + end + + describe "for datetime type" do + before do + Person.validates_datetime :birth_datetime, :on_or_after => DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 0) + end + + it "should not be valid for datetime before restriction" do + invalid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 11, 59, 59), 'must be on or after 2010-01-01 12:00:00') + end + + it "should be valid for same datetime as restriction" do + valid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 0)) + end + + it "should be valid for datetime after restriction" do + valid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 1)) + end + end +end diff --git a/spec/validates_timeliness/validator/on_or_before_spec.rb b/spec/validates_timeliness/validator/on_or_before_spec.rb new file mode 100644 index 0000000..04af38b --- /dev/null +++ b/spec/validates_timeliness/validator/on_or_before_spec.rb @@ -0,0 +1,59 @@ +require 'spec_helper' + +describe ValidatesTimeliness::Validator, ":on_or_before option" do + include ModelHelpers + + describe "for date type" do + before do + Person.validates_date :birth_date, :on_or_before => Date.new(2010, 1, 1) + end + + it "should not be valid for date after restriction" do + invalid!(:birth_date, Date.new(2010, 1, 2), 'must be on or before 2010-01-01') + end + + it "should be valid for date before restriction" do + valid!(:birth_date, Date.new(2009, 12, 31)) + end + + it "should be valid for same date value" do + valid!(:birth_date, Date.new(2010, 1, 1)) + end + end + + describe "for time type" do + before do + Person.validates_time :birth_time, :on_or_before => Time.mktime(2000, 1, 1, 12, 0, 0) + end + + it "should not be valid for time after restriction" do + invalid!(:birth_time, Time.local_time(2000, 1, 1, 12, 00, 01), 'must be on or before 12:00:00') + end + + it "should be valid for time before restriction" do + valid!(:birth_time, Time.local_time(2000, 1, 1, 11, 59, 59)) + end + + it "should be valid for same time as restriction" do + valid!(:birth_time, Time.local_time(2000, 1, 1, 12, 0, 0)) + end + end + + describe "for datetime type" do + before do + Person.validates_datetime :birth_datetime, :on_or_before => DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 0) + end + + it "should not be valid for datetime after restriction" do + invalid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 1), 'must be on or before 2010-01-01 12:00:00') + end + + it "should be valid for same datetime as restriction" do + valid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 0)) + end + + it "should not be valid for datetime before restriction" do + valid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 11, 59, 59)) + end + end +end