Skip to content

Commit 9fb9e54

Browse files
committed
create database and seed data
1 parent 7573249 commit 9fb9e54

File tree

3 files changed

+36
-21
lines changed

3 files changed

+36
-21
lines changed

tests/conftest.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
from unittest import mock
44

55
import pytest
6+
import pytest_asyncio
67
from fastapi.testclient import TestClient
78
from sqlalchemy.ext.asyncio import async_sessionmaker
89

910
from fastapi_app import create_app
10-
from fastapi_app.globals import global_storage
11+
from fastapi_app.postgres_engine import create_postgres_engine_from_env
12+
from fastapi_app.setup_postgres_database import create_db_schema
13+
from fastapi_app.setup_postgres_seeddata import seed_data
1114
from tests.mocks import MockAzureCredential
1215

1316
POSTGRES_HOST = "localhost"
@@ -51,12 +54,22 @@ def mock_session_env(monkeypatch_session):
5154
yield
5255

5356

54-
@pytest.fixture(scope="session")
55-
def app(mock_session_env):
57+
async def create_and_seed_db():
58+
"""Create and seed the database."""
59+
engine = await create_postgres_engine_from_env()
60+
await create_db_schema(engine)
61+
await seed_data(engine)
62+
await engine.dispose()
63+
64+
65+
@pytest_asyncio.fixture(scope="session")
66+
async def app(mock_session_env):
5667
"""Create a FastAPI app."""
5768
if not Path("src/static/").exists():
5869
pytest.skip("Please generate frontend files first!")
59-
return create_app(testing=True)
70+
app = create_app(testing=True)
71+
await create_and_seed_db()
72+
return app
6073

6174

6275
@pytest.fixture(scope="function")
@@ -67,20 +80,21 @@ def mock_default_azure_credential(mock_session_env):
6780
yield mock_default_azure_credential
6881

6982

70-
@pytest.fixture(scope="function")
71-
def test_client(monkeypatch, app, mock_default_azure_credential):
83+
@pytest_asyncio.fixture(scope="function")
84+
async def test_client(monkeypatch, app, mock_default_azure_credential):
7285
"""Create a test client."""
73-
7486
with TestClient(app) as test_client:
7587
yield test_client
7688

7789

78-
@pytest.fixture(scope="function")
79-
def db_session():
90+
@pytest_asyncio.fixture(scope="function")
91+
async def db_session():
8092
"""Create a new database session with a rollback at the end of the test."""
81-
async_sesion = async_sessionmaker(autocommit=False, autoflush=False, bind=global_storage.engine)
93+
engine = await create_postgres_engine_from_env()
94+
async_sesion = async_sessionmaker(autocommit=False, autoflush=False, bind=engine)
8295
session = async_sesion()
8396
session.begin()
8497
yield session
8598
session.rollback()
8699
session.close()
100+
await engine.dispose()

tests/test_api_routes.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import pytest
2+
3+
4+
@pytest.mark.asyncio
5+
async def test_chat_non_json_415(test_client):
6+
"""test the chat route with a non-json request"""
7+
response = test_client.post("/chat")
8+
9+
assert response.status_code == 422
10+
assert response.headers["Content-Type"] == "application/json"
11+
assert response.headers["Content-Length"] == "82"
12+
assert b'{"detail":[{"type":"missing"' in response.content

tests/test_endpoints.py renamed to tests/test_frontend_routes.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,3 @@ async def test_assets(test_client):
5757
assert response.status_code == 200
5858
assert response.headers["Content-Length"] == str(len(assets_file))
5959
assert assets_file == response.content
60-
61-
62-
@pytest.mark.asyncio
63-
async def test_chat_non_json_415(test_client):
64-
"""test the chat route with a non-json request"""
65-
response = test_client.post("/chat")
66-
67-
assert response.status_code == 422
68-
assert response.headers["Content-Type"] == "application/json"
69-
assert response.headers["Content-Length"] == "82"
70-
assert b'{"detail":[{"type":"missing"' in response.content

0 commit comments

Comments
 (0)