Skip to content

Commit acc17c7

Browse files
author
Sergio García Prado
committed
ISSUE #198
* Add tests for precedence constraints.
1 parent 765d278 commit acc17c7

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

packages/core/minos-microservice-saga/minos/saga/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
MultipleOnExecuteException,
4343
MultipleOnFailureException,
4444
MultipleOnSuccessException,
45+
OrderPrecedenceException,
4546
SagaException,
4647
SagaExecutionAlreadyExecutedException,
4748
SagaExecutionException,

packages/core/minos-microservice-saga/minos/saga/definitions/saga.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
AlreadyCommittedException,
2828
AlreadyOnSagaException,
2929
EmptySagaException,
30+
OrderPrecedenceException,
3031
SagaNotCommittedException,
3132
)
3233
from .operations import (
@@ -206,7 +207,9 @@ def add_step(self, step: Optional[Union[SagaOperation, T]], step_cls: type[T] =
206207
step.order = 1
207208

208209
if self.steps and step.order <= self.steps[-1].order:
209-
raise ValueError("TODO")
210+
raise OrderPrecedenceException(
211+
f"Unsatisfied precedence constraints. Previous: {self.steps[-1].order} Current: {step.order} "
212+
)
210213

211214
self.steps.append(step)
212215
return step

packages/core/minos-microservice-saga/minos/saga/exceptions.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,11 @@ class SagaRollbackExecutionStepException(SagaStepExecutionException):
159159

160160

161161
class AlreadyCommittedException(SagaException):
162-
"""Exception to be raised when trying to modifying an already committed saga."""
162+
"""Exception to be raised when trying to modify an already committed saga."""
163+
164+
165+
class OrderPrecedenceException(SagaException):
166+
"""Exception to be raised when ordering precedence of steps is not satisfied."""
163167

164168

165169
class ExecutorException(SagaException):

packages/core/minos-microservice-saga/tests/test_saga/test_definitions/test_saga.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
EmptySagaException,
1212
EmptySagaStepException,
1313
LocalSagaStep,
14+
OrderPrecedenceException,
1415
RemoteSagaStep,
1516
Saga,
1617
SagaException,
@@ -143,6 +144,7 @@ def test_step(self):
143144
saga.remote_step = mock
144145
with warnings.catch_warnings():
145146
warnings.simplefilter("ignore", DeprecationWarning)
147+
# noinspection PyDeprecation
146148
step = saga.step()
147149

148150
self.assertEqual(1, mock.call_count)
@@ -204,14 +206,30 @@ def test_build_execution(self):
204206

205207
def test_add_step(self):
206208
step = RemoteSagaStep(send_create_order)
207-
saga = Saga().remote_step(step).commit()
209+
saga = Saga().add_step(step).commit()
208210

209211
self.assertEqual([step], saga.steps)
210212

211-
def test_add_step_raises(self):
213+
def test_add_step_raises_duplicated(self):
212214
step = RemoteSagaStep(send_create_order, saga=Saga())
213215
with self.assertRaises(AlreadyOnSagaException):
214-
Saga().remote_step(step)
216+
Saga().add_step(step)
217+
218+
def test_add_step_raises_precedence_lower(self):
219+
step1 = RemoteSagaStep(send_create_order, order=2)
220+
step2 = RemoteSagaStep(send_create_order, order=1)
221+
saga = Saga()
222+
saga.add_step(step1)
223+
with self.assertRaises(OrderPrecedenceException):
224+
saga.add_step(step2)
225+
226+
def test_add_step_raises_precedence_equal(self):
227+
step1 = RemoteSagaStep(send_create_order, order=1)
228+
step2 = RemoteSagaStep(send_create_order, order=1)
229+
saga = Saga()
230+
saga.add_step(step1)
231+
with self.assertRaises(OrderPrecedenceException):
232+
saga.add_step(step2)
215233

216234
def test_raw(self):
217235
saga = ADD_ORDER

0 commit comments

Comments
 (0)