test(realestate): truncate tables for isolation instead of file delete

Replace os.remove() in conftest autouse fixture with per-table DELETE
to avoid Windows SQLite file-lock PermissionError being swallowed
silently and leaking state across tests. Remove the inline DELETE
workaround from test_profile_api.py that was coupling the test to
internal DB knowledge.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-28 08:45:03 +09:00
parent 20b51f706c
commit d46d2cb30b
2 changed files with 18 additions and 18 deletions

View File

@@ -7,27 +7,31 @@ import pytest
# mkstemp으로 충돌 없는 고유 경로 확보 후 SQLite가 직접 생성하도록 즉시 삭제 # mkstemp으로 충돌 없는 고유 경로 확보 후 SQLite가 직접 생성하도록 즉시 삭제
_fd, _TMP_DB = tempfile.mkstemp(suffix=".db") _fd, _TMP_DB = tempfile.mkstemp(suffix=".db")
os.close(_fd) os.close(_fd)
os.unlink(_TMP_DB) # let SQLite create it fresh on first init_db() os.unlink(_TMP_DB)
os.environ["REALESTATE_DB_PATH"] = _TMP_DB os.environ["REALESTATE_DB_PATH"] = _TMP_DB
# app 패키지 import 가능하게 PYTHONPATH 보정 # app 패키지 import 가능하게 PYTHONPATH 보정
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# 테이블 목록 — init_db가 생성하는 모든 테이블
_USER_TABLES = (
"match_results", # FK CASCADE 대비 자식 테이블 먼저
"announcement_models",
"announcements",
"user_profile",
"collect_log",
)
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def _clean_db(): def _clean_db():
"""각 테스트마다 DB 초기화.""" """각 테스트마다 DB 테이블 비우기. 파일 삭제 대신 TRUNCATE 패턴 사용해
try: Windows SQLite 파일 잠금 이슈를 회피한다."""
if os.path.exists(_TMP_DB):
os.remove(_TMP_DB)
except PermissionError:
pass # Windows: SQLite 파일 잠금 해제 전 삭제 실패 무시
# deferred to ensure REALESTATE_DB_PATH is set before first module load # deferred to ensure REALESTATE_DB_PATH is set before first module load
from app.db import init_db from app.db import _conn, init_db
init_db() init_db()
with _conn() as conn:
for table in _USER_TABLES:
conn.execute(f"DELETE FROM {table}")
yield yield
try:
if os.path.exists(_TMP_DB):
os.remove(_TMP_DB)
except PermissionError:
pass # Windows: SQLite 파일 잠금 해제 전 삭제 실패 무시

View File

@@ -26,11 +26,7 @@ def test_profile_update_accepts_new_fields():
def test_profile_get_returns_defaults_for_new_fields(): def test_profile_get_returns_defaults_for_new_fields():
from app.main import app from app.main import app
from app.db import upsert_profile, _conn from app.db import upsert_profile
# 이전 테스트 데이터 제거 후 새 프로필 삽입
with _conn() as conn:
conn.execute("DELETE FROM user_profile")
upsert_profile({"name": "기본"}) upsert_profile({"name": "기본"})
with TestClient(app) as client: with TestClient(app) as client: