2019-05-21 23:16:30 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe ProtocolImporters::ProtocolIntermediateObject do
|
|
|
|
subject(:pio) { described_class.new(normalized_json: normalized_result, user: user, team: team) }
|
|
|
|
let(:invalid_pio) { described_class.new(normalized_json: normalized_result, user: nil, team: team) }
|
2019-06-27 19:10:28 +08:00
|
|
|
let(:pio_without_assets) do
|
|
|
|
described_class.new(normalized_json: normalized_result, user: user, team: team, build_with_assets: false)
|
|
|
|
end
|
2019-05-21 23:16:30 +08:00
|
|
|
let(:user) { create :user }
|
|
|
|
let(:team) { create :team }
|
|
|
|
let(:normalized_result) do
|
2019-05-31 02:26:22 +08:00
|
|
|
JSON.parse(file_fixture('protocol_importers/normalized_single_protocol.json').read)
|
2019-05-30 14:35:16 +08:00
|
|
|
.to_h.with_indifferent_access
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_request(:get, 'https://pbs.twimg.com/media/Cwu3zrZWQAA7axs.jpg').to_return(status: 200, body: '', headers: {})
|
|
|
|
stub_request(:get, 'http://something.com/wp-content/uploads/2014/11/14506718045_5b3e71dacd_o.jpg')
|
|
|
|
.to_return(status: 200, body: '', headers: {})
|
2019-05-21 23:16:30 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '.build' do
|
|
|
|
it { expect(subject.build).to be_instance_of(Protocol) }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.import' do
|
|
|
|
context 'when have valid object' do
|
|
|
|
it { expect { pio.import }.to change { Protocol.all.count }.by(1) }
|
2019-05-30 14:35:16 +08:00
|
|
|
it { expect { pio.import }.to change { Step.all.count }.by(2) }
|
2019-07-03 22:11:51 +08:00
|
|
|
it { expect { pio.import }.to change { Asset.all.count }.by(3) }
|
2019-05-21 23:16:30 +08:00
|
|
|
it { expect(pio.import).to be_valid }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when have invalid object' do
|
|
|
|
it { expect(invalid_pio.import).to be_invalid }
|
|
|
|
it { expect { invalid_pio.import }.not_to(change { Protocol.all.count }) }
|
|
|
|
end
|
2019-06-27 19:10:28 +08:00
|
|
|
|
|
|
|
context 'when build wihout assets' do
|
|
|
|
it { expect { pio_without_assets.import }.to change { Protocol.all.count }.by(1) }
|
|
|
|
it { expect { invalid_pio.import }.not_to(change { Asset.all.count }) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.steps_assets' do
|
|
|
|
context 'when have default pio' do
|
|
|
|
it 'retuns empty hash for steps_assets' do
|
|
|
|
pio.build
|
|
|
|
expect(pio.steps_assets).to be == {}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when have pio built without assets' do
|
|
|
|
it 'returns hash with two assets on steps by position' do
|
|
|
|
pio_without_assets.build
|
|
|
|
expect(pio_without_assets.steps_assets.size).to be == 2
|
|
|
|
end
|
|
|
|
end
|
2019-05-21 23:16:30 +08:00
|
|
|
end
|
|
|
|
end
|