Skip to content

Commit

Permalink
Add dynamic option source identifier and option_label_from_value mism…
Browse files Browse the repository at this point in the history
…atch (#139)

* Add dynamic option source identifier and option_label_from_value mismatch
  • Loading branch information
LJBabbage authored Feb 23, 2022
1 parent 5553b4b commit 0613d24
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 12 deletions.
37 changes: 28 additions & 9 deletions app/validators/answers/option_answer_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class OptionAnswerValidator(AnswerValidator):
DYNAMIC_OPTIONS_REFERENCES_NON_CHECKBOX_ANSWER = (
"Dynamic options references non Checkbox answer"
)
DYNAMIC_OPTIONS_SOURCE_IDENTIFIER_AND_OPTION_LABEL_FROM_VALUE_MISMATCH = (
"Dynamic options source identifier and "
"option label from value answer_id do not match"
)

def __init__(self, schema_element, questionnaire_schema=None):
super().__init__(schema_element)
Expand Down Expand Up @@ -133,12 +137,27 @@ def _validate_dynamic_options_answer_source(self):
return None

value_source = self.dynamic_options["values"]
if (
value_source["source"] == "answers"
and self.questionnaire_schema.get_answer_type(value_source["identifier"])
!= AnswerType.CHECKBOX
):
self.add_error(
self.DYNAMIC_OPTIONS_REFERENCES_NON_CHECKBOX_ANSWER,
value_source=value_source,
)
transform = self.dynamic_options["transform"]

if value_source["source"] == "answers":

if (
self.questionnaire_schema.get_answer_type(value_source["identifier"])
!= AnswerType.CHECKBOX
):

self.add_error(
self.DYNAMIC_OPTIONS_REFERENCES_NON_CHECKBOX_ANSWER,
value_source=value_source,
)

if (
"option-label-from-value" in transform
and transform["option-label-from-value"][1]
!= value_source["identifier"]
):
self.add_error(
self.DYNAMIC_OPTIONS_SOURCE_IDENTIFIER_AND_OPTION_LABEL_FROM_VALUE_MISMATCH,
source_identifier=value_source["identifier"],
transform_identifier=transform["option-label-from-value"][1],
)
54 changes: 51 additions & 3 deletions tests/validators/answers/test_option_answer_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,45 @@ def test_dynamic_options_values_with_invalid_value_rule():
assert validator.errors == [expected_error]


def test_dynamic_options_transform_with_invalid_value_rule():
def test_dynamic_options_source_identifier_and_option_label_from_value_mismatch():
answer = {
"id": "answer",
"label": "Label",
"type": "Radio",
"dynamic_options": {
"values": {"source": "answers", "identifier": "checkbox-answer"},
"transform": {
"option-label-from-value": ["self", "mismatch-checkbox-answer"]
},
},
}

validator = OptionAnswerValidator(
answer,
questionnaire_schema=get_mock_schema(
answers_with_context={
"checkbox-answer": {
"answer": {"id": "checkbox-answer", "type": "Checkbox"}
},
"mismatch-checkbox-answer": {
"answer": {"id": "mismatch-checkbox-answer", "type": "Checkbox"}
},
}
),
)
validator.validate_dynamic_options()

expected_error = {
"message": validator.DYNAMIC_OPTIONS_SOURCE_IDENTIFIER_AND_OPTION_LABEL_FROM_VALUE_MISMATCH,
"source_identifier": "checkbox-answer",
"transform_identifier": "mismatch-checkbox-answer",
"answer_id": "answer",
}

assert validator.errors == [expected_error]


def test_dynamic_options_transform_with_invalid_answer_id_reference():
answer = {
"id": "answer",
"label": "Label",
Expand All @@ -227,13 +265,23 @@ def test_dynamic_options_transform_with_invalid_value_rule():
)
validator.validate_dynamic_options()

expected_error = {
option_label_from_value_do_not_match_error = {
"message": validator.DYNAMIC_OPTIONS_SOURCE_IDENTIFIER_AND_OPTION_LABEL_FROM_VALUE_MISMATCH,
"source_identifier": "checkbox-answer",
"transform_identifier": "non-existing-answer",
"answer_id": "answer",
}

answer_reference_invalid_error = {
"message": ValueSourceValidator.ANSWER_REFERENCE_INVALID,
"identifier": "non-existing-answer",
"origin_id": "answer",
}

assert validator.errors == [expected_error]
assert validator.errors == [
option_label_from_value_do_not_match_error,
answer_reference_invalid_error,
]


def test_dynamic_options_values_with_non_checkbox_answer_source():
Expand Down

0 comments on commit 0613d24

Please sign in to comment.