-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconftest.py
39 lines (29 loc) · 939 Bytes
/
conftest.py
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
import pytest
from fastapi.testclient import TestClient
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from app.dependencies import get_db
from app.main import create_app
from app.models import Base
@pytest.fixture
def app():
return create_app()
@pytest.fixture(name="session")
def sesionmaker():
engine = create_engine(
"sqlite:///.temp.db", connect_args={"check_same_thread": False}
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
return SessionLocal()
@pytest.fixture(autouse=True)
def clear_database(session):
engine = session.get_bind()
Base.metadata.drop_all(bind=engine)
Base.metadata.create_all(bind=engine)
@pytest.fixture
def client(app, session):
def override_get_db():
yield session
app.dependency_overrides[get_db] = override_get_db
yield TestClient(app)
del app.dependency_overrides[get_db]