Skip to content

Commit c8d6957

Browse files
committed
feat: enhance test workflow by adding database readiness checks and pg_cron extension
1 parent 9a5e01e commit c8d6957

1 file changed

Lines changed: 48 additions & 39 deletions

File tree

.github/workflows/tests.yml

Lines changed: 48 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -31,31 +31,26 @@ jobs:
3131
SESSION_SECRET_KEY: supersecretkeyforunittests
3232
AUTHENTIK_DISABLE_AUTHENTICATION: 1
3333

34-
services:
35-
postgis:
36-
image: postgis/postgis:17-3.5
37-
# don't test against latest. be explicit in version being tested to avoid breaking changes
38-
# image: postgis/postgis:latest
39-
40-
# These env vars are ONLY for the service container itself
41-
env:
42-
POSTGRES_PASSWORD: postgres
43-
POSTGRES_PORT: 5432
44-
45-
options: >-
46-
--health-cmd pg_isready
47-
--health-interval 10s
48-
--health-timeout 5s
49-
--health-retries 5
50-
51-
ports:
52-
# Maps tcp port 5432 on service container to the host
53-
- 5432:5432
54-
5534
steps:
5635
- name: Check out source repository
5736
uses: actions/checkout@v6.0.2
5837

38+
- name: Start database (PostGIS + pg_cron)
39+
run: |
40+
docker compose build db
41+
docker compose up -d db
42+
43+
- name: Wait for database readiness
44+
run: |
45+
for i in {1..60}; do
46+
if PGPASSWORD=postgres psql -h localhost -p 5432 -U postgres -d postgres -c "SELECT 1" >/dev/null 2>&1; then
47+
exit 0
48+
fi
49+
sleep 2
50+
done
51+
echo "Database did not become ready in time"
52+
exit 1
53+
5954
- name: Install uv
6055
uses: astral-sh/setup-uv@v7.3.0
6156
with:
@@ -81,10 +76,12 @@ jobs:
8176
- name: Show Alembic heads
8277
run: uv run alembic heads
8378

84-
- name: Create test database
79+
- name: Create test database and extensions
8580
run: |
86-
PGPASSWORD=postgres psql -h localhost -p 5432 -U postgres -c "CREATE DATABASE ocotilloapi_test"
81+
PGPASSWORD=postgres psql -h localhost -p 5432 -U postgres -tc "SELECT 1 FROM pg_database WHERE datname = 'ocotilloapi_test'" | grep -q 1 || \
82+
PGPASSWORD=postgres psql -h localhost -p 5432 -U postgres -c "CREATE DATABASE ocotilloapi_test"
8783
PGPASSWORD=postgres psql -h localhost -p 5432 -U postgres -d ocotilloapi_test -c "CREATE EXTENSION IF NOT EXISTS postgis"
84+
PGPASSWORD=postgres psql -h localhost -p 5432 -U postgres -d ocotilloapi_test -c "CREATE EXTENSION IF NOT EXISTS pg_cron"
8885
8986
- name: Run tests
9087
run: uv run pytest -vv --durations=20 --cov --cov-report=xml --junitxml=junit.xml --ignore=tests/transfers
@@ -95,6 +92,10 @@ jobs:
9592
report_type: test_results
9693
token: ${{ secrets.CODECOV_TOKEN }}
9794

95+
- name: Stop database
96+
if: always()
97+
run: docker compose down -v
98+
9899
bdd-tests:
99100
runs-on: ubuntu-latest
100101

@@ -116,24 +117,26 @@ jobs:
116117
AUTHENTIK_DISABLE_AUTHENTICATION: 1
117118
DROP_AND_REBUILD_DB: 1
118119

119-
services:
120-
postgis:
121-
image: postgis/postgis:17-3.5
122-
env:
123-
POSTGRES_PASSWORD: postgres
124-
POSTGRES_PORT: 5432
125-
options: >-
126-
--health-cmd pg_isready
127-
--health-interval 10s
128-
--health-timeout 5s
129-
--health-retries 5
130-
ports:
131-
- 5432:5432
132-
133120
steps:
134121
- name: Check out source repository
135122
uses: actions/checkout@v6.0.2
136123

124+
- name: Start database (PostGIS + pg_cron)
125+
run: |
126+
docker compose build db
127+
docker compose up -d db
128+
129+
- name: Wait for database readiness
130+
run: |
131+
for i in {1..60}; do
132+
if PGPASSWORD=postgres psql -h localhost -p 5432 -U postgres -d postgres -c "SELECT 1" >/dev/null 2>&1; then
133+
exit 0
134+
fi
135+
sleep 2
136+
done
137+
echo "Database did not become ready in time"
138+
exit 1
139+
137140
- name: Install uv
138141
uses: astral-sh/setup-uv@v7.3.0
139142
with:
@@ -159,10 +162,16 @@ jobs:
159162
- name: Show Alembic heads
160163
run: uv run alembic heads
161164

162-
- name: Create test database
165+
- name: Create test database and extensions
163166
run: |
164-
PGPASSWORD=postgres psql -h localhost -p 5432 -U postgres -c "CREATE DATABASE ocotilloapi_test"
167+
PGPASSWORD=postgres psql -h localhost -p 5432 -U postgres -tc "SELECT 1 FROM pg_database WHERE datname = 'ocotilloapi_test'" | grep -q 1 || \
168+
PGPASSWORD=postgres psql -h localhost -p 5432 -U postgres -c "CREATE DATABASE ocotilloapi_test"
165169
PGPASSWORD=postgres psql -h localhost -p 5432 -U postgres -d ocotilloapi_test -c "CREATE EXTENSION IF NOT EXISTS postgis"
170+
PGPASSWORD=postgres psql -h localhost -p 5432 -U postgres -d ocotilloapi_test -c "CREATE EXTENSION IF NOT EXISTS pg_cron"
166171
167172
- name: Run BDD tests
168173
run: uv run behave tests/features --tags="@backend and @production and not @skip" --no-capture
174+
175+
- name: Stop database
176+
if: always()
177+
run: docker compose down -v

0 commit comments

Comments
 (0)