Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Add update_service_spec.rb for IFC models

Wieland Lindenthal0e74fcc4a5aa
diff --git a/modules/ifc_models/app/services/ifc_models/update_service.rb b/modules/ifc_models/app/services/ifc_models/update_service.rb
index b553a15976c..25d8dddf28f 100644
--- a/modules/ifc_models/app/services/ifc_models/update_service.rb
+++ b/modules/ifc_models/app/services/ifc_models/update_service.rb
@@ -28,18 +28,18 @@
module IFCModels
class UpdateService < ::BaseServices::Update
-
protected
- def before_perform(params)
- ifc_attachment = params.delete('ifc_attachment')
+ attr_accessor :ifc_attachment
- super(params).tap do |result|
- result.success = replace_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 = replace_attachment(call)
+
if @ifc_attachment_replaced && call.success?
IFCConversionJob.perform_later(call.result)
end
@@ -49,14 +49,13 @@ module IFCModels
##
# Replace the IFC attachment file after saving
- def replace_attachment(result, file)
+ def replace_attachment(result)
return unless result.success?
model = result.result
-
# Uploading an IFC file is optional
- if file && file.size.positive?
- model.ifc_attachment = file
+ if ifc_attachment && ifc_attachment.size.positive?
+ model.ifc_attachment = ifc_attachment
@ifc_attachment_replaced = true
end
diff --git a/modules/ifc_models/spec/services/create_service_spec.rb b/modules/ifc_models/spec/services/create_service_spec.rb
index f2924313f4d..9d5dd8fe323 100644
--- a/modules/ifc_models/spec/services/create_service_spec.rb
+++ b/modules/ifc_models/spec/services/create_service_spec.rb
@@ -23,6 +23,7 @@ describe IFCModels::CreateService do
model = subject.result
expect(model.ifc_attachment).to be_present
+ expect(model.is_default).to be_truthy
expect(::IFCModels::IFCConversionJob)
.to have_been_enqueued
.with(model)
diff --git a/modules/ifc_models/spec/services/update_service_spec.rb b/modules/ifc_models/spec/services/update_service_spec.rb
new file mode 100644
index 00000000000..40aa2bcb8c2
--- /dev/null
+++ b/modules/ifc_models/spec/services/update_service_spec.rb
@@ -0,0 +1,82 @@
+require 'spec_helper'
+
+describe IFCModels::UpdateService do
+ let(:user) { FactoryBot.build_stubbed :admin }
+ let(:ifc_attachment_version_1) { FileHelpers.mock_uploaded_file(name: "model_1.ifc", content_type: 'application/binary', binary: true) }
+ let(:ifc_attachment_version_2) { FileHelpers.mock_uploaded_file(name: "model_2.ifc", content_type: 'application/binary', binary: true) }
+
+ let(:ifc_model) do
+ ::IFCModels::IFCModel.create(ifc_attachment: ifc_attachment_version_1,
+ title: "Architecture",
+ is_default: false,
+ uploader: user)
+ end
+
+ let(:instance) { described_class.new(user: user, model: ifc_model) }
+
+ 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_version_2
+ }.with_indifferent_access
+ end
+
+ it 'returns a model from the params and queues conversion' do
+ expect(subject).to be_success
+
+ model = subject.result
+ expect(model.ifc_attachment.filename).to eq('model_2.ifc')
+ expect(model.is_default).to be_truthy
+ expect(::IFCModels::IFCConversionJob)
+ .to have_been_enqueued
+ .with(model)
+ .on_queue('ifc_conversion')
+ end
+
+ context 'only updating title and is_default' do
+ let(:params) do
+ {
+ is_default: true,
+ title: "New title"
+ }
+ end
+
+ it 'updates the title and is_default even without updating ifc_attachments' do
+ expect(subject).to be_success
+ model = subject.result
+
+ expect(model.title).to eq('New title')
+ expect(::IFCModels::IFCConversionJob)
+ .to_not have_been_enqueued
+ .with(model)
+ end
+ 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
+ model = ifc_model.reload
+
+ expect(model.title).to_not eq('foo')
+ expect(::IFCModels::IFCConversionJob)
+ .to_not have_been_enqueued
+ .with(model)
+ end
+ end
+ end
+end
\ No newline at end of file