soulsync/tests/test_db_isolation_guard.py
BoulderBadgeDad 22f30d3f40 tests: isolate the database so the suite can never touch the real DB
Several tests exercise modules (e.g. album_mbid_cache) that call get_database()
with no path, which resolves to the real database/music_library.db. Running
those writers against the live DB over a WSL-mounted Windows drive corrupted a
user's library. conftest never isolated the DB path.

conftest.py now sets os.environ['DATABASE_PATH'] to a throwaway temp file at
import time (before any test module loads). MusicDatabase resolves its default
path from DATABASE_PATH, so EVERY default-path MusicDatabase()/get_database()
call in the suite now lands in /tmp — the real DB is never opened. Temp dir is
removed at exit.

Guard tests assert DATABASE_PATH, MusicDatabase().database_path, and
get_database().database_path all resolve into the temp dir and never to
database/music_library.db. (album_mbid_cache tests, which were failing against
the corrupted live DB, now pass clean on the isolated temp DB.)
2026-05-31 23:36:57 -07:00

32 lines
1.2 KiB
Python

"""Guard: the test suite must NEVER resolve the real database/music_library.db.
Tests exercise modules that call get_database() with no path. If that resolves
to the live DB, test writes can corrupt a real library (this happened, over a
WSL-mounted Windows drive). conftest.py sets DATABASE_PATH to a temp file before
anything imports; these tests prove it sticks for every default-path access.
"""
from __future__ import annotations
import os
from pathlib import Path
_REAL = os.path.join('database', 'music_library.db')
def test_database_path_env_is_isolated():
p = os.environ.get('DATABASE_PATH', '')
assert 'soulsync-testdb-' in p, f"DATABASE_PATH not isolated: {p!r}"
def test_musicdatabase_default_path_never_real():
from database.music_database import MusicDatabase
resolved = str(Path(MusicDatabase().database_path).resolve())
assert 'soulsync-testdb-' in resolved, resolved
assert not resolved.replace('\\', '/').endswith('database/music_library.db'), resolved
def test_get_database_path_never_real():
from database.music_database import get_database
resolved = str(Path(get_database().database_path).resolve())
assert 'soulsync-testdb-' in resolved, resolved