diff --git a/.devcontainer/aliases-devcontainer b/.devcontainer/aliases-devcontainer index 88f8c07..ae695b1 100644 --- a/.devcontainer/aliases-devcontainer +++ b/.devcontainer/aliases-devcontainer @@ -2,4 +2,4 @@ alias format='scripts/format.sh' alias makemigrations='scripts/makemigrations.sh' alias migrate='scripts/migrate.sh' alias shell='poetry run python src/helpers/shell.py' -alias test='pytest src' +alias test='scripts/test.sh' diff --git a/.github/workflows/python-app.yml b/.github/workflows/python-app.yml index 34de4ec..c6d2241 100644 --- a/.github/workflows/python-app.yml +++ b/.github/workflows/python-app.yml @@ -42,18 +42,17 @@ jobs: env: POSTGRES_USER: dev POSTGRES_PASSWORD: dev - POSTGRES_DB: dev + POSTGRES_DB: test ports: - 5432:5432 options: >- - --health-cmd "pg_isready -U dev -d dev" + --health-cmd "pg_isready -U dev -d test" --health-interval 10s --health-timeout 5s --health-retries 5 env: PROJECT_NAME: python-template - DATABASE_URL: postgresql://dev:dev@localhost:5432/dev - ASYNC_DATABASE_URL: postgresql+asyncpg://dev:dev@localhost:5432/dev + ASYNC_DATABASE_URL: postgresql+asyncpg://dev:dev@localhost:5432/test DATABASE_POOL_PRE_PING: True DATABASE_POOL_SIZE: 5 DATABASE_POOL_RECYCLE: 3600 diff --git a/scripts/test.sh b/scripts/test.sh index 65016d5..b97faca 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -1,3 +1,21 @@ #!/bin/bash -poetry run pytest src +set -e +pwd +# Ensure test database schema is up-to-date + +TEST_DB_NAME="test" +TEST_DB_CONN_STRING="dev:dev@postgres:5432" + +setup_test_database() +{ + echo "Setting up test database" + DATABASE_URL="postgresql://$TEST_DB_CONN_STRING" + psql $DATABASE_URL -c "DROP DATABASE IF EXISTS $TEST_DB_NAME" + psql $DATABASE_URL -c "CREATE DATABASE $TEST_DB_NAME" + psql $DATABASE_URL -c "GRANT ALL PRIVILEGES ON DATABASE $TEST_DB_NAME TO dev" + DATABASE_URL="$DATABASE_URL/$TEST_DB_NAME" ./scripts/migrate.sh +} + +setup_test_database +ASYNC_DATABASE_URL=postgresql+asyncpg://$TEST_DB_CONN_STRING/$TEST_DB_NAME poetry run pytest src