Files
web-page-backend/realestate-lab/tests/conftest.py
gahusb 2477342272 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>
2026-04-28 08:11:19 +09:00

34 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) # let SQLite create it fresh on first init_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__))))
@pytest.fixture(autouse=True)
def _clean_db():
"""각 테스트마다 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 PermissionError:
pass # Windows: SQLite 파일 잠금 해제 전 삭제 실패 무시