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_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_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