-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test coverage for the contextlib API (#1458)
- Loading branch information
1 parent
07e353b
commit 0f2fa02
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from __future__ import annotations | ||
|
||
from contextlib import suppress | ||
from types import TracebackType | ||
|
||
from betty.contextlib import SynchronizedContextManager | ||
|
||
|
||
class DummyAsynchronousContextManager: | ||
def __init__(self): | ||
self.entered = False | ||
self.exited = False | ||
self.exc_type: type[BaseException] | None = None | ||
self.exc_val: BaseException | None = None | ||
self.exc_tb: TracebackType | None = None | ||
|
||
async def __aenter__(self): | ||
self.entered = True | ||
|
||
async def __aexit__( | ||
self, | ||
exc_type: type[BaseException] | None, | ||
exc_val: BaseException | None, | ||
exc_tb: TracebackType | None, | ||
) -> None: | ||
self.exited = True | ||
self.exc_type = exc_type | ||
self.exc_val = exc_val | ||
self.exc_tb = exc_tb | ||
|
||
|
||
class TestSynchronizedContextManager: | ||
async def test(self) -> None: | ||
asynchronous_context_manager = DummyAsynchronousContextManager() | ||
sut = SynchronizedContextManager(asynchronous_context_manager) | ||
with sut: | ||
pass | ||
assert asynchronous_context_manager.entered | ||
assert asynchronous_context_manager.exited | ||
|
||
async def test_with_error(self) -> None: | ||
asynchronous_context_manager = DummyAsynchronousContextManager() | ||
sut = SynchronizedContextManager(asynchronous_context_manager) | ||
error = RuntimeError() | ||
with suppress(RuntimeError): | ||
with sut: | ||
raise error | ||
assert asynchronous_context_manager.entered | ||
assert asynchronous_context_manager.exited | ||
assert asynchronous_context_manager.exc_type is RuntimeError | ||
assert asynchronous_context_manager.exc_val is error | ||
assert asynchronous_context_manager.exc_tb is error.__traceback__ |