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>
38 lines
1.1 KiB
Python
38 lines
1.1 KiB
Python
import os
|
|
import sys
|
|
import tempfile
|
|
import pytest
|
|
|
|
# 테스트 임시 DB 경로를 import 전에 주입
|
|
# mkstemp으로 충돌 없는 고유 경로 확보 후 SQLite가 직접 생성하도록 즉시 삭제
|
|
_fd, _TMP_DB = tempfile.mkstemp(suffix=".db")
|
|
os.close(_fd)
|
|
os.unlink(_TMP_DB)
|
|
os.environ["REALESTATE_DB_PATH"] = _TMP_DB
|
|
|
|
# app 패키지 import 가능하게 PYTHONPATH 보정
|
|
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)
|
|
def _clean_db():
|
|
"""각 테스트마다 DB 테이블 비우기. 파일 삭제 대신 TRUNCATE 패턴 사용해
|
|
Windows SQLite 파일 잠금 이슈를 회피한다."""
|
|
# deferred to ensure REALESTATE_DB_PATH is set before first module load
|
|
from app.db import _conn, init_db
|
|
init_db()
|
|
with _conn() as conn:
|
|
for table in _USER_TABLES:
|
|
conn.execute(f"DELETE FROM {table}")
|
|
yield
|