示例#1
0
 def test_should_validate_single_answer(self):
     with self.assertRaises(AnswerHasTooManyValuesException) as e:
         clinic_field = SelectField(name="clinic type", code="Q1", label="What type of clinic is it?",
                                    language="eng", options=["village", "urban"], single_select_flag=True,
                                    ddtype=self.ddtype)
         clinic_field.validate("vu")
     self.assertEqual(e.exception.message, "Answer vu for question Q1 contains more than one value.")
示例#2
0
 def test_should_escape_special_characters_from_requested_form(self):
     dbm = Mock()
     questionnaire_mock = Mock()
     field1 = SelectField(name='name&',
                          label='name&',
                          code='selectcode',
                          instruction='instruction&',
                          options=[{
                              'text': 'option1&',
                              'val': 'o1'
                          }])
     questionnaire_mock.name = '<mock_name'
     questionnaire_mock.fields = [field1]
     questionnaire_mock.form_code = 'form_code'
     questionnaire_mock.id = 'id'
     questionnaire_mock.unique_id_field = None
     questionnaire_mock.xform = None
     with patch("mangrove.transport.xforms.xform.FormModel"
                ) as form_model_mock:
         form_model_mock.get.return_value = questionnaire_mock
         one_of_unicode_unknown_ = xform_for(dbm, "someFormId", 'rep1')
         expected = unicode(expected_xform_with_escaped_characters)
         self.assertTrue(
             self.checker.check_output(one_of_unicode_unknown_, expected,
                                       0))
    def test_should_replace_answer_option_values_with_options_text_when_answer_type_is_changed_from_single_select_choice_field(
            self):
        survey_response_doc = SurveyResponseDocument(values={
            'q1': 'a',
        })
        survey_response = SurveyResponse(Mock())
        survey_response._doc = survey_response_doc
        choice_field = SelectField(name='question one',
                                   code='q1',
                                   label='question one',
                                   options=[("one", "a"), ("two", "b"),
                                            ("three", "c"), ("four", "d")],
                                   single_select_flag=True)
        text_field = TextField(name='question one',
                               code='q1',
                               label='question one')

        questionnaire_form_model = Mock(spec=FormModel)
        questionnaire_form_model.form_code = 'test_form_code'
        questionnaire_form_model.fields = [text_field]
        questionnaire_form_model.get_field_by_code_and_rev.return_value = choice_field
        request_dict = construct_request_dict(survey_response,
                                              questionnaire_form_model, 'id')

        expected_dict = {
            'q1': 'one',
            'form_code': 'test_form_code',
            'dsid': 'id'
        }
        self.assertEqual(request_dict, expected_dict)
示例#4
0
 def _create_form_model(self):
     self.entity_type = ["HealthFacility", "Clinic"]
     question1 = UniqueIdField('clinic',
                               name="entity_question",
                               code="ID",
                               label="What is associated entity")
     question2 = TextField(
         name="question1_Name",
         code="Q1",
         label="What is your name",
         defaultValue="some default value",
         constraints=[TextLengthConstraint(5, 10),
                      RegexConstraint("\w+")])
     question3 = IntegerField(
         name="Father's age",
         code="Q2",
         label="What is your Father's Age",
         constraints=[NumericRangeConstraint(min=15, max=120)])
     question4 = SelectField(name="Color",
                             code="Q3",
                             label="What is your favourite color",
                             options=[("RED", 'a'), ("YELLOW", 'b')])
     self.form_model = FormModelBuilder(
         self.manager, self.entity_type,
         "1").label("Aids form_model").name("aids").add_fields(
             question1, question2, question3, question4).build()
     self.form_model_id = self.form_model.id
示例#5
0
 def _create_summary_form_model(self):
     question1 = TextField(
         name="question1_Name",
         code="Q1",
         label="What is your name",
         defaultValue="some default value",
         constraints=[TextLengthConstraint(5, 10),
                      RegexConstraint("\w+")],
         ddtype=self.default_ddtype)
     question2 = IntegerField(
         name="Father's age",
         code="Q2",
         label="What is your Father's Age",
         constraints=[NumericRangeConstraint(min=15, max=120)],
         ddtype=self.default_ddtype)
     question3 = SelectField(name="Color",
                             code="Q3",
                             label="What is your favourite color",
                             options=[("RED", 1), ("YELLOW", 2)],
                             ddtype=self.default_ddtype)
     self.summary_form_model = FormModel(
         self.manager,
         entity_type=["reporter"],
         name="aids",
         label="Aids form_model",
         form_code=FORM_CODE_2,
         type="survey",
         fields=[question1, question2, question3])
     self.summary_form_model__id = self.summary_form_model.save()
示例#6
0
    def _get_cascade_questionnaire(self, cascades):
        fields = []
        for cascade in cascades:
            fields.append(
                SelectField(cascade,
                            cascade,
                            "Please select",
                            cascades[cascade],
                            is_cascade=True))

        doc = ProjectDocument()
        doc.xform = self._build_xform(fields=fields,
                                      field_type="cascade",
                                      cascades=cascades)
        questionnaire = Project.new_from_doc(DatabaseManagerStub(), doc)
        questionnaire.name = "q1"
        questionnaire.form_code = "007"
        questionnaire.fields.append(
            FieldSet(code="group_outer",
                     name="group_outer",
                     label="Enter the outer group details",
                     field_set=[
                         FieldSet(code="repeat_outer",
                                  name="repeat_outer",
                                  label="Enter the details you wanna repeat",
                                  field_set=fields)
                     ]))
        return questionnaire
示例#7
0
 def test_should_create_a_questionnaire_from_dictionary(self):
     #entityQ = UniqueIdField('reporter', name="What are you reporting on?", code="eid",
     #                        label="Entity being reported on", )
     ageQ = IntegerField(
         name="What is your age?",
         code="AGE",
         label="",
         constraints=[NumericRangeConstraint(min=0, max=10)],
         required=False)
     placeQ = SelectField(name="Where do you live?",
                          code="PLC",
                          label="",
                          options=[{
                              "text": "Pune"
                          }, {
                              "text": "Bangalore"
                          }],
                          single_select_flag=False,
                          required=False)
     questions = [ageQ, placeQ]
     document = self.get_form_model_doc()
     questionnaire = FormModel.new_from_doc(self.manager, document)
     self.maxDiff = None
     self.assertListEqual(questionnaire.entity_type, [])
     self.assertEqual(questionnaire.name, "New Project")
     for i in range(len(questions)):
         self.assertEqual(questionnaire.fields[i]._to_json(),
                          questions[i]._to_json())
示例#8
0
    def test_log_edit_of_existing_successful_submission(self):
        difference = SurveyResponseDifference(submitted_on=datetime(2013, 02, 23), status_changed=True)
        difference.changed_answers = {'q1': {'old': 23, 'new': 43}, 'q2': {'old': 'text2', 'new': 'correct text'},
                                      'q3': {'old': 'a', 'new': 'ab'}}
        original_survey_response = Mock(spec=SurveyResponse)
        edited_survey_response = Mock(spec=SurveyResponse)
        edited_survey_response.differs_from.return_value = difference
        project_name = 'project_name'
        request = Mock()

        form_model = Mock(spec=FormModel)
        int_field = IntegerField(name='question one', code='q1', label='question one')
        text_field = TextField(name='question two', code='q2', label='question two')
        choice_field = SelectField(name='question three', code='q3', label='question three',
                                   options=[("one", "a"), ("two", "b"), ("three", "c"), ("four", "d")],
                                   single_select_flag=False)
        form_model.get_field_by_code.side_effect = lambda x: {'q1': int_field, 'q2': text_field, 'q3': choice_field}[x]

        with patch('datawinners.project.views.submission_views.UserActivityLog') as activity_log:
            with patch(
                    'datawinners.project.views.submission_views.get_option_value_for_field') as get_option_value_for_field:
                get_option_value_for_field.return_value = {'old': u'one', 'new': u'one, two'}
                mock_log = Mock(spec=UserActivityLog)
                activity_log.return_value = mock_log
                log_edit_action(original_survey_response, edited_survey_response, request, project_name, form_model)
                expected_changed_answer_dict = {
                    'received_on': difference.created.strftime(SUBMISSION_DATE_FORMAT_FOR_SUBMISSION),
                    'status_changed': True,
                    'changed_answers': {'question one': {'old': 23, 'new': 43},
                                        'question two': {'old': 'text2', 'new': 'correct text'},
                                        'question three': {'old': u'one', 'new': u'one, two'}}}

                form_model.get_field_by_code.assert_calls_with([call('q1'), call('q2'), call('q3')])
                mock_log.log.assert_called_once_with(request, action=EDITED_DATA_SUBMISSION, project=project_name,
                                                     detail=json.dumps(expected_changed_answer_dict))
示例#9
0
 def test_should_return_default_language_text(self):
     expected_json = {
         "choices": [{
             "text": "Lake"
         }, {
             "text": "Dam"
         }],
         "name": "type",
         'parent_field_code': None,
         "type": "select1",
         "code": "T",
         "label": "What type?",
         "required": True,
         "instruction": "test",
     }
     field = SelectField(name="type",
                         code="T",
                         label="What type?",
                         options=[{
                             "text": "Lake"
                         }, {
                             "text": "Dam"
                         }],
                         instruction="test",
                         single_select_flag=True)
     actual_json = field._to_json_view()
     self.assertEqual(actual_json, expected_json)
 def test_should_return_none_if_survey_response_questionnaire_is_different_from_form_model(
         self):
     survey_response_doc = SurveyResponseDocument(values={
         'q5': '23',
         'q6': 'sometext',
         'q7': 'ab'
     })
     survey_response = SurveyResponse(Mock())
     survey_response._doc = survey_response_doc
     int_field = IntegerField(name='question one',
                              code='q1',
                              label='question one')
     text_field = TextField(name='question two',
                            code='q2',
                            label='question two')
     choice_field = SelectField(name='question three',
                                code='Q3',
                                label='question three',
                                options=[("one", "a"), ("two", "b"),
                                         ("three", "c"), ("four", "d")],
                                single_select_flag=False)
     questionnaire_form_model = Mock(spec=FormModel)
     questionnaire_form_model.form_code = 'test_form_code'
     questionnaire_form_model.fields = [int_field, text_field, choice_field]
     result_dict = construct_request_dict(survey_response,
                                          questionnaire_form_model, 'id')
     expected_dict = {
         'q1': None,
         'q2': None,
         'Q3': None,
         'form_code': 'test_form_code',
         'dsid': 'id'
     }
     self.assertEqual(expected_dict, result_dict)
    def test_should_get_comma_separated_list_if_field_changed_from_choice_to_unique_id(
            self):
        search_dict = {}

        options = [{
            'text': 'option1',
            'val': 'a'
        }, {
            'text': 'option2',
            'val': 'b'
        }]

        self.form_model.fields = [self.field1]
        original_field = SelectField('selectField', 'q1', 'q1', options)
        self.form_model.get_field_by_code_and_rev.return_value = original_field
        values = {'q1': 'ab', 'q2': 'wrong number', 'q3': 'wrong text'}
        submission_doc = SurveyResponseDocument(values=values, status="error")
        with patch('datawinners.search.submission_index.lookup_entity_name'
                   ) as lookup_entity_name:
            lookup_entity_name.return_value = 'N/A'

            _update_with_form_model_fields(Mock(spec=DatabaseManager),
                                           submission_doc, search_dict,
                                           self.form_model)
            self.assertEquals(
                {
                    '1212_q1': 'N/A',
                    '1212_q1_unique_code': 'option1,option2',
                    'void': False
                }, search_dict)
示例#12
0
    def test_should_get_statistic_result_after_option_value_changed(self):
        """
            Function to test getting statistic result of submissions after option value changed.
            question name ordered by field
            options ordered by count(desc),option(alphabetic)
            total = submission count of this question
        """
        eid_field = TextField(label="What is associated entity?",
                              code="EID",
                              name="What is associatéd entity?",
                              entity_question_flag=True,
                              ddtype=self.ddtype)
        blood_type_field = SelectField(label="What is your blood group?",
                                       code="BG",
                                       name="What is your blood group?",
                                       options=[("Type 1", "a"),
                                                ("Type 2", "b")],
                                       single_select_flag=True,
                                       ddtype=self.ddtype)

        form_model = FormModelBuilder(self.manager, ['clinic'],
                                      form_code='cli001').add_fields(
                                          eid_field, blood_type_field).build()

        self.submit_data({'form_code': 'cli001', 'EID': 'cid001', 'BG': 'a'})

        blood_type_field = SelectField(label="What is your blood group?",
                                       code="BG",
                                       name="What is your blood group?",
                                       options=[("O+", "a"), ("O-", "b"),
                                                ("AB", "c"), ("B+", "d")],
                                       single_select_flag=True,
                                       ddtype=self.ddtype)
        self._edit_fields(form_model, blood_type_field)
        self.submit_data({'form_code': 'cli001', 'EID': 'cid001', 'BG': 'a'})

        submissions = successful_submissions(self.manager,
                                             form_model.form_code)
        analyzer = SubmissionAnalyzer(form_model, self.manager, self.org_id,
                                      submissions)
        statistics = analyzer.get_analysis_statistics()

        expected = [[
            "What is your blood group?", field_attributes.SELECT_FIELD, 2,
            [["O+", 1], ['Type 1', 1], ["AB", 0], ["B+", 0], ["O-", 0]]
        ]]
        self.assertEqual(expected, statistics)
示例#13
0
 def test_should_remove_spaces_if_present_in_answer_for_multi_select(self):
     field = SelectField(name="color",
                         code="Q3",
                         label="What is your favorite color",
                         options=[("RED", 'a'), ("YELLOW", 'b'), ('green')],
                         single_select_flag=False,
                         instruction="test_instruction")
     self.assertEqual(['RED', 'YELLOW'], field.validate('a b'))
示例#14
0
 def test_get_option_value_for_other_field_changed_to_choice_fields(self):
     choices = {"old": "hi", "new": "ab"}
     choice_field = SelectField(name='question', code='q1', label="question",
                                options=[("one", "a"), ("two", "b"), ("three", "c"), ("four", "d")],
                                single_select_flag=False, )
     result_dict = get_option_value_for_field(choices, choice_field)
     expected = {"old": "hi", "new": "one, two"}
     self.assertEqual(expected, result_dict)
示例#15
0
def _create_select_question(post_dict, single_select_flag, ddtype):
    options = [choice.get("text") for choice in post_dict["choices"]]
    return SelectField(name=post_dict["title"],
                       code=post_dict["code"].strip(),
                       label="default",
                       options=options,
                       single_select_flag=single_select_flag,
                       ddtype=ddtype,
                       instruction=post_dict.get("instruction"))
示例#16
0
 def test_select_field_creation_with_multi_select(self):
     select_field = SelectField(
         "select something",
         "some_code",
         "what do u want to select", [('opt1', 'a'), ('opt2', 'b'),
                                      ('opt3', 'c')],
         "Choose 1 or more answers from the list. Example: a or ab ",
         single_select_flag=False)
     select_field.value = 'opt1,opt2'
     choice_field = FormField().create(select_field)
     self.assertTrue(
         isinstance(choice_field.widget, forms.CheckboxSelectMultiple))
     self.assertEquals(choice_field.initial, ['a', 'b'])
     self.assertEquals([('a', 'opt1'), ('b', 'opt2'), ('c', 'opt3')],
                       choice_field.choices)
     self.assertEqual(
         choice_field.help_text,
         "Choose 1 or more answers from the list. Example: a or ab ")
示例#17
0
 def test_should_return_choices_type_as_select(self):
     field = SelectField(name="What's the color?",
                         code="nam",
                         label="naam",
                         options=[("Red", "a"), ("Green", "b"),
                                  ("Blue", "c")],
                         single_select_flag=False)
     preview = helper.get_preview_for_field(field)
     self.assertEqual("select", preview["type"])
示例#18
0
 def test_should_return_choices(self):
     field = SelectField(name="What's the color?",
                         code="nam",
                         label="naam",
                         options=[("Red", "a"), ("Green", "b"),
                                  ("Blue", "c")])
     preview = helper.get_preview_for_field(field)
     self.assertEqual("select1", preview["type"])
     self.assertEqual([("Red", "a"), ("Green", "b"), ("Blue", "c")],
                      preview["constraints"])
示例#19
0
 def _create_select_question(self, post_dict, single_select_flag, code):
     options = [(choice['value'].get("text"), choice['value'].get("val"))
                for choice in post_dict["choices"]]
     return SelectField(name=self._get_name(post_dict),
                        code=code,
                        label=post_dict["title"],
                        options=options,
                        single_select_flag=single_select_flag,
                        instruction=post_dict.get("instruction"),
                        required=post_dict.get("required"))
    def create_test_fields_and_survey_for_all_fields_type(self):
        name = TextField('name', 'name' ,'What is your name?')
        degree = TextField('degree', 'degree' ,'Degree name')
        completed_on = DateField('completed_on', 'completed_on','Degree completion year', 'dd.mm.yyyy')
        education = FieldSet('education', 'education', 'Education', field_set=[degree,completed_on])
        age = IntegerField('age', 'age' ,'What is your age?')
        opt_fav_col = [('red','Red'), ('blue','Blue'),('c','Green')]
        fav_col = SelectField('fav_color', 'fav_color', 'Which colors you like?', opt_fav_col)
        opt_pizza_col = [('yes', 'Yes'),('no','No')]
        pizza_fan = SelectField('pizza_fan', 'pizza_fan', 'Do you like pizza?', opt_pizza_col)
        other = TextField('other', 'other' ,'What else you like?')
        pizza_type = TextField('pizza_type', 'pizza_type' ,'Which pizza type you like?')
        location = GeoCodeField('location', 'location' ,'Your location?')

        form_fields = [name, education, age, fav_col, pizza_fan, other, pizza_type, location]

        # todo how required will be handled
        survey_response_values = {'name': 'Santa', 'pizza_type': None, 'age': '30', 'other': 'Samosa', 'location': '4.9158,11.9531', 'education': [{'completed_on': u'10.02.2014', 'degree': 'SantaSSC'}], 'pizza_fan': 'yes', 'fav_color': 'red blue'}
        return form_fields, survey_response_values
示例#21
0
 def test_should_convert_multiple_select_field_to_html(self):
     exptected_html = """<select name="color" MULTIPLE><option value="1">RED</option><option value="2">YELLOW</option><option value="3">green</option></select>"""
     field = SelectField(name="color",
                         code="Q3",
                         label="What is your favorite color",
                         language="eng",
                         options=[("RED", 1), ("YELLOW", 2), ('green', 3)],
                         single_select_flag=False,
                         ddtype=self.ddtype,
                         instruction="test_instruction")
     self.assertEqual(exptected_html, field.to_html())
示例#22
0
    def setUp(self):
        self.form_model = MagicMock(spec=FormModel, id="1212")
        self.field1 = UniqueIdField(unique_id_type='clinic',
                                    name='unique_id',
                                    code="q1",
                                    label='which clinic')
        self.field2 = TextField('text', "q2", "enter text")
        self.field3 = GeoCodeField('gps', "q3", "enter gps")
        self.field4 = SelectField('select',
                                  'q4',
                                  'select multple options', [{
                                      'text': 'one',
                                      'val': 'a'
                                  }, {
                                      'text': 'two',
                                      'val': 'b'
                                  }],
                                  single_select_flag=False)
        self.field5 = DateField('date', 'q5', 'enter date', 'mm.dd.yyyy')
        self.field6 = SelectField('select',
                                  'single-select',
                                  'select one option', [{
                                      'text': 'one',
                                      'val': 'a'
                                  }, {
                                      'text': 'two',
                                      'val': 'b'
                                  }],
                                  single_select_flag=True)
        self.repeat_question = FieldSet(name="repeat",
                                        code="repeat-code",
                                        label="repeat-label",
                                        fieldset_type="repeat",
                                        field_set=[self.field4, self.field6])
        self.group_question = FieldSet(name="group",
                                       code="group-code",
                                       label="group-label",
                                       fieldset_type="group",
                                       field_set=[self.field4, self.field6])

        self.dbm = MagicMock(spec=DatabaseManager)
示例#23
0
 def test_should_return_choices(self):
     type = DataDictType(Mock(DatabaseManager), name="color type")
     field = SelectField(name="What's the color?",
                         code="nam",
                         label="naam",
                         ddtype=type,
                         options=[("Red", "a"), ("Green", "b"),
                                  ("Blue", "c")])
     preview = helper.get_preview_for_field(field)
     self.assertEqual("select1", preview["type"])
     self.assertEqual([("Red", "a"), ("Green", "b"), ("Blue", "c")],
                      preview["constraints"])
示例#24
0
 def test_should_get_latest_field_if_no_revision_provided_and_no_snapshots(
         self):
     self.form_model.delete_field(code="Q3")
     field = SelectField(name="New Name",
                         code="Q3",
                         label="What is your favourite color",
                         options=[("RED", 1), ("YELLOW", 2)])
     self.form_model.add_field(field)
     self.form_model.save()
     updated_form = FormModel.get(self.manager, self.form_model_id)
     self.assertEqual("New Name",
                      updated_form.get_field_by_code_and_rev("Q3").name)
    def test_convert_survey_response_values_to_dict_format(self):
        survey_response_doc = SurveyResponseDocument(
            values={
                'q1': '23',
                'q2': 'sometext',
                'q3': 'a',
                'geo': '2.34,5.64',
                'date': '12.12.2012'
            })
        survey_response = SurveyResponse(Mock())
        survey_response._doc = survey_response_doc
        int_field = IntegerField(name='question one',
                                 code='q1',
                                 label='question one',
                                 ddtype=Mock(spec=DataDictType))
        text_field = TextField(name='question two',
                               code='q2',
                               label='question two',
                               ddtype=Mock(spec=DataDictType))
        single_choice_field = SelectField(name='question three',
                                          code='q3',
                                          label='question three',
                                          options=[("one", "a"), ("two", "b"),
                                                   ("three", "c"),
                                                   ("four", "d")],
                                          single_select_flag=True,
                                          ddtype=Mock(spec=DataDictType))
        geo_field = GeoCodeField(name='geo',
                                 code='GEO',
                                 label='geo',
                                 ddtype=Mock())
        date_field = DateField(name='date',
                               code='DATE',
                               label='date',
                               date_format='dd.mm.yyyy',
                               ddtype=Mock())
        questionnaire_form_model = Mock(spec=FormModel)
        questionnaire_form_model.form_code = 'test_form_code'
        questionnaire_form_model.fields = [
            int_field, text_field, single_choice_field, geo_field, date_field
        ]

        request_dict = construct_request_dict(survey_response,
                                              questionnaire_form_model)
        expected_dict = OrderedDict({
            'q1': '23',
            'q2': 'sometext',
            'q3': 'a',
            'GEO': '2.34,5.64',
            'DATE': '12.12.2012',
            'form_code': 'test_form_code'
        })
        self.assertEqual(expected_dict, request_dict)
示例#26
0
 def _create_select_question(self, post_dict, single_select_flag, code):
     options = [(choice['value'].get("text"), choice['value'].get("val")) for choice in post_dict["choices"]]
     return SelectField(name=self._get_name(post_dict, code), code=code, label=post_dict["title"],
                        options=options, single_select_flag=single_select_flag,
                        instruction=post_dict.get("instruction"), required=post_dict.get("required"),
                        parent_field_code=post_dict.get('parent_field_code'), has_other=post_dict.get('has_other'),
                        hint=post_dict.get('hint'), constraint_message=post_dict.get('constraint_message'),
                        appearance=post_dict.get('appearance'),
                        default=post_dict.get('default'),
                        xform_constraint=post_dict.get('xform_constraint'),
                        relevant=post_dict.get('relevant'),
                        is_cascade=post_dict.get('is_cascade'))
    def setUp(self):
        self.dbm = Mock(spec=DatabaseManager)
        self.datadict_patcher = patch(
            "mangrove.form_model.form_model.get_or_create_data_dict")
        self.datadict_mock = self.datadict_patcher.start()
        self.ddtype_mock = Mock(spec=DataDictType)
        self.datadict_mock.return_value = self.ddtype_mock

        q1 = TextField(name="entity_question",
                       code="ID",
                       label="What is associated entity",
                       language="eng",
                       entity_question_flag=True,
                       ddtype=self.ddtype_mock)
        q2 = TextField(name="question1_Name",
                       code="Q1",
                       label="What is your name",
                       defaultValue="some default value",
                       language="eng",
                       length=TextConstraint(5, 10),
                       ddtype=self.ddtype_mock)
        q3 = IntegerField(name="Father's age",
                          code="Q2",
                          label="What is your Father's Age",
                          range=NumericConstraint(min=15, max=120),
                          ddtype=self.ddtype_mock)
        q4 = SelectField(name="Color",
                         code="Q3",
                         label="What is your favourite color",
                         options=[("RED", 1), ("YELLOW", 2)],
                         ddtype=self.ddtype_mock)
        q5 = TextField(name="Desc",
                       code="Q4",
                       label="Description",
                       ddtype=self.ddtype_mock)

        self.form_model = FormModel(
            self.dbm,
            entity_type=["XYZ"],
            name="aids",
            label="Aids form_model",
            form_code="1",
            type='survey',
            fields=[  #        expected_short_code = "dog3"
                #        self.assertEqual(response.short_code, expected_short_code)
                #        b = get_by_short_code(self.dbm, expected_short_code, ["dog"])
                #        self.assertEqual(b.short_code, expected_short_code)
                q1,
                q2,
                q3,
                q4,
                q5
            ])
    def test_single_select_field_value_representation(self):
        value_mock = PropertyMock(return_value={'q4': 'b'})
        type(self.survey_response).values = value_mock
        select_field = SelectField('name', 'q4', 'select',
                                   [{'text': 'orange', 'val': 'a'}, {'text': 'mango', 'val': 'b'},
                                    {'text': 'apple', 'val': 'c'}])

        builder = EnrichedSurveyResponseBuilder(None, self.survey_response, self.form_model, {})
        dictionary = builder._create_answer_dictionary(select_field)
        self.assertEquals({'b': 'mango'}, dictionary.get('answer'))
        self.assertEquals('select', dictionary.get('label'))
        self.assertEquals('select1', dictionary.get('type'))
示例#29
0
 def test_should_create_multi_select_field_type_for_default_english_language(
         self):
     expected_json = {
         "label":
         "What is your favorite color",
         "name":
         "color",
         "choices": [{
             "text": "RED",
             "val": 'a'
         }, {
             "text": "YELLOW",
             "val": 'b'
         }, {
             "text": 'green',
             'val': 'green'
         }],
         "code":
         "Q3",
         "parent_field_code":
         None,
         "type":
         "select",
         "required":
         True,
         "instruction":
         "test_instruction",
         "appearance":
         None,
         "constraint_message":
         None,
         "default":
         None,
         "hint":
         None,
         "xform_constraint":
         None,
         "relevant":
         None,
         "is_cascade":
         False
     }
     field = SelectField(name="color",
                         code="Q3",
                         label="What is your favorite color",
                         options=[("RED", 'a'), ("YELLOW", 'b'), ('green')],
                         single_select_flag=False,
                         instruction="test_instruction")
     actual_json = field._to_json()
     self.assertEqual(actual_json, expected_json)
     field.set_value(field.validate('ab'))
     self.assertEqual("RED,YELLOW", field.convert_to_unicode())
示例#30
0
 def test_should_return_according_value_for_select_field_case_insensitively(
         self):
     ddtype = Mock(spec=DataDictType)
     single_select = SelectField(label="multiple_choice_question",
                                 code="q",
                                 options=[("red", "a"), ("yellow", "b"),
                                          ('green', "c")],
                                 name="What is your favourite colour",
                                 ddtype=ddtype)
     multiple_answer = SelectField(label="multiple_choice_question",
                                   code="q1",
                                   single_select_flag=False,
                                   options=[("value_of_a", "a"),
                                            ("value_of_b", "b"),
                                            ("value_of_c", 'c'), ("d", "d"),
                                            ("e", "e"), ('f', "f"),
                                            ("g", "g"), ("h", "h"),
                                            ('i', "i"), ("j", "j"),
                                            ("k", "k"), ('l', "l"),
                                            ("m", "m"), ("n", "n"),
                                            ('o', "o"), ("p", "p"),
                                            ("q", "q"), ('r', "r"),
                                            ("s", "s"), ("t", "t"),
                                            ('u', "u"), ("v", "v"),
                                            ("w", "w"), ('x', "x"),
                                            ("y", "y"), ("z", "z"),
                                            ("value_of_1a", '1a'),
                                            ("value_of_1b", "1b"),
                                            ("value_of_1c", "1c"),
                                            ('1d', "1d")],
                                   name="What is your favourite colour",
                                   ddtype=ddtype)
     values = dict({"q": "B", "q1": "b1A1c"})
     single_value = helper.get_according_value(values, single_select)
     multiple_value = helper.get_according_value(values, multiple_answer)
     self.assertEquals(single_value, "yellow")
     self.assertEqual(multiple_value,
                      "value_of_b, value_of_1a, value_of_1c")
 def init_form_model_fields(self):
     self.eid_field = TextField(label="What is associated entity?",
                                code="EID",
                                name="What is associatéd entity?",
                                entity_question_flag=True,
                                ddtype=Mock())
     self.rp_field = DateField(
         label="Report date",
         code="RD",
         name="What is réporting date?",
         date_format="dd.mm.yyyy",
         event_time_field_flag=True,
         ddtype=Mock(),
         instruction=
         "Answer must be a date in the following format: day.month.year. Example: 25.12.2011"
     )
     self.symptoms_field = SelectField(label="Zhat are symptoms?",
                                       code="SY",
                                       name="Zhat are symptoms?",
                                       options=[("Rapid weight loss", "a"),
                                                ("Dry cough", "2b"),
                                                ("Pneumonia", "c"),
                                                ("Memory loss", "d"),
                                                ("Neurological disorders ",
                                                 "e")],
                                       single_select_flag=False,
                                       ddtype=Mock())
     self.blood_type_field = SelectField(label="What is your blood group?",
                                         code="BG",
                                         name="What is your blood group?",
                                         options=[("O+", "a"), ("O-", "b"),
                                                  ("AB", "c"), ("B+", "d")],
                                         single_select_flag=True,
                                         ddtype=Mock())
     self.gps_field = GeoCodeField(name="field1_Loc",
                                   code="gps",
                                   label="Where do you stay?",
                                   ddtype=Mock())