Skip to content

Commit

Permalink
Merge pull request #38 from ifad/chore/improve-specs
Browse files Browse the repository at this point in the history
Improve specs
  • Loading branch information
tagliala authored Jan 20, 2024
2 parents 147514e + 1b1c4ac commit b2070e8
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 83 deletions.
4 changes: 0 additions & 4 deletions .rubocop_todo.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 0 additions & 42 deletions spec/error_handling_spec.rb

This file was deleted.

66 changes: 66 additions & 0 deletions spec/hawk/error_spec.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion spec/linker_spec.rb → spec/hawk/linker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
33 changes: 33 additions & 0 deletions spec/hawk/save_spec.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion spec/schema_spec.rb → spec/hawk/schema_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 0 additions & 35 deletions spec/writing_operations_spec.rb

This file was deleted.

0 comments on commit b2070e8

Please sign in to comment.