Example #1
0
 def test_invalid_field_type_raises_on_invalid(self):
     # Given
     invalid_field_type = 'Football'
     # When / Then
     with self.assertRaises(KeyError):
         get_field({'type': invalid_field_type}, 'Football Field',
                   error_messages, self.answer_store, self.metadata)
Example #2
0
    def test_month_year_date_field(self):
        date_json = {
            "guidance": "",
            "id": "month-year-answer",
            "label": "Date",
            "mandatory": True,
            "options": [],
            "q_code": "11",
            "type": "MonthYearDate",
            "validation": {
                "messages": {
                    "INVALID_DATE":
                    "The date entered is not valid.  Please correct your answer.",
                    "MANDATORY": "Please provide an answer to continue."
                }
            }
        }

        unbound_field = get_field(date_json, date_json['label'],
                                  error_messages)

        self.assertTrue(unbound_field.field_class == FormField)
        self.assertEquals(unbound_field.kwargs['label'], date_json['label'])
        self.assertEquals(unbound_field.kwargs['description'],
                          date_json['guidance'])
Example #3
0
    def test_integer_field(self):
        integer_json = {
            "alias": "chewies_age",
            "guidance": "",
            "id": "6cf5c72a-c1bf-4d0c-af6c-d0f07bc5b65b",
            "label": "How old is Chewy?",
            "mandatory": True,
            "q_code": "1",
            "type": "Integer",
            "validation": {
                "messages": {
                    "INTEGER_TOO_LARGE":
                    "No one lives that long, not even Yoda",
                    "NEGATIVE_INTEGER": "Negative age you can not be.",
                    "NOT_INTEGER": "Please enter your age."
                }
            }
        }

        unbound_field = get_field(integer_json, integer_json['label'],
                                  error_messages)

        self.assertTrue(unbound_field.field_class == CustomIntegerField)
        self.assertEquals(unbound_field.kwargs['label'], integer_json['label'])
        self.assertEquals(unbound_field.kwargs['description'],
                          integer_json['guidance'])
Example #4
0
    def test_currency_field(self):
        currency_json = {
            'guidance': '',
            'id': 'a04a516d-502d-4068-bbed-a43427c68cd9',
            'label': '',
            'mandatory': True,
            'q_code': '2',
            'type': 'Currency',
            'validation': {
                'messages': {
                    'NUMBER_TOO_LARGE':
                    'How much, fool you must be',
                    'NUMBER_TOO_SMALL':
                    'How can it be negative?',
                    'INVALID_NUMBER':
                    'Please only enter whole numbers into the field.'
                }
            }
        }

        unbound_field = get_field(currency_json, currency_json['label'],
                                  error_messages, self.answer_store,
                                  self.metadata)

        self.assertEqual(unbound_field.field_class, CustomIntegerField)
        self.assertEqual(unbound_field.kwargs['label'], currency_json['label'])
        self.assertEqual(unbound_field.kwargs['description'],
                         currency_json['guidance'])
    def test_dropdown_field(self):
        dropdown_json = {
            'type': 'Dropdown',
            'id': 'dropdown-mandatory-with-label-answer',
            'mandatory': True,
            'label': 'Please choose an option',
            'description': 'This is a mandatory dropdown, therefore you must select a value!.',
            'options': [
                {
                    'label': 'Liverpool',
                    'value': 'Liverpool'
                },
                {
                    'label': 'Chelsea',
                    'value': 'Chelsea'
                },
                {
                    'label': 'Rugby is better!',
                    'value': 'Rugby is better!'
                }
            ]
        }

        unbound_field = get_field(dropdown_json, dropdown_json['label'], error_messages, self.answer_store,
                                  self.metadata)

        expected_choices = [('', 'Select an answer')] + \
                           [(option['label'], option['value']) for option in dropdown_json['options']]

        self.assertEqual(unbound_field.field_class, SelectField)
        self.assertEqual(unbound_field.kwargs['label'], dropdown_json['label'])
        self.assertEqual(unbound_field.kwargs['description'], '')
        self.assertEqual(unbound_field.kwargs['default'], '')
        self.assertEqual(unbound_field.kwargs['choices'], expected_choices)
Example #6
0
    def test_integer_field(self):
        integer_json = {
            'alias': 'chewies_age',
            'guidance': '',
            'id': 'chewies-age-answer',
            'label': 'How old is Chewy?',
            'mandatory': True,
            'q_code': '1',
            'type': 'Number',
            'validation': {
                'messages': {
                    'NUMBER_TOO_LARGE':
                    'No one lives that long, not even Yoda',
                    'NUMBER_TOO_SMALL': 'Negative age you can not be.',
                    'INVALID_NUMBER': 'Please enter your age.'
                }
            }
        }

        unbound_field = get_field(integer_json, integer_json['label'],
                                  error_messages, self.answer_store,
                                  self.metadata)

        self.assertEqual(unbound_field.field_class, CustomIntegerField)
        self.assertEqual(unbound_field.kwargs['label'], integer_json['label'])
        self.assertEqual(unbound_field.kwargs['description'],
                         integer_json['guidance'])
Example #7
0
    def test_decimal_field(self):
        decimal_json = {
            'guidance': '',
            'id': 'lightsaber-cost-answer',
            'label': 'How hot is a lightsaber in degrees C?',
            'mandatory': False,
            'type': 'Number',
            'decimal_places': 2,
            'validation': {
                'messages': {
                    'NUMBER_TOO_LARGE':
                    'Thats hotter then the sun, Jar Jar Binks you must be',
                    'NUMBER_TOO_SMALL':
                    'How can it be negative?',
                    'INVALID_NUMBER':
                    'Please only enter whole numbers into the field.'
                }
            }
        }

        unbound_field = get_field(decimal_json, decimal_json['label'],
                                  error_messages, self.answer_store,
                                  self.metadata)

        self.assertEqual(unbound_field.field_class, CustomDecimalField)
        self.assertEqual(unbound_field.kwargs['label'], decimal_json['label'])
        self.assertEqual(unbound_field.kwargs['description'],
                         decimal_json['guidance'])
Example #8
0
    def test_year_date_field(self):
        date_json = {
            'guidance': '',
            'id': 'month-year-answer',
            'label': 'Date',
            'mandatory': True,
            'options': [],
            'q_code': '11',
            'type': 'YearDate',
            'validation': {
                'messages': {
                    'INVALID_DATE':
                    'The date entered is not valid.  Please correct your answer.',
                    'MANDATORY': 'Please provide an answer to continue.'
                }
            }
        }

        with self.app_request_context('/'):
            unbound_field = get_field(date_json, date_json['label'],
                                      error_messages, self.answer_store,
                                      self.metadata)

        self.assertEqual(unbound_field.field_class, YearField)
        self.assertEqual(unbound_field.kwargs['label'], date_json['label'])
        self.assertEqual(unbound_field.kwargs['description'],
                         date_json['guidance'])
    def test_percentage_field(self):
        percentage_json = {
            'description': '',
            'id': 'percentage-turnover-2016-market-new-answer',
            'label': 'New to the market in 2014-2016',
            'mandatory': False,
            'q_code': '0810',
            'type': 'Percentage',
            'max_value': {
                'value': 100
            },
            'validation': {
                'messages': {
                    'NUMBER_TOO_LARGE': 'How much, fool you must be',
                    'NUMBER_TOO_SMALL': 'How can it be negative?',
                    'INVALID_NUMBER': 'Please only enter whole numbers into the field.'
                }
            }
        }

        unbound_field = get_field(percentage_json, percentage_json['label'], error_messages, self.answer_store,
                                  self.metadata)

        self.assertEqual(unbound_field.field_class, CustomIntegerField)
        self.assertEqual(unbound_field.kwargs['label'], percentage_json['label'])
        self.assertEqual(unbound_field.kwargs['description'], percentage_json['description'])
Example #10
0
    def test_currency_field(self):
        currency_json = {
            "guidance": "",
            "id": "a04a516d-502d-4068-bbed-a43427c68cd9",
            "label": "",
            "mandatory": True,
            "q_code": "2",
            "type": "Currency",
            "validation": {
                "messages": {
                    "INTEGER_TOO_LARGE":
                    "How much, fool you must be",
                    "NEGATIVE_INTEGER":
                    "How can it be negative?",
                    "NOT_INTEGER":
                    "Please only enter whole numbers into the field."
                }
            }
        }

        unbound_field = get_field(currency_json, currency_json['label'],
                                  error_messages)

        self.assertTrue(unbound_field.field_class == CustomIntegerField)
        self.assertEquals(unbound_field.kwargs['label'],
                          currency_json['label'])
        self.assertEquals(unbound_field.kwargs['description'],
                          currency_json['guidance'])
Example #11
0
    def test_positive_integer_field(self):
        integer_json = {
            "guidance": "",
            "id": "pre49d93-cbdc-4bcb-adb2-0e0af6c9a07c",
            "label": "How hot is a lightsaber in degrees C?",
            "mandatory": False,
            "type": "PositiveInteger",
            "validation": {
                "messages": {
                    "INTEGER_TOO_LARGE":
                    "Thats hotter then the sun, Jar Jar Binks you must be",
                    "NEGATIVE_INTEGER":
                    "How can it be negative?",
                    "NOT_INTEGER":
                    "Please only enter whole numbers into the field."
                }
            }
        }

        unbound_field = get_field(integer_json, integer_json['label'],
                                  error_messages)

        self.assertTrue(unbound_field.field_class == CustomIntegerField)
        self.assertEquals(unbound_field.kwargs['label'], integer_json['label'])
        self.assertEquals(unbound_field.kwargs['description'],
                          integer_json['guidance'])
    def test_duration_field(self):
        date_json = {
            'guidance': '',
            'id': 'year-month-answer',
            'label': 'Duration',
            'mandatory': True,
            'options': [],
            'q_code': '11',
            'type': 'Duration',
            'units': ['years', 'months'],
            'validation': {
                'messages': {
                    'INVALID_DURATION': 'The duration entered is not valid.  Please correct your answer.',
                    'MANDATORY_DURATION': 'Please provide a duration to continue.'
                }
            }
        }

        with self.app_request_context('/'):
            unbound_field = get_field(date_json, date_json['label'], error_messages, self.answer_store,
                                      self.metadata)

        self.assertEqual(unbound_field.field_class, FormField)
        self.assertEqual(unbound_field.kwargs['label'], date_json['label'])
        self.assertEqual(unbound_field.kwargs['description'], date_json['guidance'])
def get_answer_fields(question, data, error_messages, schema, answer_store,
                      metadata, group_instance, group_instance_id):
    answer_fields = {}
    for answer in question.get('answers', []):

        for option in answer.get('options', []):
            disable_validation = False
            if 'detail_answer' in option:
                detail_answer = option['detail_answer']
                detail_answer_error_messages = detail_answer['validation'][
                    'messages'] if detail_answer.get(
                        'validation') else error_messages
                if isinstance(data, MultiDict):
                    option_value_in_data = option['value'] in data.to_dict(
                        flat=False).get(answer['id'], [])
                else:
                    option_value_in_data = option['value'] in dict(data).get(
                        answer['id'], [])

                if not option_value_in_data:
                    disable_validation = True

                answer_fields[detail_answer['id']] = get_field(
                    detail_answer,
                    detail_answer.get('label'),
                    detail_answer_error_messages,
                    answer_store,
                    metadata,
                    group_instance=group_instance,
                    disable_validation=disable_validation)

        name = answer.get('label') or get_question_title(
            question, answer_store, schema, metadata, group_instance,
            group_instance_id)
        answer_fields[answer['id']] = get_field(answer,
                                                name,
                                                error_messages,
                                                answer_store,
                                                metadata,
                                                group_instance=group_instance)

    return answer_fields
def get_answer_fields(question, data, error_messages):
    answer_fields = {}
    for answer in question['answers']:
        if 'parent_answer_id' in answer and answer['parent_answer_id'] in data and \
                data[answer['parent_answer_id']] == 'Other':
            answer['mandatory'] = \
                next(a['mandatory'] for a in question['answers'] if a['id'] == answer['parent_answer_id'])

        name = answer.get('label') or question.get('title')
        answer_fields[answer['id']] = get_field(answer, name, error_messages)
    return answer_fields
Example #15
0
def get_answer_fields(question, data, error_messages, schema, answer_store, metadata, group_instance, group_instance_id):
    answer_fields = {}
    for answer in question.get('answers', []):
        if 'parent_answer_id' in answer and answer['parent_answer_id'] in data and \
                data[answer['parent_answer_id']] == 'Other':
            answer['mandatory'] = \
                next(a['mandatory'] for a in question['answers'] if a['id'] == answer['parent_answer_id'])

        name = answer.get('label') or get_question_title(question, answer_store, schema, metadata, group_instance, group_instance_id)
        answer_fields[answer['id']] = get_field(answer, name, error_messages, answer_store, metadata, group_instance=group_instance)
    return answer_fields
Example #16
0
    def test_string_field(self):
        textfield_json = {
            "id": "job-title-answer",
            "label": "Job title",
            "mandatory": False,
            "guidance": "<p>Please enter your job title in the space provided.</p>",
            "type": "TextField"
        }
        unbound_field = get_field(textfield_json, textfield_json['label'], error_messages)

        self.assertTrue(unbound_field.field_class == StringField)
        self.assertEquals(unbound_field.kwargs['label'], textfield_json['label'])
        self.assertEquals(unbound_field.kwargs['description'], textfield_json['guidance'])
    def test_string_field(self):
        textfield_json = {
            'id': 'job-title-answer',
            'label': 'Job title',
            'mandatory': False,
            'guidance': '<p>Please enter your job title in the space provided.</p>',
            'type': 'TextField'
        }
        unbound_field = get_field(textfield_json, textfield_json['label'], error_messages, self.answer_store,
                                  self.metadata)

        self.assertEqual(unbound_field.field_class, StringField)
        self.assertEqual(unbound_field.kwargs['label'], textfield_json['label'])
        self.assertEqual(unbound_field.kwargs['description'], textfield_json['guidance'])
Example #18
0
    def test_text_area_field(self):
        textarea_json = {
            "guidance": "",
            "id": "answer",
            "label": "Enter your comments",
            "mandatory": False,
            "q_code": "0",
            "type": "TextArea"
        }

        unbound_field = get_field(textarea_json, textarea_json['label'], error_messages)

        self.assertTrue(unbound_field.field_class == TextAreaField)
        self.assertEquals(unbound_field.kwargs['label'], textarea_json['label'])
        self.assertEquals(unbound_field.kwargs['description'], textarea_json['guidance'])
Example #19
0
    def test_checkbox_field(self):
        checkbox_json = {
            'guidance':
            '',
            'id':
            'opening-crawler-answer',
            'label':
            '',
            'mandatory':
            False,
            'options': [{
                'label': 'Luke Skywalker',
                'value': 'Luke Skywalker'
            }, {
                'label': 'Han Solo',
                'value': 'Han Solo'
            }, {
                'label': 'The Emperor',
                'value': 'The Emperor'
            }, {
                'label': 'R2D2',
                'value': 'R2D2'
            }, {
                'label': 'Senator Amidala',
                'value': 'Senator Amidala'
            }, {
                'label': 'Yoda',
                'value': 'Yoda'
            }],
            'q_code':
            '7',
            'type':
            'Checkbox'
        }

        unbound_field = get_field(checkbox_json, checkbox_json['label'],
                                  error_messages, self.answer_store,
                                  self.metadata)

        expected_choices = [(option['value'], option['label'], None)
                            for option in checkbox_json['options']]

        self.assertEqual(unbound_field.field_class, CustomSelectMultipleField)
        self.assertEqual(unbound_field.kwargs['label'], checkbox_json['label'])
        self.assertEqual(unbound_field.kwargs['description'],
                         checkbox_json['guidance'])
        self.assertEqual(unbound_field.kwargs['choices'], expected_choices)
        self.assertEqual(len(unbound_field.kwargs['validators']), 1)
    def test_text_area_field(self):
        textarea_json = {
            'guidance': '',
            'id': 'answer',
            'label': 'Enter your comments',
            'mandatory': False,
            'q_code': '0',
            'type': 'TextArea'
        }

        unbound_field = get_field(textarea_json, textarea_json['label'], error_messages, self.answer_store,
                                  self.metadata)

        self.assertEqual(unbound_field.field_class, MaxTextAreaField)
        self.assertEqual(unbound_field.kwargs['label'], textarea_json['label'])
        self.assertEqual(unbound_field.kwargs['description'], textarea_json['guidance'])
Example #21
0
    def test_checkbox_field(self):
        checkbox_json = {
            "guidance":
            "",
            "id":
            "5587eb9b-f24e-4dc0-ac94-66117b896c10",
            "label":
            "",
            "mandatory":
            False,
            "options": [{
                "label": "Luke Skywalker",
                "value": "Luke Skywalker"
            }, {
                "label": "Han Solo",
                "value": "Han Solo"
            }, {
                "label": "The Emperor",
                "value": "The Emperor"
            }, {
                "label": "R2D2",
                "value": "R2D2"
            }, {
                "label": "Senator Amidala",
                "value": "Senator Amidala"
            }, {
                "label": "Yoda",
                "value": "Yoda"
            }],
            "q_code":
            "7",
            "type":
            "Checkbox"
        }

        unbound_field = get_field(checkbox_json, checkbox_json['label'],
                                  error_messages)

        expected_choices = [(option['label'], option['value'])
                            for option in checkbox_json['options']]

        self.assertTrue(unbound_field.field_class == SelectMultipleField)
        self.assertEquals(unbound_field.kwargs['label'],
                          checkbox_json['label'])
        self.assertEquals(unbound_field.kwargs['description'],
                          checkbox_json['guidance'])
        self.assertEquals(unbound_field.kwargs['choices'], expected_choices)
Example #22
0
    def test_radio_field(self):
        radio_json = {
            'guidance':
            '',
            'id':
            'choose-your-side-answer',
            'label':
            'Choose a side',
            'mandatory':
            True,
            'options': [{
                'label': 'Light Side',
                'value': 'Light Side',
                'description': 'The light side of the Force'
            }, {
                'label': 'Dark Side',
                'value': 'Dark Side',
                'description': 'The dark side of the Force'
            }, {
                'label': 'I prefer Star Trek',
                'value': 'I prefer Star Trek'
            }, {
                'label': 'Other',
                'value': 'Other'
            }],
            'q_code':
            '20',
            'type':
            'Radio'
        }

        unbound_field = get_field(radio_json, radio_json['label'],
                                  error_messages, self.answer_store,
                                  self.metadata)

        expected_choices = [(option['label'], option['value'], None)
                            for option in radio_json['options']]

        self.assertEqual(unbound_field.field_class, CustomSelectField)
        self.assertTrue(unbound_field.kwargs['coerce'],
                        _coerce_str_unless_none)
        self.assertEqual(unbound_field.kwargs['label'], radio_json['label'])
        self.assertEqual(unbound_field.kwargs['description'],
                         radio_json['guidance'])
        self.assertEqual(unbound_field.kwargs['choices'], expected_choices)
    def test_mutually_exclusive_checkbox_field(self):
        checkbox_json = {
            'guidance': '',
            'id': 'opening-crawler-answer',
            'label': '',
            'mandatory': False,
            'options': [
                {
                    'label': 'Luke Skywalker',
                    'value': 'Luke Skywalker'
                },
                {
                    'label': 'Han Solo',
                    'value': 'Han Solo'
                },
                {
                    'label': 'The Emperor',
                    'value': 'The Emperor'
                },
                {
                    'label': 'R2D2',
                    'value': 'R2D2'
                },
                {
                    'label': 'Senator Amidala',
                    'value': 'Senator Amidala'
                },
                {
                    'label': 'I prefer star trek',
                    'value': 'None'
                }
            ],
            'type': 'MutuallyExclusiveCheckbox'
        }

        unbound_field = get_field(checkbox_json, checkbox_json['label'], error_messages, self.answer_store,
                                  self.metadata)

        expected_choices = [(option['value'], option['label']) for option in checkbox_json['options']]

        self.assertEqual(unbound_field.field_class, SelectMultipleField)
        self.assertEqual(unbound_field.kwargs['label'], checkbox_json['label'])
        self.assertEqual(unbound_field.kwargs['description'], checkbox_json['guidance'])
        self.assertEqual(unbound_field.kwargs['choices'], expected_choices)
        self.assertEqual(type(unbound_field.kwargs['validators'][1]), MutuallyExclusive)
Example #24
0
    def test_radio_field(self):
        radio_json = {
            "guidance":
            "",
            "id":
            "ca3ce3a3-ae44-4e30-8f85-5b6a7a2fb23c",
            "label":
            "Choose a side",
            "mandatory":
            True,
            "options": [{
                "label": "Light Side",
                "value": "Light Side",
                "description": "The light side of the Force"
            }, {
                "label": "Dark Side",
                "value": "Dark Side",
                "description": "The dark side of the Force"
            }, {
                "label": "I prefer Star Trek",
                "value": "I prefer Star Trek"
            }, {
                "label": "Other",
                "value": "Other"
            }],
            "q_code":
            "20",
            "type":
            "Radio"
        }

        unbound_field = get_field(radio_json, radio_json['label'],
                                  error_messages)

        expected_choices = [(option['label'], option['value'])
                            for option in radio_json['options']]

        self.assertTrue(unbound_field.field_class == SelectField)
        self.assertEquals(unbound_field.kwargs['label'], radio_json['label'])
        self.assertEquals(unbound_field.kwargs['description'],
                          radio_json['guidance'])
        self.assertEquals(unbound_field.kwargs['choices'], expected_choices)
Example #25
0
    def test_percentage_field(self):
        percentage_json = {
            "description": "",
            "id": "percentage-turnover-2016-market-new-answer",
            "label": "New to the market in 2014-2016",
            "mandatory": False,
            "q_code": "0810",
            "type": "Percentage",
            "validation": {
                "messages" : {
                    "INTEGER_TOO_LARGE": "The maximum value allowed is 100. Please correct your answer."
                }
            }
        }

        unbound_field = get_field(percentage_json, percentage_json['label'], error_messages)

        self.assertTrue(unbound_field.field_class == CustomIntegerField)
        self.assertEquals(unbound_field.kwargs['label'], percentage_json['label'])
        self.assertEquals(unbound_field.kwargs['description'], percentage_json['description'])