From b1f4214206b37e4d7897a1739ac592d04b8ce716 Mon Sep 17 00:00:00 2001 From: Jay Shah Date: Wed, 20 Sep 2017 16:51:48 +0100 Subject: [PATCH] Validate YAML keys and add test coverage for real business data. --- .ruby-version | 1 + lib/business/calendar.rb | 6 +++++ spec/calendar_spec.rb | 33 +++++++++++++++++++----- spec/fixtures/calendars/invalid-keys.yml | 9 +++++++ 4 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 .ruby-version create mode 100644 spec/fixtures/calendars/invalid-keys.yml diff --git a/.ruby-version b/.ruby-version new file mode 100644 index 0000000..005119b --- /dev/null +++ b/.ruby-version @@ -0,0 +1 @@ +2.4.1 diff --git a/lib/business/calendar.rb b/lib/business/calendar.rb index 28a0e3d..63d4029 100644 --- a/lib/business/calendar.rb +++ b/lib/business/calendar.rb @@ -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 diff --git a/spec/calendar_spec.rb b/spec/calendar_spec.rb index d56a22d..bb6bc05 100644 --- a/spec/calendar_spec.rb +++ b/spec/calendar_spec.rb @@ -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") } @@ -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") } @@ -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 diff --git a/spec/fixtures/calendars/invalid-keys.yml b/spec/fixtures/calendars/invalid-keys.yml new file mode 100644 index 0000000..a307276 --- /dev/null +++ b/spec/fixtures/calendars/invalid-keys.yml @@ -0,0 +1,9 @@ +working_days: + - monday + - tuesday + - wednesday + - thursday + - friday + +holidayswithtypo: + - January 1st, 2015