Skip to content

Commit 8a70f2e

Browse files
authored
Feat: initial testing setup (#41)
2 parents 1b84ffe + 2a5a061 commit 8a70f2e

File tree

14 files changed

+113
-1
lines changed

14 files changed

+113
-1
lines changed

.devcontainer/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ RUN python -m pip install --upgrade pip
2626
COPY . .
2727

2828
# install devcontainer requirements
29-
RUN pip install -e .[dev]
29+
RUN pip install -e .[dev,test]
3030

3131
# install docs requirements
3232
RUN pip install --no-cache-dir -r docs/requirements.txt

.github/workflows/.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.12

.github/workflows/tests-pytest.yml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
name: Pytest
2+
3+
on: [push, pull_request, workflow_call]
4+
5+
jobs:
6+
pytest:
7+
runs-on: ubuntu-latest
8+
permissions:
9+
# Gives the action the necessary permissions for publishing new
10+
# comments in pull requests.
11+
pull-requests: write
12+
# Gives the action the necessary permissions for pushing data to the
13+
# python-coverage-comment-action branch, and for editing existing
14+
# comments (to avoid publishing multiple comments in the same PR)
15+
contents: write
16+
steps:
17+
- name: Check out code
18+
uses: actions/checkout@v4
19+
20+
- name: Install system packages
21+
run: |
22+
sudo apt-get update -y
23+
sudo apt-get install -y gettext
24+
25+
- uses: actions/setup-python@v5
26+
with:
27+
python-version-file: .github/workflows/.python-version
28+
cache: pip
29+
cache-dependency-path: "**/pyproject.toml"
30+
31+
- name: Install Python dependencies
32+
run: pip install -e .[test]
33+
34+
- name: Run setup
35+
run: ./bin/init.sh
36+
37+
- name: Run tests
38+
run: ./tests/pytest/run.sh
39+
40+
- name: Upload coverage report
41+
uses: actions/upload-artifact@v4
42+
with:
43+
name: coverage-report
44+
path: pems/static/coverage
45+
46+
- name: Coverage comment
47+
uses: py-cov-action/python-coverage-comment-action@v3
48+
with:
49+
GITHUB_TOKEN: ${{ github.token }}
50+
MINIMUM_GREEN: 90
51+
MINIMUM_ORANGE: 80

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
"editor.defaultFormatter": "ms-python.black-formatter"
2222
},
2323
"python.languageServer": "Pylance",
24+
"python.testing.pytestArgs": ["tests/pytest"],
25+
"python.testing.pytestEnabled": true,
26+
"python.testing.unittestEnabled": false,
2427
"workbench.editorAssociations": {
2528
"*.db": "sqlite-viewer.option"
2629
}

pems/core/__init__.py

Whitespace-only changes.

pems/core/middleware.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
The core application: middleware definitions for request/response cycle.
3+
"""
4+
5+
from django.http import HttpResponse
6+
7+
HEALTHCHECK_PATH = "/healthcheck"
8+
9+
10+
class Healthcheck:
11+
"""Middleware intercepts and accepts /healthcheck requests."""
12+
13+
def __init__(self, get_response):
14+
self.get_response = get_response
15+
16+
def __call__(self, request):
17+
if request.path == HEALTHCHECK_PATH:
18+
return HttpResponse("Healthy", content_type="text/plain")
19+
return self.get_response(request)

pems/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def _filter_empty(ls):
3636
MIDDLEWARE = [
3737
"django.middleware.security.SecurityMiddleware",
3838
"django.contrib.sessions.middleware.SessionMiddleware",
39+
"pems.core.middleware.Healthcheck",
3940
"django.middleware.common.CommonMiddleware",
4041
"django.middleware.csrf.CsrfViewMiddleware",
4142
"django.contrib.auth.middleware.AuthenticationMiddleware",

pyproject.toml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ dev = [
2020
"pre-commit"
2121
]
2222

23+
test = [
24+
"coverage",
25+
"pytest",
26+
"pytest-django",
27+
"pytest-mock",
28+
"pytest-socket",
29+
]
30+
2331
[project.urls]
2432
Code = "https://github.com/compilerla/pems"
2533
Homepage = "https://compilerla.github.io/pems/"
@@ -34,6 +42,14 @@ line-length = 127
3442
target-version = ['py312']
3543
include = '\.pyi?$'
3644

45+
[tool.coverage.run]
46+
branch = true
47+
relative_files = true
48+
source = ["pems"]
49+
50+
[tool.pytest.ini_options]
51+
DJANGO_SETTINGS_MODULE = "pems.settings"
52+
3753
[tool.setuptools.packages.find]
3854
include = ["pems*"]
3955
namespaces = false

tests/__init__.py

Whitespace-only changes.

tests/pytest/__init__.py

Whitespace-only changes.

tests/pytest/conftest.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from pytest_socket import disable_socket
2+
3+
4+
def pytest_runtest_setup():
5+
disable_socket()

tests/pytest/core/__init__.py

Whitespace-only changes.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from pems.core.middleware import HEALTHCHECK_PATH
2+
3+
4+
def test_healthcheck(client):
5+
response = client.get(HEALTHCHECK_PATH)
6+
assert response.status_code == 200

tests/pytest/run.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env bash
2+
set -eu
3+
4+
# run normal pytests
5+
coverage run -m pytest
6+
7+
# clean out old coverage results
8+
rm -rf pems/static/coverage
9+
10+
coverage html --directory pems/static/coverage

0 commit comments

Comments
 (0)