test: набор pytest-тестов под site/ (unit + интеграционные + безопасность)
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
"""Общие фикстуры для тестов contracts-flask (модули site/).
|
||||
|
||||
Запуск: pytest tests/ -q
|
||||
"""
|
||||
import os
|
||||
import sys
|
||||
|
||||
import pytest
|
||||
|
||||
# site/ в sys.path — импортируем config/db/services/routes напрямую
|
||||
_SITE = os.path.join(os.path.dirname(__file__), "..", "site")
|
||||
if _SITE not in sys.path:
|
||||
sys.path.insert(0, _SITE)
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True, scope="session")
|
||||
def _isolate_global_db(tmp_path_factory):
|
||||
"""Не дать импорту app.py пересоздать реальную /tmp/contracts.db."""
|
||||
from db import connection as conn
|
||||
conn.DB_PATH = str(tmp_path_factory.mktemp("dbs") / "session.db")
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def db(tmp_path, monkeypatch):
|
||||
"""Изолированная БД: отдельный DB_PATH + свежая схема (init_db)."""
|
||||
from db import connection as conn
|
||||
monkeypatch.setattr(conn, "DB_PATH", str(tmp_path / "contracts_test.db"))
|
||||
conn.init_db()
|
||||
yield conn
|
||||
# cleanup: закрыть thread-local соединение
|
||||
c = getattr(conn._local, "conn", None)
|
||||
if c is not None:
|
||||
try:
|
||||
c.close()
|
||||
except Exception:
|
||||
pass
|
||||
conn._local.conn = None
|
||||
Reference in New Issue
Block a user