def _parse_group(self, schema, questionnaire): """Parse a group element :param schema: The group schema :returns: Group object :raises: SchemaParserException """ group = None try: group = Group() group.id = ParserUtils.get_required_string(schema, "id") group.title = ParserUtils.get_optional_string(schema, "title") # Register the group questionnaire.register(group) except Exception as e: logging.error('Error parsing schema') logging.info(e) raise e if "blocks" in schema.keys(): for block_schema in schema['blocks']: group.add_block(self._parse_block(block_schema, questionnaire)) else: raise SchemaParserException('Group must contain at least one block') return group
def test_convert_answers(self): with self.application.test_request_context(): user_answer = [create_answer('ABC', '2016-01-01', group_id='group-1', block_id='block-1'), create_answer('DEF', '2016-03-30', group_id='group-1', block_id='block-1')] answer_1 = Answer() answer_1.id = "ABC" answer_1.code = "001" answer_2 = Answer() answer_2.id = "DEF" answer_2.code = "002" question = Question() question.id = 'question-1' question.add_answer(answer_1) question.add_answer(answer_2) section = Section() section.add_question(question) block = Block() block.id = 'block-1' block.add_section(section) group = Group() group.id = 'group-1' group.add_block(block) questionnaire = Questionnaire() questionnaire.survey_id = "021" questionnaire.data_version = "0.0.1" questionnaire.add_group(group) questionnaire.register(group) questionnaire.register(block) questionnaire.register(section) questionnaire.register(question) questionnaire.register(answer_1) questionnaire.register(answer_2) routing_path = [Location(group_id='group-1', group_instance=0, block_id='block-1')] answer_object = convert_answers(metadata, questionnaire, AnswerStore(user_answer), routing_path) self.assertEqual(answer_object['type'], 'uk.gov.ons.edc.eq:surveyresponse') self.assertEqual(answer_object['version'], '0.0.1') self.assertEqual(answer_object['origin'], 'uk.gov.ons.edc.eq') self.assertEqual(answer_object['survey_id'], '021') self.assertEqual(answer_object['collection']['exercise_sid'], metadata['collection_exercise_sid']) self.assertEqual(answer_object['collection']['instrument_id'], metadata['form_type']) self.assertEqual(answer_object['collection']['period'], metadata['period_id']) self.assertEqual(answer_object['metadata']['user_id'], metadata['user_id']) self.assertEqual(answer_object['metadata']['ru_ref'], metadata['ru_ref']) self.assertEqual(answer_object['data']['001'], '2016-01-01') self.assertEqual(answer_object['data']['002'], '2016-03-30')
def test_register_duplicate(self): questionnaire = Questionnaire() questionnaire.id = 'some-id' questionnaire.title = 'my questionnaire object' group1 = Group() group1.id = 'group-1' questionnaire.add_group(group1) questionnaire.register(group1) group = questionnaire.get_item_by_id('group-1') self.assertEqual(group1, group) group2 = Group() group2.id = 'group-1' # Duplicate id self.assertRaises(QuestionnaireException, questionnaire.register, group2)
def test_get_item_by_id(self): questionnaire = Questionnaire() questionnaire.id = 'some-id' questionnaire.title = 'my questionnaire object' # The order that items is added is important otherwise, # items are ot registered on the questionnaire group1 = Group() group1.id = 'group-1' questionnaire.add_group(group1) block1 = Block() block1.id = 'block-1' group1.add_block(block1) group2 = Group() group2.id = 'group-2' questionnaire.add_group(group2) block2 = Block() block2.id = 'block-2' group2.add_block(block2) self.assertRaises(QuestionnaireException, questionnaire.get_item_by_id, 'group-1') self.assertRaises(QuestionnaireException, questionnaire.get_item_by_id, 'group-2') self.assertRaises(QuestionnaireException, questionnaire.get_item_by_id, 'block-1') self.assertRaises(QuestionnaireException, questionnaire.get_item_by_id, 'block-2') questionnaire.register(group1) questionnaire.register(group2) questionnaire.register(block1) questionnaire.register(block2) self.assertEqual(questionnaire.get_item_by_id('group-1'), group1) self.assertEqual(questionnaire.get_item_by_id('group-2'), group2) self.assertEqual(questionnaire.get_item_by_id('block-1'), block1) self.assertEqual(questionnaire.get_item_by_id('block-2'), block2)
def _parse_group(self, schema, questionnaire): """Parse a group element :param schema: The group schema :returns: Group object :raises: SchemaParserException """ group = Group() group.id = ParserUtils.get_required_string(schema, "id") group.title = ParserUtils.get_optional_string(schema, "title") # Register the group questionnaire.register(group) if "blocks" in schema.keys(): for block_schema in schema['blocks']: group.add_block(self._parse_block(block_schema, questionnaire)) else: raise SchemaParserException( 'Group must contain at least one block') return group
def test_basics(self): questionnaire = Questionnaire() questionnaire.id = 'some-id' questionnaire.title = 'my questionnaire object' group1 = Group() group1.id = 'group-1' group2 = Group() group2.id = 'group-2' questionnaire.add_group(group1) questionnaire.add_group(group2) self.assertEquals(questionnaire.id, 'some-id') self.assertEquals(questionnaire.title, 'my questionnaire object') self.assertEquals(len(questionnaire.groups), 2) self.assertEquals(questionnaire.groups[0], group1) self.assertEquals(questionnaire.groups[1], group2) self.assertEquals(group1.container, questionnaire) self.assertEquals(group2.container, questionnaire)
def test_answer_with_multiple_instances(self): with self.application.test_request_context(): user_answer = [create_answer('GHI', 0, group_id='group-1', block_id='block-1'), create_answer('GHI', value=1, answer_instance=1, group_id='group-1', block_id='block-1'), create_answer('GHI', value=2, answer_instance=2, group_id='group-1', block_id='block-1')] answer = Answer() answer.id = "GHI" answer.code = "003" question = Question() question.id = 'question-2' question.add_answer(answer) section = Section() section.add_question(question) block = Block() block.id = 'block-1' block.add_section(section) group = Group() group.id = 'group-1' group.add_block(block) questionnaire = Questionnaire() questionnaire.survey_id = "021" questionnaire.data_version = "0.0.1" questionnaire.add_group(group) questionnaire.register(question) questionnaire.register(answer) routing_path = [Location(group_id='group-1', group_instance=0, block_id='block-1')] answer_object = convert_answers(metadata, questionnaire, AnswerStore(user_answer), routing_path) # Check the converter correctly self.assertEqual(answer_object["data"]["003"], ['0', '1', '2'])
def test_basics(self): group = Group() group.id = 'some-id' group.title = 'my group object' block1 = Block() block1.id = 'block-1' block2 = Block() block2.id = 'block-2' group.add_block(block1) group.add_block(block2) self.assertEquals(group.id, 'some-id') self.assertEquals(group.title, 'my group object') self.assertIsNone(group.container) self.assertEquals(len(group.blocks), 2) self.assertEquals(group.blocks[0], block1) self.assertEquals(group.blocks[1], block2) self.assertEquals(block1.container, group) self.assertEquals(block2.container, group)
def test_get_item_by_id(self): questionnaire = Questionnaire() questionnaire.id = 'some-id' questionnaire.title = 'my questionnaire object' # The order that items is added is important otherwise, # items are ot registered on the questionnaire group1 = Group() group1.id = 'group-1' questionnaire.add_group(group1) block1 = Block() block1.id = 'block-1' group1.add_block(block1) group2 = Group() group2.id = 'group-2' questionnaire.add_group(group2) block2 = Block() block2.id = 'block-2' group2.add_block(block2) self.assertRaises(QuestionnaireException, questionnaire.get_item_by_id, 'group-1') self.assertRaises(QuestionnaireException, questionnaire.get_item_by_id, 'group-2') self.assertRaises(QuestionnaireException, questionnaire.get_item_by_id, 'block-1') self.assertRaises(QuestionnaireException, questionnaire.get_item_by_id, 'block-2') questionnaire.register(group1) questionnaire.register(group2) questionnaire.register(block1) questionnaire.register(block2) self.assertEquals(questionnaire.get_item_by_id('group-1'), group1) self.assertEquals(questionnaire.get_item_by_id('group-2'), group2) self.assertEquals(questionnaire.get_item_by_id('block-1'), block1) self.assertEquals(questionnaire.get_item_by_id('block-2'), block2)
def test_basics(self): questionnaire = Questionnaire() questionnaire.id = 'some-id' questionnaire.title = 'my questionnaire object' group1 = Group() group1.id = 'group-1' group2 = Group() group2.id = 'group-2' questionnaire.add_group(group1) questionnaire.add_group(group2) self.assertEqual(questionnaire.id, 'some-id') self.assertEqual(questionnaire.title, 'my questionnaire object') self.assertEqual(len(questionnaire.groups), 2) self.assertEqual(questionnaire.groups[0], group1) self.assertEqual(questionnaire.groups[1], group2) self.assertEqual(group1.container, questionnaire) self.assertEqual(group2.container, questionnaire)
def test_basics(self): group = Group() group.id = 'some-id' group.title = 'my group object' block1 = Block() block1.id = 'block-1' block2 = Block() block2.id = 'block-2' group.add_block(block1) group.add_block(block2) self.assertEqual(group.id, 'some-id') self.assertEqual(group.title, 'my group object') self.assertIsNone(group.container) self.assertEqual(len(group.blocks), 2) self.assertEqual(group.blocks[0], block1) self.assertEqual(group.blocks[1], block2) self.assertEqual(block1.container, group) self.assertEqual(block2.container, group)
def test_converter_checkboxes_with_q_codes_and_other_value(self): with self.application.test_request_context(): routing_path = [Location(group_id='favourite food', group_instance=0, block_id='crisps')] answers = [create_answer('crisps-answer', ['Ready salted', 'other', 'Bacon'], group_id='favourite food', block_id='crisps')] answer = CheckboxAnswer() answer.id = "crisps-answer" answer.code = "" answer.options = [ { "label": "Ready salted", "value": "Ready salted", "q_code": "1" }, { "label": "Sweet chilli", "value": "Sweet chilli", "q_code": "2" }, { "label": "Cheese and onion", "value": "Cheese and onion", "q_code": "3" }, { "label": "Other", "value": "other", "q_code": "4", "description": "Choose any other flavour", "other": { "label": "Please specify other" } } ] question = Question() question.id = 'crisps-question' question.add_answer(answer) section = Section() section.add_question(question) block = Block() block.id = 'crisps' block.add_section(section) group = Group() group.id = 'favourite food' group.add_block(block) questionnaire = Questionnaire() questionnaire.survey_id = "999" questionnaire.data_version = "0.0.1" questionnaire.add_group(group) questionnaire.register(question) questionnaire.register(answer) # When answer_object = convert_answers(metadata, questionnaire, AnswerStore(answers), routing_path) # Then self.assertEqual(len(answer_object['data']), 2) self.assertEqual(answer_object['data']['1'], 'Ready salted') self.assertEqual(answer_object['data']['4'], 'Bacon')