Refactor geo re-resolve endpoint into geo_service and add typed response
This commit is contained in:
@@ -842,6 +842,54 @@ class TestLookupCachedOnly:
|
||||
assert uncached.count("9.9.9.9") == 1
|
||||
|
||||
|
||||
class TestReResolveAll:
|
||||
"""Tests for :func:`~app.services.geo_service.re_resolve_all`."""
|
||||
|
||||
async def test_returns_zero_when_no_unresolved_ips(self) -> None:
|
||||
"""The service returns zero counts when there are no unresolved IPs."""
|
||||
db = MagicMock()
|
||||
session = MagicMock()
|
||||
|
||||
with patch(
|
||||
"app.services.geo_service.get_unresolved_ips",
|
||||
AsyncMock(return_value=[]),
|
||||
), patch("app.services.geo_service.lookup_batch", AsyncMock()) as mock_lookup, patch(
|
||||
"app.services.geo_service.clear_neg_cache",
|
||||
MagicMock(),
|
||||
) as mock_clear:
|
||||
result = await geo_service.re_resolve_all(db, session)
|
||||
|
||||
assert result == {"resolved": 0, "total": 0}
|
||||
mock_clear.assert_not_called()
|
||||
mock_lookup.assert_not_called()
|
||||
|
||||
async def test_clears_neg_cache_and_returns_counts(self) -> None:
|
||||
"""The service clears negative cache and returns resolved and total counts."""
|
||||
db = MagicMock()
|
||||
session = MagicMock()
|
||||
ips = ["1.1.1.1", "2.2.2.2"]
|
||||
geo_map = {
|
||||
"1.1.1.1": GeoInfo(country_code="DE", country_name="Germany", asn=None, org=None),
|
||||
"2.2.2.2": GeoInfo(country_code=None, country_name=None, asn=None, org=None),
|
||||
}
|
||||
|
||||
with patch(
|
||||
"app.services.geo_service.get_unresolved_ips",
|
||||
AsyncMock(return_value=ips),
|
||||
), patch(
|
||||
"app.services.geo_service.lookup_batch",
|
||||
AsyncMock(return_value=geo_map),
|
||||
) as mock_lookup, patch(
|
||||
"app.services.geo_service.clear_neg_cache",
|
||||
MagicMock(),
|
||||
) as mock_clear:
|
||||
result = await geo_service.re_resolve_all(db, session)
|
||||
|
||||
assert result == {"resolved": 1, "total": 2}
|
||||
mock_clear.assert_called_once()
|
||||
mock_lookup.assert_awaited_once_with(ips, session, db=db)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Bulk DB writes via executemany (Task 3)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user