|
| 1 | +""" |
| 2 | +h/t to this blog post for the inspiration for these tests |
| 3 | +https://adamj.eu/tech/2025/08/01/django-custom-url-converter-string/ |
| 4 | +""" |
| 5 | + |
| 6 | +import pytest |
| 7 | +from django.urls import path, reverse |
| 8 | + |
| 9 | +# Import forces registration |
| 10 | +from benefits.core.urls import TransitAgencyPathConverter # noqa: F401 |
| 11 | + |
| 12 | +urlpatterns = [ |
| 13 | + path( |
| 14 | + "<agency:agency>", |
| 15 | + lambda *args, **kwargs: None, # dummy view |
| 16 | + name="sample", |
| 17 | + ) |
| 18 | +] |
| 19 | +sample_path = urlpatterns[0] |
| 20 | + |
| 21 | + |
| 22 | +@pytest.mark.django_db |
| 23 | +class TestTransitAgencyPathConverter: |
| 24 | + def test_active_agency(self, model_TransitAgency): |
| 25 | + result = sample_path.resolve(model_TransitAgency.slug) |
| 26 | + assert result.kwargs["agency"] == model_TransitAgency |
| 27 | + |
| 28 | + result = reverse("sample", urlconf=__name__, kwargs={"agency": model_TransitAgency}) |
| 29 | + assert result == f"/{model_TransitAgency.slug}" |
| 30 | + |
| 31 | + def test_active_agency__hyphen_slug(self, model_TransitAgency): |
| 32 | + model_TransitAgency.slug = "c-s-t" |
| 33 | + model_TransitAgency.save() |
| 34 | + |
| 35 | + result = sample_path.resolve(model_TransitAgency.slug) |
| 36 | + assert result.kwargs["agency"] == model_TransitAgency |
| 37 | + |
| 38 | + result = reverse("sample", urlconf=__name__, kwargs={"agency": model_TransitAgency}) |
| 39 | + assert result == f"/{model_TransitAgency.slug}" |
| 40 | + |
| 41 | + def test_inactive_agency(self, model_TransitAgency_inactive): |
| 42 | + result = sample_path.resolve(model_TransitAgency_inactive.slug) |
| 43 | + assert result is None |
| 44 | + |
| 45 | + def test_unknown_agency(self): |
| 46 | + result = sample_path.resolve("unknown") |
| 47 | + assert result is None |
0 commit comments