test(realestate): fix mktemp deprecation + narrow exception in conftest

Replace deprecated mktemp with mkstemp to eliminate TOCTOU race, narrow
OSError catches to PermissionError (Windows SQLite lock intent only), and
add a deferred-import comment for clarity.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-28 08:11:19 +09:00
parent 62a9009fea
commit 2477342272

View File

@@ -4,7 +4,10 @@ import tempfile
import pytest
# 테스트 임시 DB 경로를 import 전에 주입
_TMP_DB = tempfile.mktemp(suffix=".db")
# mkstemp으로 충돌 없는 고유 경로 확보 후 SQLite가 직접 생성하도록 즉시 삭제
_fd, _TMP_DB = tempfile.mkstemp(suffix=".db")
os.close(_fd)
os.unlink(_TMP_DB) # let SQLite create it fresh on first init_db()
os.environ["REALESTATE_DB_PATH"] = _TMP_DB
# app 패키지 import 가능하게 PYTHONPATH 보정
@@ -14,13 +17,17 @@ sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
@pytest.fixture(autouse=True)
def _clean_db():
"""각 테스트마다 DB 초기화."""
if os.path.exists(_TMP_DB):
os.remove(_TMP_DB)
try:
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
from app.db import init_db
init_db()
yield
try:
if os.path.exists(_TMP_DB):
os.remove(_TMP_DB)
except OSError:
except PermissionError:
pass # Windows: SQLite 파일 잠금 해제 전 삭제 실패 무시