From 3a1499524f407cab0bc7aa7bdfac8755153ca03c Mon Sep 17 00:00:00 2001 From: pezholio Date: Wed, 19 Sep 2018 09:46:56 +0100 Subject: [PATCH] Some initial tests --- .rspec | 2 ++ lib/uk_planning_scraper.rb | 3 ++ lib/uk_planning_scraper/authority.rb | 7 +++-- spec/authority_spec.rb | 42 ++++++++++++++++++++++++++++ spec/spec_helper.rb | 16 +++++++++++ uk_planning_scraper.gemspec | 2 ++ 6 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 .rspec create mode 100644 spec/authority_spec.rb create mode 100644 spec/spec_helper.rb diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..2c51099 --- /dev/null +++ b/.rspec @@ -0,0 +1,2 @@ +--require spec_helper +--format=documentation diff --git a/lib/uk_planning_scraper.rb b/lib/uk_planning_scraper.rb index 34abd18..0718905 100644 --- a/lib/uk_planning_scraper.rb +++ b/lib/uk_planning_scraper.rb @@ -7,4 +7,7 @@ require 'logger' module UKPlanningScraper class SystemNotSupportedError < StandardError end + + class AuthorityNotFound < StandardError + end end diff --git a/lib/uk_planning_scraper/authority.rb b/lib/uk_planning_scraper/authority.rb index 43084c1..0ff6396 100644 --- a/lib/uk_planning_scraper/authority.rb +++ b/lib/uk_planning_scraper/authority.rb @@ -72,8 +72,9 @@ module UKPlanningScraper end def self.named(name) - @@authorities.each { |a| return a if name == a.name } - nil + authority = @@authorities.find { |a| name == a.name } + raise AuthorityNotFound if authority.nil? + authority end # Tagged x @@ -99,7 +100,7 @@ module UKPlanningScraper def self.load # Don't run this method more than once - return unless @@authorities.empty? + return unless @@authorities.empty? # FIXME hardcoded file path CSV.foreach(File.join(File.dirname(__dir__), 'uk_planning_scraper', 'authorities.csv')) do |line| @@authorities << Authority.new( diff --git a/spec/authority_spec.rb b/spec/authority_spec.rb new file mode 100644 index 0000000..6c4a72f --- /dev/null +++ b/spec/authority_spec.rb @@ -0,0 +1,42 @@ +require 'spec_helper' + +describe UKPlanningScraper::Authority do + + describe '#named' do + + let(:authority) { described_class.named(authority_name) } + + context 'when authority exists' do + let(:authority_name) { 'Westminster' } + + it 'returns an authority' do + expect(authority).to be_a(UKPlanningScraper::Authority) + end + end + + context 'when authority does not exist' do + let(:authority_name) { 'Westmonster' } + + it 'raises an error' do + expect { authority }.to raise_error(UKPlanningScraper::AuthorityNotFound) + end + end + end + + describe '#all' do + + let(:all) { described_class.all } + + it 'returns all authorities' do + expect(all.count).to eq(48) + end + + it 'returns a list of authorities' do + all.each do |authority| + expect(authority).to be_a(UKPlanningScraper::Authority) + end + end + + end + +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..ec96157 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,16 @@ +require 'simplecov' +SimpleCov.start + +require 'uk_planning_scraper' + +RSpec.configure do |config| + config.expect_with :rspec do |expectations| + expectations.include_chain_clauses_in_custom_matcher_descriptions = true + end + + config.mock_with :rspec do |mocks| + mocks.verify_partial_doubles = true + end + + config.shared_context_metadata_behavior = :apply_to_host_groups +end diff --git a/uk_planning_scraper.gemspec b/uk_planning_scraper.gemspec index 025e84a..98cd9b0 100644 --- a/uk_planning_scraper.gemspec +++ b/uk_planning_scraper.gemspec @@ -30,6 +30,8 @@ Gem::Specification.new do |spec| spec.add_development_dependency "bundler", "~> 1.15" spec.add_development_dependency "rake", "~> 10.0" + spec.add_development_dependency "rspec", "~> 3.8.0" + spec.add_development_dependency "simplecov", "~> 0.16.1" spec.add_runtime_dependency "mechanize", "~> 2.7" spec.add_runtime_dependency "http"