Code Quality Pipeline / code-quality (pull_request) Successful in 24s
PR Title Check / check-title (pull_request) Successful in 35s
Test Python Package / coverage-report (pull_request) Successful in 11s
Test Python Package / unit-tests (pull_request) Successful in 12s
Test Python Package / integration-tests (pull_request) Successful in 26s
Align test layout with the one-test-file-per-interface convention by removing structural_typing_test.py. Co-authored-by: Cursor <[email protected]>
81 lines
2.2 KiB
Python
81 lines
2.2 KiB
Python
"""Unit tests for ConnectionAwareInterface."""
|
|
|
|
import pytest
|
|
|
|
from python_repositories.interfaces.connection_aware_interface import (
|
|
ConnectionAwareInterface,
|
|
)
|
|
|
|
|
|
class FakeConnection:
|
|
"""Plain class that satisfies ConnectionAwareInterface without inheritance."""
|
|
|
|
def connect(self) -> None:
|
|
pass
|
|
|
|
def disconnect(self) -> None:
|
|
pass
|
|
|
|
def is_connected(self) -> bool:
|
|
return True
|
|
|
|
|
|
def accepts_connection_aware(connection: ConnectionAwareInterface) -> None:
|
|
"""Type-checking hook for ConnectionAwareInterface structural subtyping."""
|
|
connection.is_connected()
|
|
|
|
|
|
def test_instantiation_fails_when_connect_not_implemented() -> None:
|
|
"""Test that instantiation fails if connect is not implemented."""
|
|
|
|
class Incomplete(ConnectionAwareInterface):
|
|
"""A class that does not implement connect."""
|
|
|
|
def disconnect(self) -> None:
|
|
pass
|
|
|
|
def is_connected(self) -> bool:
|
|
return False
|
|
|
|
with pytest.raises(TypeError):
|
|
_ = Incomplete() # type: ignore
|
|
|
|
|
|
def test_instantiation_fails_when_disconnect_not_implemented() -> None:
|
|
"""Test that instantiation fails if disconnect is not implemented."""
|
|
|
|
class Incomplete(ConnectionAwareInterface):
|
|
"""A class that does not implement disconnect."""
|
|
|
|
def connect(self) -> None:
|
|
pass
|
|
|
|
def is_connected(self) -> bool:
|
|
return False
|
|
|
|
with pytest.raises(TypeError):
|
|
_ = Incomplete() # type: ignore
|
|
|
|
|
|
def test_instantiation_fails_when_is_connected_not_implemented() -> None:
|
|
"""Test that instantiation fails if is_connected is not implemented."""
|
|
|
|
class Incomplete(ConnectionAwareInterface):
|
|
"""A class that does not implement is_connected."""
|
|
|
|
def connect(self) -> None:
|
|
pass
|
|
|
|
def disconnect(self) -> None:
|
|
pass
|
|
|
|
with pytest.raises(TypeError):
|
|
_ = Incomplete() # type: ignore
|
|
|
|
|
|
def test_structural_subtyping() -> None:
|
|
"""Test that a plain class satisfies ConnectionAwareInterface structurally."""
|
|
connection: ConnectionAwareInterface = FakeConnection()
|
|
accepts_connection_aware(connection)
|
|
assert isinstance(connection, ConnectionAwareInterface)
|