-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconftest.py
More file actions
54 lines (41 loc) · 1.31 KB
/
conftest.py
File metadata and controls
54 lines (41 loc) · 1.31 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
"""Tests fixtures module."""
import os
from collections.abc import Generator
from pathlib import Path
import pytest
import sentry_sdk
from alembic import command
from alembic.config import Config
from fastapi.testclient import TestClient
from sqlalchemy import MetaData
from sqlmodel import Session
from app.core.config import get_config
from app.core.db import get_engine
from main import app
@pytest.fixture(scope="session", autouse=True)
def set_env() -> None:
sentry_sdk.init() # disable sentry
get_config.cache_clear()
os.environ["LGS_TESTS"] = "1"
@pytest.fixture
def client_without_token() -> TestClient:
return TestClient(app)
@pytest.fixture
def client(client_without_token: TestClient) -> TestClient:
client_without_token.headers = {"X-token": get_config().api_key}
return client_without_token
@pytest.fixture
def session() -> Generator[Session]:
"""Return DB session."""
# drop database
engine = get_engine()
assert str(engine.url).endswith("_test")
metadata = MetaData()
metadata.reflect(bind=engine, schema="public")
metadata.drop_all(bind=engine)
# run migrations
alembic_cfg = Config(Path(__file__).resolve().parent / "alembic.ini")
command.upgrade(alembic_cfg, "head")
# provide session
with Session(engine) as session:
yield session