This repository has been archived on 2024-07-19. You can view files and clone it, but cannot push or open issues or pull requests.
rotini/backend/script/test
Marc Cataford a737e954aa
build(deps): refactor dependency metadata to live in pyproject.toml (#40)
* build(deps): refactor dependency metadata to live in pyproject.toml

* build(test): wait once after successful status to ensure DB is ready
2023-11-16 00:36:58 -05:00

46 lines
1.1 KiB
Bash

#!/bin/bash
# Test runner script
#
# This sets up an ephemeral database for test purposes, runs migrations on it
# and triggers pytest. Regardless of outcome, the database container is cleaned
# up after the fact.
#
# This will return 0 or 1 as exit code, depending on outcome.
TEST_DB_CONTAINER="rotini-test-ephemeral-$(date +%s)"
VENV_PYTHON=.venv/bin/python
VENV_PYTEST=.venv/bin/pytest
HEALTHCHECK_SLEEP=0.5
# Log & exit.
function fail {
echo $1
exit 1
}
# Cleanup before exit (success/failure)
function cleanup {
docker rm $TEST_DB_CONTAINER -f > /dev/null || echo "Failed to clean up test database container."
}
trap cleanup EXIT
docker run \
--name $TEST_DB_CONTAINER \
-e POSTGRES_PASSWORD=test \
-p 5431:5432 \
-d \
postgres:15.4
until [ -n "$(docker exec $TEST_DB_CONTAINER pg_isready | grep accepting)" ]; do
echo "Waiting for DB to come alive..."
sleep $HEALTHCHECK_SLEEP
done;
sleep $HEALTHCHECK_SLEEP
ROTINI_TEST=1 PYTHONPATH=rotini $VENV_PYTHON rotini/migrations/migrate.py up || fail "Migrations failed."
ROTINI_TEST=1 $VENV_PYTEST . -vv -s || fail "Test run failed."