Skip to content

Commit

Permalink
chore: add pytest and github workflows (#1)
Browse files Browse the repository at this point in the history
Co-authored-by: Sven Groot <[email protected]>
  • Loading branch information
svengt and Sven Groot authored Dec 14, 2024
1 parent 52c1518 commit 192909f
Show file tree
Hide file tree
Showing 10 changed files with 227 additions and 1 deletion.
58 changes: 58 additions & 0 deletions .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: Pytest

on: [push, pull_request]

env:
UV_SYSTEM_PYTHON: 1

jobs:
test:
name: Test on Python ${{ matrix.python-version }} and Django ${{ matrix.django-version }}
runs-on: ubuntu-latest
outputs:
failing-test: ${{ steps.test.outputs.fail }}
strategy:
matrix:
python-version: [
"3.8",
"3.9",
"3.10",
"3.11",
"3.12",
"3.13",
]
django-version: [
"django>=4.2,<5.0",
"django>=5.0,<5.1",
"django>=5.1,<5.2",
]
exclude:
- python-version: "3.8"
django-version: "django>=5.0,<5.1"
- python-version: "3.8"
django-version: "django>=5.1,<5.2"
- python-version: "3.9"
django-version: "django>=5.0,<5.1"
- python-version: "3.9"
django-version: "django>=5.1,<5.2"

steps:
- uses: actions/checkout@v4

- name: Install uv
uses: astral-sh/setup-uv@v4

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: '${{ matrix.python-version }}'

- name: Install the project
run: |
uv sync --all-extras --dev
uv add '${{ matrix.django-version }}; python_version == "${{ matrix.python-version }}"'
- name: Run tests
id: test
run: |
uv run pytest
8 changes: 8 additions & 0 deletions .github/workflows/ruff.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: Ruff
on: [ push, pull_request ]
jobs:
ruff:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: astral-sh/ruff-action@v2
2 changes: 1 addition & 1 deletion .python-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.13
>=3.8
Empty file added tests/__init__.py
Empty file.
Empty file added tests/pytest/__init__.py
Empty file.
Empty file.
38 changes: 38 additions & 0 deletions tests/pytest/_testsite/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from django.conf.urls.i18n import i18n_patterns
from django.contrib import admin
from django.urls import include, path, re_path

from . import views

included_urlpatterns = [
re_path(r"^test/(\d+)/foo/(\w+)/bar/$", views.ping, name="included_test_with_args"),
]

urlpatterns = [
re_path(r"^admin/", admin.site.urls),
re_path(r"^ping/$", views.ping, name="ping"),
re_path(r"^ping/(\d+)/foo/(\w+)/bar/$", views.ping, name="ping_with_args"),
re_path(
r"^ping/(?P<pk1>\w+)/foo/(?P<pk2>\d+)/bar/$",
views.ping,
name="ping_with_kwargs",
),
re_path(
r"^ping/(\d+)/fooo?/baa?r/$", views.ping, name="ping_with_optional_character"
),
re_path(r"^ping/(\d+)/foo(?:bar)?/$", views.ping, name="ping_with_optional_group"),
re_path(
r"^ping/(\d+)/(?:/(?P<op>[a-zA-Z]+)/)?$",
views.ping,
name="ping_with_optional_kwarg",
),
re_path(r"^included/(?P<pk1>\w+)/", include(included_urlpatterns)),
path("ping/<int:year>/", views.ping, name="ping_with_path"),
path("ping/<int:year>/<int:month>/", views.ping, name="ping_with_paths"),
re_path(r"ping/(?P<year>[0-9]{4})/$", views.ping, name="ping_with_re_path"),
path("ping/<slug>/", views.ping, name="ping_with_path_without_converter"),
]

urlpatterns += i18n_patterns(
path("ping-i18n/", views.ping, name="ping_i18n"),
)
2 changes: 2 additions & 0 deletions tests/pytest/_testsite/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def ping(request):
return "pong"
41 changes: 41 additions & 0 deletions tests/pytest/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
SECRET_KEY = "test"

ROOT_URLCONF = "tests.pytest._testsite.urls"

USE_TZ = True
LANGUAGE_CODE = "en"
LANGUAGES = [("en", "English")]

INSTALLED_APPS = [
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.humanize",
"django.contrib.sessions",
"django.contrib.sitemaps",
"django.contrib.messages",
"django.contrib.staticfiles",
"ts_routes",
"django.contrib.admin",
]

TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"APP_DIRS": True,
},
]

TS_ROUTES_INCLUSION_LIST = [
"included_test_with_args",
"ping",
"ping_with_args",
"ping_with_kwargs",
"ping_with_optional_character",
"ping_with_optional_group",
"ping_with_optional_kwarg",
"ping_with_path",
"ping_with_paths",
"ping_with_re_path",
"ping_with_path_without_converter",
"ping_i18n",
]
79 changes: 79 additions & 0 deletions tests/pytest/test_commands_dump_routes_resolver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import tempfile
from os import path

from django.core.management import call_command
from django.template.loader import render_to_string
from django.test.testcases import SimpleTestCase, override_settings
from django.utils import translation

from ts_routes.serializers import url_patterns_serializer


class TestDumpRoutesResolverCommand(SimpleTestCase):
@override_settings(LANGUAGES=[("en", "English")])
def test_can_export_the_routes_and_resolver_module_into_a_specific_directory(self):
export_dir = tempfile.mkdtemp()
call_command("dump_routes_resolver", output_dir=export_dir)

index_filename = path.join(export_dir, "index.ts")
en_filename = path.join(export_dir, "en.ts")
nl_filename = path.join(export_dir, "nl.ts")

assert path.exists(index_filename)
assert path.exists(en_filename)
assert not path.exists(nl_filename)

result = render_to_string(
"ts_routes/_dump/resolver.ts", {"routes": url_patterns_serializer.to_json()}
)

with open(index_filename) as fd:
assert fd.read() == result

with open(en_filename) as fd:
assert fd.read() == result

@override_settings(LANGUAGES=[("en", "English"), ("nl", "Dutch")])
def test_can_export_the_routes_and_resolver_module_into_a_specific_directory_languages(
self,
):
export_dir = tempfile.mkdtemp()
call_command("dump_routes_resolver", output_dir=export_dir)

index_filename = path.join(export_dir, "index.ts")
en_filename = path.join(export_dir, "en.ts")
nl_filename = path.join(export_dir, "nl.ts")

assert path.exists(index_filename)
assert path.exists(en_filename)
assert path.exists(nl_filename)

with translation.override("en"):
result = render_to_string(
"ts_routes/_dump/resolver.ts",
{"routes": url_patterns_serializer.to_json()},
)

with open(index_filename) as fd:
assert fd.read() == result

with open(en_filename) as fd:
assert fd.read() == result

with open(nl_filename) as fd:
assert fd.read() != result, result

with translation.override("nl"):
result = render_to_string(
"ts_routes/_dump/resolver.ts",
{"routes": url_patterns_serializer.to_json()},
)

with open(index_filename) as fd:
assert fd.read() != result

with open(en_filename) as fd:
assert fd.read() != result

with open(nl_filename) as fd:
assert fd.read() == result

0 comments on commit 192909f

Please sign in to comment.