diff --git a/Gemfile b/Gemfile index 93dc9a6..98429ba 100644 --- a/Gemfile +++ b/Gemfile @@ -14,6 +14,7 @@ end group :test do gem 'factory_bot', '~> 6.2' + gem 'rack-test', '~> 2.1' gem 'rspec', '~> 3.12' gem 'rspec-github', '~> 2.4' gem 'simplecov', '~> 0.22' diff --git a/Gemfile.lock b/Gemfile.lock index 84170bd..9a59457 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -76,6 +76,8 @@ GEM rack (3.0.10) rack-accept (0.4.5) rack (>= 0.4) + rack-test (2.1.0) + rack (>= 1.3) rackup (2.1.0) rack (>= 3) webrick (~> 1.8) @@ -146,6 +148,7 @@ PLATFORMS DEPENDENCIES cmr-entity-resolution! factory_bot (~> 6.2) + rack-test (~> 2.1) rake (~> 13.0) rspec (~> 3.12) rspec-github (~> 2.4) diff --git a/spec/unit/api_spec.rb b/spec/unit/api_spec.rb new file mode 100644 index 0000000..d76326b --- /dev/null +++ b/spec/unit/api_spec.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +require 'rack/test' + +require_relative '../../lib/api' + +describe API do + include Rack::Test::Methods + + def app + API + end + + describe 'GET /health' do + it 'returns 200' do + get '/health' + expect(last_response.status).to eq(200) + end + + it 'includes a status message' do + get '/health' + expect(last_response.body).to eq({ status: 'ok' }.to_json) + end + end + + # TODO: Write tests for a valid source. + describe 'POST /import' do + context 'with an invalid source' do + it 'returns 404' do + post '/import', { source: 'invalid' } + expect(last_response.status).to eq(404) + end + + it 'includes a message' do + post '/import', { source: 'invalid' } + expect(last_response.body).to \ + eq({ status: 'error', message: 'Source "invalid" not found.' }.to_json) + end + end + end +end diff --git a/spec/unit/source/api/base_spec.rb b/spec/unit/source/api/base_spec.rb new file mode 100644 index 0000000..783a020 --- /dev/null +++ b/spec/unit/source/api/base_spec.rb @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +require_relative '../../../../lib/source/api/base' + +describe Source::API::Base do + subject(:source) { described_class.new(config) } + + let(:config) { { type: 'API::Base', payload: } } + let(:payload) { [{ id: 'rspec-0' }, { id: 'rspec-1' }] } + + describe '#each' do + context 'when the payload is an array' do + it 'yields each record' do + expect { |b| source.each(&b) }.to yield_successive_args(*payload) + end + end + + context 'when the payload is not an array' do + let(:payload) { { id: 'rspec-3' } } + + it 'yields for the single record' do + expect { |b| source.each(&b) }.to yield_with_args(payload) + end + end + end + + describe '#name' do + context 'when no name was specified' do + it 'uses the class name' do + expect(source.name).to eq('API::Base') + end + end + + context 'when a name was specified' do + let(:config) { { name: 'specified' } } + + it 'uses the specified name' do + expect(source.name).to eq('specified') + end + end + end +end diff --git a/spec/unit/source/base_spec.rb b/spec/unit/source/base_spec.rb new file mode 100644 index 0000000..fe4a58d --- /dev/null +++ b/spec/unit/source/base_spec.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require_relative '../../../lib/source/base' + +describe Source::Base do + subject(:source) { described_class.new(config) } + + let(:config) { {} } + + describe '#each' do + it 'raises an exception' do + expect { source.each }.to raise_error(NotImplementedError) + end + end + + describe '#name' do + context 'when no name was specified' do + it 'uses the class name' do + expect(source.name).to eq('Base') + end + end + + context 'when a name was specified' do + let(:config) { { name: 'specified' } } + + it 'uses the specified name' do + expect(source.name).to eq('specified') + end + end + end +end