-
Notifications
You must be signed in to change notification settings - Fork 0
Test infra: switch test DB to local Docker Postgres #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
6d02aab
20ebf6a
f7271d0
4bac03d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| services: | ||
| db: | ||
| image: postgres:16 | ||
| environment: | ||
| POSTGRES_DB: homik_test | ||
| POSTGRES_USER: postgres | ||
| POSTGRES_PASSWORD: postgres | ||
| ports: | ||
| - "5432:5432" | ||
| volumes: | ||
| - postgres_data:/var/lib/postgresql/data | ||
| healthcheck: | ||
| test: ["CMD-SHELL", "pg_isready -U postgres -q"] | ||
| interval: 5s | ||
| timeout: 5s | ||
| retries: 5 | ||
|
|
||
| volumes: | ||
| postgres_data: | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,7 +6,15 @@ | |
| # Assumptions: | ||
| # - Run from the project root (not from backend/) | ||
| # - `uv` is installed and on PATH (https://docs.astral.sh/uv/) | ||
| # - TEST_DATABASE_URL is set in backend/.env | ||
| # - `docker` is installed and the Docker daemon is running | ||
| # - TEST_DATABASE_URL in backend/.env points at the local Docker container | ||
| # (postgresql+asyncpg://postgres:postgres@localhost:5432/homik_test) | ||
| # | ||
| # Database setup: | ||
| # Tests run against a local Postgres container defined in docker-compose.yml. | ||
| # This script starts the container automatically if it isn't already running. | ||
| # To start it manually: docker compose up -d db | ||
| # To stop it: docker compose down | ||
| # | ||
| # Usage: chmod +x scripts/run-tests.sh && ./scripts/run-tests.sh | ||
| # Shortcut: alias run-tests='./scripts/run-tests.sh' | ||
|
|
@@ -31,6 +39,10 @@ if [ ! -d "$BACKEND_DIR" ]; then | |
| exit 1 | ||
| fi | ||
|
|
||
| echo "Starting local test database..." | ||
| docker compose up -d db | ||
| until docker compose exec -T db pg_isready -U postgres -q; do sleep 1; done | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice pattern: This is the right way to wait for Postgres — The One minor enhancement if you want to prevent infinite hangs: add a timeout counter: WAIT_TIME=0
until docker compose exec -T db pg_isready -U postgres -q; do
sleep 1
WAIT_TIME=$((WAIT_TIME + 1))
if [ $WAIT_TIME -gt 30 ]; then
echo "Error: Database failed to start after 30 seconds"
exit 1
fi
doneBut for local dev, the current infinite loop is fine — if Docker/Postgres is broken, you want to know immediately anyway.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. decided to skip, easy to spot when testing locally |
||
|
|
||
| echo "Test run: $TIMESTAMP" > "$OUTPUT" | ||
| (cd "$BACKEND_DIR" && uv run pytest -v 2>&1 | tee -a "../$OUTPUT") || true | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor: Port conflict consideration
The hardcoded
5432:5432mapping will conflict if the developer already has a Postgres instance running locally on the default port.This is acceptable for a simple local dev setup, but you could consider:
"5433:5432"to avoid conflictsrun-tests.shto fail early with a helpful message if the port is in useFor a portfolio project targeting simplicity, option 2 (just documenting it) is probably sufficient. The current approach is fine — just something to be aware of if you run into "port already allocated" errors.