Skip to content

Commit

Permalink
Added tests for API methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesiarmes committed Mar 28, 2024
1 parent 77ebdea commit 24a7b9e
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 0 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
41 changes: 41 additions & 0 deletions spec/unit/api_spec.rb
Original file line number Diff line number Diff line change
@@ -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)

Check notice on line 17 in spec/unit/api_spec.rb

View workflow job for this annotation

GitHub Actions / RuboCop Results

spec/unit/api_spec.rb#L17

Prefer `expect(last_response).to have_http_status(200)` over `expect(last_response.status).to eq(200)`. [RSpec/Rails/HaveHttpStatus]
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

Check failure on line 29 in spec/unit/api_spec.rb

View workflow job for this annotation

GitHub Actions / spec

API POST /import with an invalid source returns 404 Failure/Error: options = Psych.load_file(path, symbolize_names: true) Errno::ENOENT: No such file or directory @ rb_sysopen - config/config.yml
post '/import', { source: 'invalid' }
expect(last_response.status).to eq(404)

Check notice on line 31 in spec/unit/api_spec.rb

View workflow job for this annotation

GitHub Actions / RuboCop Results

spec/unit/api_spec.rb#L31

Prefer `expect(last_response).to have_http_status(404)` over `expect(last_response.status).to eq(404)`. [RSpec/Rails/HaveHttpStatus]
end

it 'includes a message' do

Check failure on line 34 in spec/unit/api_spec.rb

View workflow job for this annotation

GitHub Actions / spec

API POST /import with an invalid source includes a message Failure/Error: options = Psych.load_file(path, symbolize_names: true) Errno::ENOENT: No such file or directory @ rb_sysopen - config/config.yml
post '/import', { source: 'invalid' }
expect(last_response.body).to \
eq({ status: 'error', message: 'Source "invalid" not found.' }.to_json)
end
end
end
end
42 changes: 42 additions & 0 deletions spec/unit/source/api/base_spec.rb
Original file line number Diff line number Diff line change
@@ -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
31 changes: 31 additions & 0 deletions spec/unit/source/base_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 24a7b9e

Please sign in to comment.