Add create service spec

pull/7924/head
Oliver Günther 5 years ago committed by Wieland Lindenthal
parent e6cdefdf63
commit 84b4feb1f3
  1. 1
      modules/ifc_models/app/contracts/ifc_models/base_contract.rb
  2. 22
      modules/ifc_models/app/services/ifc_models/create_service.rb
  3. 46
      modules/ifc_models/spec/services/create_service_spec.rb

@ -33,6 +33,7 @@ module IFCModels
to: :model
attribute :title
attribute :is_default
attribute :project
attribute :uploader
attribute :is_default

@ -28,18 +28,18 @@
module IFCModels
class CreateService < ::BaseServices::Create
protected
def before_perform(params)
ifc_attachment = params.delete('ifc_attachment')
attr_accessor :ifc_attachment
super(params).tap do |result|
result.success = add_attachment(result, ifc_attachment)
end
def before_perform(params)
self.ifc_attachment = params.delete(:ifc_attachment)
super(params)
end
def after_perform(call)
call.success = add_attachment(call)
if call.success?
IFCConversionJob.perform_later(call.result)
end
@ -53,16 +53,16 @@ module IFCModels
##
# Add the IFC attachment file after saving
def add_attachment(result, file)
def add_attachment(result)
return unless result.success?
model = result.result
if file && file.size.positive?
model.ifc_attachment = file
model.title = file.original_filename
if ifc_attachment && ifc_attachment.size.positive?
model.ifc_attachment = ifc_attachment
model.title = ifc_attachment.original_filename
model.save
else
result.errors.add(:ifc_attachment, t('ifc_models.could_not_save_file'))
result.errors.add(:ifc_attachment, I18n.t('ifc_models.could_not_save_file'))
false
end
end

@ -0,0 +1,46 @@
require 'spec_helper'
describe IFCModels::CreateService do
let(:user) { FactoryBot.build_stubbed :admin }
let(:ifc_attachment) { FileHelpers.mock_uploaded_file name: "model.ifc", content_type: 'application/binary', binary: true }
let(:instance) { described_class.new(user: user) }
subject { instance.call(params) }
describe '#call' do
let (:contract_result) { }
context 'when user is allowed' do
let(:params) do
{
is_default: true,
ifc_attachment: ifc_attachment
}
end
it 'returns a model from the params and queues conversion' do
expect(subject).to be_success
model = subject.result
expect(model.ifc_attachment).to be_present
expect(::IFCModels::IFCConversionJob)
.to have_been_enqueued
.with(model)
end
end
context 'when contract does not validate' do
before do
allow(instance)
.to(receive(:validate_contract))
.and_return(ServiceResult.new(success: false))
end
let(:params) { { title: 'foo' } }
it 'returns the service result from contract' do
expect(subject).not_to be_success
end
end
end
end
Loading…
Cancel
Save