Remove dead task DB fast-path and update task tests

This commit is contained in:
2026-04-07 20:00:13 +02:00
parent 0a70e40d8b
commit e21f153946
9 changed files with 96 additions and 25 deletions

View File

@@ -36,8 +36,12 @@ def _make_app(
"""
app = MagicMock()
app.state.db = MagicMock()
app.state.db.close = AsyncMock()
app.state.http_session = MagicMock()
app.state.settings.fail2ban_socket = "/var/run/fail2ban/fail2ban.sock"
app.state.settings = MagicMock(
fail2ban_socket="/var/run/fail2ban/fail2ban.sock",
database_path="/tmp/fake.db",
)
return app
@@ -93,6 +97,10 @@ class TestRunImport:
result = _make_import_result(total_imported=100, total_skipped=2, errors_count=0)
with patch(
"app.tasks.blocklist_import.open_db",
new_callable=AsyncMock,
return_value=app.state.db,
), patch(
"app.tasks.blocklist_import.blocklist_service.import_all",
new_callable=AsyncMock,
return_value=result,
@@ -112,6 +120,10 @@ class TestRunImport:
result = _make_import_result(total_imported=42, total_skipped=3, errors_count=1)
with patch(
"app.tasks.blocklist_import.open_db",
new_callable=AsyncMock,
return_value=app.state.db,
), patch(
"app.tasks.blocklist_import.blocklist_service.import_all",
new_callable=AsyncMock,
return_value=result,
@@ -132,6 +144,10 @@ class TestRunImport:
result = _make_import_result()
with patch(
"app.tasks.blocklist_import.open_db",
new_callable=AsyncMock,
return_value=app.state.db,
), patch(
"app.tasks.blocklist_import.blocklist_service.import_all",
new_callable=AsyncMock,
return_value=result,
@@ -147,6 +163,10 @@ class TestRunImport:
app = _make_app()
with patch(
"app.tasks.blocklist_import.open_db",
new_callable=AsyncMock,
return_value=app.state.db,
), patch(
"app.tasks.blocklist_import.blocklist_service.import_all",
new_callable=AsyncMock,
side_effect=RuntimeError("unexpected failure"),
@@ -288,12 +308,18 @@ class TestRegister:
app = MagicMock()
app.state.db = MagicMock()
app.state.db.close = AsyncMock()
app.state.settings = MagicMock(database_path="/tmp/fake.db")
app.state.scheduler = MagicMock()
app.state.scheduler.get_job.return_value = None
config = ScheduleConfig(frequency=ScheduleFrequency.daily, hour=3, minute=0)
with patch(
"app.tasks.blocklist_import.open_db",
new_callable=AsyncMock,
return_value=app.state.db,
), patch(
"app.tasks.blocklist_import.blocklist_service.get_schedule",
new_callable=AsyncMock,
return_value=config,
@@ -314,6 +340,8 @@ class TestRegister:
app = MagicMock()
app.state.db = MagicMock()
app.state.db.close = AsyncMock()
app.state.settings = MagicMock(database_path="/tmp/fake.db")
app.state.scheduler = MagicMock()
config = ScheduleConfig(frequency=ScheduleFrequency.daily)
@@ -321,14 +349,22 @@ class TestRegister:
mock_loop = MagicMock()
mock_loop.run_until_complete.side_effect = RuntimeError("already running")
def _close_coro(coro: Any) -> None:
coro.close()
with (
patch(
"app.tasks.blocklist_import.open_db",
new_callable=AsyncMock,
return_value=app.state.db,
),
patch(
"app.tasks.blocklist_import.blocklist_service.get_schedule",
new_callable=AsyncMock,
return_value=config,
),
patch("asyncio.get_event_loop", return_value=mock_loop),
patch("asyncio.ensure_future") as mock_ensure_future,
patch("asyncio.ensure_future", side_effect=_close_coro) as mock_ensure_future,
):
register(app)
@@ -344,9 +380,18 @@ class TestReschedule:
app = MagicMock()
app.state.db = MagicMock()
app.state.db.close = AsyncMock()
app.state.settings = MagicMock(database_path="/tmp/fake.db")
app.state.scheduler = MagicMock()
with patch("asyncio.ensure_future") as mock_ensure_future:
def _close_coro(coro: Any) -> None:
coro.close()
with patch(
"app.tasks.blocklist_import.open_db",
new_callable=AsyncMock,
return_value=app.state.db,
), patch("asyncio.ensure_future", side_effect=_close_coro) as mock_ensure_future:
reschedule(app)
mock_ensure_future.assert_called_once()

View File

@@ -31,7 +31,9 @@ def _make_app(flush_count: int = 0) -> MagicMock:
"""
app = MagicMock()
app.state.db = MagicMock()
app.state.db.close = AsyncMock()
app.state.scheduler = MagicMock()
app.state.settings = MagicMock(database_path="/tmp/fake.db")
return app
@@ -49,6 +51,10 @@ class TestRunFlush:
app = _make_app()
with patch(
"app.tasks.geo_cache_flush.open_db",
new_callable=AsyncMock,
return_value=app.state.db,
), patch(
"app.tasks.geo_cache_flush.geo_service.flush_dirty",
new_callable=AsyncMock,
return_value=0,
@@ -63,6 +69,10 @@ class TestRunFlush:
app = _make_app()
with patch(
"app.tasks.geo_cache_flush.open_db",
new_callable=AsyncMock,
return_value=app.state.db,
), patch(
"app.tasks.geo_cache_flush.geo_service.flush_dirty",
new_callable=AsyncMock,
return_value=15,
@@ -79,6 +89,10 @@ class TestRunFlush:
app = _make_app()
with patch(
"app.tasks.geo_cache_flush.open_db",
new_callable=AsyncMock,
return_value=app.state.db,
), patch(
"app.tasks.geo_cache_flush.geo_service.flush_dirty",
new_callable=AsyncMock,
return_value=0,

View File

@@ -69,6 +69,7 @@ def _make_app(
app = MagicMock()
app.state.db = db
app.state.http_session = http_session
app.state.settings = MagicMock(database_path="/tmp/fake.db")
return app
@@ -78,7 +79,11 @@ async def test_run_re_resolve_no_unresolved_ips_skips() -> None:
"""The task should return immediately when no NULL-country IPs exist."""
app = _make_app(unresolved_ips=[])
with patch("app.tasks.geo_re_resolve.geo_service") as mock_geo:
with patch(
"app.tasks.geo_re_resolve.open_db",
new_callable=AsyncMock,
return_value=app.state.db,
), patch("app.tasks.geo_re_resolve.geo_service") as mock_geo:
mock_geo.get_unresolved_ips = AsyncMock(return_value=[])
await _run_re_resolve(app)
@@ -116,7 +121,11 @@ async def test_run_re_resolve_calls_lookup_batch_with_db() -> None:
}
app = _make_app(unresolved_ips=ips, lookup_result=result)
with patch("app.tasks.geo_re_resolve.geo_service") as mock_geo:
with patch(
"app.tasks.geo_re_resolve.open_db",
new_callable=AsyncMock,
return_value=app.state.db,
), patch("app.tasks.geo_re_resolve.geo_service") as mock_geo:
mock_geo.get_unresolved_ips = AsyncMock(return_value=ips)
mock_geo.lookup_batch = AsyncMock(return_value=result)
@@ -140,7 +149,11 @@ async def test_run_re_resolve_logs_correct_counts(caplog: Any) -> None:
}
app = _make_app(unresolved_ips=ips, lookup_result=result)
with patch("app.tasks.geo_re_resolve.geo_service") as mock_geo:
with patch(
"app.tasks.geo_re_resolve.open_db",
new_callable=AsyncMock,
return_value=app.state.db,
), patch("app.tasks.geo_re_resolve.geo_service") as mock_geo:
mock_geo.get_unresolved_ips = AsyncMock(return_value=ips)
mock_geo.lookup_batch = AsyncMock(return_value=result)
@@ -163,7 +176,11 @@ async def test_run_re_resolve_handles_all_resolved() -> None:
}
app = _make_app(unresolved_ips=ips, lookup_result=result)
with patch("app.tasks.geo_re_resolve.geo_service") as mock_geo:
with patch(
"app.tasks.geo_re_resolve.open_db",
new_callable=AsyncMock,
return_value=app.state.db,
), patch("app.tasks.geo_re_resolve.geo_service") as mock_geo:
mock_geo.get_unresolved_ips = AsyncMock(return_value=ips)
mock_geo.lookup_batch = AsyncMock(return_value=result)

View File

@@ -36,8 +36,10 @@ class TestHistorySyncTask:
fake_app.state = type("FakeState", (), {})()
fake_app.state.settings = type("FakeSettings", (), {})()
fake_app.state.settings.fail2ban_socket = "/tmp/fake.sock"
fake_app.state.settings.database_path = "/tmp/fake.db"
fake_app.state.db = MagicMock()
fake_app.state.db.close = AsyncMock()
async def fake_get_history_page(*, db_path: str, since: int, page: int, page_size: int, **kwargs):
assert since == 1001
@@ -47,6 +49,10 @@ class TestHistorySyncTask:
return "/tmp/fake.sqlite3"
with patch(
"app.tasks.history_sync.open_db",
new_callable=AsyncMock,
return_value=fake_app.state.db,
), patch(
"app.tasks.history_sync._get_last_archive_ts",
new=AsyncMock(return_value=1000),
), patch(