Esempio n. 1
0
    def test_get_item_by_id_answer_without_instance_id(self):

        items_by_id = {'item1': 'Item 1'}

        questionnaire = Questionnaire()
        questionnaire.items_by_id = items_by_id

        item_id = questionnaire.get_item_by_id('item1')
        self.assertEqual(item_id, 'Item 1')
Esempio n. 2
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))
Esempio n. 3
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')
Esempio 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'])
    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)
Esempio n. 6
0
    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)
Esempio n. 7
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')
Esempio n. 8
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'])
    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)
Esempio n. 10
0
    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)
Esempio n. 11
0
    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)
Esempio n. 12
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')
Esempio n. 13
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')
Esempio n. 14
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
    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)
Esempio n. 16
0
    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