Fix tests after BuildProtocolFromClientService refactor

Closes SCI-3544
This commit is contained in:
Jure Grabnar 2019-06-10 13:08:30 +02:00 committed by Urban Rotnik
parent 1e8627c450
commit 640f6c1211
6 changed files with 26 additions and 30 deletions

View file

@ -1,6 +1,6 @@
# frozen_string_literal: true
module Protocols
module ProtocolImporters
class BuildProtocolFromClientService
extend Service
@ -19,7 +19,7 @@ module Protocols
# TODO: check for errors
api_response = api_client.single_protocol(id: @id)
normalized_hash = normalizer.load_protocol(api_response)
normalized_hash = normalizer.load_protocol(api_response.parsed_response)
pio = ProtocolImporters::ProtocolIntermediateObject.new(normalized_json: normalized_hash,
user: @user,

View file

@ -2,11 +2,11 @@
module ProtocolImporters
class ProtocolNormalizer
def load_all_protocols(api_response)
def normalize_all_protocols(client_data)
raise NotImplementedError
end
def load_protocol(api_response)
def normalize_protocol(client_data)
raise NotImplementedError
end
end

View file

@ -4,17 +4,12 @@ module ProtocolImporters
module ProtocolsIO
module V3
class ProtocolNormalizer < ProtocolImporters::ProtocolNormalizer
def load_protocol(api_response)
normalize(api_response)
end
private
def normalize(response)
protocol_hash = response.parsed_response.with_indifferent_access[:protocol]
def normalize_protocol(client_data)
# client_data is HttpParty ApiReponse object
protocol_hash = client_data.parsed_response.with_indifferent_access[:protocol]
normalized_data = {
uri: response.request.last_uri.to_s,
uri: client_data.request.last_uri.to_s,
source: Constants::PROTOCOLS_IO_V3_API[:source_id],
doi: protocol_hash[:doi],
published_on: protocol_hash[:published_on],

View file

@ -2,11 +2,11 @@
require 'rails_helper'
describe Protocols::BuildProtocolFromClientService do
describe ProtocolImporters::BuildProtocolFromClientService do
let(:user) { create :user }
let(:team) { create :team }
let(:service_call) do
Protocols::BuildProtocolFromClientService
ProtocolImporters::BuildProtocolFromClientService
.call(protocol_client_id: 'id', protocol_source: 'protocolsio/v3', user_id: user.id, team_id: team.id)
end
let(:normalized_response) do
@ -25,7 +25,7 @@ describe Protocols::BuildProtocolFromClientService do
context 'when have valid arguments' do
before do
allow_any_instance_of(ProtocolImporters::ProtocolsIO::V3::ProtocolNormalizer)
.to(receive(:load_protocol).and_return(normalized_response))
.to(receive(:normalize_protocol).and_return(normalized_response))
# Do not generate and request real images
allow(ProtocolImporters::AttachmentsBuilder).to(receive(:generate).and_return([]))
end

View file

@ -3,11 +3,11 @@
require 'rails_helper'
describe ProtocolImporters::ProtocolNormalizer do
describe '.load_all_protocols' do
it { expect { subject.load_all_protocols({}) }.to raise_error(NotImplementedError) }
describe '.normalize_all_protocols' do
it { expect { subject.normalize_all_protocols({}) }.to raise_error(NotImplementedError) }
end
describe '.load_protocol' do
it { expect { subject.load_protocol({}) }.to raise_error(NotImplementedError) }
describe '.normalize_protocols' do
it { expect { subject.normalize_protocol({}) }.to raise_error(NotImplementedError) }
end
end

View file

@ -3,6 +3,8 @@
require 'rails_helper'
describe ProtocolImporters::ProtocolsIO::V3::ProtocolNormalizer do
let(:client_data) { double('api_response') }
let(:response) do
JSON.parse(file_fixture('protocol_importers/protocols_io/v3/single_protocol.json').read)
.to_h.with_indifferent_access
@ -19,28 +21,27 @@ describe ProtocolImporters::ProtocolsIO::V3::ProtocolNormalizer do
.to_h.with_indifferent_access
end
describe '#load_protocol' do
describe '#normalize_protocol' do
before do
allow_any_instance_of(ProtocolImporters::ProtocolsIO::V3::ApiClient)
.to(receive_message_chain(:single_protocol, :request, :last_uri, :to_s)
.and_return('https://www.protocols.io/api/v3/protocols/9451'))
allow(client_data).to(receive_message_chain(:request, :last_uri, :to_s)
.and_return('https://www.protocols.io/api/v3/protocols/9451'))
end
context 'when have all data' do
it 'should normalize data correctly' do
allow_any_instance_of(ProtocolImporters::ProtocolsIO::V3::ApiClient)
.to receive_message_chain(:single_protocol, :parsed_response).and_return(response)
allow(client_data).to receive_message_chain(:parsed_response)
.and_return(response)
expect(subject.load_protocol(response).deep_stringify_keys).to be == normalized_result
expect(subject.normalize_protocol(client_data).deep_stringify_keys).to be == normalized_result
end
end
context 'when do not have name' do
it 'sets nil for name' do
allow_any_instance_of(ProtocolImporters::ProtocolsIO::V3::ApiClient)
.to receive_message_chain(:single_protocol, :parsed_response).and_return(response_without_title)
allow(client_data).to receive_message_chain(:parsed_response)
.and_return(response_without_title)
expect(subject.load_protocol(response)[:protocol][:name]).to be_nil
expect(subject.normalize_protocol(client_data)[:protocol][:name]).to be_nil
end
end
end