Skip to content

Commit

Permalink
Merge pull request #355 from praekeltfoundation/fix-answers-in-api
Browse files Browse the repository at this point in the history
Filter answers for imported and non-imported forms
  • Loading branch information
HawkiesZA committed Aug 13, 2024
2 parents 45e47a8 + fad9717 commit 8dd1d81
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
2 changes: 1 addition & 1 deletion home/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ def to_representation(self, page):
"max": question.get("value", {}).get("max"),
"semantic_id": question.get("value", {}).get("semantic_id"),
"answers": [
x["value"] for x in question["value"].get("answers", [])
x.get("value", x) for x in question["value"].get("answers", [])
],
}
)
Expand Down
65 changes: 65 additions & 0 deletions home/tests/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,45 @@ def create_form_with_fields() -> Assessment:
return form


def create_form_with_imported_fields() -> Assessment:
"""
Create form with fields ommitted during import and pass it
through the serializer
"""

form = Assessment.objects.create(
title="test page",
slug="test-page",
version="v1.0",
locale=Locale.objects.get(language_code="en"),
high_result_page=ContentPage.objects.get(slug="high-inflection"),
high_inflection=3,
medium_result_page=ContentPage.objects.get(slug="medium-score"),
medium_inflection=2,
low_result_page=ContentPage.objects.get(slug="low-score"),
skip_threshold=2,
skip_high_result_page=ContentPage.objects.get(slug="skip-score"),
generic_error="error",
questions=[
{
"type": "categorical_question",
"value": {
"question": "test question",
"explainer": "We need to know this",
"error": "test error",
"semantic_id": "test-semantic-id",
"answers": [
{"score": 2.0, "answer": "A"},
{"score": 1.0, "answer": "B"},
{"score": 2.0, "answer": "C"},
],
},
}
],
)
return form


def create_form_with_missing_fields() -> Assessment:
"""
Create form without an explainer field and pass it
Expand Down Expand Up @@ -505,6 +544,32 @@ def test_form_with_fields(self) -> None:
result[0].pop("id")
assert result == expected_result

def test_form_with_imported_fields(self) -> None:
"""
Test form with all fields
"""
page = create_form_with_imported_fields()
form_page = QuestionField()
result = form_page.to_representation(page)
expected_result = [
{
"question_type": "categorical_question",
"question": "test question",
"explainer": "We need to know this",
"error": "test error",
"min": None,
"max": None,
"semantic_id": "test-semantic-id",
"answers": [
{"score": 2.0, "answer": "A"},
{"score": 1.0, "answer": "B"},
{"score": 2.0, "answer": "C"},
],
}
]
result[0].pop("id")
assert result == expected_result

def test_form_with_missing_fields(self) -> None:
"""
Test form with missing fields.
Expand Down

0 comments on commit 8dd1d81

Please sign in to comment.