Skip to content

Commit

Permalink
Move unittest filtering to the Form.dict() function
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisLovering committed Jul 9, 2024
1 parent 03afb94 commit ae2d6cc
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 26 deletions.
16 changes: 9 additions & 7 deletions backend/models/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,19 @@ def validate_role(cls, values: dict[str, t.Any]) -> dict[str, t.Any]:
def dict(self, admin: bool = True, **kwargs) -> dict[str, t.Any]: # noqa: FBT001, FBT002
"""Wrapper for original function to exclude private data for public access."""
data = super().dict(**kwargs)
if admin:
return data

returned_data = {}

if not admin:
for field in PUBLIC_FIELDS:
fetch_field = "_id" if field == "id" and kwargs.get("by_alias") else field

returned_data[field] = data[fetch_field]
else:
returned_data = data
for field in PUBLIC_FIELDS:
fetch_field = "_id" if field == "id" and kwargs.get("by_alias") else field
returned_data[field] = data[fetch_field]

# Replace the unittest data section of code questions with the number of test cases.
for question in returned_data["questions"]:
if question["type"] == "code" and question["data"]["unittests"] is not None:
question["data"]["unittests"]["tests"] = len(question["data"]["unittests"]["tests"])
return returned_data


Expand Down
7 changes: 1 addition & 6 deletions backend/routes/forms/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from backend.models import Form
from backend.route import Route
from backend.routes.forms.discover import AUTH_FORM
from backend.routes.forms.unittesting import filter_unittests
from backend.validation import ErrorMessage, OkayResponse, api

PUBLIC_FORM_FEATURES = (constants.FormFeatures.OPEN, constants.FormFeatures.DISCOVERABLE)
Expand Down Expand Up @@ -57,11 +56,7 @@ async def get(self, request: Request) -> JSONResponse:
if not form:
return JSONResponse({"error": "not_found"}, status_code=404)

form = Form(**form)
if not admin:
form = filter_unittests(form)

return JSONResponse(form.dict(admin=admin))
return JSONResponse(Form(**form).dict(admin=admin))

@requires(["authenticated"])
@api.validate(
Expand Down
13 changes: 0 additions & 13 deletions backend/routes/forms/unittesting.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,6 @@ class UnittestResult(NamedTuple):
result: str


def filter_unittests(form: Form) -> Form:
"""
Replace the unittest data section of code questions with the number of test cases.
This is used to redact the exact tests when sending the form back to the frontend.
"""
for question in form.questions:
if question.type == "code" and question.data["unittests"] is not None:
question.data["unittests"]["tests"] = len(question.data["unittests"]["tests"])

return form


def _make_unit_code(units: dict[str, str]) -> str:
"""Compose a dict mapping unit names to their code into an actual class body."""
result = ""
Expand Down

0 comments on commit ae2d6cc

Please sign in to comment.