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 test for base callable service

Oliver Günther530e9174b033
diff --git a/app/services/base_services/base_callable.rb b/app/services/base_services/base_callable.rb
index f3c35bbaa7e..5253d5d95da 100644
--- a/app/services/base_services/base_callable.rb
+++ b/app/services/base_services/base_callable.rb
@@ -33,7 +33,6 @@ module BaseServices
extend ActiveModel::Callbacks
define_model_callbacks :call
- include ::Shared::ServiceContext
include ::WithReversibleState
def call(*args)
diff --git a/app/services/shared/service_state.rb b/app/services/shared/service_state.rb
index 05170cf9157..3d69bc41cce 100644
--- a/app/services/shared/service_state.rb
+++ b/app/services/shared/service_state.rb
@@ -47,14 +47,14 @@ module Shared
##
# Remember that the state was passed to the given service
def called!(service)
- _called << service
+ service_chain << service
end
# Roll back the context on all used services
def rollback!
return false if @rolled_back
- _called.reverse_each do |service|
+ service_chain.reverse_each do |service|
Rails.logger.debug { "[Service state] Rolling back execution of #{service}." }
service.rollback
end
@@ -62,8 +62,8 @@ module Shared
end
# Remembered service calls this context was used against
- def _called
- @_called ||= []
+ def service_chain
+ @service_chain ||= []
end
end
end
diff --git a/spec/services/base/base_callable_spec.rb b/spec/services/base/base_callable_spec.rb
new file mode 100644
index 00000000000..1bde67d52e2
--- /dev/null
+++ b/spec/services/base/base_callable_spec.rb
@@ -0,0 +1,111 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2020 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2017 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See docs/COPYRIGHT.rdoc for more details.
+#++
+
+require 'spec_helper'
+
+describe ::BaseServices::BaseCallable, type: :model do
+ let(:test_service) do
+ Class.new(::BaseServices::BaseCallable) do
+ def perform
+ state.test = 'foo'
+ ServiceResult.new(success: true, result: 'something')
+ end
+
+ def rollback
+ state.test = 'rolled back!'
+ end
+ end
+ end
+
+ let(:test_service2) do
+ Class.new(::BaseServices::BaseCallable) do
+ def perform
+ state.test2 = 'foo'
+ ServiceResult.new(success: true, result: 'something')
+ end
+
+ def rollback
+ state.test = 'Roll back other value'
+ state.test2 = nil
+ end
+ end
+ end
+
+ let(:instance) { test_service.new }
+ subject { instance.call }
+
+ describe 'state' do
+ let(:result_state) { subject.state }
+
+ it 'is returned from the call', :aggregate_failures do
+ expect(result_state).to be_kind_of(::Shared::ServiceState)
+ expect(result_state.test).to eq 'foo'
+ expect(subject).to be_kind_of ::ServiceResult
+
+ expect(result_state.service_chain.count).to eq(1)
+ expect(result_state.service_chain).to include(test_service)
+
+ # Roll back
+ expect(instance).to receive(:rollback).once.and_call_original
+ result_state.rollback!
+ # Second call does not go through
+ result_state.rollback!
+
+ expect(result_state.test).to eq 'rolled back!'
+ end
+
+ describe 'with state already passed into the service' do
+ let(:instance) { test_service.new.with_state(bar: 'some value') }
+
+ it 'keeps that value', :aggregate_failures do
+ expect(result_state).to be_kind_of(::Shared::ServiceState)
+ expect(result_state.test).to eq 'foo'
+ expect(result_state.bar).to eq 'some value'
+ end
+ end
+ end
+
+ describe 'rolling back two services' do
+ it 'rolls back in that order' do
+ # Execute first service
+ result = test_service.new.call
+ state = result.state
+
+ # Execute second service
+ test_service2
+ .new
+ .with_state(state)
+ .call
+
+ expect(state.service_chain.map(&:class)).to eq [test_service, test_service2]
+ state.rollback!
+ expect(state.test2).to eq nil
+ expect(state.test).to eq 'rolled back!'
+ end
+ end
+end