Move structural typing tests into interface unit test files.
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
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]>
This commit is contained in:
co-authored by
Cursor
parent
f6ee9a3724
commit
44b15cb21a
@@ -7,6 +7,24 @@ from python_repositories.interfaces.connection_aware_interface import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
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:
|
def test_instantiation_fails_when_connect_not_implemented() -> None:
|
||||||
"""Test that instantiation fails if connect is not implemented."""
|
"""Test that instantiation fails if connect is not implemented."""
|
||||||
|
|
||||||
@@ -53,3 +71,10 @@ def test_instantiation_fails_when_is_connected_not_implemented() -> None:
|
|||||||
|
|
||||||
with pytest.raises(TypeError):
|
with pytest.raises(TypeError):
|
||||||
_ = Incomplete() # type: ignore
|
_ = 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)
|
||||||
|
|||||||
@@ -7,6 +7,24 @@ import pytest
|
|||||||
from python_repositories.interfaces.context_aware_interface import ContextAwareInterface
|
from python_repositories.interfaces.context_aware_interface import ContextAwareInterface
|
||||||
|
|
||||||
|
|
||||||
|
class FakeContextManager:
|
||||||
|
"""Plain class that satisfies ContextAwareInterface without inheritance."""
|
||||||
|
|
||||||
|
def __enter__(self) -> FakeContextManager:
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(
|
||||||
|
self, exc_type: type | None, exc_val: object | None, exc_tb: object | None
|
||||||
|
) -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def accepts_context_aware(context: ContextAwareInterface) -> None:
|
||||||
|
"""Type-checking hook for ContextAwareInterface structural subtyping."""
|
||||||
|
with context:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
def test_instantiation_fails_when_enter_not_implemented() -> None:
|
def test_instantiation_fails_when_enter_not_implemented() -> None:
|
||||||
"""Test that instantiation fails if __enter__ is not implemented."""
|
"""Test that instantiation fails if __enter__ is not implemented."""
|
||||||
|
|
||||||
@@ -33,3 +51,10 @@ def test_instantiation_fails_when_exit_not_implemented() -> None:
|
|||||||
|
|
||||||
with pytest.raises(TypeError):
|
with pytest.raises(TypeError):
|
||||||
_ = Incomplete() # type: ignore
|
_ = Incomplete() # type: ignore
|
||||||
|
|
||||||
|
|
||||||
|
def test_structural_subtyping() -> None:
|
||||||
|
"""Test that a plain class satisfies ContextAwareInterface structurally."""
|
||||||
|
context: ContextAwareInterface = FakeContextManager()
|
||||||
|
accepts_context_aware(context)
|
||||||
|
assert isinstance(context, ContextAwareInterface)
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
"""Unit tests for JsonRepositoryInterface."""
|
"""Unit tests for JsonRepositoryInterface."""
|
||||||
|
|
||||||
|
from collections.abc import Iterator
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
@@ -9,6 +10,36 @@ from python_repositories.interfaces.json_repository_interface import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class InMemoryJsonRepo:
|
||||||
|
"""Plain class that satisfies JsonRepositoryInterface without inheritance."""
|
||||||
|
|
||||||
|
def get(self, key: str) -> dict[str, Any] | None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
def set(self, key: str, data: dict[str, Any]) -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def delete(self, key: str) -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def list_keys(self, pattern: str) -> list[str]:
|
||||||
|
return []
|
||||||
|
|
||||||
|
def scan_keys(
|
||||||
|
self,
|
||||||
|
pattern: str,
|
||||||
|
*,
|
||||||
|
count: int | None = None,
|
||||||
|
) -> Iterator[str]:
|
||||||
|
del count
|
||||||
|
yield from self.list_keys(pattern)
|
||||||
|
|
||||||
|
|
||||||
|
def accepts_json_repo(repo: JsonRepositoryInterface) -> None:
|
||||||
|
"""Type-checking hook for JsonRepositoryInterface structural subtyping."""
|
||||||
|
repo.get("key")
|
||||||
|
|
||||||
|
|
||||||
def test_instantiation_fails_when_get_not_implemented() -> None:
|
def test_instantiation_fails_when_get_not_implemented() -> None:
|
||||||
"""Test that instantiation fails if get is not implemented."""
|
"""Test that instantiation fails if get is not implemented."""
|
||||||
|
|
||||||
@@ -104,3 +135,10 @@ def test_scan_keys_defaults_to_list_keys() -> None:
|
|||||||
repository = Complete()
|
repository = Complete()
|
||||||
|
|
||||||
assert list(repository.scan_keys("user")) == ["user-1", "user-2"]
|
assert list(repository.scan_keys("user")) == ["user-1", "user-2"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_structural_subtyping() -> None:
|
||||||
|
"""Test that a plain class satisfies JsonRepositoryInterface structurally."""
|
||||||
|
repo: JsonRepositoryInterface = InMemoryJsonRepo()
|
||||||
|
accepts_json_repo(repo)
|
||||||
|
assert isinstance(repo, JsonRepositoryInterface)
|
||||||
|
|||||||
@@ -9,6 +9,32 @@ from python_repositories.interfaces.object_repository_interface import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class InMemoryObjectRepo:
|
||||||
|
"""Plain class that satisfies ObjectRepositoryInterface without inheritance."""
|
||||||
|
|
||||||
|
def get(self, object_name: str) -> BytesIO | None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
def put(
|
||||||
|
self,
|
||||||
|
object_name: str,
|
||||||
|
data: BytesIO,
|
||||||
|
content_type: str = "application/octet-stream",
|
||||||
|
) -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def delete(self, object_name: str) -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def list_objects(self, prefix: str = "") -> list[str]:
|
||||||
|
return []
|
||||||
|
|
||||||
|
|
||||||
|
def accepts_object_repo(repo: ObjectRepositoryInterface) -> None:
|
||||||
|
"""Type-checking hook for ObjectRepositoryInterface structural subtyping."""
|
||||||
|
repo.get("object")
|
||||||
|
|
||||||
|
|
||||||
def test_instantiation_fails_when_get_not_implemented() -> None:
|
def test_instantiation_fails_when_get_not_implemented() -> None:
|
||||||
"""Test that instantiation fails if get is not implemented."""
|
"""Test that instantiation fails if get is not implemented."""
|
||||||
|
|
||||||
@@ -98,3 +124,10 @@ def test_instantiation_fails_when_list_objects_not_implemented() -> None:
|
|||||||
|
|
||||||
with pytest.raises(TypeError):
|
with pytest.raises(TypeError):
|
||||||
_ = Incomplete() # type: ignore
|
_ = Incomplete() # type: ignore
|
||||||
|
|
||||||
|
|
||||||
|
def test_structural_subtyping() -> None:
|
||||||
|
"""Test that a plain class satisfies ObjectRepositoryInterface structurally."""
|
||||||
|
repo: ObjectRepositoryInterface = InMemoryObjectRepo()
|
||||||
|
accepts_object_repo(repo)
|
||||||
|
assert isinstance(repo, ObjectRepositoryInterface)
|
||||||
|
|||||||
@@ -1,134 +0,0 @@
|
|||||||
"""Unit tests for structural typing of public interfaces."""
|
|
||||||
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
from collections.abc import Iterator
|
|
||||||
from io import BytesIO
|
|
||||||
from typing import Any
|
|
||||||
|
|
||||||
from python_repositories.interfaces import (
|
|
||||||
ConnectionAwareInterface,
|
|
||||||
ContextAwareInterface,
|
|
||||||
JsonRepositoryInterface,
|
|
||||||
ObjectRepositoryInterface,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class InMemoryJsonRepo:
|
|
||||||
"""Plain class that satisfies JsonRepositoryInterface without inheritance."""
|
|
||||||
|
|
||||||
def get(self, key: str) -> dict[str, Any] | None:
|
|
||||||
return None
|
|
||||||
|
|
||||||
def set(self, key: str, data: dict[str, Any]) -> None:
|
|
||||||
pass
|
|
||||||
|
|
||||||
def delete(self, key: str) -> None:
|
|
||||||
pass
|
|
||||||
|
|
||||||
def list_keys(self, pattern: str) -> list[str]:
|
|
||||||
return []
|
|
||||||
|
|
||||||
def scan_keys(
|
|
||||||
self,
|
|
||||||
pattern: str,
|
|
||||||
*,
|
|
||||||
count: int | None = None,
|
|
||||||
) -> Iterator[str]:
|
|
||||||
del count
|
|
||||||
yield from self.list_keys(pattern)
|
|
||||||
|
|
||||||
|
|
||||||
class InMemoryObjectRepo:
|
|
||||||
"""Plain class that satisfies ObjectRepositoryInterface without inheritance."""
|
|
||||||
|
|
||||||
def get(self, object_name: str) -> BytesIO | None:
|
|
||||||
return None
|
|
||||||
|
|
||||||
def put(
|
|
||||||
self,
|
|
||||||
object_name: str,
|
|
||||||
data: BytesIO,
|
|
||||||
content_type: str = "application/octet-stream",
|
|
||||||
) -> None:
|
|
||||||
pass
|
|
||||||
|
|
||||||
def delete(self, object_name: str) -> None:
|
|
||||||
pass
|
|
||||||
|
|
||||||
def list_objects(self, prefix: str = "") -> list[str]:
|
|
||||||
return []
|
|
||||||
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
class FakeContextManager:
|
|
||||||
"""Plain class that satisfies ContextAwareInterface without inheritance."""
|
|
||||||
|
|
||||||
def __enter__(self) -> FakeContextManager:
|
|
||||||
return self
|
|
||||||
|
|
||||||
def __exit__(
|
|
||||||
self, exc_type: type | None, exc_val: object | None, exc_tb: object | None
|
|
||||||
) -> None:
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def accepts_json_repo(repo: JsonRepositoryInterface) -> None:
|
|
||||||
"""Type-checking hook for JsonRepositoryInterface structural subtyping."""
|
|
||||||
repo.get("key")
|
|
||||||
|
|
||||||
|
|
||||||
def accepts_object_repo(repo: ObjectRepositoryInterface) -> None:
|
|
||||||
"""Type-checking hook for ObjectRepositoryInterface structural subtyping."""
|
|
||||||
repo.get("object")
|
|
||||||
|
|
||||||
|
|
||||||
def accepts_connection_aware(connection: ConnectionAwareInterface) -> None:
|
|
||||||
"""Type-checking hook for ConnectionAwareInterface structural subtyping."""
|
|
||||||
connection.is_connected()
|
|
||||||
|
|
||||||
|
|
||||||
def accepts_context_aware(context: ContextAwareInterface) -> None:
|
|
||||||
"""Type-checking hook for ContextAwareInterface structural subtyping."""
|
|
||||||
with context:
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def test_json_repository_structural_subtyping() -> None:
|
|
||||||
"""Test that a plain class satisfies JsonRepositoryInterface structurally."""
|
|
||||||
repo: JsonRepositoryInterface = InMemoryJsonRepo()
|
|
||||||
accepts_json_repo(repo)
|
|
||||||
assert isinstance(repo, JsonRepositoryInterface)
|
|
||||||
|
|
||||||
|
|
||||||
def test_object_repository_structural_subtyping() -> None:
|
|
||||||
"""Test that a plain class satisfies ObjectRepositoryInterface structurally."""
|
|
||||||
repo: ObjectRepositoryInterface = InMemoryObjectRepo()
|
|
||||||
accepts_object_repo(repo)
|
|
||||||
assert isinstance(repo, ObjectRepositoryInterface)
|
|
||||||
|
|
||||||
|
|
||||||
def test_connection_aware_structural_subtyping() -> None:
|
|
||||||
"""Test that a plain class satisfies ConnectionAwareInterface structurally."""
|
|
||||||
connection: ConnectionAwareInterface = FakeConnection()
|
|
||||||
accepts_connection_aware(connection)
|
|
||||||
assert isinstance(connection, ConnectionAwareInterface)
|
|
||||||
|
|
||||||
|
|
||||||
def test_context_aware_structural_subtyping() -> None:
|
|
||||||
"""Test that a plain class satisfies ContextAwareInterface structurally."""
|
|
||||||
context: ContextAwareInterface = FakeContextManager()
|
|
||||||
accepts_context_aware(context)
|
|
||||||
assert isinstance(context, ContextAwareInterface)
|
|
||||||
Reference in New Issue
Block a user