-
-
Notifications
You must be signed in to change notification settings - Fork 279
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
httpx and pytest added and lockfile regenerated, test cases init setup
- Loading branch information
Showing
7 changed files
with
207 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,3 +74,6 @@ postgres_data/ | |
|
||
# Variables | ||
scripts/aws/infra/staging/variables.py | ||
|
||
# Files | ||
tests/test_db_dump/tm_sample_db.sql |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import logging | ||
import os | ||
import pytest | ||
import asyncpg | ||
import subprocess | ||
import shlex | ||
from databases import Database | ||
from sqlalchemy.ext.asyncio import create_async_engine | ||
from httpx import ASGITransport, AsyncClient | ||
from backend.config import test_settings as settings | ||
from backend.db import Base, db_connection | ||
from backend.main import api as fastapi_app | ||
|
||
logger = logging.getLogger(__name__) | ||
logging.basicConfig(level=logging.INFO) | ||
|
||
ASYNC_TEST_DATABASE_URL = settings.SQLALCHEMY_DATABASE_URI.unicode_string() | ||
ASYNCPG_DNS_URL = str(settings.SQLALCHEMY_DATABASE_URI).replace( | ||
"postgresql+asyncpg://", "postgresql://" | ||
) | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def anyio_backend(): | ||
return "asyncio" | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
async def create_test_database(): | ||
logger.info("Creating test database.") | ||
conn = await asyncpg.connect(dsn=ASYNCPG_DNS_URL) | ||
await conn.execute(f"DROP DATABASE IF EXISTS {settings.POSTGRES_TEST_DB}_test") | ||
await conn.execute(f"CREATE DATABASE {settings.POSTGRES_TEST_DB}_test") | ||
await conn.close() | ||
|
||
logger.info("Test database created. Now creating tables in test database.") | ||
engine = create_async_engine(ASYNC_TEST_DATABASE_URL) | ||
async with engine.begin() as conn: | ||
await conn.run_sync(Base.metadata.create_all) | ||
await engine.dispose() | ||
logger.info("Tables created in test database.") | ||
|
||
dump_path = os.path.join("tests", "test_db_dump", "tm_sample_db.sql") | ||
logger.info("Restoring SQL dump from %s", dump_path) | ||
|
||
command = f"psql {ASYNCPG_DNS_URL} -f {dump_path}" | ||
|
||
result = subprocess.run(shlex.split(command), capture_output=True, text=True) | ||
if result.returncode != 0: | ||
logger.error("psql restore failed: %s", result.stderr) | ||
raise Exception("SQL restore failed") | ||
else: | ||
logger.info("SQL dump restored successfully using psql.") | ||
|
||
yield | ||
|
||
logger.info("Cleaning up: Dropping test database.") | ||
conn = await asyncpg.connect(dsn=ASYNCPG_DNS_URL) | ||
await conn.execute(f"DROP DATABASE IF EXISTS {settings.POSTGRES_TEST_DB}_test") | ||
await conn.close() | ||
logger.info("Test database dropped.") | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
async def app(create_test_database): | ||
logger.info("Setting up the FastAPI app for testing.") | ||
test_db = Database( | ||
ASYNC_TEST_DATABASE_URL, min_size=4, max_size=8, force_rollback=True | ||
) | ||
await test_db.connect() | ||
db_connection.database = test_db | ||
logger.info("FastAPI app setup complete. Yielding app instance for tests.") | ||
yield fastapi_app | ||
|
||
logger.info("Disconnecting test database.") | ||
await test_db.disconnect() | ||
|
||
|
||
@pytest.fixture | ||
async def client(app): | ||
logger.info("Creating test client for FastAPI app.") | ||
async with AsyncClient( | ||
transport=ASGITransport(app=app), base_url="https://test" | ||
) as ac: | ||
logger.info("Test client created; yielding client for test execution.") | ||
yield ac | ||
logger.info("Test client closed.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import pytest | ||
import logging | ||
|
||
logger = logging.getLogger(__name__) | ||
logging.basicConfig(level=logging.INFO) | ||
|
||
|
||
@pytest.mark.anyio | ||
class TestSystemHealth: | ||
async def test_heartbeat(self, client): | ||
logger.info("Starting test: heartbeat without release version data.") | ||
response = await client.get("/api/v2/system/heartbeat/") | ||
logger.info("Response status code: %s", response.status_code) | ||
assert response.status_code == 200 | ||
data = response.json() | ||
logger.info("Response JSON: %s", data) | ||
assert data["status"] == "Fastapi healthy" | ||
logger.info("Completed test: heartbeat without release version data.") |