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(self): """Parse the schema :returns: A questionnaire object :raises: A SchemaParserException if there is a problem while parsing the schema """ questionnaire = Questionnaire() questionnaire.id = ParserUtils.get_required_string( self._schema, "questionnaire_id") questionnaire.title = ParserUtils.get_required_string( self._schema, "title") questionnaire.survey_id = ParserUtils.get_required_string( self._schema, "survey_id") logger.debug("title: " + questionnaire.title) questionnaire.description = ParserUtils.get_optional_string( self._schema, "description") questionnaire.theme = ParserUtils.get_required_string( self._schema, "theme") questionnaire.data_version = ParserUtils.get_required_string( self._schema, "data_version") if "introduction" in self._schema.keys(): questionnaire.introduction = self._parse_introduction( self._schema['introduction']) if "groups" in self._schema.keys(): for group_schema in self._schema['groups']: questionnaire.add_group( self._parse_group(group_schema, questionnaire)) else: raise SchemaParserException( 'Questionnaire must contain at least one group') if 'messages' in self._schema.keys(): # re-use the parse validation method self._parse_validation(questionnaire, self._schema) questionnaire.register_aliases() return questionnaire
def parse(self): """Parse the schema :returns: A questionnaire object :raises: A SchemaParserException if there is a problem while parsing the schema """ questionnaire = None try: questionnaire = Questionnaire() questionnaire.id = ParserUtils.get_required_string(self._schema, "questionnaire_id") questionnaire.eq_id = ParserUtils.get_required_string(self._schema, "eq_id") logger.debug("eq_id: " + questionnaire.eq_id) questionnaire.title = ParserUtils.get_required_string(self._schema, "title") questionnaire.survey_id = ParserUtils.get_required_string(self._schema, "survey_id") logger.debug("title: " + questionnaire.title) questionnaire.description = ParserUtils.get_required_string(self._schema, "description") questionnaire.theme = ParserUtils.get_required_string(self._schema, "theme") questionnaire.submission_page = ParserUtils.get_optional_string(self._schema, "submission_page", questionnaire.submission_page) except Exception as e: logging.error('Error parsing schema') logging.info(e) raise e if questionnaire: if "introduction" in self._schema.keys(): questionnaire.introduction = self._parse_introduction(self._schema['introduction']) if "groups" in self._schema.keys(): for group_schema in self._schema['groups']: questionnaire.add_group(self._parse_group(group_schema, questionnaire)) else: raise SchemaParserException('Questionnaire must contain at least one group') if 'messages' in self._schema.keys(): # re-use the parse validation method self._parse_validation(questionnaire, self._schema) return questionnaire
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): 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_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_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')