Skip to content

Commit

Permalink
refactor(routes): move logic from context processor into class
Browse files Browse the repository at this point in the history
add tests and improve clarity of the condition
  • Loading branch information
thekaveman committed Aug 19, 2024
1 parent c921fd0 commit 99fce51
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
12 changes: 2 additions & 10 deletions benefits/core/context_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from django.conf import settings

from benefits.routes import Routes, routes as app_routes
from benefits.routes import routes as app_routes

from . import models, session

Expand Down Expand Up @@ -99,12 +99,4 @@ def origin(request):
def routes(request):
"""Context processor adds information about each application route to the context."""

# get a dict[str,str] mapping property name --> value
# for each @property in the Routes collection
routes_dict = dict(
(prop, str(getattr(app_routes, prop)))
for prop in dir(app_routes)
if prop not in dir(Routes) or isinstance(getattr(Routes, prop), property)
)

return {"routes": routes_dict}
return {"routes": app_routes.to_dict()}
4 changes: 4 additions & 0 deletions benefits/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ def IN_PERSON_ENROLLMENT(self):
"""In-person (e.g. agency assisted) enrollment"""
return "in_person:enrollment"

def to_dict(self) -> dict[str, str]:
"""Get a mapping of property name --> value for each `@property` in the Routes collection."""
return {prop: str(getattr(self, prop)) for prop in dir(Routes) if isinstance(getattr(Routes, prop), property)}

@staticmethod
def name(route: str) -> str:
"""Returns just the name portion of the app:name structure of the route."""
Expand Down
9 changes: 5 additions & 4 deletions tests/pytest/core/test_context_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ def test_enrollment_expiration(app_request, model_EnrollmentFlow_supports_expira

@pytest.mark.django_db
def test_routes(app_request):
app_routes_dict = app_routes.to_dict()

context = routes(app_request)
assert "routes" in context

context_routes = context["routes"]
for prop in dir(app_routes):
if isinstance(prop, property):
assert prop in context_routes
assert context_routes[prop] == getattr(app_routes, prop)
for route_name in app_routes_dict.keys():
assert route_name in context_routes
assert context_routes[route_name] == app_routes_dict[route_name]
15 changes: 14 additions & 1 deletion tests/pytest/test_routes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from benefits.routes import routes
from benefits.routes import Routes, routes


@pytest.mark.parametrize(("route", "expected_name"), [("app:name", "name"), ("core:index", "index")])
Expand All @@ -11,3 +11,16 @@ def test_name_route(route: str, expected_name: str):
@pytest.mark.parametrize(("route"), ["app", "core"])
def test_name_not_route(route: str):
assert routes.name(route) == route


def test_to_dict():
routes_dict = routes.to_dict()

# this is in fact, a dict!
assert isinstance(routes_dict, dict)
# all keys are strings
assert all((isinstance(k, str) for k in routes_dict.keys()))
# all keys are @property on the original routes object
assert all((hasattr(routes, k) and isinstance(getattr(Routes, k), property) for k in routes_dict.keys()))
# all key values equal their corresponding attribute value
assert all((routes_dict[k] == getattr(routes, k)) for k in routes_dict.keys())

0 comments on commit 99fce51

Please sign in to comment.