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:
@@ -173,7 +173,7 @@ class TestImportLogRepo:
|
||||
)
|
||||
items, total = await import_log_repo.list_logs(db, source_id=source_id)
|
||||
assert total == 1
|
||||
assert items[0]["source_url"] == "https://s.test/"
|
||||
assert items[0].source_url == "https://s.test/"
|
||||
|
||||
async def test_get_last_log_empty(self, db: aiosqlite.Connection) -> None:
|
||||
"""get_last_log returns None when no logs exist."""
|
||||
@@ -200,7 +200,7 @@ class TestImportLogRepo:
|
||||
)
|
||||
last = await import_log_repo.get_last_log(db)
|
||||
assert last is not None
|
||||
assert last["source_url"] == "https://last.test/"
|
||||
assert last.source_url == "https://last.test/"
|
||||
|
||||
async def test_compute_total_pages(self) -> None:
|
||||
"""compute_total_pages returns correct page count."""
|
||||
|
||||
@@ -82,7 +82,7 @@ async def test_load_all_and_count_unresolved(tmp_path: Path) -> None:
|
||||
unresolved = await geo_cache_repo.count_unresolved(db)
|
||||
|
||||
assert unresolved == 1
|
||||
assert any(row["ip"] == "6.6.6.6" and row["country_code"] == "FR" for row in rows)
|
||||
assert any(row.ip == "6.6.6.6" and row.country_code == "FR" for row in rows)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
|
||||
Reference in New Issue
Block a user