コード例 #1
0
    def test_generate_relationship_form_errors_are_correctly_mapped(self):
        with self.test_request_context():
            survey = load_schema_file("test_relationship_household.json")
            block_json = SchemaHelper.get_block(survey, 'relationships')
            error_messages = SchemaHelper.get_messages(survey)

            answer = SchemaHelper.get_first_answer_for_block(block_json)

            generated_form = generate_relationship_form(
                block_json, 3, None, error_messages=error_messages)

            form = generate_relationship_form(
                block_json,
                3, {
                    'csrf_token': generated_form.csrf_token.current_token,
                    '{answer_id}-0'.format(answer_id=answer['id']): '1',
                    '{answer_id}-1'.format(answer_id=answer['id']): '3',
                },
                error_messages=error_messages)

            form.validate()
            mapped_errors = form.map_errors()

            message = "Not a valid choice"

            self.assertTrue(
                self._error_exists(answer['id'], message, mapped_errors))
コード例 #2
0
    def test_generate_relationship_form_creates_form_from_data(self):
        with self.test_request_context():
            survey = load_schema_file("test_relationship_household.json")
            block_json = SchemaHelper.get_block(survey, 'relationships')
            error_messages = SchemaHelper.get_messages(survey)

            answer = SchemaHelper.get_first_answer_for_block(block_json)

            form = generate_relationship_form(
                block_json,
                3, {
                    '{answer_id}-0'.format(answer_id=answer['id']):
                    'Husband or Wife',
                    '{answer_id}-1'.format(answer_id=answer['id']):
                    'Brother or Sister',
                    '{answer_id}-2'.format(answer_id=answer['id']):
                    'Relation - other',
                },
                error_messages=error_messages)

            self.assertTrue(hasattr(form, answer['id']))

            expected_form_data = [
                'Husband or Wife', 'Brother or Sister', 'Relation - other'
            ]
            self.assertEqual(form.data[answer['id']], expected_form_data)
コード例 #3
0
    def test_post_form_for_radio_other_selected(self):
        with self.test_request_context():
            survey = load_schema_file("test_radio.json")

            block_json = SchemaHelper.get_block(survey, 'radio-mandatory')
            location = Location('radio', 0, 'radio-mandatory')
            error_messages = SchemaHelper.get_messages(survey)

            answer_store = AnswerStore([{
                'answer_id': 'radio-mandatory-answer',
                'block_id': 'radio-mandatory',
                'value': 'Other',
                'answer_instance': 0,
            }, {
                'answer_id': 'other-answer-mandatory',
                'block_id': 'block-1',
                'value': 'Other text field value',
                'answer_instance': 0,
            }])

            radio_answer = SchemaHelper.get_first_answer_for_block(block_json)
            text_answer = 'other-answer-mandatory'

            form, _ = post_form_for_location(
                block_json, location, answer_store,
                MultiDict({
                    '{answer_id}'.format(answer_id=radio_answer['id']):
                    'Other',
                    '{answer_id}'.format(answer_id=text_answer):
                    'Other text field value',
                }), error_messages)

            other_text_field = getattr(form, 'other-answer-mandatory')
            self.assertEqual(other_text_field.data, 'Other text field value')
コード例 #4
0
def generate_relationship_form(block_json, number_of_entries, data,
                               error_messages):

    answer = SchemaHelper.get_first_answer_for_block(block_json)

    class HouseHoldRelationshipForm(FlaskForm):
        question_errors = {}

        def map_errors(self):
            ordered_errors = []

            if len(self.errors) > 0:
                for answer_id, error_list in self.errors.items():
                    for errors in error_list:
                        for error in errors:
                            ordered_errors.append((answer_id, error))

            return ordered_errors

        def answer_errors(self, input_id):
            return [
                error[1] for error in self.map_errors() if input_id == error[0]
            ]

        def serialise(self, location):
            """
            Returns a list of answers representing the form data
            :param location: The location to associate the form data with
            :return:
            """
            list_field = getattr(self, answer['id'])

            return serialise_relationship_answers(location, answer['id'],
                                                  list_field.data)

    choices = [('', 'Select relationship')] + build_choices(answer['options'])

    field = FieldList(SelectField(
        label=answer.get('guidance'),
        description=answer.get('label'),
        choices=choices,
        default='',
        validators=get_mandatory_validator(answer, error_messages),
    ),
                      min_entries=number_of_entries)

    setattr(HouseHoldRelationshipForm, answer['id'], field)

    if data:
        form = HouseHoldRelationshipForm(MultiDict(data))
    else:
        form = HouseHoldRelationshipForm()

    return form
コード例 #5
0
    def test_generate_relationship_form_creates_empty_form(self):
        survey = load_schema_file("test_relationship_household.json")
        block_json = SchemaHelper.get_block(survey, 'relationships')
        error_messages = SchemaHelper.get_messages(survey)

        answer = SchemaHelper.get_first_answer_for_block(block_json)

        form = generate_relationship_form(block_json,
                                          3, {},
                                          error_messages=error_messages)

        self.assertTrue(hasattr(form, answer['id']))
        self.assertEqual(len(form.data[answer['id']]), 3)
コード例 #6
0
    def test_get_form_for_household_relationship(self):
        with self.test_request_context():
            survey = load_schema_file("census_household.json")

            block_json = SchemaHelper.get_block(survey,
                                                'household-relationships')
            location = Location('who-lives-here-relationship', 0,
                                'household-relationships')
            error_messages = SchemaHelper.get_messages(survey)

            answer_store = AnswerStore([{
                'group_id': 'who-lives-here-relationship',
                'group_instance': 0,
                'answer_id': 'first-name',
                'block_id': 'household-composition',
                'value': 'Joe',
                'answer_instance': 0,
            }, {
                'group_id': 'who-lives-here-relationship',
                'group_instance': 0,
                'answer_id': 'last-name',
                'block_id': 'household-composition',
                'value': 'Bloggs',
                'answer_instance': 0,
            }, {
                'group_id': 'who-lives-here-relationship',
                'group_instance': 1,
                'answer_id': 'first-name',
                'block_id': 'household-composition',
                'value': 'Jane',
                'answer_instance': 1,
            }, {
                'group_id': 'who-lives-here-relationship',
                'group_instance': 1,
                'answer_id': 'last-name',
                'block_id': 'household-composition',
                'value': 'Bloggs',
                'answer_instance': 1,
            }])
            form, _ = get_form_for_location(block_json, location, answer_store,
                                            error_messages)

            answer = SchemaHelper.get_first_answer_for_block(block_json)

            self.assertTrue(hasattr(form, answer['id']))

            field_list = getattr(form, answer['id'])

            # With two people, we need to define 1 relationship
            self.assertEqual(len(field_list.entries), 1)
コード例 #7
0
    def test_post_form_for_household_relationship(self):
        with self.test_request_context():
            survey = load_schema_file("census_household.json")

            block_json = SchemaHelper.get_block(survey,
                                                'household-relationships')
            location = Location('who-lives-here-relationship', 0,
                                'household-relationships')
            error_messages = SchemaHelper.get_messages(survey)

            answer_store = AnswerStore([{
                'answer_id': 'first-name',
                'block_id': 'household-composition',
                'value': 'Joe',
                'answer_instance': 0,
            }, {
                'answer_id': 'last-name',
                'block_id': 'household-composition',
                'value': 'Bloggs',
                'answer_instance': 0,
            }, {
                'answer_id': 'first-name',
                'block_id': 'household-composition',
                'value': 'Jane',
                'answer_instance': 1,
            }, {
                'answer_id': 'last-name',
                'block_id': 'household-composition',
                'value': 'Bloggs',
                'answer_instance': 1,
            }])

            answer = SchemaHelper.get_first_answer_for_block(block_json)

            form, _ = post_form_for_location(
                block_json, location, answer_store,
                MultiDict(
                    {'{answer_id}-0'.format(answer_id=answer['id']): '3'}),
                error_messages)

            self.assertTrue(hasattr(form, answer['id']))

            field_list = getattr(form, answer['id'])

            # With two people, we need to define 1 relationship
            self.assertEqual(len(field_list.entries), 1)

            # Check the data matches what was passed from request
            self.assertEqual(field_list.entries[0].data, "3")