Skip to content

Commit

Permalink
add tests for background-geopoint questions
Browse files Browse the repository at this point in the history
  • Loading branch information
RuthShryock committed Sep 27, 2024
1 parent 08999fe commit 8770059
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pyxform/question.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,18 @@ def validate(self):
# question type dictionary.
if self.type not in QUESTION_TYPE_DICT:
raise PyXFormError(f"Unknown question type '{self.type}'.")
# ensure that background-geopoint questions have non-null triggers that correspond to an exsisting questions
if self.type == "background-geopoint":
if not self.trigger:
raise PyXFormError(
f"background-geopoint question '{self.name}' must have a non-null trigger."
)
trigger_cleaned = self.trigger.strip("${}")
if not self.get_root().question_exists(trigger_cleaned):
raise PyXFormError(
f"Trigger '{trigger_cleaned}' for background-geopoint question '{self.name}' "
"does not correspond to an existing question."
)

def xml_instance(self, **kwargs):
survey = self.get_root()
Expand Down
9 changes: 9 additions & 0 deletions pyxform/survey.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,15 @@ def get_trigger_values_for_question_name(self, question_name, trigger_type):

return trigger_map.get(trigger_type, {}).get(f"${{{question_name}}}")

def question_exists(self, question_name: str) -> bool:
"""
Check if a question with the given name exists in the survey.
"""
for element in self.iter_descendants():
if isinstance(element, Question) and element.name == question_name:
return True
return False

def _generate_static_instances(self, list_name, choice_list) -> InstanceInfo:
"""
Generate <instance> elements for static data (e.g. choices for selects)
Expand Down
71 changes: 71 additions & 0 deletions tests/test_background_geopoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from tests.pyxform_test_case import PyxformTestCase


class BackgroundGeopointTest(PyxformTestCase):
"""Test background-geopoint question type."""

def test_background_geopoint(self):
self.assertPyxformXform(
name="data",
md="""
| survey | | | |
| | type | name | label | trigger |
| | integer | temp | Enter the current temperature | |
| | background-geopoint| temp_geo | | ${temp} |
| | note | show_temp_geo | location: ${temp_geo} | |
""",
xml__contains=[
'<bind nodeset="/data/temp_geo" type="geopoint"/>',
'<odk:setgeopoint event="xforms-value-changed" ref="/data/temp_geo"/>',
],
)

def test_background_geopoint_missing_trigger(self):
"""Test that background-geopoint question raises error when trigger is empty."""
self.assertPyxformXform(
name="data",
md="""
| survey | | | |
| | type | name | label | trigger |
| | integer | temp | Enter the current temperature | |
| | background-geopoint| temp_geo | | |
| | note | show_temp_geo | location: ${temp_geo} | |
""",
errored=True,
error__contains=[
"background-geopoint question 'temp_geo' must have a non-null trigger"
],
)

def test_invalid_trigger_background_geopoint(self):
self.assertPyxformXform(
name="data",
md="""
| survey | | | |
| | type | name | label | trigger |
| | integer | temp | Enter the current temperature | |
| | background-geopoint| temp_geo | | ${invalid_trigger} |
| | note | show_temp_geo | location: ${temp_geo} | |
""",
errored=True,
error__contains=[
"Trigger 'invalid_trigger' for background-geopoint question 'temp_geo' does not correspond to an existing question"
],
)

def test_background_geopoint_requires_null_calculation(self):
"""Test that background-geopoint raises an error if there is a calculation."""
self.assertPyxformXform(
name="data",
md="""
| survey | | | | |
| | type | name | label | trigger | calculation |
| | integer | temp | Enter the current temperature | | |
| | background-geopoint| temp_geo | | ${temp} | 5 * temp |
| | note | show_temp_geo | location: ${temp_geo} | | |
""",
errored=True,
error__contains=[
"'temp_geo' is triggered by a geopoint action, so the calculation must be null."
],
)

0 comments on commit 8770059

Please sign in to comment.