Skip to content

Feat: initial testing setup #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ RUN python -m pip install --upgrade pip
COPY . .

# install devcontainer requirements
RUN pip install -e .[dev]
RUN pip install -e .[dev,test]

# install docs requirements
RUN pip install --no-cache-dir -r docs/requirements.txt
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/.python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.12
51 changes: 51 additions & 0 deletions .github/workflows/tests-pytest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: Pytest

on: [push, pull_request, workflow_call]

jobs:
pytest:
runs-on: ubuntu-latest
permissions:
# Gives the action the necessary permissions for publishing new
# comments in pull requests.
pull-requests: write
# Gives the action the necessary permissions for pushing data to the
# python-coverage-comment-action branch, and for editing existing
# comments (to avoid publishing multiple comments in the same PR)
contents: write
steps:
- name: Check out code
uses: actions/checkout@v4

- name: Install system packages
run: |
sudo apt-get update -y
sudo apt-get install -y gettext

- uses: actions/setup-python@v5
with:
python-version-file: .github/workflows/.python-version
cache: pip
cache-dependency-path: "**/pyproject.toml"

- name: Install Python dependencies
run: pip install -e .[test]

- name: Run setup
run: ./bin/init.sh

- name: Run tests
run: ./tests/pytest/run.sh

- name: Upload coverage report
uses: actions/upload-artifact@v4
with:
name: coverage-report
path: pems/static/coverage

- name: Coverage comment
uses: py-cov-action/python-coverage-comment-action@v3
with:
GITHUB_TOKEN: ${{ github.token }}
MINIMUM_GREEN: 90
MINIMUM_ORANGE: 80
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
"editor.defaultFormatter": "ms-python.black-formatter"
},
"python.languageServer": "Pylance",
"python.testing.pytestArgs": ["tests/pytest"],
"python.testing.pytestEnabled": true,
"python.testing.unittestEnabled": false,
"workbench.editorAssociations": {
"*.db": "sqlite-viewer.option"
}
Expand Down
Empty file added pems/core/__init__.py
Empty file.
19 changes: 19 additions & 0 deletions pems/core/middleware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""
The core application: middleware definitions for request/response cycle.
"""

from django.http import HttpResponse

HEALTHCHECK_PATH = "/healthcheck"


class Healthcheck:
"""Middleware intercepts and accepts /healthcheck requests."""

def __init__(self, get_response):
self.get_response = get_response

def __call__(self, request):
if request.path == HEALTHCHECK_PATH:
return HttpResponse("Healthy", content_type="text/plain")
return self.get_response(request)
1 change: 1 addition & 0 deletions pems/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def _filter_empty(ls):
MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"pems.core.middleware.Healthcheck",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
Expand Down
16 changes: 16 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ dev = [
"pre-commit"
]

test = [
"coverage",
"pytest",
"pytest-django",
"pytest-mock",
"pytest-socket",
]

[project.urls]
Code = "https://github.com/compilerla/pems"
Homepage = "https://compilerla.github.io/pems/"
Expand All @@ -34,6 +42,14 @@ line-length = 127
target-version = ['py312']
include = '\.pyi?$'

[tool.coverage.run]
branch = true
relative_files = true
source = ["pems"]

[tool.pytest.ini_options]
DJANGO_SETTINGS_MODULE = "pems.settings"

[tool.setuptools.packages.find]
include = ["pems*"]
namespaces = false
Empty file added tests/__init__.py
Empty file.
Empty file added tests/pytest/__init__.py
Empty file.
5 changes: 5 additions & 0 deletions tests/pytest/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from pytest_socket import disable_socket


def pytest_runtest_setup():
disable_socket()
Empty file added tests/pytest/core/__init__.py
Empty file.
6 changes: 6 additions & 0 deletions tests/pytest/core/test_middleware_healthcheck.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from pems.core.middleware import HEALTHCHECK_PATH


def test_healthcheck(client):
response = client.get(HEALTHCHECK_PATH)
assert response.status_code == 200
10 changes: 10 additions & 0 deletions tests/pytest/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash
set -eu

# run normal pytests
coverage run -m pytest

# clean out old coverage results
rm -rf pems/static/coverage

coverage html --directory pems/static/coverage
Loading