diff --git a/realestate-lab/app/db.py b/realestate-lab/app/db.py index 45b4dc6..255ebe3 100644 --- a/realestate-lab/app/db.py +++ b/realestate-lab/app/db.py @@ -1,5 +1,6 @@ # realestate-lab/app/db.py import json +import os import sqlite3 import logging from typing import Dict, Any, List, Optional @@ -7,7 +8,7 @@ from datetime import date logger = logging.getLogger("realestate-lab") -DB_PATH = "/app/data/realestate.db" +DB_PATH = os.getenv("REALESTATE_DB_PATH", "/app/data/realestate.db") def _conn(): diff --git a/realestate-lab/tests/__init__.py b/realestate-lab/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/realestate-lab/tests/conftest.py b/realestate-lab/tests/conftest.py new file mode 100644 index 0000000..023f392 --- /dev/null +++ b/realestate-lab/tests/conftest.py @@ -0,0 +1,26 @@ +import os +import sys +import tempfile +import pytest + +# 테스트 임시 DB 경로를 import 전에 주입 +_TMP_DB = tempfile.mktemp(suffix=".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 초기화.""" + if os.path.exists(_TMP_DB): + os.remove(_TMP_DB) + from app.db import init_db + init_db() + yield + try: + if os.path.exists(_TMP_DB): + os.remove(_TMP_DB) + except OSError: + pass # Windows: SQLite 파일 잠금 해제 전 삭제 실패 무시 diff --git a/realestate-lab/tests/test_db_basic.py b/realestate-lab/tests/test_db_basic.py new file mode 100644 index 0000000..9268465 --- /dev/null +++ b/realestate-lab/tests/test_db_basic.py @@ -0,0 +1,11 @@ +def test_init_db_creates_tables(): + from app.db import _conn + with _conn() as conn: + tables = {row[0] for row in conn.execute( + "SELECT name FROM sqlite_master WHERE type='table'" + )} + assert "announcements" in tables + assert "announcement_models" in tables + assert "user_profile" in tables + assert "match_results" in tables + assert "collect_log" in tables