def test_child_answers_define_parent(self): for schema_file in self.all_schema_files(): with open(schema_file, encoding="utf8") as file: schema_json = load(file) for block in SchemaHelper.get_blocks(schema_json): answers_by_id = SchemaHelper.get_answers_by_id_for_block( block) for answer_id, answer in answers_by_id.items(): if answer['type'] in ['Radio', 'Checkbox']: child_answer_ids = (o['child_answer_id'] for o in answer['options'] if 'child_answer_id' in o) for child_answer_id in child_answer_ids: if child_answer_id not in answers_by_id: self.fail( "Child answer with id '%s' does not exist in schema %s" % (child_answer_id, schema_file)) if 'parent_answer_id' not in answers_by_id[ child_answer_id]: self.fail( "Child answer '%s' does not define parent_answer_id '%s' in schema %s" % (child_answer_id, answer_id, schema_file)) if answers_by_id[child_answer_id][ 'parent_answer_id'] != answer_id: self.fail( "Child answer '%s' defines incorrect parent_answer_id '%s' in schema %s: " "Should be '%s" % (child_answer_id, answers_by_id[child_answer_id] ['parent_answer_id'], schema_file, answer_id))
def validate_routing_rules_has_default_if_not_all_answers_routed(file, json_to_validate): errors = [] for block in SchemaHelper.get_blocks(json_to_validate): for section in (s for s in block['sections'] if 'sections' in block): for question in (q for q in section['questions'] if 'questions' in section): for answer in (a for a in question['answers'] if 'answers' in question): errors.extend(TestSchemaValidation.validate_answer(file, block, answer)) return errors
def validate_schema_contains_valid_routing_rules(cls, file, json_to_validate): errors = [] blocks = [SchemaHelper.get_blocks(json_to_validate)] for block in blocks: if 'routing_rules' in block and len(block['routing_rules']) > 0: for rule in block['routing_rules']: if 'goto' in rule and 'id' in rule['goto'].keys(): block_id = rule['goto']['id'] if block_id == 'summary': continue if not cls.contains_block(blocks, block_id): invalid_block_error = "Routing rule routes to invalid block [{}]".format(block_id) errors.append(TestSchemaValidation._error_message(invalid_block_error, file)) return errors
def test_get_blocks(self): survey = load_schema_file("test_repeating_household.json") blocks = [b for b in SchemaHelper.get_blocks(survey)] self.assertEqual(len(blocks), 5)
def contains_block(json, block_id): matching_blocks = [ b for b in SchemaHelper.get_blocks(json) if b["id"] == block_id ] return len(matching_blocks) == 1