From 640f6c1211a8a9219943582327a7bf9baf8d415f Mon Sep 17 00:00:00 2001 From: Jure Grabnar Date: Mon, 10 Jun 2019 13:08:30 +0200 Subject: [PATCH] Fix tests after BuildProtocolFromClientService refactor Closes SCI-3544 --- .../build_protocol_from_client_service.rb | 4 ++-- .../protocol_importers/protocol_normalizer.rb | 4 ++-- .../protocols_io/v3/protocol_normalizer.rb | 13 ++++-------- ...build_protocol_from_client_service_spec.rb | 6 +++--- .../protocol_normalizer_spec.rb | 8 +++---- .../v3/protocol_normalizer_spec.rb | 21 ++++++++++--------- 6 files changed, 26 insertions(+), 30 deletions(-) diff --git a/app/services/protocol_importers/build_protocol_from_client_service.rb b/app/services/protocol_importers/build_protocol_from_client_service.rb index 9766b0e32..3fbc90ab8 100644 --- a/app/services/protocol_importers/build_protocol_from_client_service.rb +++ b/app/services/protocol_importers/build_protocol_from_client_service.rb @@ -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, diff --git a/app/utilities/protocol_importers/protocol_normalizer.rb b/app/utilities/protocol_importers/protocol_normalizer.rb index 696215ef4..bd98725b8 100644 --- a/app/utilities/protocol_importers/protocol_normalizer.rb +++ b/app/utilities/protocol_importers/protocol_normalizer.rb @@ -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 diff --git a/app/utilities/protocol_importers/protocols_io/v3/protocol_normalizer.rb b/app/utilities/protocol_importers/protocols_io/v3/protocol_normalizer.rb index 34325188b..9a24d71fa 100644 --- a/app/utilities/protocol_importers/protocols_io/v3/protocol_normalizer.rb +++ b/app/utilities/protocol_importers/protocols_io/v3/protocol_normalizer.rb @@ -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], diff --git a/spec/services/protocol_importers/build_protocol_from_client_service_spec.rb b/spec/services/protocol_importers/build_protocol_from_client_service_spec.rb index cb53431eb..9f0dfd09d 100644 --- a/spec/services/protocol_importers/build_protocol_from_client_service_spec.rb +++ b/spec/services/protocol_importers/build_protocol_from_client_service_spec.rb @@ -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 diff --git a/spec/utilities/protocol_importers/protocol_normalizer_spec.rb b/spec/utilities/protocol_importers/protocol_normalizer_spec.rb index a22ad921d..926103b67 100644 --- a/spec/utilities/protocol_importers/protocol_normalizer_spec.rb +++ b/spec/utilities/protocol_importers/protocol_normalizer_spec.rb @@ -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 diff --git a/spec/utilities/protocol_importers/protocols_io/v3/protocol_normalizer_spec.rb b/spec/utilities/protocol_importers/protocols_io/v3/protocol_normalizer_spec.rb index 323750761..6ba04df62 100644 --- a/spec/utilities/protocol_importers/protocols_io/v3/protocol_normalizer_spec.rb +++ b/spec/utilities/protocol_importers/protocols_io/v3/protocol_normalizer_spec.rb @@ -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