def test_invalid_source_reference(value_source): questionnaire_schema = QuestionnaireSchema( {"metadata": [{ "name": "metatata-1", "type": "string" }]}) questionnaire_schema.list_names = ["list-1"] questionnaire_schema.calculated_summary_block_ids = ["block-1"] questionnaire_schema.answers_with_context = { "answer-1": { "answer": { "id": "answer-1", "type": "TextField" } } } validator = ValueSourceValidator(value_source, "some.json.path", questionnaire_schema) validator.validate() error = validator.errors[0] assert error[ "message"] == ValueSourceValidator.SOURCE_REFERENCE_INVALID.format( value_source["source"]) assert error["identifier"] == value_source["identifier"] assert error["json_path"] == "some.json.path"
def test_validate_options(operator_name, first_argument, second_argument): rule = {operator_name: [first_argument, second_argument]} questionnaire_schema = QuestionnaireSchema({}) questionnaire_schema.answers_with_context = { "string-answer": { "answer": { "id": "string-answer", "type": "Radio" } }, "array-answer": { "answer": { "id": "array-answer", "type": "Checkbox" } }, } questionnaire_schema.answer_id_to_option_values_map = { "string-answer": ["Yes", "No"], "array-answer": ["Yes", "No"], } validator = get_validator(rule, questionnaire_schema=questionnaire_schema) validator.validate() expected_error = { "message": validator.VALUE_DOESNT_EXIST_IN_ANSWER_OPTIONS, "origin_id": ORIGIN_ID, "answer_options": ["Yes", "No"], "value": "Maybe", } assert validator.errors == [expected_error]
def test_invalid_self_reference(): block = { "id": "block-1", "title": { "text": "test {simple_answer}", "placeholders": [ { "placeholder": "simple_answer", "value": {"source": "answers", "identifier": "answer-1"}, } ], }, } questionnaire_schema = QuestionnaireSchema({}) questionnaire_schema.answers_with_context = { "answer-1": { "answer": {"id": "answer-1", "type": "TextField"}, "block": "block-1", } } validator = BlockValidator(block, questionnaire_schema) validator.validate_placeholder_answer_self_references() expected_errors = [ { "message": BlockValidator.PLACEHOLDER_ANSWER_SELF_REFERENCE, "identifier": "answer-1", "block_id": "block-1", } ] assert validator.errors == expected_errors
def test_validate_options_null_value_is_valid(operator_name, first_argument, second_argument): rule = {operator_name: [first_argument, second_argument]} questionnaire_schema = QuestionnaireSchema({}) questionnaire_schema.answers_with_context = { "string-answer": { "answer": { "id": "string-answer", "type": "Radio" } }, "array-answer": { "answer": { "id": "array-answer", "type": "Checkbox" } }, } questionnaire_schema.answer_id_to_option_values_map = { "string-answer": ["Yes", "No"], "array-answer": ["Yes", "No"], } validator = get_validator(rule, questionnaire_schema=questionnaire_schema) validator.validate() assert not validator.errors
def get_mock_schema(questionnaire_schema=None, answers_with_context=None): if not questionnaire_schema: questionnaire_schema = QuestionnaireSchema({}) if answers_with_context: questionnaire_schema.answers_with_context = answers_with_context return questionnaire_schema
def test_invalid_reference(): value_source = {"source": "answers", "identifier": "answer-2"} questionnaire_schema = QuestionnaireSchema({}) questionnaire_schema.answers_with_context = { "answer-1": { "answer": { "id": "answer-1", "type": "TextField" } } } validator = ValueSourceValidator(value_source, "", questionnaire_schema) validator.validate() error = validator.errors[0] assert error[ "message"] == ValueSourceValidator.ANSWER_SOURCE_REFERENCE_INVALID assert error["identifier"] == "answer-2"
def test_validate_options_multiple_errors(): rule = { "in": [ { "source": "answers", "identifier": "string-answer" }, ["Maybe", "Not sure"], ] } questionnaire_schema = QuestionnaireSchema({}) questionnaire_schema.answers_with_context = { "string-answer": { "answer": { "id": "string-answer", "type": "Radio" } } } questionnaire_schema.answer_id_to_option_values_map = { "string-answer": ["Yes", "No"] } validator = get_validator(rule, questionnaire_schema=questionnaire_schema) validator.validate() expected_errors = [ { "message": validator.VALUE_DOESNT_EXIST_IN_ANSWER_OPTIONS, "origin_id": ORIGIN_ID, "answer_options": ["Yes", "No"], "value": "Maybe", }, { "message": validator.VALUE_DOESNT_EXIST_IN_ANSWER_OPTIONS, "origin_id": ORIGIN_ID, "answer_options": ["Yes", "No"], "value": "Not sure", }, ] assert validator.errors == expected_errors
def test_answer_comparisons_different_types_skip_group(): """Ensures that when answer comparison is used, the type of the variables must be the same""" when_rule = { "id": "comparison-1-answer", "condition": "less than", "comparison": { "id": "comparison-2-answer", "source": "answers" }, } questionnaire_schema = QuestionnaireSchema({}) questionnaire_schema.answers_with_context = { "comparison-1-answer": { "answer": { "id": "comparison-1-answer", "type": "Number" }, "block": "route-comparison-1", }, "comparison-2-answer": { "answer": { "id": "comparison-2-answer", "type": "TextField" }, "block": "comparison-2", }, } validator = WhenRuleValidator(when_rule, {}, questionnaire_schema) validator.validate_comparison_in_when_rule(when_rule, "block-id") expected_errors = [{ "message": WhenRuleValidator.NON_MATCHING_WHEN_ANSWER_AND_COMPARISON_TYPES, "comparison_id": "comparison-2-answer", "answer_id": "comparison-1-answer", "referenced_id": "block-id", }] assert expected_errors == validator.errors
def test_invalid_composite_answer_in_selector(): value_source = { "identifier": "name-answer", "source": "answers", "selector": "line1", } questionnaire_schema = QuestionnaireSchema({}) questionnaire_schema.answers_with_context = { "name-answer": { "answer": { "id": "name-answer", "type": "TextField" }, "block": "name", } } validator = ValueSourceValidator(value_source, "", questionnaire_schema) validator.validate() error = validator.errors[0] assert error["message"] == ValueSourceValidator.COMPOSITE_ANSWER_INVALID assert error["identifier"] == "name-answer"
def test_answer_comparisons_different_types(): """Ensures that when answer comparison is used, the type of the variables must be the same""" when_rule = { "id": "comparison-1-answer", "condition": "equals any", "comparison": { "id": "comparison-2-answer", "source": "answers" }, } questionnaire_schema = QuestionnaireSchema({}) questionnaire_schema.answers_with_context = { "comparison-1-answer": { "answer": { "id": "comparison-1-answer", "type": "Number" }, "block": "route-comparison-1", }, "comparison-2-answer": { "answer": { "id": "comparison-2-answer", "type": "TextField" }, "block": "comparison-2", }, } validator = WhenRuleValidator(when_rule, {}, questionnaire_schema) validator.validate_comparison_in_when_rule(when_rule, "block-id") expected_errors = [{ "message": WhenRuleValidator.NON_CHECKBOX_COMPARISON_ID, "comparison_id": "comparison-2-answer", "condition": "equals any", }] assert expected_errors == validator.errors