Skip to content

Commit

Permalink
feat(docker): devcontainer image + compose service
Browse files Browse the repository at this point in the history
project config for python development

basic pytest setup
  • Loading branch information
thekaveman committed Jan 24, 2025
1 parent 67cea0c commit 36878be
Show file tree
Hide file tree
Showing 18 changed files with 223 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM cdt/ddrc:app

COPY . .

# install devcontainer requirements
RUN pip install -e .[dev,test]
37 changes: 37 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "cdt/ddrc",
"dockerComposeFile": ["../compose.yml"],
"service": "dev",
"runServices": ["dev"],
"workspaceFolder": "/cdt/app",
"postStartCommand": ["/bin/bash", "bin/reset_db.sh"],
"postAttachCommand": ["/bin/bash", ".devcontainer/postAttach.sh"],
"customizations": {
"vscode": {
// Set *default* container specific settings.json values on container create.
"settings": {
"terminal.integrated.defaultProfile.linux": "bash",
"terminal.integrated.profiles.linux": {
"bash": {
"path": "/bin/bash"
}
}
},
// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"batisteo.vscode-django",
"bpruitt-goddard.mermaid-markdown-syntax-highlighting",
"DavidAnson.vscode-markdownlint",
"eamodio.gitlens",
"esbenp.prettier-vscode",
"mhutchie.git-graph",
"monosans.djlint",
"ms-python.python",
"ms-python.black-formatter",
"ms-python.flake8",
"qwtel.sqlite-viewer",
"tamasfe.even-better-toml"
]
}
}
}
6 changes: 6 additions & 0 deletions .devcontainer/postAttach.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash
set -eux

# initialize pre-commit
git config --global --add safe.directory /$USER/app
pre-commit install --install-hooks --overwrite
4 changes: 3 additions & 1 deletion .env.sample
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
DJANGO_LOCAL_PORT=8000

# Django superuser account for backend admin access
DJANGO_SUPERUSER_USERNAME=ddrc-admin
DJANGO_SUPERUSER_EMAIL=[email protected]
DJANGO_SUPERUSER_PASSWORD=superuser12345!

# Django storage
DJANGO_DB_FILE=django.db
DJANGO_DB_RESET=true
DJANGO_STORAGE_DIR=.
DJANGO_DB_FILE=django.db
2 changes: 2 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[flake8]
max-line-length = 127
26 changes: 26 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
ci:
autofix_commit_msg: "chore(pre-commit): autofix run"
autoupdate_commit_msg: "chore(pre-commit): autoupdate hooks"

default_install_hook_types:
- pre-commit
- commit-msg

repos:
- repo: https://github.com/compilerla/conventional-pre-commit
rev: v4.0.0
hooks:
- id: conventional-pre-commit
stages: [commit-msg]

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
hooks:
- id: check-added-large-files
- id: check-merge-conflict
- id: check-yaml
args: [--unsafe]
- id: end-of-file-fixer
- id: fix-byte-order-marker
- id: mixed-line-ending
- id: trailing-whitespace
32 changes: 32 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Django: DDRC Client",
"type": "debugpy",
"request": "launch",
"program": "${workspaceFolder}/manage.py",
"args": ["runserver", "--insecure", "0.0.0.0:8000"],
"django": true,
"env": {
"DJANGO_DEBUG": "true",
"PYTHONWARNINGS": "default"
}
},
{
"name": "Django: DDRC Client, Debug=False",
"type": "debugpy",
"request": "launch",
"program": "${workspaceFolder}/manage.py",
"args": ["runserver", "--insecure", "0.0.0.0:8000"],
"django": true,
"env": {
"DJANGO_DEBUG": "false",
"DJANGO_STATICFILES_STORAGE": "django.contrib.staticfiles.storage.StaticFilesStorage"
}
}
]
}
33 changes: 33 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"[django-html][html]": {
"editor.defaultFormatter": "monosans.djlint",
"djlint.enableLinting": true,
"djlint.profile": "django"
},
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"files.associations": {
".pages": "yaml"
},
"files.encoding": "utf8",
"files.eol": "\n",
"files.insertFinalNewline": true,
"files.trimFinalNewlines": true,
"files.trimTrailingWhitespace": true,
"[markdown]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter"
},
"[toml]": {
"editor.defaultFormatter": "tamasfe.even-better-toml"
},
"python.languageServer": "Pylance",
"python.testing.pytestArgs": ["tests/pytest"],
"python.testing.pytestEnabled": true,
"python.testing.unittestEnabled": false,
"workbench.editorAssociations": {
"*.db": "sqlite-viewer.option"
}
}
2 changes: 2 additions & 0 deletions bin/build.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
set -eux

docker compose build app

docker compose build dev
24 changes: 24 additions & 0 deletions bin/reset_db.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash
set -ex

# whether to reset database file, defaults to true
DB_RESET="${DJANGO_DB_RESET:-true}"

if [[ $DB_RESET = true ]]; then
# construct the path to the database file from environment or default
DB_DIR="${DJANGO_STORAGE_DIR:-.}"
DB_FILE="${DJANGO_DB_FILE:-django.db}"
DB_PATH="${DB_DIR}/${DB_FILE}"

rm -f "${DB_PATH}"

# run database migrations and other initialization
bin/init.sh

# create a superuser account for backend admin access
# set username, email, and password using environment variables
# DJANGO_SUPERUSER_USERNAME, DJANGO_SUPERUSER_EMAIL, and DJANGO_SUPERUSER_PASSWORD
python manage.py createsuperuser --no-input
else
echo "DB_RESET is false, skipping"
fi
13 changes: 13 additions & 0 deletions compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,16 @@ services:
env_file: .env
ports:
- "${DJANGO_LOCAL_PORT:-8000}:8000"

dev:
build:
context: .
dockerfile: .devcontainer/Dockerfile
image: cdt/ddrc:dev
env_file: .env
# https://code.visualstudio.com/docs/remote/create-dev-container#_use-docker-compose
entrypoint: sleep infinity
ports:
- "${DJANGO_LOCAL_PORT:-8000}:8000"
volumes:
- ./:/cdt/app
18 changes: 18 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,31 @@ requires-python = ">=3.12"
maintainers = [{ name = "Compiler LLC", email = "[email protected]" }]
dependencies = ["Django==5.1.4", "gunicorn==23.0.0"]

[project.optional-dependencies]
dev = ["black", "djlint", "flake8", "pre-commit", "setuptools_scm>=8"]
test = ["coverage", "pytest", "pytest-django", "pytest-mock", "pytest-socket"]

[project.urls]
Code = "https://github.com/compilerla/ddrc"
Issues = "https://github.com/compilerla/ddrc/issues"

[build-system]
requires = ["setuptools>=75", "setuptools_scm>=8"]
build-backend = "setuptools.build_meta"

[tool.black]
line-length = 127
target-version = ["py312"]
include = '\.pyi?$'

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

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

[tool.setuptools.packages.find]
include = ["web*"]
namespaces = false
Expand Down
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 web.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 app/static/coverage

coverage html --directory app/static/coverage

0 comments on commit 36878be

Please sign in to comment.