Skip to content

Commit

Permalink
Validate YAML keys and add test coverage for real business data.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jay Shah authored and jaysh committed Sep 21, 2017
1 parent e476059 commit b1f4214
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.4.1
6 changes: 6 additions & 0 deletions lib/business/calendar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ def self.load(calendar)
raise "No such calendar '#{calendar}'" unless directory

yaml = YAML.load_file(File.join(directory, "#{calendar}.yml"))
valid_keys = ['holidays', 'working_days']

unless (yaml.keys - valid_keys).empty?
raise "Only valid keys are: #{valid_keys.join(', ')}"
end

self.new(holidays: yaml['holidays'], working_days: yaml['working_days'])
end

Expand Down
33 changes: 27 additions & 6 deletions spec/calendar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

describe Business::Calendar do
describe ".load" do
let(:data_path) { File.join(File.dirname(__FILE__), '..', 'lib', 'business', 'data') }

context "when given a valid calendar" do
subject { Business::Calendar.load("weekdays") }

Expand All @@ -21,12 +23,14 @@
it { is_expected.to be_a Business::Calendar }
end

load_additional_paths = proc do
Business::Calendar.additional_load_paths = [
File.join(File.dirname(__FILE__), 'fixtures', 'calendars')
]
end

context "when given a calendar from a custom directory" do
before do
Business::Calendar.additional_load_paths = [
File.join(File.dirname(__FILE__), 'fixtures', 'calendars')
]
end
before &load_additional_paths
after { Business::Calendar.additional_load_paths = nil }
subject { Business::Calendar.load("ecb") }

Expand All @@ -49,10 +53,27 @@
end
end

context "when given an invalid calendar" do
context "when given a calendar that does not exist" do
subject { Business::Calendar.load("invalid-calendar") }
specify { expect { subject }.to raise_error(/No such calendar/) }
end

context "when given a calendar that has invalid keys" do
before &load_additional_paths
subject { Business::Calendar.load("invalid-keys") }
specify { expect { subject }.to raise_error("Only valid keys are: holidays, working_days") }
end

context "when given real business data" do
it "validates they are all loadable by the calendar" do
Dir.glob("#{data_path}/*").each do |filename|
calendar_name = File.basename(filename, ".yml")
calendar = Business::Calendar.load(calendar_name)

expect(calendar.working_days.length).to be >= 1
end
end
end
end

describe "#set_working_days" do
Expand Down
9 changes: 9 additions & 0 deletions spec/fixtures/calendars/invalid-keys.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
working_days:
- monday
- tuesday
- wednesday
- thursday
- friday

holidayswithtypo:
- January 1st, 2015

0 comments on commit b1f4214

Please sign in to comment.