2024-02-25 19:39:33 +01:00
|
|
|
from collections.abc import Generator
|
2020-04-20 19:03:13 +02:00
|
|
|
|
2019-02-09 19:42:36 +04:00
|
|
|
import pytest
|
2020-04-20 20:31:29 +02:00
|
|
|
from fastapi.testclient import TestClient
|
2024-03-01 12:26:05 -05:00
|
|
|
from sqlmodel import Session, delete
|
2019-02-09 19:42:36 +04:00
|
|
|
|
2020-04-16 23:56:10 -06:00
|
|
|
from app.core.config import settings
|
2024-03-02 05:01:59 -05:00
|
|
|
from app.core.db import engine, init_db
|
2020-04-20 20:31:29 +02:00
|
|
|
from app.main import app
|
2024-03-01 12:26:05 -05:00
|
|
|
from app.models import Item, User
|
2020-01-19 13:25:17 +01:00
|
|
|
from app.tests.utils.user import authentication_token_from_email
|
2020-04-20 20:31:29 +02:00
|
|
|
from app.tests.utils.utils import get_superuser_token_headers
|
2020-04-20 19:03:13 +02:00
|
|
|
|
|
|
|
|
|
2024-03-01 12:26:05 -05:00
|
|
|
@pytest.fixture(scope="session", autouse=True)
|
2020-04-20 20:31:29 +02:00
|
|
|
def db() -> Generator:
|
2023-11-25 00:08:22 +01:00
|
|
|
with Session(engine) as session:
|
2024-03-01 12:26:05 -05:00
|
|
|
init_db(session)
|
2023-11-25 00:08:22 +01:00
|
|
|
yield session
|
2024-03-01 12:26:05 -05:00
|
|
|
statement = delete(Item)
|
|
|
|
|
session.execute(statement)
|
|
|
|
|
statement = delete(User)
|
|
|
|
|
session.execute(statement)
|
|
|
|
|
session.commit()
|
2019-02-09 19:42:36 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
2020-04-20 20:31:29 +02:00
|
|
|
def client() -> Generator:
|
|
|
|
|
with TestClient(app) as c:
|
|
|
|
|
yield c
|
2019-02-09 19:42:36 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
2024-02-25 19:39:33 +01:00
|
|
|
def superuser_token_headers(client: TestClient) -> dict[str, str]:
|
2020-04-20 20:31:29 +02:00
|
|
|
return get_superuser_token_headers(client)
|
2020-01-19 13:25:17 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
2024-02-25 19:39:33 +01:00
|
|
|
def normal_user_token_headers(client: TestClient, db: Session) -> dict[str, str]:
|
2020-04-20 20:31:29 +02:00
|
|
|
return authentication_token_from_email(
|
|
|
|
|
client=client, email=settings.EMAIL_TEST_USER, db=db
|
|
|
|
|
)
|