11from __future__ import annotations
22
3- from collections .abc import Mapping
4- from enum import IntEnum
5- from typing import Any , NamedTuple
3+ from typing import Any
64
75import pytest
86
97from cq ._core .middleware import MiddlewareGroup , MiddlewareResult
8+ from tests .helpers .history import HistoryMiddleware
109
1110
1211class TestMiddlewareGroup :
1312 @pytest .fixture (scope = "function" )
1413 def group (self ) -> MiddlewareGroup [..., Any ]:
1514 return MiddlewareGroup ()
1615
17- @pytest .fixture (scope = "function" )
18- def history (self ) -> _HistoryMiddleware :
19- return _HistoryMiddleware ()
20-
2116 def test_add_with_success_return_self (
2217 self ,
2318 group : MiddlewareGroup [..., Any ],
24- history : _HistoryMiddleware ,
19+ history : HistoryMiddleware ,
2520 ) -> None :
2621 assert group .add (history ) is group
2722
2823 async def test_invoke_with_success_return_any (
2924 self ,
3025 group : MiddlewareGroup [..., Any ],
31- history : _HistoryMiddleware ,
26+ history : HistoryMiddleware ,
3227 ) -> None :
3328 async def handler () -> str :
3429 return "I'm a handler..."
@@ -43,18 +38,20 @@ async def handler() -> str:
4338 assert record .args == ()
4439 assert record .kwargs == {}
4540 assert record .result == result
46- assert record .status == _Status . SUCCESS
41+ assert record .is_success
4742
4843 async def test_invoke_with_exception_raise_any (
4944 self ,
5045 group : MiddlewareGroup [..., Any ],
51- history : _HistoryMiddleware ,
46+ history : HistoryMiddleware ,
5247 ) -> None :
5348 async def handler () -> str :
5449 raise ValueError ("I failed..." )
5550
5651 group .add (history )
57- assert await group .invoke (handler ) is NotImplemented
52+
53+ with pytest .raises (ValueError ):
54+ await group .invoke (handler )
5855
5956 records = history .records
6057 assert len (records ) == 1
@@ -63,12 +60,12 @@ async def handler() -> str:
6360 assert record .args == ()
6461 assert record .kwargs == {}
6562 assert isinstance (record .result , ValueError )
66- assert record .status == _Status . FAILED
63+ assert record .is_failed
6764
6865 async def test_invoke_with_multiple_yield_return_any (
6966 self ,
7067 group : MiddlewareGroup [..., Any ],
71- history : _HistoryMiddleware ,
68+ history : HistoryMiddleware ,
7269 ) -> None :
7370 async def handler () -> str :
7471 return "I'm a handler..."
@@ -80,37 +77,6 @@ async def handler() -> str:
8077 assert len (records ) == 2
8178
8279
83- class _Status (IntEnum ):
84- SUCCESS = 1
85- FAILED = 0
86-
87-
88- class _Record (NamedTuple ):
89- args : tuple [Any , ...]
90- kwargs : Mapping [str , Any ]
91- result : Any
92- status : _Status
93-
94-
95- class _HistoryMiddleware :
96- def __init__ (self ) -> None :
97- self .__records : list [_Record ] = []
98-
99- @property
100- def records (self ) -> tuple [_Record , ...]:
101- return tuple (self .__records )
102-
103- async def __call__ (self , / , * args : Any , ** kwargs : Any ) -> MiddlewareResult [Any ]:
104- try :
105- result = yield
106- except BaseException as exc :
107- record = _Record (args , kwargs , exc , _Status .FAILED )
108- else :
109- record = _Record (args , kwargs , result , _Status .SUCCESS )
110-
111- self .__records .append (record )
112-
113-
11480async def _exec_2_times_middleware (* args : Any , ** kwargs : Any ) -> MiddlewareResult [Any ]:
11581 yield
11682 yield
0 commit comments