From 1b1c4acc3872cd8bc4d1c591a7ff53ad4bde60a4 Mon Sep 17 00:00:00 2001 From: Geremia Taglialatela Date: Sat, 20 Jan 2024 20:23:25 +0100 Subject: [PATCH] Improve specs - Move to proper path - Increase coverage --- .rubocop_todo.yml | 4 -- spec/error_handling_spec.rb | 42 --------------------- spec/hawk/error_spec.rb | 66 +++++++++++++++++++++++++++++++++ spec/{ => hawk}/linker_spec.rb | 2 +- spec/hawk/save_spec.rb | 33 +++++++++++++++++ spec/{ => hawk}/schema_spec.rb | 2 +- spec/writing_operations_spec.rb | 35 ----------------- 7 files changed, 101 insertions(+), 83 deletions(-) delete mode 100644 spec/error_handling_spec.rb create mode 100644 spec/hawk/error_spec.rb rename spec/{ => hawk}/linker_spec.rb (96%) create mode 100644 spec/hawk/save_spec.rb rename spec/{ => hawk}/schema_spec.rb (96%) delete mode 100644 spec/writing_operations_spec.rb diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 63581b6..e7b1f0d 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -12,8 +12,4 @@ RSpec/DescribeClass: - 'spec/associations_spec.rb' - 'spec/basic_operations_spec.rb' - 'spec/collections_spec.rb' - - 'spec/error_handling_spec.rb' - - 'spec/linker_spec.rb' - 'spec/namespacing_spec.rb' - - 'spec/schema_spec.rb' - - 'spec/writing_operations_spec.rb' diff --git a/spec/error_handling_spec.rb b/spec/error_handling_spec.rb deleted file mode 100644 index 6284098..0000000 --- a/spec/error_handling_spec.rb +++ /dev/null @@ -1,42 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -class Failure < Hawk::Model::Base - url 'https://example.org/' - client_name 'Foobar' -end - -RSpec.describe 'error handling' do - it 'raises an exception when the record is missing' do - stub_request(:GET, 'https://example.org/failures/404') - .with(headers: { 'User-Agent' => 'Foobar' }) - .to_return(status: 404, body: 'Not found', headers: {}) - - expect { Failure.find(404) }.to raise_error(Hawk::Error::NotFound) - end - - it 'raises an exception when the server returns an HTTP 500' do - stub_request(:GET, 'https://example.org/failures/500') - .with(headers: { 'User-Agent' => 'Foobar' }) - .to_return(status: 500, body: ':(', headers: {}) - - expect { Failure.find(500) }.to raise_error(Hawk::Error::InternalServerError) - end - - it 'raises an exception when the server returns unexpected HTTP codes' do - stub_request(:GET, 'https://example.org/failures/666') - .with(headers: { 'User-Agent' => 'Foobar' }) - .to_return(status: 666, body: '{}', headers: {}) - - expect { Failure.find(666) }.to raise_error(Hawk::Error) - end - - it 'raises an exception when the server times out' do - stub_request(:GET, 'https://example.org/failures/123') - .with(headers: { 'User-Agent' => 'Foobar' }) - .to_timeout - - expect { Failure.find(123) }.to raise_error(Hawk::Error::Timeout) - end -end diff --git a/spec/hawk/error_spec.rb b/spec/hawk/error_spec.rb new file mode 100644 index 0000000..ffbf5f3 --- /dev/null +++ b/spec/hawk/error_spec.rb @@ -0,0 +1,66 @@ +# frozen_string_literal: true + +require 'spec_helper' + +class Failure < Hawk::Model::Base + url 'https://example.org/' + client_name 'Foobar' +end + +RSpec.describe Hawk::Error do + it 'raises an exception when the response is 0' do + stub_request(:GET, 'https://example.org/failures/0') + .with(headers: { 'User-Agent' => 'Foobar' }) + .to_return(status: 0, body: 'Not found', headers: {}) + + expect { Failure.find(0) }.to raise_error(described_class::Empty) + end + + it 'raises an exception when the request is bad' do + stub_request(:GET, 'https://example.org/failures/400') + .with(headers: { 'User-Agent' => 'Foobar' }) + .to_return(status: 400, body: 'Not found', headers: {}) + + expect { Failure.find(400) }.to raise_error(described_class::BadRequest) + end + + it 'raises an exception when the request is forbidden' do + stub_request(:GET, 'https://example.org/failures/403') + .with(headers: { 'User-Agent' => 'Foobar' }) + .to_return(status: 403, body: 'Not found', headers: {}) + + expect { Failure.find(403) }.to raise_error(described_class::Forbidden) + end + + it 'raises an exception when the record is missing' do + stub_request(:GET, 'https://example.org/failures/404') + .with(headers: { 'User-Agent' => 'Foobar' }) + .to_return(status: 404, body: 'Not found', headers: {}) + + expect { Failure.find(404) }.to raise_error(described_class::NotFound) + end + + it 'raises an exception when the server returns an HTTP 500' do + stub_request(:GET, 'https://example.org/failures/500') + .with(headers: { 'User-Agent' => 'Foobar' }) + .to_return(status: 500, body: ':(', headers: {}) + + expect { Failure.find(500) }.to raise_error(described_class::InternalServerError) + end + + it 'raises an exception when the server returns unexpected HTTP codes' do + stub_request(:GET, 'https://example.org/failures/666') + .with(headers: { 'User-Agent' => 'Foobar' }) + .to_return(status: 666, body: '{}', headers: {}) + + expect { Failure.find(666) }.to raise_error(described_class) + end + + it 'raises an exception when the server times out' do + stub_request(:GET, 'https://example.org/failures/123') + .with(headers: { 'User-Agent' => 'Foobar' }) + .to_timeout + + expect { Failure.find(123) }.to raise_error(described_class::Timeout) + end +end diff --git a/spec/linker_spec.rb b/spec/hawk/linker_spec.rb similarity index 96% rename from spec/linker_spec.rb rename to spec/hawk/linker_spec.rb index e181cce..ca0d56b 100644 --- a/spec/linker_spec.rb +++ b/spec/hawk/linker_spec.rb @@ -21,7 +21,7 @@ class Post resource_accessor :author end -RSpec.describe 'linker' do +RSpec.describe Hawk::Linker do let(:author_attributes) do { id: 1, diff --git a/spec/hawk/save_spec.rb b/spec/hawk/save_spec.rb new file mode 100644 index 0000000..713bdb3 --- /dev/null +++ b/spec/hawk/save_spec.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +require 'spec_helper' + +class Bear < Hawk::Model::Base + url 'https://example.org/' + client_name 'Foobar' + + schema do + integer :id + string :name + end +end + +RSpec.describe Hawk, '#save' do + let(:bear_attributes) do + { + id: 1, name: 'Paddington' + } + end + + it 'triggers an HTTP request' do + stub_request(:PUT, 'https://example.org/bears') + .with(body: { 'name' => 'Paddington' }, + headers: { + 'Content-Type' => 'application/x-www-form-urlencoded', + 'User-Agent' => 'Typhoeus - https://github.com/typhoeus/typhoeus' + }) + .to_return(status: 200, body: bear_attributes.to_json, headers: {}) + paddington = Bear.new.tap { |b| b.name = 'Paddington' } + expect { paddington.save }.not_to raise_error + end +end diff --git a/spec/schema_spec.rb b/spec/hawk/schema_spec.rb similarity index 96% rename from spec/schema_spec.rb rename to spec/hawk/schema_spec.rb index e0fd53c..86271a0 100644 --- a/spec/schema_spec.rb +++ b/spec/hawk/schema_spec.rb @@ -17,7 +17,7 @@ class Car < Hawk::Model::Base end end -RSpec.describe 'schema' do +RSpec.describe Hawk, '.schema' do specify do expect(Car.schema).to be_a(Hash) end diff --git a/spec/writing_operations_spec.rb b/spec/writing_operations_spec.rb deleted file mode 100644 index 968d747..0000000 --- a/spec/writing_operations_spec.rb +++ /dev/null @@ -1,35 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -class Bear < Hawk::Model::Base - url 'https://example.org/' - client_name 'Foobar' - - schema do - integer :id - string :name - end -end - -RSpec.describe 'persisting operations' do - let(:bear_attributes) do - { - id: 1, name: 'Paddington' - } - end - - describe '#save' do - it 'triggers an HTTP request' do - stub_request(:PUT, 'https://example.org/bears') - .with(body: { 'name' => 'Paddington' }, - headers: { - 'Content-Type' => 'application/x-www-form-urlencoded', - 'User-Agent' => 'Typhoeus - https://github.com/typhoeus/typhoeus' - }) - .to_return(status: 200, body: bear_attributes.to_json, headers: {}) - paddington = Bear.new.tap { |b| b.name = 'Paddington' } - expect { paddington.save }.not_to raise_error - end - end -end