Skip to content

Commit d61d5ec

Browse files
author
Sergio García Prado
committed
ISSUE #198
* Add tests for `LocalSagaStepDecoratorMeta`.
1 parent e83584d commit d61d5ec

File tree

2 files changed

+51
-7
lines changed
  • packages/core/minos-microservice-saga

2 files changed

+51
-7
lines changed

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

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
from typing import (
1212
Any,
1313
Optional,
14+
Protocol,
1415
Union,
16+
runtime_checkable,
1517
)
1618

1719
from cached_property import (
@@ -45,7 +47,8 @@
4547
)
4648

4749

48-
class LocalSagaStepDecoratorWrapper(SagaStepDecoratorWrapper):
50+
@runtime_checkable
51+
class LocalSagaStepDecoratorWrapper(SagaStepDecoratorWrapper, Protocol):
4952
"""TODO"""
5053

5154
meta: LocalSagaStepDecoratorMeta
@@ -112,10 +115,12 @@ def __call__(self, func: LocalCallback) -> LocalSagaStepDecoratorWrapper:
112115
# noinspection PyTypeChecker
113116
return func
114117

115-
def on_execute(self, callback: LocalCallback, parameters: Optional[SagaContext] = None, **kwargs) -> LocalSagaStep:
118+
def on_execute(
119+
self, operation: Union[SagaOperation, LocalCallback], parameters: Optional[SagaContext] = None, **kwargs
120+
) -> LocalSagaStep:
116121
"""On execute method.
117122
118-
:param callback: The callback function to be called.
123+
:param operation: The callback function to be called.
119124
:param parameters: A mapping of named parameters to be passed to the callback.
120125
:param kwargs: A set of named arguments to be passed to the callback. ``parameters`` has order if it is not
121126
``None``.
@@ -124,14 +129,19 @@ def on_execute(self, callback: LocalCallback, parameters: Optional[SagaContext]
124129
if self.on_execute_operation is not None:
125130
raise MultipleOnExecuteException()
126131

127-
self.on_execute_operation = SagaOperation(callback, parameters, **kwargs)
132+
if not isinstance(operation, SagaOperation):
133+
operation = SagaOperation(operation, parameters, **kwargs)
134+
135+
self.on_execute_operation = operation
128136

129137
return self
130138

131-
def on_failure(self, callback: LocalCallback, parameters: Optional[SagaContext] = None, **kwargs) -> LocalSagaStep:
139+
def on_failure(
140+
self, operation: Union[SagaOperation, LocalCallback], parameters: Optional[SagaContext] = None, **kwargs
141+
) -> LocalSagaStep:
132142
"""On failure method.
133143
134-
:param callback: The callback function to be called.
144+
:param operation: The callback function to be called.
135145
:param parameters: A mapping of named parameters to be passed to the callback.
136146
:param kwargs: A set of named arguments to be passed to the callback. ``parameters`` has order if it is not
137147
``None``.
@@ -140,7 +150,10 @@ def on_failure(self, callback: LocalCallback, parameters: Optional[SagaContext]
140150
if self.on_failure_operation is not None:
141151
raise MultipleOnFailureException()
142152

143-
self.on_failure_operation = SagaOperation(callback, parameters, **kwargs)
153+
if not isinstance(operation, SagaOperation):
154+
operation = SagaOperation(operation, parameters, **kwargs)
155+
156+
self.on_failure_operation = operation
144157

145158
return self
146159

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
from minos.saga import (
44
EmptySagaStepException,
55
LocalSagaStep,
6+
LocalSagaStepDecoratorMeta,
7+
LocalSagaStepDecoratorWrapper,
68
MultipleOnExecuteException,
79
MultipleOnFailureException,
10+
SagaContext,
811
SagaOperation,
912
SagaStep,
1013
UndefinedOnExecuteException,
@@ -15,6 +18,34 @@
1518
)
1619

1720

21+
class TestLocalSagaStepDecoratorMeta(unittest.TestCase):
22+
def test_constructor(self):
23+
# noinspection PyUnusedLocal
24+
@LocalSagaStep()
25+
def _fn(context: SagaContext) -> SagaContext:
26+
"""For testing purposes"""
27+
28+
self.assertIsInstance(_fn, LocalSagaStepDecoratorWrapper)
29+
meta = _fn.meta
30+
self.assertIsInstance(meta, LocalSagaStepDecoratorMeta)
31+
self.assertEqual(LocalSagaStep(_fn), meta.definition)
32+
33+
def test_on_failure(self):
34+
# noinspection PyUnusedLocal
35+
@LocalSagaStep()
36+
def _fn(context: SagaContext) -> SagaContext:
37+
"""For testing purposes"""
38+
39+
@_fn.on_failure()
40+
def _fn2(context: SagaContext) -> SagaContext:
41+
"""For testing purposes"""
42+
43+
self.assertIsInstance(_fn, LocalSagaStepDecoratorWrapper)
44+
meta = _fn.meta
45+
self.assertIsInstance(meta, LocalSagaStepDecoratorMeta)
46+
self.assertEqual(LocalSagaStep(_fn, on_failure=_fn2), meta.definition)
47+
48+
1849
class TestLocalSagaStep(unittest.TestCase):
1950
def test_on_execute_constructor(self):
2051
step = LocalSagaStep(on_execute=create_payment)

0 commit comments

Comments
 (0)