Skip to content

Commit fb3b63a

Browse files
browniebrokemgaligniana
authored andcommitted
Setup CI to run tests with Postgres
1 parent deb6657 commit fb3b63a

3 files changed

Lines changed: 57 additions & 6 deletions

File tree

.github/workflows/main.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,21 @@ jobs:
2828
name: Python ${{ matrix.python-version }}
2929
runs-on: ubuntu-24.04
3030

31+
services:
32+
postgres:
33+
image: postgres:16
34+
env:
35+
POSTGRES_DB: postgres
36+
POSTGRES_USER: postgres
37+
POSTGRES_PASSWORD: postgres
38+
ports:
39+
- 5432:5432
40+
options: >-
41+
--health-cmd pg_isready
42+
--health-interval 10s
43+
--health-timeout 5s
44+
--health-retries 5
45+
3146
strategy:
3247
matrix:
3348
python-version:
@@ -37,6 +52,9 @@ jobs:
3752
- '3.13'
3853
- '3.14'
3954

55+
env:
56+
DATABASE_URL: postgres://postgres:postgres@localhost:5432/postgres
57+
4058
steps:
4159
- uses: actions/checkout@v6
4260

@@ -52,6 +70,9 @@ jobs:
5270
- name: Install dependencies
5371
run: python -m pip install --upgrade tox
5472

73+
- name: Create unaccent extension
74+
run: PGPASSWORD=postgres psql -h localhost -U postgres -d postgres -c 'CREATE EXTENSION IF NOT EXISTS unaccent;'
75+
5576
- name: Run tox targets for ${{ matrix.python-version }}
5677
run: tox run -f py$(echo ${{ matrix.python-version }} | tr -d . | cut -f 1 -d '-')
5778

pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ dev = [
4444
{ include-group = "test" },
4545
]
4646
test = [
47+
"dj-database-url>=3.1.0",
4748
"importlib-metadata<10.0",
4849
# Pytest for running the tests.
4950
"pytest==9.*",
@@ -116,6 +117,9 @@ keep_full_version = true
116117
[tool.pytest.ini_options]
117118
addopts = "--tb=short --strict-markers -ra"
118119
testpaths = [ "tests" ]
120+
markers = [
121+
"requires_postgres: marks tests as requiring a PostgreSQL database backend",
122+
]
119123
filterwarnings = [
120124
"ignore:'cgi' is deprecated:DeprecationWarning",
121125
]

tests/conftest.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import os
22

3+
import dj_database_url
34
import django
5+
import pytest
46
from django.core import management
57

68

@@ -13,19 +15,27 @@ def pytest_addoption(parser):
1315
def pytest_configure(config):
1416
from django.conf import settings
1517

16-
settings.configure(
17-
DEBUG_PROPAGATE_EXCEPTIONS=True,
18-
DEFAULT_AUTO_FIELD="django.db.models.AutoField",
19-
DATABASES={
18+
if os.getenv('DATABASE_URL'):
19+
databases = {
20+
'default': dj_database_url.config(),
21+
'secondary': dj_database_url.config(),
22+
}
23+
else:
24+
databases = {
2025
'default': {
2126
'ENGINE': 'django.db.backends.sqlite3',
2227
'NAME': ':memory:'
2328
},
2429
'secondary': {
2530
'ENGINE': 'django.db.backends.sqlite3',
2631
'NAME': ':memory:'
27-
}
28-
},
32+
},
33+
}
34+
35+
settings.configure(
36+
DEBUG_PROPAGATE_EXCEPTIONS=True,
37+
DEFAULT_AUTO_FIELD="django.db.models.AutoField",
38+
DATABASES=databases,
2939
SITE_ID=1,
3040
SECRET_KEY='not very secret in tests',
3141
USE_I18N=True,
@@ -65,6 +75,12 @@ def pytest_configure(config):
6575
),
6676
)
6777

78+
# Add django.contrib.postgres when using a PostgreSQL database
79+
if settings.DATABASES['default']['ENGINE'] == 'django.db.backends.postgresql':
80+
settings.INSTALLED_APPS += (
81+
'django.contrib.postgres',
82+
)
83+
6884
# guardian is optional
6985
try:
7086
import guardian # NOQA
@@ -91,3 +107,13 @@ def pytest_configure(config):
91107

92108
if config.getoption('--staticfiles'):
93109
management.call_command('collectstatic', verbosity=0, interactive=False)
110+
111+
112+
def pytest_collection_modifyitems(config, items):
113+
from django.conf import settings
114+
115+
if settings.DATABASES['default']['ENGINE'] != 'django.db.backends.postgresql':
116+
skip_postgres = pytest.mark.skip(reason='Requires PostgreSQL database backend')
117+
for item in items:
118+
if 'requires_postgres' in item.keywords:
119+
item.add_marker(skip_postgres)

0 commit comments

Comments
 (0)