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
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]
def validate_groups(self): for group in self.section["groups"]: group_routing_validator = RoutingValidator( group, group, self.questionnaire_schema) self.errors += group_routing_validator.validate() self.validate_blocks(self.section["id"], group["id"])
def validate_blocks(self, section_id, group_id): group = self.questionnaire_schema.get_group(group_id) last_section = self.questionnaire_schema.sections[-1] last_group = last_section["groups"][-1] last_block = last_group["blocks"][-1] for block in group.get("blocks"): if (section_id == last_section["id"] and group_id == last_group["id"] and block["id"] == last_block["id"]): self.validate_block_is_submission(block) block_routing_validator = RoutingValidator( block, group, self.questionnaire_schema) self.errors += block_routing_validator.validate() block_validator = get_block_validator(block, self.questionnaire_schema) self.errors += block_validator.validate() self.validate_question(block) self.validate_variants(block)
def validate_routing(self, schema_element, group): if "routing_rules" in schema_element: if any("goto" in rule for rule in schema_element["routing_rules"]): routing_validator = RoutingValidator( schema_element, group, self.questionnaire_schema ) else: routing_validator = NewRoutingValidator( routing_rules=schema_element["routing_rules"], group=group, origin_id=schema_element["id"], questionnaire_schema=self.questionnaire_schema, ) self.errors += routing_validator.validate() if "skip_conditions" in schema_element: if isinstance(schema_element["skip_conditions"], list): self.validate_skip_conditions( schema_element["skip_conditions"], schema_element["id"] ) elif isinstance(schema_element["skip_conditions"], dict): self.validate_new_skip_conditions( schema_element["skip_conditions"], schema_element["id"] )
def test_invalid_routing_default_block(): rules = [ { "goto": { "block": "response-yes", "when": [{ "condition": "equals", "id": "conditional-routing-answer", "value": "Yes", }], } }, { "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_rules_have_default(rules, "conditional-routing-block") expected_error = { "message": validator.ROUTE_MUST_CONTAIN_DEFAULT, "block_or_group_id": "conditional-routing-block", } assert validator.errors[0] == expected_error