conftest.py
1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import sys
from pathlib import Path
def _ensure_local_deps_on_path() -> None:
deps = Path(__file__).resolve().parents[1] / ".deps"
if deps.exists():
deps_str = str(deps)
if deps_str not in sys.path:
sys.path.insert(0, deps_str)
_ensure_local_deps_on_path()
import pytest
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import StaticPool
from app.infrastructure.database import Base, get_db
import app.model # noqa: F401
from app.main import app
from app.infrastructure.session_store import clear_default_store_for_tests
SQLALCHEMY_DATABASE_URL = "sqlite://"
engine = create_engine(
SQLALCHEMY_DATABASE_URL,
connect_args={"check_same_thread": False},
poolclass=StaticPool,
)
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base.metadata.create_all(bind=engine)
def override_get_db():
try:
db = TestingSessionLocal()
yield db
finally:
db.close()
app.dependency_overrides[get_db] = override_get_db
@pytest.fixture
def db_session():
db = TestingSessionLocal()
yield db
db.close()
@pytest.fixture(autouse=True)
def clear_session_kv_store():
clear_default_store_for_tests()
yield