Skip to content

Commit c3c82d4

Browse files
author
remimd
committed
tests
1 parent 812e810 commit c3c82d4

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
from cq import ContextCommandPipeline, ContextPipeline, command_handler
2+
from tests.helpers.history import HistoryMiddleware
3+
4+
5+
class TestContextCommandPipeline:
6+
async def test_dispatch_with_success_return_any(
7+
self,
8+
history: HistoryMiddleware,
9+
) -> None:
10+
class Command1: ...
11+
12+
class Command2: ...
13+
14+
class Command3: ...
15+
16+
class Foo: ...
17+
18+
class Bar: ...
19+
20+
class Baz: ...
21+
22+
@command_handler
23+
class CommandHandler1:
24+
async def handle(self, command: Command1) -> Foo:
25+
return Foo()
26+
27+
@command_handler
28+
class CommandHandler2:
29+
async def handle(self, command: Command2) -> Bar:
30+
return Bar()
31+
32+
@command_handler
33+
class CommandHandler3:
34+
async def handle(self, command: Command3) -> Baz:
35+
return Baz()
36+
37+
class Context:
38+
foo: Foo
39+
bar: Bar
40+
baz: Baz
41+
42+
pipeline: ContextPipeline[Command1] = ContextCommandPipeline()
43+
44+
@pipeline.step
45+
async def _(self, foo: Foo) -> Command2:
46+
self.foo = foo
47+
return Command2()
48+
49+
@pipeline.step
50+
async def _(self, bar: Bar) -> Command3:
51+
self.bar = bar
52+
return Command3()
53+
54+
@pipeline.step
55+
async def _(self, baz: Baz) -> None:
56+
self.baz = baz
57+
58+
cmd = Command1()
59+
ctx = await Context().pipeline.dispatch(cmd)
60+
61+
assert isinstance(ctx, Context)
62+
assert isinstance(ctx.foo, Foo)
63+
assert isinstance(ctx.bar, Bar)
64+
assert isinstance(ctx.baz, Baz)
65+
assert len(history.records) == 3

0 commit comments

Comments
 (0)