Skip to content

Commit 9b1bd32

Browse files
committed
feat: enhance test infrastructure and utility functions
1 parent 8c7b079 commit 9b1bd32

File tree

4 files changed

+99
-12
lines changed

4 files changed

+99
-12
lines changed

backend/app/api/routes/utils.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,28 @@ def test_email(email_to: EmailStr) -> Message:
2727

2828

2929
@router.get("/health-check/")
30-
async def health_check() -> bool:
31-
return True
30+
async def health_check() -> dict[str, bool]:
31+
"""
32+
Health check endpoint including database and Redis status.
33+
"""
34+
from app.api.deps import get_db
35+
from sqlalchemy import text
36+
from app.core.redis import redis_client
37+
38+
# Check database connection
39+
db_status = False
40+
try:
41+
db = next(get_db())
42+
db.execute(text("SELECT 1"))
43+
db_status = True
44+
except Exception:
45+
pass
46+
47+
# Check Redis connection
48+
redis_status = redis_client.ping()
49+
50+
return {
51+
"database": db_status,
52+
"redis": redis_status,
53+
"overall": db_status and redis_status,
54+
}

backend/scripts/prestart.sh

100644100755
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ set -e
44
set -x
55

66
# Let the DB start
7-
python app/backend_pre_start.py
7+
uv run python app/backend_pre_start.py
88

99
# Run migrations
10-
alembic upgrade head
10+
uv run alembic upgrade head
1111

1212
# Create initial data in DB
13-
python app/initial_data.py
13+
uv run python app/initial_data.py

backend/scripts/tests-start.sh

100644100755
Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,65 @@
1-
#! /usr/bin/env bash
1+
#!/bin/bash
2+
3+
# Development test setup script for backend project
4+
# This script starts necessary services for testing
5+
26
set -e
3-
set -x
47

5-
python app/tests_pre_start.py
8+
echo "🚀 Starting development environment for testing..."
9+
10+
# Check if virtual environment is active
11+
if [[ "$VIRTUAL_ENV" == "" ]]; then
12+
echo "❌ ERROR: Virtual environment not activated!"
13+
echo "Please run: source .venv/bin/activate"
14+
exit 1
15+
fi
16+
17+
# Check if uv is installed
18+
if ! command -v uv &> /dev/null; then
19+
echo "❌ ERROR: uv is not installed!"
20+
echo "Please install uv: pip install uv"
21+
exit 1
22+
fi
23+
24+
echo "✅ Environment checks passed"
25+
26+
# Sync dependencies if needed
27+
echo "📦 Syncing dependencies..."
28+
uv sync
29+
30+
# Validate critical versions
31+
echo "🔍 Validating critical dependency versions..."
32+
33+
# Check httpx version
34+
HTTPX_VERSION=$(uv run python -c "import httpx; print(httpx.__version__)" 2>/dev/null || echo "not installed")
35+
if [[ "$HTTPX_VERSION" == "0.24.1" ]]; then
36+
echo "✅ httpx version: $HTTPX_VERSION (correct)"
37+
else
38+
echo "❌ httpx version: $HTTPX_VERSION (expected: 0.24.1)"
39+
echo "Please run: uv sync"
40+
exit 1
41+
fi
42+
43+
# Check fastapi version
44+
FASTAPI_VERSION=$(uv run python -c "import fastapi; print(fastapi.__version__)" 2>/dev/null || echo "not installed")
45+
if [[ "$FASTAPI_VERSION" == "0.109.2" ]]; then
46+
echo "✅ fastapi version: $FASTAPI_VERSION (correct)"
47+
else
48+
echo "❌ fastapi version: $FASTAPI_VERSION (expected: 0.109.2)"
49+
echo "Please run: uv sync"
50+
exit 1
51+
fi
52+
53+
# Run pre-start script if it exists
54+
if [[ -f "app/tests_pre_start.py" ]]; then
55+
echo "🔧 Running test pre-start script..."
56+
python app/tests_pre_start.py
57+
fi
658

7-
bash scripts/test.sh "$@"
59+
echo "✅ All validations passed!"
60+
echo "🎯 Development environment ready for testing!"
61+
echo ""
62+
echo "Usage:"
63+
echo " ./scripts/run-tests.sh # Run all tests"
64+
echo " ./scripts/run-tests.sh test_file.py # Run specific test"
65+
echo " ./scripts/run-tests.sh -v # Verbose output"

backend/tests/conftest.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,22 @@
77
from app.core.config import settings
88
from app.core.db import engine, init_db
99
from app.main import app
10-
from app.models import Item, User
10+
from app.models import Item, User, Image, ImageVariant, ImageProcessingJob
1111
from tests.utils.user import authentication_token_from_email
1212
from tests.utils.utils import get_superuser_token_headers
1313

1414

15-
@pytest.fixture(scope="session", autouse=True)
15+
@pytest.fixture(scope="session")
1616
def db() -> Generator[Session, None, None]:
1717
with Session(engine) as session:
1818
init_db(session)
1919
yield session
20+
statement = delete(ImageProcessingJob)
21+
session.execute(statement)
22+
statement = delete(ImageVariant)
23+
session.execute(statement)
24+
statement = delete(Image)
25+
session.execute(statement)
2026
statement = delete(Item)
2127
session.execute(statement)
2228
statement = delete(User)
@@ -25,7 +31,7 @@ def db() -> Generator[Session, None, None]:
2531

2632

2733
@pytest.fixture(scope="module")
28-
def client() -> Generator[TestClient, None, None]:
34+
def client(db: Session) -> Generator[TestClient, None, None]:
2935
with TestClient(app) as c:
3036
yield c
3137

0 commit comments

Comments
 (0)