Skip to content

Commit

Permalink
Apply Rspec/MultipleMemoizedHelpers
Browse files Browse the repository at this point in the history
  • Loading branch information
iusztin committed Sep 9, 2020
1 parent b3b9c97 commit 97dd391
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 75 deletions.
5 changes: 3 additions & 2 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
inherit_from: .rubocop_todo.yml

require:
- rubocop-rspec

Expand Down Expand Up @@ -75,6 +73,9 @@ RSpec/ContextWording:
- without
- and

RSpec/MultipleMemoizedHelpers:
Max: 7

Layout/EmptyLinesAroundAttributeAccessor: # (new in 0.83)
Enabled: true

Expand Down
12 changes: 0 additions & 12 deletions .rubocop_todo.yml

This file was deleted.

102 changes: 54 additions & 48 deletions spec/scnnr/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,22 @@
RSpec.describe Scnnr::Client do
let(:client) do
described_class.new do |config|
config.api_key = api_key
config.api_version = api_version
config.timeout = timeout
config.logger = logger
config.logger.level = logger_level
config.api_key = 'dummy_key'
config.api_version = 'v1'
config.timeout = 0
config.logger = Logger.new('/dev/null')
config.logger.level = :info
end
end
let(:api_key) { 'dummy_key' }
let(:api_version) { 'v1' }
let(:timeout) { 0 }
let(:logger) { Logger.new('/dev/null') }
let(:logger_level) { :info }

let(:expected_uri_base) { "https://#{Scnnr::Routing::API_HOST}/#{api_version}" }

let(:mock_connection) { instance_double(Scnnr::Connection) }
let(:mock_origin_response) { instance_double(Net::HTTPResponse) }
let(:mock_response) { instance_double(Scnnr::Response) }

def api_uri(path)
URI.parse "https://#{Scnnr::Routing::API_HOST}/v1#{path}"
end

before do
allow(mock_origin_response).to receive(:body) { fixture('queued_recognition.json') }
allow(mock_origin_response).to receive(:content_type).and_return(Scnnr::Response::SUPPORTED_CONTENT_TYPE)
Expand All @@ -32,27 +29,39 @@
describe '#config' do
subject { client.config }

let(:client) do
described_class.new do |config|
config.api_key = 'dummy_key'
config.api_version = 'v1'
config.timeout = 0
config.logger = logger
config.logger.level = :info
end
end
let(:logger) { Logger.new('/dev/null') }

it 'can set api settings via block' do
expect(subject.api_key).to eq api_key
expect(subject.api_version).to eq api_version
expect(subject.timeout).to eq timeout
expect(subject.api_key).to eq 'dummy_key'
expect(subject.api_version).to eq 'v1'
expect(subject.timeout).to eq 0
expect(subject.logger).to eq logger
expect(subject.logger.level).to eq Logger.const_get(logger_level.upcase)
expect(subject.logger.level).to eq Logger::INFO
end
end

describe '#recognize_image' do
subject { client.recognize_image(image, options) }
subject { client.recognize_image(image, public: true) }

let(:image) { fixture('images/sample.png') }
let(:uri) { URI.parse "#{expected_uri_base}/recognitions?public=true" }
let(:options) { { public: true } }
let(:expected_recognition) { Scnnr::Recognition.new }

it do
expect(Scnnr::PollingManager)
.to receive(:start).with(client, hash_including(client.config.to_h)).and_call_original
expect(Scnnr::Connection).to receive(:new).with(uri, :post, api_key, logger) { mock_connection }
expect(Scnnr::Connection)
.to receive(:new).with(
api_uri('/recognitions?public=true'), :post, client.config.api_key, client.config.logger
) { mock_connection }
expect(mock_connection).to receive(:send_stream).with(image) { mock_origin_response }
expect(Scnnr::Response).to receive(:new).with(mock_origin_response) { mock_response }
expect(mock_response).to receive(:build_recognition) { expected_recognition }
Expand All @@ -61,17 +70,18 @@
end

describe '#recognize_url' do
subject { client.recognize_url(url, options) }
subject { client.recognize_url(url, force: true) }

let(:url) { 'https://example.com/dummy.jpg' }
let(:uri) { URI.parse "#{expected_uri_base}/remote/recognitions?force=true" }
let(:options) { { force: true } }
let(:expected_recognition) { Scnnr::Recognition.new }

it do
expect(Scnnr::PollingManager)
.to receive(:start).with(client, hash_including(client.config.to_h)).and_call_original
expect(Scnnr::Connection).to receive(:new).with(uri, :post, api_key, logger) { mock_connection }
expect(Scnnr::Connection)
.to receive(:new).with(
api_uri('/remote/recognitions?force=true'), :post, client.config.api_key, client.config.logger
) { mock_connection }
expect(mock_connection).to receive(:send_json).with({ url: url }) { mock_origin_response }
expect(Scnnr::Response).to receive(:new).with(mock_origin_response) { mock_response }
expect(mock_response).to receive(:build_recognition) { expected_recognition }
Expand All @@ -80,15 +90,16 @@
end

describe '#fetch' do
subject { client.fetch(recognition_id, options) }
subject { client.fetch(recognition_id) }

let(:uri) { URI.parse "#{expected_uri_base}/recognitions/#{recognition_id}" }
let(:recognition_id) { 'dummy_id' }
let(:options) { {} }
let(:expected_recognition) { Scnnr::Recognition.new }

it do
expect(Scnnr::Connection).to receive(:new).with(uri, :get, nil, logger) { mock_connection }
expect(Scnnr::Connection)
.to receive(:new).with(
api_uri("/recognitions/#{recognition_id}"), :get, nil, client.config.logger
) { mock_connection }
expect(mock_connection).to receive(:send_request) { mock_origin_response }
expect(Scnnr::Response).to receive(:new).with(mock_origin_response) { mock_response }
expect(mock_response).to receive(:build_recognition) { expected_recognition }
Expand All @@ -97,41 +108,36 @@
end

describe '#coordinate' do
subject { client.coordinate(category, labels, taste_with_unknown, options) }
subject { client.coordinate(category, labels, taste.merge(unknown: 0.4)) }

let(:category) { 'tops' }
let(:labels) { %w[ホワイト スカート] }
let(:taste) { { casual: 0.3, girly: 0.7 } }
let(:taste_with_unknown) { taste.merge(unknown: 0.4) }
let(:options) { {} }

shared_examples_for('sending an expected request and a coordinate returns successfully') do
let(:uri) { URI.parse "#{expected_uri_base}/coordinates" }
let(:expected_payload) do
{
item: { category: category, labels: labels },
taste: taste,
}
end
let(:expected_coordinate) { nil }

shared_examples_for('sending an expected request and a coordinate returns successfully') do |api_path|
expected_coordinate = nil

it do
expect(Scnnr::Connection).to receive(:new).with(uri, :post, api_key, logger) { mock_connection }
expect(mock_connection).to receive(:send_json).with(expected_payload) { mock_origin_response }
expect(Scnnr::Connection)
.to receive(:new).with(
api_uri(api_path), :post, client.config.api_key, client.config.logger
) { mock_connection }
expect(mock_connection)
.to receive(:send_json).with(
item: { category: category, labels: labels }, taste: taste
) { mock_origin_response }
expect(Scnnr::Response).to receive(:new).with(mock_origin_response) { mock_response }
expect(mock_response).to receive(:build_coordinate) { expected_coordinate }
expect(subject).to eq expected_coordinate
end
end

it_behaves_like 'sending an expected request and a coordinate returns successfully'
it_behaves_like 'sending an expected request and a coordinate returns successfully', '/coordinates'

context 'when `target` option is passed' do
let(:options) { { target: 8 } }
subject { client.coordinate(category, labels, taste.merge(unknown: 0.4), target: 8) }

it_behaves_like 'sending an expected request and a coordinate returns successfully' do
let(:uri) { URI.parse "#{expected_uri_base}/coordinates?target=8" }
end
it_behaves_like 'sending an expected request and a coordinate returns successfully', '/coordinates?target=8'
end
end
end
22 changes: 9 additions & 13 deletions spec/scnnr/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@
RSpec.describe Scnnr::Connection do
before { stub_request(method, uri).to_return(body: expected_body, status: 200) }

let(:connection) { described_class.new(uri, method, api_key, logger) }
let(:connection) { described_class.new(uri, method, api_key, Logger.new('/dev/null')) }
let(:uri) { URI.parse('https://dummy.scnnr.cubki.jp') }
let(:logger) { Logger.new('/dev/null') }
let(:api_key) { nil }
let(:expected_body) { fixture('queued_recognition.json').read }

describe '#send_request' do
subject { connection.send_request(&block) }
subject { connection.send_request }

let(:method) { %i[get post].sample }
let(:block) { nil }

context 'when the api_key is not set' do
it do
Expand All @@ -37,16 +35,14 @@
end

context 'when passing block' do
let(:block) { ->(request) { request.content_type = requested_content_type } }
subject { connection.send_request { |request| request.content_type = requested_content_type } }

let(:requested_content_type) { 'application/json' }
let(:requested_options) do
{ headers: { 'Content-Type' => requested_content_type } }
end

it do
expect(subject).to be_a Net::HTTPSuccess
expect(subject.body).to eq expected_body
expect(WebMock).to have_requested(method, uri).with(requested_options)
expect(WebMock).to have_requested(method, uri).with(headers: { 'Content-Type' => requested_content_type })
end
end

Expand Down Expand Up @@ -86,10 +82,11 @@
let(:method) { :post }
let(:api_key) { 'dummy_key' }
let(:image) { fixture('images/sample.png') }
let(:requested_content_type) { 'application/octet-stream' }
let(:requested_options) do
{
headers: { 'x-api-key' => api_key, 'Content-Type' => requested_content_type, 'Transfer-Encoding' => 'chunked' },
headers: {
'x-api-key' => api_key, 'Content-Type' => 'application/octet-stream', 'Transfer-Encoding' => 'chunked'
},
}
end

Expand All @@ -108,10 +105,9 @@
let(:method) { :post }
let(:api_key) { 'dummy_key' }
let(:data) { { data: 'dummy_data' } }
let(:requested_content_type) { 'application/json' }
let(:requested_options) do
{
headers: { 'x-api-key' => api_key, 'Content-Type' => requested_content_type },
headers: { 'x-api-key' => api_key, 'Content-Type' => 'application/json' },
body: data.to_json,
}
end
Expand Down

0 comments on commit 97dd391

Please sign in to comment.