Document task DB access and unify background task DB handling
This commit is contained in:
@@ -98,7 +98,7 @@ class TestRunImport:
|
||||
result = _make_import_result(total_imported=100, total_skipped=2, errors_count=0)
|
||||
|
||||
with patch(
|
||||
"app.tasks.blocklist_import.open_db",
|
||||
"app.tasks.db.open_db",
|
||||
new_callable=AsyncMock,
|
||||
return_value=app.state.db,
|
||||
), patch(
|
||||
@@ -122,7 +122,7 @@ class TestRunImport:
|
||||
result = _make_import_result(total_imported=42, total_skipped=3, errors_count=1)
|
||||
|
||||
with patch(
|
||||
"app.tasks.blocklist_import.open_db",
|
||||
"app.tasks.db.open_db",
|
||||
new_callable=AsyncMock,
|
||||
return_value=app.state.db,
|
||||
), patch(
|
||||
@@ -146,7 +146,7 @@ class TestRunImport:
|
||||
result = _make_import_result()
|
||||
|
||||
with patch(
|
||||
"app.tasks.blocklist_import.open_db",
|
||||
"app.tasks.db.open_db",
|
||||
new_callable=AsyncMock,
|
||||
return_value=app.state.db,
|
||||
), patch(
|
||||
@@ -165,7 +165,7 @@ class TestRunImport:
|
||||
app = _make_app()
|
||||
|
||||
with patch(
|
||||
"app.tasks.blocklist_import.open_db",
|
||||
"app.tasks.db.open_db",
|
||||
new_callable=AsyncMock,
|
||||
return_value=app.state.db,
|
||||
), patch(
|
||||
@@ -323,7 +323,7 @@ class TestRegister:
|
||||
config = ScheduleConfig(frequency=ScheduleFrequency.daily, hour=3, minute=0)
|
||||
|
||||
with patch(
|
||||
"app.tasks.blocklist_import.open_db",
|
||||
"app.tasks.db.open_db",
|
||||
new_callable=AsyncMock,
|
||||
return_value=app.state.db,
|
||||
), patch(
|
||||
@@ -353,7 +353,7 @@ class TestReschedule:
|
||||
coro.close()
|
||||
|
||||
with patch(
|
||||
"app.tasks.blocklist_import.open_db",
|
||||
"app.tasks.db.open_db",
|
||||
new_callable=AsyncMock,
|
||||
return_value=app.state.db,
|
||||
), patch("asyncio.ensure_future", side_effect=_close_coro) as mock_ensure_future:
|
||||
|
||||
@@ -34,6 +34,7 @@ def _make_app(flush_count: int = 0) -> MagicMock:
|
||||
app.state.db.close = AsyncMock()
|
||||
app.state.scheduler = MagicMock()
|
||||
app.state.settings = MagicMock(database_path="/tmp/fake.db")
|
||||
app.state.runtime_settings = None
|
||||
return app
|
||||
|
||||
|
||||
@@ -51,7 +52,7 @@ class TestRunFlush:
|
||||
app = _make_app()
|
||||
|
||||
with patch(
|
||||
"app.tasks.geo_cache_flush.open_db",
|
||||
"app.tasks.db.open_db",
|
||||
new_callable=AsyncMock,
|
||||
return_value=app.state.db,
|
||||
), patch(
|
||||
@@ -69,7 +70,7 @@ class TestRunFlush:
|
||||
app = _make_app()
|
||||
|
||||
with patch(
|
||||
"app.tasks.geo_cache_flush.open_db",
|
||||
"app.tasks.db.open_db",
|
||||
new_callable=AsyncMock,
|
||||
return_value=app.state.db,
|
||||
), patch(
|
||||
@@ -89,7 +90,7 @@ class TestRunFlush:
|
||||
app = _make_app()
|
||||
|
||||
with patch(
|
||||
"app.tasks.geo_cache_flush.open_db",
|
||||
"app.tasks.db.open_db",
|
||||
new_callable=AsyncMock,
|
||||
return_value=app.state.db,
|
||||
), patch(
|
||||
|
||||
@@ -70,6 +70,7 @@ def _make_app(
|
||||
app.state.db = db
|
||||
app.state.http_session = http_session
|
||||
app.state.settings = MagicMock(database_path="/tmp/fake.db")
|
||||
app.state.runtime_settings = None
|
||||
|
||||
return app
|
||||
|
||||
@@ -80,7 +81,7 @@ async def test_run_re_resolve_no_unresolved_ips_skips() -> None:
|
||||
app = _make_app(unresolved_ips=[])
|
||||
|
||||
with patch(
|
||||
"app.tasks.geo_re_resolve.open_db",
|
||||
"app.tasks.db.open_db",
|
||||
new_callable=AsyncMock,
|
||||
return_value=app.state.db,
|
||||
), patch("app.tasks.geo_re_resolve.geo_service") as mock_geo:
|
||||
@@ -104,6 +105,7 @@ async def test_run_re_resolve_clears_neg_cache() -> None:
|
||||
|
||||
with patch("app.tasks.geo_re_resolve.geo_service") as mock_geo:
|
||||
mock_geo.get_unresolved_ips = AsyncMock(return_value=ips)
|
||||
mock_geo.clear_neg_cache = AsyncMock()
|
||||
mock_geo.lookup_batch = AsyncMock(return_value=result)
|
||||
|
||||
await _run_re_resolve(app)
|
||||
@@ -122,11 +124,12 @@ 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.open_db",
|
||||
"app.tasks.db.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.clear_neg_cache = AsyncMock()
|
||||
mock_geo.lookup_batch = AsyncMock(return_value=result)
|
||||
|
||||
await _run_re_resolve(app)
|
||||
@@ -150,11 +153,12 @@ 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.open_db",
|
||||
"app.tasks.db.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.clear_neg_cache = AsyncMock()
|
||||
mock_geo.lookup_batch = AsyncMock(return_value=result)
|
||||
|
||||
await _run_re_resolve(app)
|
||||
@@ -177,11 +181,12 @@ 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.open_db",
|
||||
"app.tasks.db.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.clear_neg_cache = AsyncMock()
|
||||
mock_geo.lookup_batch = AsyncMock(return_value=result)
|
||||
|
||||
await _run_re_resolve(app)
|
||||
|
||||
@@ -37,12 +37,13 @@ class TestHistorySyncTask:
|
||||
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.runtime_settings = None
|
||||
|
||||
fake_db = AsyncMock()
|
||||
fake_db.close = AsyncMock()
|
||||
|
||||
with patch(
|
||||
"app.tasks.history_sync.open_db",
|
||||
"app.tasks.db.open_db",
|
||||
new_callable=AsyncMock,
|
||||
return_value=fake_db,
|
||||
), patch(
|
||||
|
||||
44
backend/tests/test_tasks/test_task_db.py
Normal file
44
backend/tests/test_tasks/test_task_db.py
Normal file
@@ -0,0 +1,44 @@
|
||||
"""Tests for the shared background task database helper."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from app.tasks.db import task_db
|
||||
|
||||
|
||||
class FakeSettings:
|
||||
database_path = "/tmp/fake.db"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_task_db_opens_and_closes_connection() -> None:
|
||||
"""``task_db`` must open a DB connection and close it after use."""
|
||||
fake_db = AsyncMock()
|
||||
fake_db.close = AsyncMock()
|
||||
|
||||
with patch("app.tasks.db.open_db", new_callable=AsyncMock, return_value=fake_db) as mock_open_db:
|
||||
async with task_db(FakeSettings()) as db:
|
||||
assert db is fake_db
|
||||
|
||||
mock_open_db.assert_awaited_once_with("/tmp/fake.db")
|
||||
fake_db.close.assert_awaited_once()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_task_db_closes_connection_on_exception() -> None:
|
||||
"""``task_db`` must close the connection even when the body raises."""
|
||||
fake_db = AsyncMock()
|
||||
fake_db.close = AsyncMock()
|
||||
|
||||
with patch(
|
||||
"app.tasks.db.open_db",
|
||||
new_callable=AsyncMock,
|
||||
return_value=fake_db,
|
||||
), pytest.raises(RuntimeError, match="boom"):
|
||||
async with task_db(FakeSettings()):
|
||||
raise RuntimeError("boom")
|
||||
|
||||
fake_db.close.assert_awaited_once()
|
||||
Reference in New Issue
Block a user