aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLouise Crow <louise.crow@gmail.com>2014-11-28 15:27:36 +0000
committerLouise Crow <louise.crow@gmail.com>2014-12-15 17:28:01 +0000
commitfdcd1c204c5e20bcea3a132f4f24da4e4f23bbb4 (patch)
tree93d02b7e5f516a6e58af7415bd1a2d435705dfe9
parentc793ac3b8c0ac20e795e086544ebfcd96be1d0c0 (diff)
Add validation for presence of day.
-rw-r--r--app/models/holiday.rb2
-rw-r--r--spec/models/holiday_spec.rb133
2 files changed, 74 insertions, 61 deletions
diff --git a/app/models/holiday.rb b/app/models/holiday.rb
index 4c4941589..34044683a 100644
--- a/app/models/holiday.rb
+++ b/app/models/holiday.rb
@@ -22,6 +22,8 @@
class Holiday < ActiveRecord::Base
+ validates_presence_of :day
+
def self.holidays
@@holidays ||= all.collect { |h| h.day }.to_set
end
diff --git a/spec/models/holiday_spec.rb b/spec/models/holiday_spec.rb
index 89849abb7..8c19f4aac 100644
--- a/spec/models/holiday_spec.rb
+++ b/spec/models/holiday_spec.rb
@@ -9,87 +9,98 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
-describe Holiday, " when calculating due date" do
+describe Holiday do
- def due_date(ymd)
- return Holiday.due_date_from_working_days(Date.strptime(ymd), 20).strftime("%F")
- end
+ describe :new do
- context "in working days" do
- it "handles no holidays" do
- due_date('2008-10-01').should == '2008-10-29'
+ it 'should require a day' do
+ holiday = Holiday.new
+ holiday.valid?.should be_false
+ holiday.errors[:day].should == "can't be blank"
end
+ end
- it "handles non leap years" do
- due_date('2007-02-01').should == '2007-03-01'
- end
+ describe " when calculating due date" do
- it "handles leap years" do
- due_date('2008-02-01').should == '2008-02-29'
+ def due_date(ymd)
+ return Holiday.due_date_from_working_days(Date.strptime(ymd), 20).strftime("%F")
end
- it "handles Thursday start" do
- due_date('2009-03-12').should == '2009-04-14'
- end
+ context "in working days" do
+ it "handles no holidays" do
+ due_date('2008-10-01').should == '2008-10-29'
+ end
- it "handles Friday start" do
- due_date('2009-03-13').should == '2009-04-15'
- end
+ it "handles non leap years" do
+ due_date('2007-02-01').should == '2007-03-01'
+ end
- # Delivery at the weekend ends up the same due day as if it had arrived on
- # the Friday before. This is because the next working day (Monday) counts
- # as day 1.
- # See http://www.whatdotheyknow.com/help/officers#days
- it "handles Saturday start" do
- due_date('2009-03-14').should == '2009-04-15'
- end
- it "handles Sunday start" do
- due_date('2009-03-15').should == '2009-04-15'
- end
+ it "handles leap years" do
+ due_date('2008-02-01').should == '2008-02-29'
+ end
- it "handles Monday start" do
- due_date('2009-03-16').should == '2009-04-16'
- end
+ it "handles Thursday start" do
+ due_date('2009-03-12').should == '2009-04-14'
+ end
- it "handles Time objects" do
- Holiday.due_date_from_working_days(Time.utc(2009, 03, 16, 12, 0, 0), 20).strftime('%F').should == '2009-04-16'
- end
- end
+ it "handles Friday start" do
+ due_date('2009-03-13').should == '2009-04-15'
+ end
- context "in calendar days" do
- it "handles no holidays" do
- Holiday.due_date_from_calendar_days(Date.new(2008, 10, 1), 20).should == Date.new(2008, 10, 21)
- end
+ # Delivery at the weekend ends up the same due day as if it had arrived on
+ # the Friday before. This is because the next working day (Monday) counts
+ # as day 1.
+ # See http://www.whatdotheyknow.com/help/officers#days
+ it "handles Saturday start" do
+ due_date('2009-03-14').should == '2009-04-15'
+ end
+ it "handles Sunday start" do
+ due_date('2009-03-15').should == '2009-04-15'
+ end
- it "handles the due date falling on a Friday" do
- Holiday.due_date_from_calendar_days(Date.new(2008, 10, 4), 20).should == Date.new(2008, 10, 24)
- end
+ it "handles Monday start" do
+ due_date('2009-03-16').should == '2009-04-16'
+ end
- # If the due date would fall on a Saturday it should in fact fall on the next day that isn't a weekend
- # or a holiday
- it "handles the due date falling on a Saturday" do
- Holiday.due_date_from_calendar_days(Date.new(2008, 10, 5), 20).should == Date.new(2008, 10, 27)
+ it "handles Time objects" do
+ Holiday.due_date_from_working_days(Time.utc(2009, 03, 16, 12, 0, 0), 20).strftime('%F').should == '2009-04-16'
+ end
end
- it "handles the due date falling on a Sunday" do
- Holiday.due_date_from_calendar_days(Date.new(2008, 10, 6), 20).should == Date.new(2008, 10, 27)
- end
+ context "in calendar days" do
+ it "handles no holidays" do
+ Holiday.due_date_from_calendar_days(Date.new(2008, 10, 1), 20).should == Date.new(2008, 10, 21)
+ end
- it "handles the due date falling on a Monday" do
- Holiday.due_date_from_calendar_days(Date.new(2008, 10, 7), 20).should == Date.new(2008, 10, 27)
- end
+ it "handles the due date falling on a Friday" do
+ Holiday.due_date_from_calendar_days(Date.new(2008, 10, 4), 20).should == Date.new(2008, 10, 24)
+ end
- it "handles the due date falling on a day before a Holiday" do
- Holiday.due_date_from_calendar_days(Date.new(2008, 12, 4), 20).should == Date.new(2008, 12, 24)
- end
+ # If the due date would fall on a Saturday it should in fact fall on the next day that isn't a weekend
+ # or a holiday
+ it "handles the due date falling on a Saturday" do
+ Holiday.due_date_from_calendar_days(Date.new(2008, 10, 5), 20).should == Date.new(2008, 10, 27)
+ end
- it "handles the due date falling on a Holiday" do
- Holiday.due_date_from_calendar_days(Date.new(2008, 12, 5), 20).should == Date.new(2008, 12, 29)
- end
+ it "handles the due date falling on a Sunday" do
+ Holiday.due_date_from_calendar_days(Date.new(2008, 10, 6), 20).should == Date.new(2008, 10, 27)
+ end
+
+ it "handles the due date falling on a Monday" do
+ Holiday.due_date_from_calendar_days(Date.new(2008, 10, 7), 20).should == Date.new(2008, 10, 27)
+ end
+
+ it "handles the due date falling on a day before a Holiday" do
+ Holiday.due_date_from_calendar_days(Date.new(2008, 12, 4), 20).should == Date.new(2008, 12, 24)
+ end
- it "handles Time objects" do
- Holiday.due_date_from_calendar_days(Time.utc(2009, 03, 17, 12, 0, 0), 20).should == Date.new(2009, 4, 6)
+ it "handles the due date falling on a Holiday" do
+ Holiday.due_date_from_calendar_days(Date.new(2008, 12, 5), 20).should == Date.new(2008, 12, 29)
+ end
+
+ it "handles Time objects" do
+ Holiday.due_date_from_calendar_days(Time.utc(2009, 03, 17, 12, 0, 0), 20).should == Date.new(2009, 4, 6)
+ end
end
end
end
-