Ejemplo n.º 1
0
    def test_submitted_at_should_be_set_in_payload(self):
        with self.application.test_request_context():
            user_answer = [create_answer('GHI', 0)]
            questionnaire = Questionnaire()
            questionnaire.survey_id = "021"
            questionnaire.data_version = '0.0.2'

            answer_object = convert_answers(metadata, questionnaire, AnswerStore(user_answer), {})

            self.assertLess(datetime.now(timezone.utc) - dateutil.parser.parse(answer_object['submitted_at']), timedelta(seconds=5))
Ejemplo n.º 2
0
    def test_converter_raises_runtime_error_for_unsupported_version(self):
        with self.application.test_request_context():
            questionnaire = Questionnaire()
            questionnaire.survey_id = '021'
            questionnaire.data_version = '-0.0.1'

            with self.assertRaises(DataVersionError) as err:
                convert_answers(metadata, questionnaire, AnswerStore(), {})

            self.assertEqual(str(err.exception), 'Data version -0.0.1 not supported')
Ejemplo n.º 3
0
    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')
Ejemplo n.º 4
0
    def test_convert_census_answers_multiple_answers(self):
        with self.application.test_request_context():
            routing_path = [Location(group_id='favourite food', group_instance=0, block_id='crisps')]
            answers = [create_answer('name', ['Ready salted', 'Sweet chilli'], group_id='favourite food', block_id='crisps')]
            questionnaire = Questionnaire()
            questionnaire.survey_id = '021'
            questionnaire.data_version = '0.0.2'

            # When
            answer_object = convert_answers(metadata, questionnaire, AnswerStore(answers), routing_path)

            # Then
            self.assertEqual(len(answer_object['data']), 1)
            self.assertEqual(answer_object['data'][0]['group_id'], 'favourite food')
            self.assertEqual(answer_object['data'][0]['group_instance'], 0)
            self.assertEqual(answer_object['data'][0]['block_id'], 'crisps')
            self.assertEqual(answer_object['data'][0]['answer_instance'], 0)
            self.assertEqual(answer_object['data'][0]['value'], ['Ready salted', 'Sweet chilli'])
Ejemplo n.º 5
0
    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
Ejemplo n.º 6
0
    def test_convert_census_answers(self):
        with self.application.test_request_context():
            routing_path = [Location(group_id='personal details', group_instance=0, block_id='about you'),
                            Location(group_id='household', group_instance=0, block_id='where you live'),
                            Location(group_id='household', group_instance=1, block_id='where you live')]
            answers = [create_answer('name', 'Joe Bloggs', group_id='personal details', block_id='about you'),
                       create_answer('name', 'Fred Bloggs', group_id='personal details', block_id='about you', answer_instance=1),
                       create_answer('address', '62 Somewhere', group_id='household', block_id='where you live'),
                       create_answer('address', '63 Somewhere', group_id='household', block_id='where you live', group_instance=1)]
            questionnaire = Questionnaire()
            questionnaire.survey_id = '021'
            questionnaire.data_version = '0.0.2'

            # When
            answer_object = convert_answers(metadata, questionnaire, AnswerStore(answers), routing_path)

            # Then
            self.assertEqual(len(answer_object['data']), 4)
            self.assertEqual(answer_object['data'][0]['group_id'], 'personal details')
            self.assertEqual(answer_object['data'][0]['group_instance'], 0)
            self.assertEqual(answer_object['data'][0]['block_id'], 'about you')
            self.assertEqual(answer_object['data'][0]['answer_instance'], 0)
            self.assertEqual(answer_object['data'][0]['value'], 'Joe Bloggs')
            self.assertEqual(answer_object['data'][1]['group_id'], 'personal details')
            self.assertEqual(answer_object['data'][1]['group_instance'], 0)
            self.assertEqual(answer_object['data'][1]['block_id'], 'about you')
            self.assertEqual(answer_object['data'][1]['answer_instance'], 1)
            self.assertEqual(answer_object['data'][1]['value'], 'Fred Bloggs')
            self.assertEqual(answer_object['data'][2]['group_id'], 'household')
            self.assertEqual(answer_object['data'][2]['group_instance'], 0)
            self.assertEqual(answer_object['data'][2]['block_id'], 'where you live')
            self.assertEqual(answer_object['data'][2]['answer_instance'], 0)
            self.assertEqual(answer_object['data'][2]['value'], '62 Somewhere')
            self.assertEqual(answer_object['data'][3]['group_id'], 'household')
            self.assertEqual(answer_object['data'][3]['group_instance'], 1)
            self.assertEqual(answer_object['data'][3]['block_id'], 'where you live')
            self.assertEqual(answer_object['data'][3]['answer_instance'], 0)
            self.assertEqual(answer_object['data'][3]['value'], '63 Somewhere')
Ejemplo n.º 7
0
    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'])
Ejemplo n.º 8
0
    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')