Skip to content

Commit

Permalink
Log tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
bakar-io committed Apr 22, 2024
1 parent 566b28c commit e134f14
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion backend/tests/unit_tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import asyncio
import os
import subprocess
Expand All @@ -20,6 +21,12 @@
os.environ["POSTGRES_DB"] = TEST_DB


logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)


async def _get_conn() -> asyncpg.Connection:
return await asyncpg.connect(
user=os.environ["POSTGRES_USER"],
Expand All @@ -32,6 +39,7 @@ async def _get_conn() -> asyncpg.Connection:

async def _create_test_db() -> None:
"""Check if the test database exists and create it if it doesn't."""
logger.info("Creating test database")
conn = await _get_conn()
exists = await conn.fetchval("SELECT 1 FROM pg_database WHERE datname=$1", TEST_DB)
if not exists:
Expand All @@ -41,6 +49,7 @@ async def _create_test_db() -> None:

async def _drop_test_db() -> None:
"""Check if the test database exists and if so, drop it."""
logger.info("Dropping test database")
conn = await _get_conn()
exists = await conn.fetchval("SELECT 1 FROM pg_database WHERE datname=$1", TEST_DB)
if exists:
Expand All @@ -49,7 +58,20 @@ async def _drop_test_db() -> None:


def _migrate_test_db() -> None:
subprocess.run(["make", "migrate"], check=True)
logger.info("Migrating test database")
# Run subprocess and capture output and errors
result = subprocess.run(["make", "migrate"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)

# Log standard output and errors
if result.stdout:
logger.info("Subprocess output: %s", result.stdout)
if result.stderr:
logger.error("Subprocess error: %s", result.stderr)

# Check if the subprocess exited with a non-zero exit code
if result.returncode != 0:
logger.error("Subprocess failed with return code %s", result.returncode)
raise subprocess.CalledProcessError(result.returncode, result.args, output=result.stdout, stderr=result.stderr)


@pytest.fixture(scope="session")
Expand Down

0 comments on commit e134f14

Please sign in to comment.