Skip to content

Commit 7f67728

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

File tree

3 files changed

+106
-13
lines changed

3 files changed

+106
-13
lines changed

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

Lines changed: 40 additions & 13 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 (
@@ -48,7 +50,8 @@
4850
)
4951

5052

51-
class RemoteSagaStepDecoratorWrapper(SagaStepDecoratorWrapper):
53+
@runtime_checkable
54+
class RemoteSagaStepDecoratorWrapper(SagaStepDecoratorWrapper, Protocol):
5255
"""TODO"""
5356

5457
meta: RemoteSagaStepDecoratorMeta
@@ -150,11 +153,14 @@ def __call__(self, func: RequestCallBack) -> RemoteSagaStepDecoratorWrapper:
150153
return func
151154

152155
def on_execute(
153-
self, callback: RequestCallBack, parameters: Optional[SagaContext] = None, **kwargs
156+
self,
157+
operation: Union[SagaOperation[RequestCallBack], RequestCallBack],
158+
parameters: Optional[SagaContext] = None,
159+
**kwargs,
154160
) -> RemoteSagaStep:
155161
"""On execute method.
156162
157-
:param callback: The callback function to be called.
163+
:param operation: The callback function to be called.
158164
:param parameters: A mapping of named parameters to be passed to the callback.
159165
:param kwargs: A set of named arguments to be passed to the callback. ``parameters`` has order if it is not
160166
``None``.
@@ -163,16 +169,22 @@ def on_execute(
163169
if self.on_execute_operation is not None:
164170
raise MultipleOnExecuteException()
165171

166-
self.on_execute_operation = SagaOperation(callback, parameters, **kwargs)
172+
if not isinstance(operation, SagaOperation):
173+
operation = SagaOperation(operation, parameters, **kwargs)
174+
175+
self.on_execute_operation = operation
167176

168177
return self
169178

170179
def on_failure(
171-
self, callback: RequestCallBack, parameters: Optional[SagaContext] = None, **kwargs
180+
self,
181+
operation: Union[SagaOperation[RequestCallBack], RequestCallBack],
182+
parameters: Optional[SagaContext] = None,
183+
**kwargs,
172184
) -> RemoteSagaStep:
173185
"""On failure method.
174186
175-
:param callback: The callback function to be called.
187+
:param operation: The callback function to be called.
176188
:param parameters: A mapping of named parameters to be passed to the callback.
177189
:param kwargs: A set of named arguments to be passed to the callback. ``parameters`` has order if it is not
178190
``None``.
@@ -181,16 +193,22 @@ def on_failure(
181193
if self.on_failure_operation is not None:
182194
raise MultipleOnFailureException()
183195

184-
self.on_failure_operation = SagaOperation(callback, parameters, **kwargs)
196+
if not isinstance(operation, SagaOperation):
197+
operation = SagaOperation(operation, parameters, **kwargs)
198+
199+
self.on_failure_operation = operation
185200

186201
return self
187202

188203
def on_success(
189-
self, callback: ResponseCallBack, parameters: Optional[SagaContext] = None, **kwargs
204+
self,
205+
operation: Union[SagaOperation[ResponseCallBack], ResponseCallBack],
206+
parameters: Optional[SagaContext] = None,
207+
**kwargs,
190208
) -> RemoteSagaStep:
191209
"""On success method.
192210
193-
:param callback: The callback function to be called.
211+
:param operation: The callback function to be called.
194212
:param parameters: A mapping of named parameters to be passed to the callback.
195213
:param kwargs: A set of named arguments to be passed to the callback. ``parameters`` has order if it is not
196214
``None``.
@@ -199,16 +217,22 @@ def on_success(
199217
if self.on_success_operation is not None:
200218
raise MultipleOnSuccessException()
201219

202-
self.on_success_operation = SagaOperation(callback, parameters, **kwargs)
220+
if not isinstance(operation, SagaOperation):
221+
operation = SagaOperation(operation, parameters, **kwargs)
222+
223+
self.on_success_operation = operation
203224

204225
return self
205226

206227
def on_error(
207-
self, callback: ResponseCallBack, parameters: Optional[SagaContext] = None, **kwargs
228+
self,
229+
operation: Union[SagaOperation[ResponseCallBack], ResponseCallBack],
230+
parameters: Optional[SagaContext] = None,
231+
**kwargs,
208232
) -> RemoteSagaStep:
209233
"""On error method.
210234
211-
:param callback: The callback function to be called.
235+
:param operation: The callback function to be called.
212236
:param parameters: A mapping of named parameters to be passed to the callback.
213237
:param kwargs: A set of named arguments to be passed to the callback. ``parameters`` has order if it is not
214238
``None``.
@@ -217,7 +241,10 @@ def on_error(
217241
if self.on_error_operation is not None:
218242
raise MultipleOnErrorException()
219243

220-
self.on_error_operation = SagaOperation(callback, parameters, **kwargs)
244+
if not isinstance(operation, SagaOperation):
245+
operation = SagaOperation(operation, parameters, **kwargs)
246+
247+
self.on_error_operation = operation
221248

222249
return self
223250

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def test_on_failure(self):
3636
def _fn(context: SagaContext) -> SagaContext:
3737
"""For testing purposes"""
3838

39+
# noinspection PyUnusedLocal
3940
@_fn.on_failure()
4041
def _fn2(context: SagaContext) -> SagaContext:
4142
"""For testing purposes"""

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
MultipleOnFailureException,
88
MultipleOnSuccessException,
99
RemoteSagaStep,
10+
RemoteSagaStepDecoratorMeta,
11+
RemoteSagaStepDecoratorWrapper,
12+
SagaContext,
1013
SagaOperation,
14+
SagaRequest,
1115
SagaStep,
1216
UndefinedOnExecuteException,
1317
)
@@ -19,6 +23,67 @@
1923
)
2024

2125

26+
class TestRemoteSagaStepDecoratorMeta(unittest.TestCase):
27+
def test_constructor(self):
28+
# noinspection PyUnusedLocal
29+
@RemoteSagaStep()
30+
def _fn(context: SagaContext) -> SagaContext:
31+
"""For testing purposes"""
32+
33+
self.assertIsInstance(_fn, RemoteSagaStepDecoratorWrapper)
34+
meta = _fn.meta
35+
self.assertIsInstance(meta, RemoteSagaStepDecoratorMeta)
36+
self.assertEqual(RemoteSagaStep(_fn), meta.definition)
37+
38+
def test_on_failure(self):
39+
# noinspection PyUnusedLocal
40+
@RemoteSagaStep()
41+
def _fn(context: SagaContext) -> SagaContext:
42+
"""For testing purposes"""
43+
44+
# noinspection PyUnusedLocal
45+
@_fn.on_failure()
46+
def _fn2(context: SagaContext) -> SagaRequest:
47+
"""For testing purposes"""
48+
49+
self.assertIsInstance(_fn, RemoteSagaStepDecoratorWrapper)
50+
meta = _fn.meta
51+
self.assertIsInstance(meta, RemoteSagaStepDecoratorMeta)
52+
self.assertEqual(RemoteSagaStep(_fn, on_failure=_fn2), meta.definition)
53+
54+
def test_on_success(self):
55+
# noinspection PyUnusedLocal
56+
@RemoteSagaStep()
57+
def _fn(context: SagaContext) -> SagaContext:
58+
"""For testing purposes"""
59+
60+
# noinspection PyUnusedLocal
61+
@_fn.on_success()
62+
def _fn2(context: SagaContext) -> SagaContext:
63+
"""For testing purposes"""
64+
65+
self.assertIsInstance(_fn, RemoteSagaStepDecoratorWrapper)
66+
meta = _fn.meta
67+
self.assertIsInstance(meta, RemoteSagaStepDecoratorMeta)
68+
self.assertEqual(RemoteSagaStep(_fn, on_success=_fn2), meta.definition)
69+
70+
def test_on_error(self):
71+
# noinspection PyUnusedLocal
72+
@RemoteSagaStep()
73+
def _fn(context: SagaContext) -> SagaContext:
74+
"""For testing purposes"""
75+
76+
# noinspection PyUnusedLocal
77+
@_fn.on_error()
78+
def _fn2(context: SagaContext) -> SagaContext:
79+
"""For testing purposes"""
80+
81+
self.assertIsInstance(_fn, RemoteSagaStepDecoratorWrapper)
82+
meta = _fn.meta
83+
self.assertIsInstance(meta, RemoteSagaStepDecoratorMeta)
84+
self.assertEqual(RemoteSagaStep(_fn, on_error=_fn2), meta.definition)
85+
86+
2287
class TestRemoteSagaStep(unittest.TestCase):
2388
def test_on_execute_constructor(self):
2489
step = RemoteSagaStep(on_execute=send_create_ticket)

0 commit comments

Comments
 (0)