|
| 1 | +"""What a protocol promises is what the Postgres implementation provides, in both mirrors. |
| 2 | +
|
| 3 | +A toolkit hands its parts back under the protocols' types, so a method the |
| 4 | +implementation has and the protocol lacks is one a caller cannot reach |
| 5 | +without narrowing first -- which is what happened to ``is_partition_closed``. |
| 6 | +Both directions are held here: every protocol method exists on the |
| 7 | +implementation with the same signature, and every public method of the |
| 8 | +implementation is on the protocol unless it is listed as the implementation's |
| 9 | +own. |
| 10 | +""" |
| 11 | + |
| 12 | +from __future__ import annotations |
| 13 | + |
| 14 | +import inspect |
| 15 | +from typing import Any |
| 16 | +from unittest.mock import MagicMock |
| 17 | + |
| 18 | +import pytest |
| 19 | + |
| 20 | +from pg_partsmith.aio import metadata as aio_metadata |
| 21 | +from pg_partsmith.aio import protocols as aio_protocols |
| 22 | +from pg_partsmith.aio import repositories as aio_repositories |
| 23 | +from pg_partsmith.sync import metadata as sync_metadata |
| 24 | +from pg_partsmith.sync import protocols as sync_protocols |
| 25 | +from pg_partsmith.sync import repositories as sync_repositories |
| 26 | + |
| 27 | +# Reads only the Postgres provider offers; a caller that wants them narrows to it on purpose. |
| 28 | +POSTGRES_ONLY = frozenset({"evaluate_sql_predicate", "get_partition_boundaries", "get_partition_column"}) |
| 29 | + |
| 30 | +PAIRS = [ |
| 31 | + pytest.param( |
| 32 | + aio_protocols.PartitionMetadataProvider, |
| 33 | + aio_metadata.PostgresMetadataProvider, |
| 34 | + POSTGRES_ONLY, |
| 35 | + id="aio-metadata", |
| 36 | + ), |
| 37 | + pytest.param( |
| 38 | + sync_protocols.PartitionMetadataProvider, |
| 39 | + sync_metadata.PostgresMetadataProvider, |
| 40 | + POSTGRES_ONLY, |
| 41 | + id="sync-metadata", |
| 42 | + ), |
| 43 | + pytest.param( |
| 44 | + aio_protocols.PartitionRepository, |
| 45 | + aio_repositories.PostgresPartitionRepository, |
| 46 | + frozenset(), |
| 47 | + id="aio-repository", |
| 48 | + ), |
| 49 | + pytest.param( |
| 50 | + sync_protocols.PartitionRepository, |
| 51 | + sync_repositories.PostgresPartitionRepository, |
| 52 | + frozenset(), |
| 53 | + id="sync-repository", |
| 54 | + ), |
| 55 | +] |
| 56 | + |
| 57 | +METADATA_PROTOCOLS = [ |
| 58 | + pytest.param(aio_protocols.PartitionMetadataProvider, id="aio"), |
| 59 | + pytest.param(sync_protocols.PartitionMetadataProvider, id="sync"), |
| 60 | +] |
| 61 | + |
| 62 | + |
| 63 | +def _methods(cls: type[Any]) -> set[str]: |
| 64 | + """The public functions declared on the class itself; a property is not a protocol member here.""" |
| 65 | + return {name for name, member in vars(cls).items() if inspect.isfunction(member) and not name.startswith("_")} |
| 66 | + |
| 67 | + |
| 68 | +@pytest.mark.parametrize(("protocol", "implementation", "implementation_only"), PAIRS) |
| 69 | +def test__protocol__every_method__is_implemented_with_the_same_signature( |
| 70 | + protocol: type[Any], implementation: type[Any], implementation_only: frozenset[str] |
| 71 | +) -> None: |
| 72 | + # Arrange |
| 73 | + names = sorted(_methods(protocol)) |
| 74 | + |
| 75 | + # Act -- both files start with ``from __future__ import annotations``, so the signatures compare |
| 76 | + # as written: the sync mirror is generated textually from aio, and a spelling that drifts is a |
| 77 | + # mirror that drifted. |
| 78 | + drift = { |
| 79 | + name: (str(inspect.signature(getattr(protocol, name))), str(inspect.signature(getattr(implementation, name)))) |
| 80 | + for name in names |
| 81 | + if inspect.signature(getattr(protocol, name)) != inspect.signature(getattr(implementation, name)) |
| 82 | + } |
| 83 | + |
| 84 | + # Assert |
| 85 | + assert names, "an empty protocol promises nothing" |
| 86 | + assert drift == {} |
| 87 | + assert isinstance(implementation(MagicMock()), protocol) |
| 88 | + assert not implementation_only & set(names), "on the protocol and the implementation's own at once" |
| 89 | + |
| 90 | + |
| 91 | +@pytest.mark.parametrize(("protocol", "implementation", "implementation_only"), PAIRS) |
| 92 | +def test__implementation__every_public_method__is_on_the_protocol_or_listed_as_its_own( |
| 93 | + protocol: type[Any], implementation: type[Any], implementation_only: frozenset[str] |
| 94 | +) -> None: |
| 95 | + # Arrange / Act |
| 96 | + unreachable = _methods(implementation) - _methods(protocol) - implementation_only |
| 97 | + |
| 98 | + # Assert |
| 99 | + assert unreachable == set(), "reachable only after narrowing to the implementation" |
| 100 | + assert implementation_only <= _methods(implementation), "the list names a method that is gone" |
| 101 | + |
| 102 | + |
| 103 | +@pytest.mark.parametrize("protocol", METADATA_PROTOCOLS) |
| 104 | +def test__metadata_protocol__the_closed_check__is_askable_through_it(protocol: type[Any]) -> None: |
| 105 | + # Arrange / Act |
| 106 | + member = vars(protocol).get("is_partition_closed") |
| 107 | + |
| 108 | + # Assert |
| 109 | + assert member is not None, "a toolkit's provider is typed as the protocol; the export question must be on it" |
| 110 | + signature = inspect.signature(member) |
| 111 | + assert list(signature.parameters) == ["self", "partition_name", "settle_seconds", "boundaries"] |
| 112 | + assert signature.parameters["settle_seconds"].default == 0 |
| 113 | + assert signature.parameters["boundaries"].default is None |
0 commit comments