forked from NEWROPE/scnnr-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_spec.rb
137 lines (114 loc) · 5.23 KB
/
client_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# frozen_string_literal: true
require 'spec_helper'
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
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) }
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)
end
describe '#config' do
subject { client.config }
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.logger).to eq logger
expect(subject.logger.level).to eq Logger.const_get(logger_level.upcase)
end
end
describe '#recognize_image' do
subject { client.recognize_image(image, options) }
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(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 }
expect(subject).to eq expected_recognition
end
end
describe '#recognize_url' do
subject { client.recognize_url(url, options) }
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(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 }
expect(subject).to eq expected_recognition
end
end
describe '#fetch' do
subject { client.fetch(recognition_id, options) }
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(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 }
expect(subject).to eq expected_recognition
end
end
describe '#coordinate' do
subject { client.coordinate(category, labels, taste_with_unknown, options) }
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 }
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::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'
context 'when `target` option is passed' do
let(:options) { { 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
end
end
end