File size: 1,042 Bytes
0359012
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pytest
from fastapi.testclient import TestClient
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from app.core.config import settings
from app.core.database import Base
from app.core.database import SessionLocal


@pytest.fixture(scope="module")
def test_app():
    # set up test app with client
    from app.main import app

    client = TestClient(app)
    # set up test database
    TEST_DATABASE_URL = settings.TEST_DATABASE_URL
    engine = create_engine(TEST_DATABASE_URL)
    TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
    Base.metadata.create_all(bind=engine)
    # yield the app and database session to the test
    try:
        yield client, TestingSessionLocal()
    finally:
        # clean up test database
        Base.metadata.drop_all(bind=engine)


@pytest.fixture(scope="function")
def db_session():
    # set up a new database session for each test
    session = SessionLocal()
    try:
        yield session
    finally:
        session.close()