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]>
134 lines
3.6 KiB
Python
134 lines
3.6 KiB
Python
"""Unit tests for ObjectRepositoryInterface."""
|
|
|
|
from io import BytesIO
|
|
|
|
import pytest
|
|
|
|
from python_repositories.interfaces.object_repository_interface import (
|
|
ObjectRepositoryInterface,
|
|
)
|
|
|
|
|
|
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:
|
|
"""Test that instantiation fails if get is not implemented."""
|
|
|
|
class Incomplete(ObjectRepositoryInterface):
|
|
"""A class that does not implement get."""
|
|
|
|
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 []
|
|
|
|
with pytest.raises(TypeError):
|
|
_ = Incomplete() # type: ignore
|
|
|
|
|
|
def test_instantiation_fails_when_put_not_implemented() -> None:
|
|
"""Test that instantiation fails if put is not implemented."""
|
|
|
|
class Incomplete(ObjectRepositoryInterface):
|
|
"""A class that does not implement put."""
|
|
|
|
def get(self, object_name: str) -> BytesIO | None:
|
|
return None
|
|
|
|
def delete(self, object_name: str) -> None:
|
|
pass
|
|
|
|
def list_objects(self, prefix: str = "") -> list[str]:
|
|
return []
|
|
|
|
with pytest.raises(TypeError):
|
|
_ = Incomplete() # type: ignore
|
|
|
|
|
|
def test_instantiation_fails_when_delete_not_implemented() -> None:
|
|
"""Test that instantiation fails if delete is not implemented."""
|
|
|
|
class Incomplete(ObjectRepositoryInterface):
|
|
"""A class that does not implement delete."""
|
|
|
|
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 list_objects(self, prefix: str = "") -> list[str]:
|
|
return []
|
|
|
|
with pytest.raises(TypeError):
|
|
_ = Incomplete() # type: ignore
|
|
|
|
|
|
def test_instantiation_fails_when_list_objects_not_implemented() -> None:
|
|
"""Test that instantiation fails if list_objects is not implemented."""
|
|
|
|
class Incomplete(ObjectRepositoryInterface):
|
|
"""A class that does not implement list_objects."""
|
|
|
|
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
|
|
|
|
with pytest.raises(TypeError):
|
|
_ = 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)
|