Skip to content

Commit 7d12350

Browse files
JelteFsimi
andauthored
Add basic tests (#99)
Add initial testing infrastructure. Close #98 --------- Co-authored-by: Josef Šimánek <[email protected]>
1 parent fdf5d92 commit 7d12350

File tree

4 files changed

+141
-0
lines changed

4 files changed

+141
-0
lines changed

.github/workflows/ci.yaml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,37 @@ jobs:
3333

3434
- name: Run djhtml
3535
run: djhtml pgcommitfest/*/templates/*.html pgcommitfest/*/templates/*.inc --tabwidth=1 --check
36+
37+
test:
38+
runs-on: ubuntu-24.04
39+
name: "Django Tests"
40+
41+
services:
42+
postgres:
43+
image: postgres:18
44+
env:
45+
POSTGRES_USER: postgres
46+
POSTGRES_PASSWORD: postgres
47+
options: >-
48+
--health-cmd pg_isready
49+
--health-interval 10s
50+
--health-timeout 5s
51+
--health-retries 5
52+
ports:
53+
- 5432:5432
54+
55+
steps:
56+
- name: Checkout code
57+
uses: actions/checkout@v4
58+
59+
- name: Set up Python with uv
60+
uses: astral-sh/setup-uv@v4
61+
62+
- name: Create CI settings
63+
run: cp pgcommitfest/local_settings_example.py pgcommitfest/local_settings.py
64+
65+
- name: Install dependencies
66+
run: uv sync --extra dev
67+
68+
- name: Run tests
69+
run: uv run pytest --create-db
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Tests for the commitfest application
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
from django.test import override_settings
2+
3+
import json
4+
from datetime import date
5+
6+
import pytest
7+
8+
from pgcommitfest.commitfest.models import CommitFest
9+
10+
pytestmark = pytest.mark.django_db
11+
12+
13+
@pytest.fixture
14+
def commitfests():
15+
"""Create test commitfests with various statuses."""
16+
return {
17+
"open": CommitFest.objects.create(
18+
name="2025-01",
19+
status=CommitFest.STATUS_OPEN,
20+
startdate=date(2025, 1, 1),
21+
enddate=date(2025, 1, 31),
22+
draft=False,
23+
),
24+
"in_progress": CommitFest.objects.create(
25+
name="2024-11",
26+
status=CommitFest.STATUS_INPROGRESS,
27+
startdate=date(2024, 11, 1),
28+
enddate=date(2024, 11, 30),
29+
draft=False,
30+
),
31+
"recent_previous": CommitFest.objects.create(
32+
name="2024-09",
33+
status=CommitFest.STATUS_CLOSED,
34+
startdate=date(2024, 9, 1),
35+
enddate=date(2024, 9, 30),
36+
draft=False,
37+
),
38+
"old_previous": CommitFest.objects.create(
39+
name="2024-07",
40+
status=CommitFest.STATUS_CLOSED,
41+
startdate=date(2024, 7, 1),
42+
enddate=date(2024, 7, 31),
43+
draft=False,
44+
),
45+
"draft": CommitFest.objects.create(
46+
name="2025-03-draft",
47+
status=CommitFest.STATUS_OPEN,
48+
startdate=date(2025, 3, 1),
49+
enddate=date(2025, 3, 31),
50+
draft=True,
51+
),
52+
}
53+
54+
55+
def test_needs_ci_endpoint(client, commitfests):
56+
"""Test the /api/v1/commitfests/needs_ci endpoint returns correct data."""
57+
with override_settings(AUTO_CREATE_COMMITFESTS=False):
58+
response = client.get("/api/v1/commitfests/needs_ci")
59+
60+
# Check response metadata
61+
assert response.status_code == 200
62+
assert response["Content-Type"] == "application/json"
63+
assert response["Access-Control-Allow-Origin"] == "*"
64+
65+
# Parse and compare response
66+
data = json.loads(response.content)
67+
68+
expected = {
69+
"commitfests": {
70+
"open": {
71+
"id": commitfests["open"].id,
72+
"name": "2025-01",
73+
"status": "Open",
74+
"startdate": "2025-01-01",
75+
"enddate": "2025-01-31",
76+
},
77+
"in_progress": {
78+
"id": commitfests["in_progress"].id,
79+
"name": "2024-11",
80+
"status": "In Progress",
81+
"startdate": "2024-11-01",
82+
"enddate": "2024-11-30",
83+
},
84+
"draft": {
85+
"id": commitfests["draft"].id,
86+
"name": "2025-03-draft",
87+
"status": "Open",
88+
"startdate": "2025-03-01",
89+
"enddate": "2025-03-31",
90+
},
91+
}
92+
}
93+
94+
assert data == expected

pyproject.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ dev = [
1818
"pycodestyle",
1919
"ruff",
2020
"djhtml",
21+
"pytest",
22+
"pytest-django",
2123
]
2224

2325
[tool.setuptools.packages.find]
@@ -47,3 +49,13 @@ section-order = [
4749
[tool.ruff.lint.isort.sections]
4850
# Group all Django imports into a separate section.
4951
django = ["django"]
52+
53+
[tool.pytest.ini_options]
54+
DJANGO_SETTINGS_MODULE = "pgcommitfest.settings"
55+
python_files = ["tests.py", "test_*.py", "*_tests.py"]
56+
testpaths = ["pgcommitfest"]
57+
addopts = [
58+
"--reuse-db",
59+
"--strict-markers",
60+
"-vv",
61+
]

0 commit comments

Comments
 (0)