From 71ca3a0d6e4826aa4a8a947041fbe09b9f7218c0 Mon Sep 17 00:00:00 2001 From: xiaoxia Date: Thu, 13 Aug 2026 20:43:13 +0800 Subject: [PATCH] fix(test): add autouse fixture to reset worker_app.db module between tests Add _reset_worker_app_db pytest fixture with autouse=True to prevent mock pollution from leaking between tests. This fixes the flaky test_batch_download_session_closed test that would fail when running the full test suite due to worker_app.db being replaced with a MagicMock from a previous test. --- tests/unit/test_batch_download.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/unit/test_batch_download.py b/tests/unit/test_batch_download.py index 58c669bce..e12de3456 100755 --- a/tests/unit/test_batch_download.py +++ b/tests/unit/test_batch_download.py @@ -13,6 +13,20 @@ from unittest.mock import MagicMock, patch import pytest + +@pytest.fixture(autouse=True) +def _reset_worker_app_db(): + """Prevent mock pollution from leaking between tests.""" + import importlib + import sys + # Save original module reference + original = sys.modules.get("worker_app.db") + yield + # Restore original module after each test + if original is not None: + sys.modules["worker_app.db"] = original + + # ── Fake repository ─────────────────────────────────────────────────────────