test(realestate): add pytest harness with isolated SQLite fixture
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
# realestate-lab/app/db.py
|
# realestate-lab/app/db.py
|
||||||
import json
|
import json
|
||||||
|
import os
|
||||||
import sqlite3
|
import sqlite3
|
||||||
import logging
|
import logging
|
||||||
from typing import Dict, Any, List, Optional
|
from typing import Dict, Any, List, Optional
|
||||||
@@ -7,7 +8,7 @@ from datetime import date
|
|||||||
|
|
||||||
logger = logging.getLogger("realestate-lab")
|
logger = logging.getLogger("realestate-lab")
|
||||||
|
|
||||||
DB_PATH = "/app/data/realestate.db"
|
DB_PATH = os.getenv("REALESTATE_DB_PATH", "/app/data/realestate.db")
|
||||||
|
|
||||||
|
|
||||||
def _conn():
|
def _conn():
|
||||||
|
|||||||
0
realestate-lab/tests/__init__.py
Normal file
0
realestate-lab/tests/__init__.py
Normal file
26
realestate-lab/tests/conftest.py
Normal file
26
realestate-lab/tests/conftest.py
Normal file
@@ -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 파일 잠금 해제 전 삭제 실패 무시
|
||||||
11
realestate-lab/tests/test_db_basic.py
Normal file
11
realestate-lab/tests/test_db_basic.py
Normal file
@@ -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
|
||||||
Reference in New Issue
Block a user