def test_answer_comparisons_different_types():
    """ Ensures that when answer comparison is used, the type of the variables must be the same """
    filename = "schemas/invalid/test_invalid_answer_comparison_types.json"
    questionnaire_schema = QuestionnaireSchema(
        _open_and_load_schema_file(filename))
    group = questionnaire_schema.get_group("route-group")
    block = questionnaire_schema.get_block("route-comparison-2")
    validator = RoutingValidator(block, group, questionnaire_schema)
    validator.validate()

    expected_errors = [
        {
            "message":
            WhenRuleValidator.NON_MATCHING_WHEN_ANSWER_AND_COMPARISON_TYPES,
            "comparison_id": "route-comparison-1-answer",
            "answer_id": "route-comparison-2-answer",
            "referenced_id": "route-comparison-2",
        },
        {
            "message": WhenRuleValidator.NON_CHECKBOX_COMPARISON_ID,
            "comparison_id": "route-comparison-2-answer",
            "condition": "equals any",
        },
    ]

    assert expected_errors == validator.errors
Пример #2
0
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_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 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 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_get_blocks():
    filename = "schemas/valid/test_list_collector_driving_question.json"

    questionnaire_schema = QuestionnaireSchema(
        _open_and_load_schema_file(filename))

    driving_question_blocks = questionnaire_schema.get_blocks(
        type="ListCollectorDrivingQuestion", for_list="people")

    assert len(driving_question_blocks) == 1
    assert driving_question_blocks[0]["id"] == "anyone-usually-live-at"
def test_invalid_calculated_summary():
    """Asserts invalid `when` types, currencies or units are not of the same type for CalculatedSummary"""
    filename = "schemas/invalid/test_invalid_calculated_summary.json"
    json_to_validate = _open_and_load_schema_file(filename)

    expected_error_messages = [
        {
            "message":
            CalculatedSummaryBlockValidator.ANSWERS_MUST_HAVE_SAME_TYPE,
            "block_id": "total-playback-type-error",
        },
        {
            "message":
            CalculatedSummaryBlockValidator.ANSWERS_MUST_HAVE_SAME_CURRENCY,
            "block_id": "total-playback-currency-error",
        },
        {
            "message":
            CalculatedSummaryBlockValidator.ANSWERS_MUST_HAVE_SAME_UNIT,
            "block_id": "total-playback-unit-error",
        },
        {
            "message": CalculatedSummaryBlockValidator.ANSWERS_HAS_INVALID_ID,
            "answer_id": "seventh-number-answer",
            "block_id": "total-playback-answer-error",
        },
        {
            "message": CalculatedSummaryBlockValidator.ANSWERS_HAS_DUPLICATES,
            "block_id": "total-playback-duplicate-error",
            "duplicate_answers":
            ["fourth-number-answer", "sixth-number-answer"],
        },
        {
            "message":
            CalculatedSummaryBlockValidator.ANSWERS_MUST_HAVE_SAME_TYPE,
            "block_id": "total-playback-duplicate-error",
        },
    ]

    questionnaire_schema = QuestionnaireSchema(json_to_validate)
    errors = []
    for block_id in [
            "total-playback-type-error",
            "total-playback-currency-error",
            "total-playback-unit-error",
            "total-playback-answer-error",
            "total-playback-duplicate-error",
    ]:
        block = questionnaire_schema.get_block(block_id)
        validator = CalculatedSummaryBlockValidator(block,
                                                    questionnaire_schema)
        errors += validator.validate()

    assert errors == expected_error_messages
def test_get_other_blocks():
    filename = "schemas/valid/test_list_collector.json"

    questionnaire_schema = QuestionnaireSchema(_open_and_load_schema_file(filename))

    other_list_collectors = questionnaire_schema.get_other_blocks(
        block_id_to_filter="list-collector", type="ListCollector", for_list="people"
    )

    assert len(other_list_collectors) == 1
    assert other_list_collectors[0]["id"] == "another-list-collector"
def test_invalid_list_collector_non_radio():
    filename = "schemas/invalid/test_invalid_list_collector_non_radio.json"
    questionnaire_schema = QuestionnaireSchema(
        _open_and_load_schema_file(filename))
    validator = ListCollectorValidator(
        questionnaire_schema.get_block("list-collector"), questionnaire_schema)
    validator.validate()

    expected_error_messages = [{
        "message": validator.NO_RADIO_FOR_LIST_COLLECTOR,
        "block_id": "list-collector"
    }]

    assert expected_error_messages == validator.errors
Пример #11
0
def test_invalid_hub_and_spoke_and_summary_confirmation_non_existent():
    filename = "schemas/invalid/test_invalid_hub_and_spoke_and_summary_confirmation_non_existent.json"
    questionnaire_schema = QuestionnaireSchema(
        _open_and_load_schema_file(filename))
    section = questionnaire_schema.get_section("accommodation-section")
    validator = SectionValidator(section, questionnaire_schema)

    expected_errors = [{
        "section_id": "accommodation-section",
        "message": SectionValidator.UNDEFINED_SUBMISSION_PAGE,
    }]

    validator.validate()

    assert validator.errors == expected_errors
def test_invalid_composite_answer_field_in_selector():
    questionnaire_schema = QuestionnaireSchema({})

    validator = BlockValidator({"id": "confirm-address"}, questionnaire_schema)
    validator.questionnaire_schema.answers_with_context = {
        "address-answer": {
            "answer": {"id": "address-answer", "type": "Address"},
            "block": "name",
        },
        "confirm-address-answer": {
            "answer": {"id": "confirm-address-answer"},
            "block": "confirm-address",
        },
    }
    validator.validate_source_references(
        [
            {
                "identifier": "address-answer",
                "source": "answers",
                "selector": "invalid-field",
            }
        ]
    )

    expected_errors = [
        {
            "message": BlockValidator.COMPOSITE_ANSWER_FIELD_INVALID,
            "referenced_id": "address-answer",
            "block_id": "confirm-address",
        }
    ]
    assert validator.errors == expected_errors
def test_invalid_reference():
    known_identifiers = ["answer-1", "answer-2"]

    questionnaire_schema = QuestionnaireSchema({})
    validator = BlockValidator({"id": "block-1"}, questionnaire_schema)
    validator.questionnaire_schema.answers_with_context = {
        "answer-1": {
            "answer": {
                "decimal_places": 2,
                "id": "answer-1",
                "label": "Answer 1",
                "mandatory": False,
                "type": "Number",
            },
            "block": "block-1",
        }
    }

    validator.validate_answer_source_reference(identifiers=known_identifiers)

    expected_errors = [
        {
            "message": BlockValidator.ANSWER_SELF_REFERENCE,
            "referenced_id": "answer-1",
            "block_id": "block-1",
        },
        {
            "message": BlockValidator.ANSWER_REFERENCE_INVALID,
            "referenced_id": "answer-2",
            "block_id": "block-1",
        },
    ]
    assert validator.errors == expected_errors
def test_invalid_relationship_multiple_answers():
    filename = "schemas/invalid/test_invalid_relationship_multiple_answers.json"

    questionnaire_schema = QuestionnaireSchema(
        _open_and_load_schema_file(filename))
    block = questionnaire_schema.get_block("relationships")
    validator = RelationshipCollectorValidator(block, questionnaire_schema)

    expected_errors = [{
        "message": validator.RELATIONSHIP_COLLECTOR_HAS_MULTIPLE_ANSWERS,
        "block_id": "relationships",
    }]

    validator.validate()

    assert validator.errors == expected_errors
def test_invalid_driving_question_multiple_driving_questions():
    filename = "schemas/invalid/test_invalid_list_collector_driving_question_multiple_driving_questions.json"

    questionnaire_schema = QuestionnaireSchema(
        _open_and_load_schema_file(filename))
    block = questionnaire_schema.get_block("anyone-usually-live-at")
    validator = ListCollectorDrivingQuestionValidator(block,
                                                      questionnaire_schema)

    expected_error_messages = [{
        "message": validator.MULTIPLE_DRIVING_QUESTIONS_FOR_LIST,
        "block_id": "anyone-usually-live-at",
        "for_list": "people",
    }]

    assert expected_error_messages == validator.validate()
def test_invalid_list_collector_with_different_answer_ids_in_add_and_edit():
    filename = "schemas/invalid/test_invalid_list_collector_with_different_answer_ids_in_add_and_edit.json"

    questionnaire_schema = QuestionnaireSchema(
        _open_and_load_schema_file(filename))
    block = questionnaire_schema.get_block("list-collector")
    validator = ListCollectorValidator(block, questionnaire_schema)

    expected_errors = [{
        "message": validator.LIST_COLLECTOR_ADD_EDIT_IDS_DONT_MATCH,
        "block_id": "list-collector",
    }]

    validator.validate()

    assert validator.errors == expected_errors
def test_invalid_list_collector_same_name_answer_id_reference():
    filename = "schemas/invalid/test_invalid_list_collector_same_name_answer_ids.json"

    questionnaire_schema = QuestionnaireSchema(
        _open_and_load_schema_file(filename))
    validator = ListCollectorValidator(
        questionnaire_schema.get_block("list-collector"), questionnaire_schema)
    validator.validate()

    expected_errors = [{
        "message": validator.MISSING_SAME_NAME_ANSWER_ID,
        "block_id": "list-collector",
        "answer_id": "surname",
    }]

    assert expected_errors == validator.errors
def test_invalid_primary_person_list_collector_with_no_add_or_edit_answer_action(
):
    filename = "schemas/invalid/test_invalid_primary_person_list_collector_no_add_edit_action.json"
    questionnaire_schema = QuestionnaireSchema(
        _open_and_load_schema_file(filename))
    block = questionnaire_schema.get_block("primary-person-list-collector")

    validator = PrimaryPersonListCollectorValidator(block,
                                                    questionnaire_schema)

    expected_errors = [{
        "message": validator.NO_REDIRECT_TO_LIST_ADD_BLOCK,
        "block_id": "primary-person-list-collector",
    }]

    assert validator.validate() == expected_errors
def get_validator(routing_rules, origin_id):
    return NewRoutingValidator(
        routing_rules=routing_rules,
        group={},
        origin_id=origin_id,
        questionnaire_schema=QuestionnaireSchema({}),
    )
def test_get_sub_block_context():
    filename = "schemas/valid/test_list_collector_driving_question.json"
    questionnaire_schema = QuestionnaireSchema(_open_and_load_schema_file(filename))

    assert [
        (question["id"], context)
        for question, context in questionnaire_schema.questions_with_context
    ] == [
        (
            "anyone-usually-live-at-question",
            {
                "group_id": "group",
                "section": "section",
                "block": "anyone-usually-live-at",
            },
        ),
        (
            "confirmation-question",
            {"group_id": "group", "section": "section", "block": "anyone-else-live-at"},
        ),
        (
            "add-question",
            {"group_id": "group", "section": "section", "block": "add-person"},
        ),
        (
            "edit-question",
            {"group_id": "group", "section": "section", "block": "edit-person"},
        ),
        (
            "remove-question",
            {"group_id": "group", "section": "section", "block": "remove-person"},
        ),
    ]
def test_invalid_list_reference_in_custom_summary():
    filename = "schemas/invalid/test_invalid_custom_list_summary.json"
    questionnaire_schema = QuestionnaireSchema(
        _open_and_load_schema_file(filename))
    section = questionnaire_schema.get_section("section")
    validator = SectionValidator(section, questionnaire_schema)

    expected_errors = [{
        "message": error_messages.FOR_LIST_NEVER_POPULATED,
        "list_name": "household",
        "section_id": "section",
    }]

    validator.validate()

    assert validator.errors == expected_errors
def test_invalid_list_collector_with_different_add_block_answer_ids():
    filename = "schemas/invalid/test_invalid_list_collector_with_different_add_block_answer_ids.json"

    questionnaire_schema = QuestionnaireSchema(
        _open_and_load_schema_file(filename))
    block = questionnaire_schema.get_block("list-collector")
    validator = ListCollectorValidator(block, questionnaire_schema)
    validator.validate()

    expected_errors = [{
        "message": validator.NON_UNIQUE_ANSWER_ID_FOR_LIST_COLLECTOR_ADD,
        "list_name": "people",
        "block_id": "list-collector",
    }]

    assert expected_errors == validator.errors
def test_invalid_routing_block_id():

    rule = {
        "goto": {
            "block":
            "invalid-location",
            "when": [{
                "condition": "equals",
                "id": "conditional-routing-answer",
                "value": "No, I prefer tea",
            }],
        }
    }
    questionnaire_schema = QuestionnaireSchema({})
    validator = RoutingValidator({}, {}, questionnaire_schema)
    validator.validate_routing_rule_target([{
        "id": "mock-block"
    }], "block", rule)
    expected_error = {
        "message": validator.ROUTE_TARGET_INVALID,
        "goto_key": "block",
        "referenced_id": "invalid-location",
    }

    assert validator.errors == [expected_error]
Пример #24
0
def test_invalid_hub_and_spoke_with_summary_confirmation():
    filename = (
        "schemas/invalid/test_invalid_hub_and_spoke_with_summary_confirmation.json"
    )
    questionnaire_schema = QuestionnaireSchema(
        _open_and_load_schema_file(filename))
    section = questionnaire_schema.get_section("accommodation-section")
    validator = SectionValidator(section, questionnaire_schema)

    expected_errors = [{
        "message": SectionValidator.MULTIPLE_SUBMISSION_PAGES,
        "section_id": "accommodation-section",
    }]

    validator.validate()

    assert validator.errors == expected_errors
def test_invalid_list_collector_with_no_remove_answer_action():
    filename = (
        "schemas/invalid/test_invalid_list_collector_with_no_remove_answer_action.json"
    )

    questionnaire_schema = QuestionnaireSchema(
        _open_and_load_schema_file(filename))
    validator = ListCollectorValidator(
        questionnaire_schema.get_block("list-collector"), questionnaire_schema)
    validator.validate()

    expected_errors = [{
        "message": validator.NO_REMOVE_LIST_ITEM_AND_ANSWERS_ACTION,
        "block_id": "list-collector",
    }]

    assert expected_errors == validator.errors
def test_primary_person_invalid_list_collector_non_radio():
    filename = (
        "schemas/invalid/test_invalid_primary_person_list_collector_no_radio.json"
    )
    questionnaire_schema = QuestionnaireSchema(
        _open_and_load_schema_file(filename))
    validator = PrimaryPersonListCollectorValidator(
        questionnaire_schema.get_block("primary-person-list-collector"),
        questionnaire_schema,
    )

    expected_errors = [{
        "message": validator.NO_RADIO_FOR_PRIMARY_PERSON_LIST_COLLECTOR,
        "block_id": "primary-person-list-collector",
    }]

    assert expected_errors == validator.validate()
def test_get_context_from_match():
    filename = "schemas/valid/test_question_variants.json"
    questionnaire_schema = QuestionnaireSchema(_open_and_load_schema_file(filename))

    matches = parse("$..blocks[*]").find(questionnaire_schema.schema)
    context = get_context_from_match(matches[0])

    assert context == {"section": "section", "group_id": "group", "block": "block-1"}
def test_invalid_actions():
    filename = "schemas/invalid/test_invalid_relationships_unrelated.json"

    questionnaire_schema = QuestionnaireSchema(
        _open_and_load_schema_file(filename))
    block = questionnaire_schema.get_block("relationships").get(
        "unrelated_block")
    validator = UnrelatedBlockValidator(block, questionnaire_schema)

    expected_errors = [{
        "message": validator.ACTION_PARAMS_MISSING,
        "block_id": "related-to-anyone-else",
    }]

    validator.validate()

    assert validator.errors == expected_errors
def test_invalid_answer_action_redirect_to_list_add_block_unexpected_params():
    filename = "schemas/invalid/test_invalid_answer_action_redirect_to_list_add_block_unexpected_params.json"
    questionnaire_schema = QuestionnaireSchema(_open_and_load_schema_file(filename))
    validator = BlockValidator(
        questionnaire_schema.get_block("list-collector"), questionnaire_schema
    )

    expected_error_messages = [
        {
            "message": validator.ACTION_PARAMS_SHOULDNT_EXIST,
            "block_id": "list-collector",
        }
    ]

    validator.validate()

    assert expected_error_messages == validator.errors
def test_invalid_use_of_id_relationships_with_type():
    filename = "schemas/invalid/test_invalid_use_of_block_id_relationships.json"
    questionnaire_schema = QuestionnaireSchema(_open_and_load_schema_file(filename))
    validator = BlockValidator(
        questionnaire_schema.get_block("relationships"), questionnaire_schema
    )

    expected_error_messages = [
        {
            "message": validator.ID_RELATIONSHIPS_NOT_USED_WITH_RELATIONSHIP_COLLECTOR,
            "block_id": "relationships",
        }
    ]

    validator.validate()

    assert expected_error_messages == validator.errors