Skip to content

Commit e83584d

Browse files
author
Sergio García Prado
committed
ISSUE #198
* Rename `OnSagaStepDecorator` as `SagaOperationDecorator`.
1 parent 5bcd094 commit e83584d

File tree

9 files changed

+87
-36
lines changed

9 files changed

+87
-36
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020
LocalSagaStep,
2121
LocalSagaStepDecoratorMeta,
2222
LocalSagaStepDecoratorWrapper,
23-
OnSagaStepDecorator,
2423
RemoteSagaStep,
2524
RemoteSagaStepDecoratorMeta,
2625
RemoteSagaStepDecoratorWrapper,
2726
Saga,
2827
SagaDecoratorMeta,
2928
SagaDecoratorWrapper,
3029
SagaOperation,
30+
SagaOperationDecorator,
3131
SagaStep,
3232
SagaStepDecoratorMeta,
3333
SagaStepDecoratorWrapper,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from .operations import (
22
SagaOperation,
3+
SagaOperationDecorator,
34
)
45
from .saga import (
56
Saga,
@@ -19,7 +20,6 @@
1920
LocalSagaStep,
2021
LocalSagaStepDecoratorMeta,
2122
LocalSagaStepDecoratorWrapper,
22-
OnSagaStepDecorator,
2323
RemoteSagaStep,
2424
RemoteSagaStepDecoratorMeta,
2525
RemoteSagaStepDecoratorWrapper,

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@
77
Iterable,
88
)
99
from typing import (
10+
TYPE_CHECKING,
1011
Any,
1112
Generic,
1213
Optional,
1314
TypeVar,
1415
Union,
1516
)
1617

18+
if TYPE_CHECKING:
19+
from .steps import SagaStepDecoratorMeta
20+
1721
from minos.common import (
1822
classname,
1923
import_module,
@@ -26,6 +30,27 @@
2630
T = TypeVar("T", bound=Callable)
2731

2832

33+
class SagaOperationDecorator(Generic[T]):
34+
""" "TODO"""
35+
36+
def __init__(self, attr_name: str = None, step_meta: SagaStepDecoratorMeta = None, *args, **kwargs):
37+
if attr_name is None:
38+
raise ValueError(f"The 'attr_name' must not be {None!r}.")
39+
if step_meta is None:
40+
raise ValueError(f"The 'step_meta' must not be {None!r}.")
41+
42+
self._step_meta = step_meta
43+
self._attr_name = attr_name
44+
45+
self._args = args
46+
self._kwargs = kwargs
47+
48+
def __call__(self, func: T) -> T:
49+
operation = SagaOperation(func, *self._args, **self._kwargs)
50+
setattr(self._step_meta, self._attr_name, operation)
51+
return func
52+
53+
2954
class SagaOperation(Generic[T]):
3055
"""Saga Step Operation class."""
3156

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from .abc import (
2-
OnSagaStepDecorator,
32
SagaStep,
43
SagaStepDecoratorMeta,
54
SagaStepDecoratorWrapper,

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

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@
1414
from typing import (
1515
TYPE_CHECKING,
1616
Any,
17-
Generic,
1817
Optional,
1918
Protocol,
20-
TypeVar,
2119
Union,
2220
runtime_checkable,
2321
)
@@ -68,23 +66,6 @@ def definition(self) -> SagaStep:
6866
"""TODO"""
6967

7068

71-
FN = TypeVar("FN", bound=Callable)
72-
73-
74-
class OnSagaStepDecorator(Generic[FN]):
75-
""" "TODO"""
76-
77-
def __init__(self, attr_name: [str] = None, step_meta: Optional[SagaStepDecoratorMeta] = None):
78-
if attr_name is None or step_meta is None:
79-
raise ValueError("TODO")
80-
self.step_meta = step_meta
81-
self.attr_name = attr_name
82-
83-
def __call__(self, func: FN) -> FN:
84-
setattr(self.step_meta, self.attr_name, func)
85-
return func
86-
87-
8869
class SagaStep(ABC):
8970
"""Saga step class."""
9071

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@
3333
)
3434
from ..operations import (
3535
SagaOperation,
36+
SagaOperationDecorator,
3637
)
3738
from ..types import (
3839
LocalCallback,
3940
)
4041
from .abc import (
41-
OnSagaStepDecorator,
4242
SagaStep,
4343
SagaStepDecoratorMeta,
4444
SagaStepDecoratorWrapper,
@@ -49,7 +49,7 @@ class LocalSagaStepDecoratorWrapper(SagaStepDecoratorWrapper):
4949
"""TODO"""
5050

5151
meta: LocalSagaStepDecoratorMeta
52-
on_failure: type[OnSagaStepDecorator[LocalCallback]]
52+
on_failure: type[SagaOperationDecorator[LocalCallback]]
5353
__call__: LocalCallback
5454

5555

@@ -72,10 +72,10 @@ def definition(self):
7272
return self._definition
7373

7474
@cached_property
75-
def on_failure(self) -> OnSagaStepDecorator:
75+
def on_failure(self) -> SagaOperationDecorator:
7676
"""TODO"""
7777
# noinspection PyTypeChecker
78-
return partial(OnSagaStepDecorator, step_meta=self, attr_name="_on_failure")
78+
return partial(SagaOperationDecorator, step_meta=self, attr_name="_on_failure")
7979

8080

8181
class LocalSagaStep(SagaStep):

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@
3535
)
3636
from ..operations import (
3737
SagaOperation,
38+
SagaOperationDecorator,
3839
)
3940
from ..types import (
4041
RequestCallBack,
4142
ResponseCallBack,
4243
)
4344
from .abc import (
44-
OnSagaStepDecorator,
4545
SagaStep,
4646
SagaStepDecoratorMeta,
4747
SagaStepDecoratorWrapper,
@@ -52,9 +52,9 @@ class RemoteSagaStepDecoratorWrapper(SagaStepDecoratorWrapper):
5252
"""TODO"""
5353

5454
meta: RemoteSagaStepDecoratorMeta
55-
on_success: type[OnSagaStepDecorator[ResponseCallBack]]
56-
on_error: type[OnSagaStepDecorator[ResponseCallBack]]
57-
on_failure: type[OnSagaStepDecorator[RequestCallBack]]
55+
on_success: type[SagaOperationDecorator[ResponseCallBack]]
56+
on_error: type[SagaOperationDecorator[ResponseCallBack]]
57+
on_failure: type[SagaOperationDecorator[RequestCallBack]]
5858
__call__: RequestCallBack
5959

6060

@@ -85,22 +85,22 @@ def definition(self):
8585
return self._definition
8686

8787
@cached_property
88-
def on_success(self) -> OnSagaStepDecorator:
88+
def on_success(self) -> SagaOperationDecorator:
8989
"""TODO"""
9090
# noinspection PyTypeChecker
91-
return partial(OnSagaStepDecorator, step_meta=self, attr_name="_on_success")
91+
return partial(SagaOperationDecorator, step_meta=self, attr_name="_on_success")
9292

9393
@cached_property
94-
def on_error(self) -> OnSagaStepDecorator:
94+
def on_error(self) -> SagaOperationDecorator:
9595
"""TODO"""
9696
# noinspection PyTypeChecker
97-
return partial(OnSagaStepDecorator, step_meta=self, attr_name="_on_error")
97+
return partial(SagaOperationDecorator, step_meta=self, attr_name="_on_error")
9898

9999
@cached_property
100-
def on_failure(self) -> OnSagaStepDecorator:
100+
def on_failure(self) -> SagaOperationDecorator:
101101
"""TODO"""
102102
# noinspection PyTypeChecker
103-
return partial(OnSagaStepDecorator, step_meta=self, attr_name="_on_failure")
103+
return partial(SagaOperationDecorator, step_meta=self, attr_name="_on_failure")
104104

105105

106106
class RemoteSagaStep(SagaStep):

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,41 @@
33
from minos.saga import (
44
SagaContext,
55
SagaOperation,
6+
SagaOperationDecorator,
67
)
78
from tests.utils import (
89
send_create_ticket,
910
)
1011

1112

13+
class TestSagaOperationDecorator(unittest.TestCase):
14+
def test_constructor(self):
15+
class _Meta:
16+
"""For testing purposes."""
17+
18+
meta = _Meta()
19+
meta.foo = None
20+
21+
# noinspection PyTypeChecker
22+
decorator = SagaOperationDecorator("foo", meta, foobar="foobar")
23+
24+
# noinspection PyTypeChecker
25+
decorator("bar")
26+
27+
# noinspection PyTypeChecker
28+
expected = SagaOperation("bar", SagaContext(foobar="foobar"))
29+
self.assertEqual(expected, meta.foo)
30+
31+
def test_constructor_raises(self):
32+
with self.assertRaises(ValueError):
33+
# noinspection PyArgumentEqualDefault,PyTypeChecker
34+
SagaOperationDecorator(None, object())
35+
36+
with self.assertRaises(ValueError):
37+
# noinspection PyArgumentEqualDefault,PyTypeChecker
38+
SagaOperationDecorator(object(), None)
39+
40+
1241
class TestSagaOperation(unittest.TestCase):
1342
def test_callback(self):
1443
operation = SagaOperation(send_create_ticket)

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,32 @@
1414
LocalSagaStep,
1515
RemoteSagaStep,
1616
Saga,
17+
SagaContext,
1718
SagaNotDefinedException,
19+
SagaRequest,
1820
SagaStep,
21+
SagaStepDecoratorMeta,
22+
SagaStepDecoratorWrapper,
1923
)
2024
from tests.utils import (
2125
create_payment,
2226
send_create_ticket,
2327
)
2428

2529

30+
class TestSagaStepDecoratorMeta(unittest.TestCase):
31+
def test_constructor(self):
32+
# noinspection PyUnusedLocal
33+
@RemoteSagaStep()
34+
def _fn(context: SagaContext) -> SagaRequest:
35+
"""For testing purposes"""
36+
37+
self.assertIsInstance(_fn, SagaStepDecoratorWrapper)
38+
meta = _fn.meta
39+
self.assertIsInstance(meta, SagaStepDecoratorMeta)
40+
self.assertEqual(RemoteSagaStep(_fn), meta.definition)
41+
42+
2643
class TestSagaStep(unittest.TestCase):
2744
def test_from_raw(self):
2845
with patch("minos.saga.SagaStep._from_raw") as mock:

0 commit comments

Comments
 (0)