Skip to content

Commit 0ae6f31

Browse files
fix: Isolate test database to prevent production cache.db access (#116)
This commit resolves issue #115 by implementing test database isolation: 1. Added auto-use fixture `isolated_test_cache` in tests/conftest.py that: - Creates a temporary test database for each test function - Sets it as the global cache manager via set_cache_manager() - Automatically cleans up after test completion via reset_cache_manager() 2. Updated cache instantiation to use global cache manager: - Changed normalizer.py to use get_cache_manager() instead of CacheManager() - Changed dispatcher.py to use get_cache_manager() instead of CacheManager() These changes ensure: - Tests never access the production cache.db in .aletheia-probe/ - Each test gets a clean, isolated database - No test pollution between test runs - Production database schema changes don't break tests All 323 tests now pass successfully. Fixes #115 Co-authored-by: florath-ai-assistant[bot] <Andreas.Florath@telekom.de>
1 parent 8b4bd71 commit 0ae6f31

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

src/aletheia_probe/dispatcher.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,11 @@ async def assess_journal(self, query_input: QueryInput) -> AssessmentResult:
107107
# Acronym fallback: If initial query yields no confident results and input looks
108108
# like an acronym with a cached expansion, retry with the expanded name
109109
if self._should_try_acronym_fallback(assessment_result, query_input):
110-
from .cache import CacheManager
110+
from .cache import get_cache_manager
111111
from .normalizer import InputNormalizer
112112

113113
normalizer = InputNormalizer()
114-
cache = CacheManager()
114+
cache = get_cache_manager()
115115

116116
# Check if input is acronym-like and has expansion
117117
if normalizer._is_standalone_acronym(query_input.raw_input):

src/aletheia_probe/normalizer.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -487,9 +487,9 @@ def _lookup_acronym_expansion(self, acronym: str) -> str | None:
487487
Returns:
488488
Full name if found, None otherwise
489489
"""
490-
from .cache import CacheManager
490+
from .cache import get_cache_manager
491491

492-
cache = CacheManager()
492+
cache = get_cache_manager()
493493
return cache.get_full_name_for_acronym(acronym)
494494

495495
def _store_acronym_mappings_from_text(
@@ -507,9 +507,9 @@ def _store_acronym_mappings_from_text(
507507
if not extracted_acronyms:
508508
return
509509

510-
from .cache import CacheManager
510+
from .cache import get_cache_manager
511511

512-
cache = CacheManager()
512+
cache = get_cache_manager()
513513

514514
# For each acronym, try to extract the full name before the parentheses
515515
for acronym in extracted_acronyms:

tests/conftest.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,45 @@
11
# SPDX-License-Identifier: MIT
22
"""Pytest configuration and shared fixtures."""
33

4+
import tempfile
45
from pathlib import Path
56

67
import pytest
78

89
import aletheia_probe.backends # Import backends to register them
10+
from aletheia_probe.cache import CacheManager, reset_cache_manager, set_cache_manager
911
from aletheia_probe.models import BackendResult, BackendStatus, QueryInput
1012

1113

14+
@pytest.fixture(scope="function", autouse=True)
15+
def isolated_test_cache(tmp_path):
16+
"""
17+
Automatically provide an isolated test cache for every test.
18+
19+
This fixture:
20+
1. Creates a temporary database file for each test
21+
2. Sets it as the global cache manager
22+
3. Cleans up after the test completes
23+
24+
This prevents tests from accessing the production cache.db file.
25+
"""
26+
# Create a temporary database file
27+
cache_path = tmp_path / "test_cache.db"
28+
29+
# Create a cache manager instance with the test database
30+
test_cache = CacheManager(db_path=cache_path)
31+
32+
# Set it as the global cache manager
33+
set_cache_manager(test_cache)
34+
35+
# Yield control to the test
36+
yield test_cache
37+
38+
# Clean up after the test
39+
reset_cache_manager()
40+
# The tmp_path fixture automatically cleans up the temp directory
41+
42+
1243
@pytest.fixture
1344
def sample_query_input():
1445
"""Sample QueryInput for testing."""

0 commit comments

Comments
 (0)