Task 8: Standardize modeling style (TypedDict vs Pydantic)
Convert inconsistent modeling style to standardized Pydantic models for all external-facing data structures while maintaining TypedDict compatibility where appropriate for internal layer-private structures. Changes: - Converted IpLookupResult TypedDict to use IpLookupResponse Pydantic model in jail_service.lookup_ip() for consistency with routers - Added GeoCacheEntry Pydantic model for geo cache repository rows - Converted GeoCacheRow TypedDict to use GeoCacheEntry alias - Converted ImportLogRow TypedDict to use ImportLogEntry alias - Updated routers and services to work with Pydantic models - Updated all tests to use Pydantic model field access (attributes) instead of dict subscripting Documentation: - Added 'Model Type Usage by Layer' section to Backend-Development.md - Defines when TypedDict is allowed (internal structures) vs Pydantic (external-facing, cross-boundary data) - Provides clear guidance on modeling conventions per layer Benefits: - Consistent validation and serialization behavior - Better IDE support and type checking - Clearer separation of concerns by layer - Reduced maintenance cost from mixed validation approaches Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -847,8 +847,8 @@ class TestLookupIp:
|
||||
with _patch_client(responses):
|
||||
result = await jail_service.lookup_ip(_SOCKET, "1.2.3.4")
|
||||
|
||||
assert result["ip"] == "1.2.3.4"
|
||||
assert "sshd" in result["currently_banned_in"]
|
||||
assert result.ip == "1.2.3.4"
|
||||
assert "sshd" in result.currently_banned_in
|
||||
|
||||
async def test_geo_enricher_returns_geo_detail(self) -> None:
|
||||
"""lookup_ip converts GeoInfo from the enricher into GeoDetail."""
|
||||
@@ -868,11 +868,11 @@ class TestLookupIp:
|
||||
geo_enricher=_enricher,
|
||||
)
|
||||
|
||||
assert isinstance(result["geo"], GeoDetail)
|
||||
assert result["geo"].country_code == "DE"
|
||||
assert result["geo"].country_name == "Germany"
|
||||
assert result["geo"].asn == "AS123"
|
||||
assert result["geo"].org == "Acme"
|
||||
assert isinstance(result.geo, GeoDetail)
|
||||
assert result.geo.country_code == "DE"
|
||||
assert result.geo.country_name == "Germany"
|
||||
assert result.geo.asn == "AS123"
|
||||
assert result.geo.org == "Acme"
|
||||
|
||||
async def test_http_session_uses_geo_service_lookup(self) -> None:
|
||||
"""lookup_ip uses geo_service.lookup when http_session is provided."""
|
||||
@@ -896,11 +896,11 @@ class TestLookupIp:
|
||||
)
|
||||
|
||||
mock_lookup.assert_awaited_once_with("1.2.3.4", mock_session)
|
||||
assert isinstance(result["geo"], GeoDetail)
|
||||
assert result["geo"].country_code == "JP"
|
||||
assert result["geo"].country_name == "Japan"
|
||||
assert result["geo"].asn is None
|
||||
assert result["geo"].org is None
|
||||
assert isinstance(result.geo, GeoDetail)
|
||||
assert result.geo.country_code == "JP"
|
||||
assert result.geo.country_name == "Japan"
|
||||
assert result.geo.asn is None
|
||||
assert result.geo.org is None
|
||||
|
||||
async def test_invalid_ip_raises(self) -> None:
|
||||
"""lookup_ip raises ValueError for invalid IP."""
|
||||
@@ -917,7 +917,7 @@ class TestLookupIp:
|
||||
with _patch_client(responses):
|
||||
result = await jail_service.lookup_ip(_SOCKET, "9.9.9.9")
|
||||
|
||||
assert result["currently_banned_in"] == []
|
||||
assert result.currently_banned_in == []
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user