Пример #1
0
    def test_percentage_answer(self):
        with self._app.test_request_context():
            routing_path = [
                Location(group_id='percentage-group',
                         group_instance=0,
                         block_id='percentage-block')
            ]
            user_answer = [
                create_answer('percentage-answer',
                              99,
                              group_id='percentage-group',
                              block_id='percentage-block')
            ]

            questionnaire = make_schema(
                '0.0.2', 'section-1', 'percentage-group', 'percentage-block',
                [{
                    'id': 'percentage-question',
                    'answers': [{
                        'id': 'percentage-answer',
                        'type': 'Percentage'
                    }]
                }])

            # When
            answer_object = convert_answers(self.metadata,
                                            self.collection_metadata,
                                            QuestionnaireSchema(questionnaire),
                                            AnswerStore(user_answer),
                                            routing_path)

            # Then
            self.assertEqual(len(answer_object['data']), 1)
            self.assertEqual(answer_object['data'][0]['value'], 99)
            self.assertEqual(answer_object['data'][0]['group_instance'], 0)
            self.assertEqual(answer_object['data'][0]['answer_instance'], 0)
Пример #2
0
def test_textarea_answer(fake_questionnaire_store):
    full_routing_path = [RoutingPath(["textarea-block"], section_id="section-1")]
    answers = AnswerStore(
        [Answer("textarea-answer", "This is an example text!").to_dict()]
    )
    fake_questionnaire_store.answer_store = answers

    questionnaire = make_schema(
        "0.0.3",
        "section-1",
        "textarea-group",
        "textarea-block",
        {
            "id": "textarea-question",
            "answers": [{"id": "textarea-answer", "type": "TextArea"}],
        },
    )

    answer_object = convert_answers(
        QuestionnaireSchema(questionnaire), fake_questionnaire_store, full_routing_path
    )

    assert len(answer_object["data"]["answers"]) == 1
    assert answer_object["data"]["answers"][0].value == "This is an example text!"
def test_dropdown_answer(fake_questionnaire_store):
    full_routing_path = [RoutingPath(["dropdown-block"], section_id="section-1")]
    answers = AnswerStore([Answer("dropdown-answer", "Rugby is better!").to_dict()])
    fake_questionnaire_store.answer_store = answers

    questionnaire = make_schema(
        "0.0.3",
        "section-1",
        "dropdown-group",
        "dropdown-block",
        {
            "id": "dropdown-question",
            "answers": [
                {
                    "id": "dropdown-answer",
                    "type": "Dropdown",
                    "options": [
                        {"label": "Liverpool", "value": "Liverpool"},
                        {"label": "Chelsea", "value": "Chelsea"},
                        {"label": "Rugby is better!", "value": "Rugby is better!"},
                    ],
                }
            ],
        },
    )

    answer_object = convert_answers(
        QuestionnaireSchema(questionnaire),
        fake_questionnaire_store,
        full_routing_path,
        SUBMITTED_AT,
    )

    # Then
    assert len(answer_object["data"]["answers"]) == 1
    assert answer_object["data"]["answers"][0].value == "Rugby is better!"
def test_unit_answer(fake_questionnaire_store):
    routing_path = RoutingPath(["unit-block"], section_id="section-1")
    fake_questionnaire_store.answer_store = AnswerStore(
        [Answer("unit-answer", 10).to_dict()])

    question = {
        "id": "unit-question",
        "answers": [{
            "id": "unit-answer",
            "type": "Unit",
            "q_code": "1"
        }],
    }

    questionnaire = make_schema("0.0.1", "section-1", "unit-block",
                                "unit-block", question)

    # When
    answer_object = convert_answers(QuestionnaireSchema(questionnaire),
                                    fake_questionnaire_store, routing_path)

    # Then
    assert len(answer_object["data"]) == 1
    assert answer_object["data"]["1"] == "10"
def test_textarea_answer(fake_questionnaire_store):
    routing_path = RoutingPath(["textarea-block"], section_id="section-1")
    fake_questionnaire_store.answer_store = AnswerStore(
        [Answer("textarea-answer", "example text.").to_dict()])

    question = {
        "id": "textarea-question",
        "answers": [{
            "id": "textarea-answer",
            "q_code": "1",
            "type": "TextArea"
        }],
    }

    questionnaire = make_schema("0.0.1", "section-1", "textarea-block",
                                "textarea-block", question)

    # When
    answer_object = convert_answers(QuestionnaireSchema(questionnaire),
                                    fake_questionnaire_store, routing_path)

    # Then
    assert len(answer_object["data"]) == 1
    assert answer_object["data"]["1"] == "example text."
def test_date_answer(fake_questionnaire_store):
    routing_path = RoutingPath(["date-block"], section_id="section-1")

    fake_questionnaire_store.answer_store = AnswerStore([
        create_answer("single-date-answer", "1990-02-01"),
        create_answer("month-year-answer", "1990-01"),
    ])

    question = {
        "id":
        "single-date-question",
        "answers": [
            {
                "id": "single-date-answer",
                "type": "Date",
                "q_code": "1"
            },
            {
                "id": "month-year-answer",
                "type": "MonthYearDate",
                "q_code": "2"
            },
        ],
    }

    questionnaire = make_schema("0.0.1", "section-1", "date-block",
                                "date-block", question)

    # When
    answer_object = convert_answers(QuestionnaireSchema(questionnaire),
                                    fake_questionnaire_store, routing_path)

    # Then
    assert len(answer_object["data"]) == 2
    assert answer_object["data"]["1"] == "01/02/1990"
    assert answer_object["data"]["2"] == "01/1990"
def test_converter_q_codes_for_empty_strings(fake_questionnaire_store):
    routing_path = RoutingPath(["crisps"], section_id="food")
    fake_questionnaire_store.answer_store = AnswerStore([
        Answer("crisps-answer", "").to_dict(),
        Answer("other-crisps-answer", "Ready salted").to_dict(),
    ])

    question = {
        "id":
        "crisps-question",
        "answers": [
            {
                "id": "crisps-answer",
                "type": "TextArea",
                "options": [],
                "q_code": "1"
            },
            {
                "id": "other-crisps-answer",
                "type": "TextArea",
                "options": [],
                "q_code": "2",
            },
        ],
    }

    questionnaire = make_schema("0.0.1", "section-1", "favourite-food",
                                "crisps", question)

    # When
    answer_object = convert_answers(QuestionnaireSchema(questionnaire),
                                    fake_questionnaire_store, routing_path)

    # Then
    assert len(answer_object["data"]) == 1
    assert answer_object["data"]["2"] == "Ready salted"
    def test_unit_answer(self):
        with self._app.test_request_context():
            routing_path = [Location(group_id='unit-group', group_instance=0, block_id='unit-block')]
            user_answer = [create_answer('unit-answer', 10, group_id='unit-group', block_id='unit-block')]

            questionnaire = make_schema('0.0.1', 'section-1', 'unit-block', 'unit-block', [
                {
                    'id': 'unit-question',
                    'answers': [
                        {
                            'id': 'unit-answer',
                            'type': 'Unit',
                            'q_code': '1'
                        }
                    ]
                }
            ])

            # When
            answer_object = convert_answers(self.metadata, self.collection_metadata, QuestionnaireSchema(questionnaire), AnswerStore(user_answer), routing_path)

            # Then
            self.assertEqual(len(answer_object['data']), 1)
            self.assertEqual(answer_object['data']['1'], '10')
Пример #9
0
    def test_relationship_answer(self):
        with self._app.test_request_context():
            routing_path = [
                Location(group_id='relationship-group',
                         group_instance=0,
                         block_id='relationship-block')
            ]
            user_answer = [
                create_answer('relationship-answer',
                              'Unrelated',
                              group_id='relationship-group',
                              block_id='relationship-block'),
                create_answer('relationship-answer',
                              'Partner',
                              group_id='relationship-group',
                              block_id='relationship-block',
                              answer_instance=1),
                create_answer('relationship-answer',
                              'Husband or wife',
                              group_id='relationship-group',
                              block_id='relationship-block',
                              answer_instance=2)
            ]

            questionnaire = make_schema(
                '0.0.2', 'section-1', 'relationship-group',
                'relationship-block', [{
                    'id':
                    'relationship-question',
                    'type':
                    'Relationship',
                    'answers': [{
                        'id':
                        'relationship-answer',
                        'q_code':
                        '1',
                        'type':
                        'Relationship',
                        'options': [{
                            'label': 'Husband or wife',
                            'value': 'Husband or wife'
                        }, {
                            'label': 'Partner',
                            'value': 'Partner'
                        }, {
                            'label': 'Unrelated',
                            'value': 'Unrelated'
                        }]
                    }]
                }])

            # When
            answer_object = convert_answers(self.metadata,
                                            self.collection_metadata,
                                            QuestionnaireSchema(questionnaire),
                                            AnswerStore(user_answer),
                                            routing_path)

            # Then
            self.assertEqual(len(answer_object['data']), 3)
            self.assertEqual(answer_object['data'][0]['value'], 'Unrelated')

            self.assertEqual(answer_object['data'][0]['group_instance'], 0)
            self.assertEqual(answer_object['data'][0]['answer_instance'], 0)

            self.assertEqual(answer_object['data'][1]['value'], 'Partner')
            self.assertEqual(answer_object['data'][1]['group_instance'], 0)
            self.assertEqual(answer_object['data'][1]['answer_instance'], 1)

            self.assertEqual(answer_object['data'][2]['value'],
                             'Husband or wife')
            self.assertEqual(answer_object['data'][2]['group_instance'], 0)
            self.assertEqual(answer_object['data'][2]['answer_instance'], 2)
def test_converter_checkboxes_with_missing_q_codes_uses_answer_q_code(
    fake_questionnaire_store, ):
    full_routing_path = [
        RoutingPath(["crisps"], section_id="food", list_item_id=None)
    ]

    fake_questionnaire_store.answer_store = AnswerStore(
        [Answer("crisps-answer", ["Ready salted", "Sweet chilli"]).to_dict()])

    question = {
        "id":
        "crisps-question",
        "answers": [{
            "id":
            "crisps-answer",
            "type":
            "Checkbox",
            "q_code":
            "0",
            "options": [
                {
                    "label": "Ready salted",
                    "value": "Ready salted",
                    "q_code": "1"
                },
                {
                    "label": "Sweet chilli",
                    "value": "Sweet chilli"
                },
                {
                    "label": "Cheese and onion",
                    "value": "Cheese and onion",
                    "q_code": "3",
                },
                {
                    "label": "Other",
                    "q_code": "4",
                    "description": "Choose any other flavour",
                    "value": "Other",
                    "detail_answer": {
                        "mandatory": True,
                        "id": "other-answer-mandatory",
                        "label": "Please specify other",
                        "type": "TextField",
                    },
                },
            ],
        }],
    }

    questionnaire = make_schema("0.0.1", "section-1", "favourite-food",
                                "crisps", question)

    # When
    answer_object = convert_answers(
        QuestionnaireSchema(questionnaire),
        fake_questionnaire_store,
        full_routing_path,
        SUBMITTED_AT,
    )

    # Then
    assert len(answer_object["data"]) == 1
    assert answer_object["data"]["0"], "['Ready salted' == 'Sweet chilli']"
Пример #11
0
    def test_converter_checkboxes_with_q_codes_and_empty_other_value(self):
        with self._app.test_request_context():
            routing_path = [
                Location(group_id='favourite-food',
                         group_instance=0,
                         block_id='crisps')
            ]
            answers = [
                create_answer('crisps-answer', ['Ready salted', 'Other'],
                              group_id='favourite-food',
                              block_id='crisps')
            ]

            answers += [
                create_answer('other-answer-mandatory',
                              '',
                              group_id='favourite-food',
                              block_id='crisps')
            ]

            questionnaire = make_schema(
                '0.0.1', 'section-1', 'favourite-food', 'crisps', [{
                    'id':
                    'crisps-question',
                    'answers': [{
                        'id':
                        'crisps-answer',
                        'type':
                        'Checkbox',
                        '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',
                            'q_code': '4',
                            'description': 'Choose any other flavour',
                            'value': 'Other',
                            'child_answer_id': 'other-answer-mandatory'
                        }]
                    }, {
                        'parent_answer_id': 'crisps-answer',
                        'mandatory': True,
                        'id': 'other-answer-mandatory',
                        'label': 'Please specify other',
                        'type': 'TextField'
                    }]
                }])

            # When STANDARD CHECKBOX
            answer_object = convert_answers(self.metadata,
                                            QuestionnaireSchema(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'], 'Other')

            # when MUTUALLY EXCLUSIVE CHECKBOX
            questionnaire['sections'][0]['groups'][0]['blocks'][0][
                'questions'][0]['answers'][0][
                    'type'] = 'MutuallyExclusiveCheckbox'
            answer_object = convert_answers(self.metadata,
                                            QuestionnaireSchema(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'], 'Other')
Пример #12
0
    def test_get_checkbox_answer_with_duplicate_child_answer_ids(self):
        with self._app.test_request_context():
            routing_path = [
                Location(group_id='favourite-food',
                         group_instance=0,
                         block_id='crisps')
            ]
            answers = [
                create_answer('crisps-answer', ['Ready salted', 'Other'],
                              group_id='favourite-food',
                              block_id='crisps')
            ]

            answers += [
                create_answer('other-answer-mandatory',
                              'Other',
                              group_id='favourite-food',
                              block_id='crisps',
                              group_instance=1)
            ]
            answers += [
                create_answer('other-answer-mandatory',
                              'Other',
                              group_id='favourite-food',
                              block_id='crisps',
                              group_instance=1)
            ]

            questionnaire = make_schema(
                '0.0.1', 'section-1', 'favourite-food', 'crisps', [{
                    'id':
                    'crisps-question',
                    'answers': [{
                        'id':
                        'crisps-answer',
                        'type':
                        'Checkbox',
                        'options': [{
                            'label': 'Other',
                            'q_code': '4',
                            'description': 'Choose any other flavour',
                            'value': 'Other',
                            'child_answer_id': 'other-answer-mandatory'
                        }]
                    }]
                }])

        #  STANDARD CHECKBOX
        with self.assertRaises(Exception) as err:
            convert_answers(self.metadata, QuestionnaireSchema(questionnaire),
                            AnswerStore(answers), routing_path)
        self.assertEqual(
            'Multiple answers found for {}'.format('other-answer-mandatory'),
            str(err.exception))

        #  MUTUALLY EXCLUSIVE CHECKBOX
        questionnaire['sections'][0]['groups'][0]['blocks'][0]['questions'][0][
            'answers'][0]['type'] = 'MutuallyExclusiveCheckbox'
        with self.assertRaises(Exception) as err:
            convert_answers(self.metadata, QuestionnaireSchema(questionnaire),
                            AnswerStore(answers), routing_path)
        self.assertEqual(
            'Multiple answers found for {}'.format('other-answer-mandatory'),
            str(err.exception))
Пример #13
0
    def test_converter_checkboxes_with_q_codes(self):
        with self._app.test_request_context():
            routing_path = [
                Location(group_id='favourite-food',
                         group_instance=0,
                         block_id='crisps')
            ]
            answers = [
                create_answer('crisps-answer',
                              ['Ready salted', 'Sweet chilli'],
                              group_id='favourite-food',
                              block_id='crisps')
            ]

            questionnaire = make_schema(
                '0.0.1', 'section-1', 'favourite-food', 'crisps', [{
                    'id':
                    'crisps-question',
                    'answers': [{
                        'id':
                        'crisps-answer',
                        'type':
                        'Checkbox',
                        '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',
                            'q_code': '4',
                            'description': 'Choose any other flavour',
                            'value': 'Other',
                            'child_answer_id': 'other-answer-mandatory'
                        }]
                    }, {
                        'parent_answer_id': 'crisps-answer',
                        'mandatory': True,
                        'id': 'other-answer-mandatory',
                        'label': 'Please specify other',
                        'type': 'TextField'
                    }]
                }])

            # When
            answer_object = convert_answers(self.metadata,
                                            self.collection_metadata,
                                            QuestionnaireSchema(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']['2'], 'Sweet chilli')