Example #1
0
    def test_should_return_unique_entries_when_multiple_unique_id_fields_of_same_type_are_present_from_questionnaire(
            self):
        fields = [
            TextField("Some word question", "q1", "Some word question"),
            UniqueIdField("goats", "What goat are you reporting on?", "q2",
                          "What goat are you reporting on?"),
            UniqueIdField("chicken", "What chicken are you reporting on?",
                          "q3", "What chicken are you reporting on?"),
            UniqueIdField("goats", "What goat are you reporting on?", "q4",
                          "What goat are you reporting on?"),
        ]

        fields_array = get_filterable_fields(fields, [])
        print[r for r in fields_array]
        self.assertEqual(len(fields_array), 2)
        self.assertDictEqual(fields_array[0], {
            'type': 'unique_id',
            'code': 'q2',
            'entity_type': 'goats'
        })
        self.assertDictEqual(fields_array[1], {
            'type': 'unique_id',
            'code': 'q3',
            'entity_type': 'chicken'
        })
 def setUp(self):
     self.field1 = TextField('text', 'q1', 'Enter Text')
     self.field2 = IntegerField('integer', 'q2', 'Enter a Number')
     self.field3 = UniqueIdField('clinic', 'unique_id_field', 'q3', 'Which clinic are you reporting on')
     self.field4 = UniqueIdField('school', 'unique_id_field2', 'q4', 'Which school are you reporting on')
     self.form_model = MagicMock(spec=FormModel)
     self.form_model.id = 'form_model_id'
Example #3
0
    def test_should_return_unique_entries_when_multiple_unique_id_fields_are_present_in_group(
            self):
        group_fields = [
            UniqueIdField("chicken",
                          "What chicken are you reporting on?",
                          "q3",
                          "What chicken are you reporting on?",
                          parent_field_code='group'),
            UniqueIdField("goats",
                          "What goat are you reporting on?",
                          "q4",
                          "What goat are you reporting on?",
                          parent_field_code='group'),
        ]
        fields = [
            TextField("Some word question", "q1", "Some word question"),
            UniqueIdField("goats", "What goat are you reporting on?", "q2",
                          "What goat are you reporting on?"),
            FieldSet('group', 'group', 'animal group', field_set=group_fields)
        ]

        fields_array = get_filterable_fields(fields, [])
        print[r for r in fields_array]
        self.assertEqual(len(fields_array), 2)
        self.assertDictEqual(fields_array[0], {
            'type': 'unique_id',
            'code': 'q2',
            'entity_type': 'goats'
        })
        self.assertDictEqual(fields_array[1], {
            'type': 'unique_id',
            'code': 'group----q3',
            'entity_type': 'chicken'
        })
Example #4
0
 def setUp(self):
     self.validator = UniqueIdExistsValidator()
     self.field1 = TextField('a', 'a', 'a')
     self.field2 = TextField('b', 'b', 'b')
     self.field3 = UniqueIdField('unique_id_type1', 'name1', 'code1', 'label1')
     self.field4 = UniqueIdField('unique_id_type2', 'name2', 'code2', 'label2')
     self.fields = [self.field1, self.field2, self.field3,
                    self.field4
     ]
Example #5
0
 def setUp(self):
     self.field1 = TextField('text', 'q1', 'Enter Text')
     self.field2 = IntegerField('integer', 'q2', 'Enter a Number')
     self.field3 = UniqueIdField('clinic', 'unique_id_field', 'q3', 'Which clinic are you reporting on')
     self.field4 = UniqueIdField('school', 'unique_id_field2', 'q4', 'Which school are you reporting on')
     self.repeat_field = FieldSet('repeat','repeat', 'repeat label', field_set=[self.field1, self.field4])
     self.form_model = MagicMock(spec=Project)
     self.form_model.id = 'form_model_id'
     self.patch_get_entity_type_info = patch('datawinners.entity.import_data.get_entity_type_info')
     self.get_entity_type_info_mock = self.patch_get_entity_type_info.start()
Example #6
0
 def setUp(self):
     self.field1 = TextField('text', 'q1', 'Enter Text')
     self.field2 = IntegerField('integer', 'q2', 'Enter a Number')
     self.field3 = UniqueIdField('clinic', 'unique_id_field', 'q3',
                                 'Which clinic are you reporting on')
     self.field4 = UniqueIdField('school', 'unique_id_field2', 'q4',
                                 'Which school are you reporting on')
     self.repeat_field = FieldSet('repeat',
                                  'repeat',
                                  'repeat label',
                                  field_set=[self.field1, self.field4])
     self.form_model = MagicMock(spec=Project)
     self.form_model.id = 'form_model_id'
Example #7
0
    def test_should_throw_exception_when_unique_id_type_field_code_changes(
            self):
        new_fields = [
            DateField(name='q3',
                      code='q3',
                      label='Reporting date',
                      date_format='dd.mm.yyyy'),
            UniqueIdField(unique_id_type='clinic',
                          name="Q1",
                          code="EID new",
                          label="What is the clinic id?")
        ]
        latest_form_model = FormModel(self.dbm,
                                      "abc",
                                      "abc",
                                      form_code="cli001",
                                      fields=new_fields)
        es_mock = MagicMock()

        with patch(
                "datawinners.search.submission_index.get_elasticsearch_handle"
        ) as get_elasticsearch_handle_mock:
            with self.assertRaises(FieldTypeChangeException) as e:
                get_elasticsearch_handle_mock.return_value = es_mock

                SubmissionSearchStore(
                    self.dbm, latest_form_model,
                    self.form_model)._verify_unique_id_change()
Example #8
0
    def test_should_not_throw_exception_when_unique_id_field_reordered(self):
        new_fields = [
            UniqueIdField(unique_id_type='clinic',
                          name="Q1",
                          code="EID",
                          label="What is the clinic id?"),
            DateField(name='q3',
                      code='q3',
                      label='Reporting date',
                      date_format='dd.mm.yyyy')
        ]
        es_mock = MagicMock()
        latest_form_model = FormModel(self.dbm,
                                      "abc",
                                      "abc",
                                      form_code="cli001",
                                      fields=new_fields)

        with patch(
                "datawinners.search.submission_index.get_elasticsearch_handle"
        ) as get_elasticsearch_handle_mock:
            get_elasticsearch_handle_mock.return_value = es_mock

            SubmissionSearchStore(self.dbm, latest_form_model,
                                  self.form_model)._verify_unique_id_change()

            self.assertFalse(self.es.put_mapping.called)
Example #9
0
    def _mock_form_model(self):
        self.get_form_model_mock_player_patcher = patch(
            'mangrove.transport.services.survey_response_service.get_form_model_by_code'
        )
        self.get_form_model_mock_parser_patcher = patch(
            'mangrove.transport.player.parser.get_form_model_by_code')
        # self.get_form_model_mock_player_v2_patcher = patch('mangrove.transport.player.new_players.get_form_model_by_code')
        get_form_model_player_mock = self.get_form_model_mock_player_patcher.start(
        )
        get_form_model_parser_mock = self.get_form_model_mock_parser_patcher.start(
        )
        # get_form_model_player_v2_mock = self.get_form_model_mock_player_v2_patcher.start()
        self.form_model_mock = MagicMock(spec=FormModel)
        self.form_model_mock.is_entity_registration_form.return_value = True
        self.form_model_mock.entity_type = ["clinic"]
        self.form_model_mock.get_field_by_name = self._location_field
        field = UniqueIdField('clinic', 'q1', 'id', 'q1')
        self.form_model_mock.fields = [field]
        self.form_model_mock.is_open_survey = False
        self.form_model_mock.validate_submission.return_value = OrderedDict(
        ), OrderedDict()

        self.form_submission_mock = mock_form_submission(self.form_model_mock)

        get_form_model_player_mock.return_value = self.form_model_mock
        get_form_model_parser_mock.return_value = self.form_model_mock
Example #10
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
Example #11
0
 def setUpClass(cls):
     FormModelDocument.registered_functions = []
     cls.db_name = uniq('mangrove-test')
     cls.manager = get_db_manager('http://localhost:5984/', cls.db_name)
     initializer._create_views(cls.manager)
     create_views(cls.manager)
     question1 = UniqueIdField(unique_id_type='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)])
     cls.project1 = Project(dbm=cls.manager,
                            name=project1_name,
                            goals="Testing",
                            devices=['web'],
                            form_code="abc",
                            fields=[question1, question2])
     cls.project1_id = cls.project1.save()
     cls.project2 = Project(dbm=cls.manager,
                            name=project2_name,
                            goals="Testing",
                            devices=['web'],
                            form_code="def",
                            fields=[question1, question2])
     cls.project2_id = cls.project2.save()
Example #12
0
 def test_generate_elastic_index_for_a_unique_id_field_within_group(self):
     search_dict = {}
     unique_id_field = UniqueIdField('clinic', 'my_unique_id',
                                     'my_unique_id', 'My Unique ID')
     group_field = FieldSet('group_name',
                            'group_name',
                            'My Label',
                            field_set=[unique_id_field])
     self.form_model.fields = [group_field]
     value = {'group_name': [{'my_unique_id': 'cli001'}]}
     submission = SurveyResponseDocument(values=value, status='success')
     with patch('datawinners.search.submission_index.lookup_entity'
                ) as lookup_entity:
         lookup_entity.return_value = {'q2': 'my_clinic'}
         _update_with_form_model_fields(Mock(spec=DatabaseManager),
                                        submission, search_dict,
                                        self.form_model)
         self.assertEqual(
             search_dict, {
                 '1212_group_name-my_unique_id_details': {
                     'q2': 'my_clinic'
                 },
                 '1212_group_name-my_unique_id': 'my_clinic',
                 '1212_group_name-my_unique_id_unique_code': 'cli001',
                 'is_anonymous': False,
                 'media': {},
                 'void': False
             })
Example #13
0
 def _create_data_submission_form(self):
     question1 = UniqueIdField('clinic',name="entity_question", code="ID", label="What is associated entity")
     question2 = TextField(name="Name", code="Q1", label="What is your name",
                           defaultValue="some default value")
     return FormModel(self.dbm, name="aids", label="Aids form_model",
                      form_code="AIDS",
                      fields=[question1, question2])
Example #14
0
    def test_should_return_date_and_unique_id_fields_from_questionnaire(self):
        fields = [
            TextField("Some word question", "q1", "Some word question"),
            UniqueIdField("goats", "What goat are you reporting on?", "q2",
                          "What goat are you reporting on?"),
            DateField("When did you buy the goat?", "q3",
                      "When did you buy the goat?", "dd.mm.yyyy"),
            DateField("When did you sell the goat?", "q4",
                      "When did you sell the goat?", "mm.yyyy")
        ]

        fields_array = get_filterable_fields(fields, [])

        self.assertEqual(len(fields_array), 3)
        self.assertDictEqual(fields_array[0], {
            'type': 'unique_id',
            'code': 'q2',
            'entity_type': 'goats'
        })
        self.assertDictEqual(
            fields_array[1], {
                'type': 'date',
                'code': 'q3',
                'label': 'When did you buy the goat?',
                'is_month_format': False,
                'format': 'dd.mm.yyyy'
            })
        self.assertDictEqual(
            fields_array[2], {
                'type': 'date',
                'code': 'q4',
                'label': 'When did you sell the goat?',
                'is_month_format': True,
                'format': 'mm.yyyy'
            })
Example #15
0
    def test_should_return_specific_form_for_project_with_unique_id(self):
        dbm = Mock(spec=DatabaseManager)
        questionnaire_mock = MagicMock()
        field1 = self.text_field(code='code')
        questionnaire_mock.name = 'name'
        unique_id_field = UniqueIdField("clinic",
                                        "cl",
                                        "cl",
                                        "Clinic",
                                        instruction="")
        questionnaire_mock.fields = [field1, unique_id_field]
        questionnaire_mock.form_code = 'form_code'
        questionnaire_mock.id = 'id'

        questionnaire_mock.activeLanguages = ["en"]
        questionnaire_mock.entity_questions = [
            self.text_field(code='entity_question_code')
        ]
        questionnaire_mock.xform = None
        entity1 = Entity(dbm, short_code="shortCode1", entity_type="someType")
        entity1._doc.data['name'] = {'value': 'nameOfEntity'}
        entities = [entity1, entity1]
        with patch("mangrove.transport.xforms.xform.FormModel"
                   ) as form_model_mock:
            with patch("mangrove.form_model.field.get_all_entities"
                       ) as get_all_entities_mock:
                get_all_entities_mock.return_value = entities
                form_model_mock.get.return_value = questionnaire_mock
                actual_response = xform_for(dbm, "someFormId", 'rep1')
                self.assertTrue(
                    self.checker.check_output(
                        actual_response,
                        unicode(expected_xform_for_project_with_unique_id), 0),
                    actual_response)
Example #16
0
 def create_form_for_entity_type(self):
     school_name_field = TextField(name="name", code="q1", label="What's the name?")
     address_field = TextField(name="address", code="q2", label="Where is the clinic?")
     unique_id_field = UniqueIdField('clinic',name="unique_id", code="q3", label="What is the clinic's Unique ID Number?")
     form_model = FormModel(self.manager, "clinic", form_code=FORM_CODE, entity_type=["clinic"],
                            is_registration_model=True,
                            fields=[school_name_field, address_field, unique_id_field])
     form_model.save()
 def form_model(self):
     question1 = UniqueIdField(unique_id_type='clinic', name="entity_question", code="q1",
                               label="What is associated entity",
                               )
     question2 = IntegerField(name="question1_Name", code="q2", label="What is your name",
                              constraints=[NumericRangeConstraint(min=10, max=100)])
     return FormModel(self.dbm, name="aids", label="Aids form_model",
                      form_code="aids", fields=[question1, question2])
Example #18
0
 def test_should_filter_duplicate_unique_id_types(self):
     field1 = UniqueIdField(unique_id_type='clinic',
                            name='q1',
                            code='q1',
                            label='q1')
     field2 = UniqueIdField(unique_id_type='school',
                            name='q2',
                            code='q2',
                            label='q2')
     field3 = UniqueIdField(unique_id_type='clinic',
                            name='q3',
                            code='q3',
                            label='q3')
     fields = [field1, field2, field3]
     form_model = FormModel(Mock(spec=DatabaseManager))
     form_model._form_fields = fields
     self.assertListEqual(['clinic', 'school'], form_model.entity_type)
 def _get_unique_id_field(self, code):
     field_name = 'unique_id_field'
     return UniqueIdField(unique_id_type='clinic',
                          name=field_name,
                          code=code,
                          label=field_name,
                          instruction=self.instruction,
                          required=True,
                          constraints=[TextLengthConstraint(1, 20)])
Example #20
0
 def _create_unique_id_question(self, post_dict, code):
     return UniqueIdField(
         unique_id_type=self._get_unique_id_type(post_dict),
         name=self._get_name(post_dict),
         code=code,
         label=post_dict["title"],
         instruction=ugettext(
             "Answer must be the Identification Number of the %s you are reporting on."
         ) % self._get_unique_id_type(post_dict))
Example #21
0
    def test_should_get_header_information_for_submission_excel_with_multiple_unique_id_questions(self):
        fields = [{"name": "first name", "code": 'q1', "label": 'What is your name',
                   "type": "text"},
                  {"name": "age", "code": 'q2', "label": 'What is your age', "type": "integer", "constraints": [
                      [
                          "range",
                          {
                              "max": "15",
                              "min": "12"
                          }
                      ]
                  ]},
                  {"name": "reporting date", "code": 'q3', "label": 'What is the reporting date',
                   "date_format": "dd.mm.yyyy", "type": "date"},
                  {"name": "choices", "code": 'q5', "label": 'Your choices', "type": "select"},
                  {"name": "What game are you reporting on?", "code": 'q6', "label": 'What game are you reporting on?',
                    "unique_id_type": "game", "type": "unique_id"},
                  {"name": "What waterpoint are you reporting on?", "code": 'q7', "label": 'What waterpoint are you reporting on?',
                    "unique_id_type": "waterpoint", "type": "unique_id"}]
        form_model_fields = [TextField("first_name","q1","What is your name"),
                             IntegerField("age","q2","What is your age"),
                             DateField("reporting date","q3","What is the reporting date","dd.mm.yyyy"),
                             UniqueIdField("game","What game are you reporting on?","q6","What game are you reporting on?"),
                             UniqueIdField("waterpoint","What waterpoint are you reporting on?","q7","What waterpoint are you reporting on?")]
        form_model = FormModel(Mock(spec=DatabaseManager), name="some_name", form_code="cli00_mp", fields=form_model_fields)

        headers = get_submission_headers(fields, form_model)

        headers_text = self._get_header_component(headers, 0)
        self.assertEqual(
            ["What is your name", "What is your age", "What is the reporting date",
             "Your choices", "What game are you reporting on?", "What waterpoint are you reporting on?"], headers_text)

        header_instructions = self._get_header_component(headers, 1)
        self.assertEqual(
            ["\n\nAnswer must be a word", "\n\nEnter a number between 12-15.",
             "\n\nAnswer must be a date in the following format: day.month.year",
             "\n\nEnter 1 or more answers from the list.", '\n\nEnter the unique ID for each game.\nYou can find the game List on the My Identification Numbers page.',
             '\n\nEnter the unique ID for each waterpoint.\nYou can find the waterpoint List on the My Identification Numbers page.'], header_instructions)

        header_examples = self._get_header_component(headers, 2)
        self.assertEqual(
            ["\n\n", "\n\n", "\n\nExample: 25.12.2011",
             "\n\nExample: a or ab", "\n\nExample: cli01", "\n\nExample: cli01"], header_examples)
    def test_should_add_unique_id_validator_to_form_model_if_unique_id_field_is_present(
            self):
        dbm = Mock(spec=DatabaseManager)
        form_model = MagicMock(spec=FormModel)
        form_model.fields = [UniqueIdField('clinic', 'name', 'code', 'label')]

        QuestionnaireBuilder(form_model, dbm)._update_unique_id_validator()

        form_model.add_validator.assert_called_once_with(
            UniqueIdExistsValidator)
Example #23
0
 def setUp(self):
     self.dbm = Mock(spec=DatabaseManager)
     fields = [DateField(name='q3', code='q3', label='Reporting date', date_format='dd.mm.yyyy'),
          UniqueIdField(unique_id_type='clinic',name="Q1", code="EID", label="What is the clinic id?")]
     self.form_model = FormModel(self.dbm, "abc", "abc", form_code="cli001", fields=fields)
     self.dbm = MagicMock(spec=DatabaseManager)
     dbm_view = MagicMock()
     self.dbm.database_name = 'somedb'
     self.dbm.view = dbm_view
     self.es = Mock()
Example #24
0
 def create_entity_id_question_for_activity_report(self):
     entity_id_code = "eid"
     name = ugettext("I am submitting this data on behalf of")
     entity_id_question = UniqueIdField(
         'reporter',
         name=name,
         code=entity_id_code,
         label=name,
         instruction=ugettext("Choose Data Sender from this list."))
     return entity_id_question
Example #25
0
 def test_hidden_form_code_field_created(self):
     entity_field = UniqueIdField('clinic', "reporting on", "rep_on", "rep")
     form_model = FormModel(self.dbm,
                            'some form',
                            'some',
                            'form_code_1',
                            fields=[entity_field])
     form = WebForm(form_model, None)
     self.assertEquals(len(form.fields), 1)
     self.assertEquals(type(form.fields['form_code'].widget), HiddenInput)
Example #26
0
    def test_edit_survey_response_form_for_previously_errored_survey_response(self):
        question1 = UniqueIdField('patient',name="entity_question", code="q1", label="What is associated entity")
        question2 = TextField(name="question1_Name", code="q2", label="What is your name",
            defaultValue="some default value")
        form_model = FormModel(self.dbm, name="aids", label="Aids form_model",
            form_code="clinic", fields=[question1, question2])

        errored_survey_response = self.errored_survey_response()
        form = EditSurveyResponseForm(self.dbm, errored_survey_response, form_model, {'q1': 'a1', 'q2': 'a2'})
        form.save()
        self.assertTrue(form.is_valid)
Example #27
0
    def test_should_create_subject_field(self):
        self.dbm.database.view = Mock(return_value=Mock(rows=[{"key":(0,"cli1"),"value":"Clinic One"}]))
        entity_field = UniqueIdField("","reporting on", "rep_on", "rep", instruction="")
        project = Project(self.dbm, 'some form', 'some', 'form_code_1', fields=[entity_field])

        subject_field_creator = Mock(spec=SubjectQuestionFieldCreator)
        mock_field = Mock()
        subject_field_creator.create.return_value = mock_field
        form = SurveyResponseForm(project, None,  True)

        self.assertEquals(form.fields["rep_on"].choices, [('cli1', 'Clinic One(cli1)')])
Example #28
0
 def _create_unique_id_question(self, post_dict, code):
     return UniqueIdField(unique_id_type=self._get_unique_id_type(post_dict), name=self._get_name(post_dict, code),
                          code=code,
                          label=post_dict["title"],
                          instruction=post_dict.get("instruction"),
                          parent_field_code=post_dict.get('parent_field_code'),
                          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'),
                          required=post_dict.get('required'))
Example #29
0
 def test_should_give_back_unique_id_field(self):
     question1 = UniqueIdField('entity_type',
                               name="question1_Name",
                               code="Q1",
                               label="What is your name",
                               defaultValue="some default value")
     activity_report = FormModel(self.dbm,
                                 name="aids",
                                 label="Aids form_model",
                                 form_code="1",
                                 fields=[question1])
     self.assertIsNotNone(activity_report.entity_questions)
Example #30
0
 def test_should_return_code_title_tuple_list(self):
     question1 = UniqueIdField(unique_id_type='clinic',
                               label="entity_question",
                               code="ID",
                               name="What is associated entity")
     question2 = TextField(label="question1_Name",
                           code="Q1",
                           name="What is your name",
                           defaultValue="some default value")
     code_and_title = [(each_field.code, each_field.name)
                       for each_field in [question1, question2]]
     self.assertEquals([("ID", "What is associated entity"),
                        ("Q1", "What is your name")], code_and_title)