Exemplo n.º 1
0
 def create_entities(self):
     self.entities = [
         Entity(self.dbm, entity_type="clinic", location=["India"]),
         Entity(self.dbm, entity_type="clinic", location=["India"]),
     ]
     for e in self.entities:
         e.save()
Exemplo n.º 2
0
 def test_xform_with_unique_ids_substituted(self):
     self.questionnaire.xform = open(
         os.path.join(os.path.dirname(__file__),
                      "xform_with_unique_id_at_all_levels.xml"),
         'r').read()
     entity1 = Entity(self.dbm,
                      short_code="shortCode1",
                      entity_type="clinic")
     entity1._doc.data['name'] = {'value': 'nameOfEntity1'}
     entity2 = Entity(self.dbm,
                      short_code="shortCode2",
                      entity_type="clinic")
     entity2._doc.data['name'] = {'value': 'nameOfEntity2'}
     entities = [entity1, entity2]
     with patch(
             'mangrove.form_model.field.get_all_entities') as get_entities:
         get_entities.return_value = entities
         self.questionnaire.xform_model = Xform(self.questionnaire.xform)
         actual_xform = self.questionnaire.xform_with_unique_ids_substituted(
         )
         expected_xform = open(
             os.path.join(os.path.dirname(__file__),
                          "xform_with_choices_substituted.xml"),
             'r').read()
         self.assertEqual(actual_xform, expected_xform)
Exemplo n.º 3
0
 def _create_clinic_and_reporter(self):
     clinic_entity_short_code = 'clinic01'
     clinic_entity = Entity(self.dbm,
                            entity_type="clinic",
                            location=["India", "MH", "Pune"],
                            short_code=clinic_entity_short_code)
     clinic_entity.save()
     reporter_entity_short_code = 'reporter01'
     reporter_entity = Entity(self.dbm,
                              entity_type="reporter",
                              short_code=reporter_entity_short_code)
     reporter_entity.save()
     return clinic_entity, clinic_entity_short_code, reporter_entity, reporter_entity_short_code
Exemplo n.º 4
0
 def test_should_add_location_hierarchy_on_create(self):
     e = Entity(self.dbm,
                entity_type="clinic",
                location=["India", "MH", "Pune"])
     uuid = e.save()
     saved = get(self.dbm, uuid)
     self.assertEqual(saved.location_path, ["India", "MH", "Pune"])
Exemplo n.º 5
0
 def test_invalidate_entity(self):
     e = Entity(self.dbm, entity_type='store', location=['nyc'])
     e.save()
     self.assertFalse(e._doc.void)
     apple_type = DataDictType(self.dbm,
                               name='Apples',
                               slug='apples',
                               primitive_type='number')
     orange_type = DataDictType(self.dbm,
                                name='Oranges',
                                slug='oranges',
                                primitive_type='number')
     apple_type.save()
     orange_type.save()
     data = [[('apples', 20, apple_type), ('oranges', 30, orange_type)],
             [('apples', 10, apple_type), ('oranges', 20, orange_type)]]
     data_ids = []
     for d in data:
         id = e.add_data(d)
         self.assertFalse(self.dbm._load_document(id).void)
         data_ids.append(id)
     e.invalidate()
     self.assertTrue(e._doc.void)
     for id in data_ids:
         self.assertTrue(self.dbm._load_document(id).void)
Exemplo n.º 6
0
 def test_create_entity(self):
     e = Entity(self.dbm,
                entity_type="clinic",
                location=["India", "MH", "Pune"])
     uuid = e.save()
     self.assertTrue(uuid)
     self.dbm.delete(e)
    def _setup_entities(self):
        ENTITY_TYPE = ["Health_Facility", "Clinic"]
        AGGREGATION_PATH_NAME = "governance"

        # Entities for State 1: Maharashtra
        # location, aggregation_path
        locations = [
            ['India', 'MH', 'Pune'],
            ['India', 'MH', 'Mumbai'],
            ['India', 'Karnataka', 'Bangalore'],
            ['India', 'Karnataka', 'Hubli'],
            ['India', 'Kerala', 'Kochi'],
        ]
        aggregation_paths = [
            ["Director", "Med_Supervisor", "Surgeon"],
            ["Director", "Med_Supervisor", "Nurse"],
            ["Director", "Med_Officer", "Doctor"],
            ["Director", "Med_Officer", "Surgeon"],
            ["Director", "Med_Officer", "Nurse"],
        ]

        self.entities = []
        for i in range(self._number_of_entities):
            location = random.choice(locations)
            aggregation_path = random.choice(aggregation_paths)
            e = Entity(self.manager,
                       entity_type=ENTITY_TYPE,
                       location=location)
            e.set_aggregation_path(AGGREGATION_PATH_NAME, aggregation_path)
            e.save()
            self.entities.append(e)
Exemplo n.º 8
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)
Exemplo n.º 9
0
 def test_create_entity_with_id(self):
     e = Entity(self.dbm,
                entity_type="clinic",
                location=["India", "MH", "Pune"],
                id="-1000")
     uuid = e.save()
     self.assertEqual(uuid, "-1000")
     self.dbm.delete(e)
Exemplo n.º 10
0
 def setUp(self):
     self.dbm = get_db_manager(database='mangrove-test')
     define_type(self.dbm, ["Reporter"])
     reporter = Entity(self.dbm,
                       entity_type=["Reporter"],
                       location=["Pune", "India"],
                       short_code="REPX")
     reporter.save()
     reporter = Entity(self.dbm,
                       entity_type=["Reporter"],
                       location=["Pune", "India"],
                       short_code="REP1")
     reporter.save()
     reporter = Entity(self.dbm,
                       entity_type=["Reporter"],
                       location=["Pune", "India"],
                       short_code="REP2")
     reporter.save()
Exemplo n.º 11
0
 def test_should_create_location_geojson_for_unknown_location(self):
     expected_geojson = '{"type": "FeatureCollection", "features": []}'
     entity1 = Entity(self.dbm,
                      entity_type="Water Point",
                      location=["India", "MH", "Pune"],
                      short_code="WP002")
     entity_list = [entity1]
     self.assertEqual(expected_geojson,
                      helper.create_location_geojson(entity_list))
Exemplo n.º 12
0
 def test_get_entities(self):
     e2 = Entity(self.dbm, "hospital", ["India", "TN", "Chennai"])
     id2 = e2.save()
     entities = get_entities(self.dbm, [self.uuid, id2])
     self.assertEqual(len(entities), 2)
     saved = dict([(e.id, e) for e in entities])
     self.assertEqual(saved[id2].type_string, "hospital")
     self.assertEqual(saved[self.uuid].type_string, "clinic")
     self.dbm.delete(e2)
Exemplo n.º 13
0
    def test_should_add_email_to_datasender(self):
        email = "*****@*****.**"
        entity = Entity(self.manager, entity_type=REPORTER_ENTITY_TYPE, short_code="short_code")
        entity.save()
        create_default_reg_form_model(self.manager)

        put_email_information_to_entity(self.manager, entity, email=email)

        entity_values = entity.data
        self.assertEquals(entity_values["email"]["value"], email)
Exemplo n.º 14
0
def delete_and_create_entity_instance(manager, ENTITY_TYPE, location, short_code):
    try:
        entity = get_by_short_code(dbm=manager, short_code=short_code,entity_type=ENTITY_TYPE)
        entity.delete()
    except DataObjectNotFound:
        pass

    e = Entity(manager, entity_type=ENTITY_TYPE, location=location, short_code=short_code)
    id1 = e.save()
    return e, id1
Exemplo n.º 15
0
 def test_should_not_raise_exception_if_coordinates_is_empty(self):
     expected_geojson = '{"type": "FeatureCollection", "features": [{"geometry": {"type": "Point", "coordinates": [3, 1]}, "type": "Feature"}]}'
     entity1 = Entity(self.dbm,
                      entity_type="Water Point",
                      location=["India", "MH", "Pune"],
                      short_code="WP002",
                      geometry={
                          'type': 'Point',
                          'coordinates': []
                      })
     entity2 = Entity(self.dbm,
                      entity_type="Water Point",
                      location=["India", "MH", "Pune"],
                      short_code="WP002",
                      geometry={
                          'type': 'Point',
                          'coordinates': [1, 3]
                      })
     entity_list = [entity1, entity2]
     self.assertEqual(expected_geojson,
                      helper.create_location_geojson(entity_list))
Exemplo n.º 16
0
 def test_should_resolve_location_to_centriod(self):
     expected_geojson = '{"type": "FeatureCollection", "features": [{"geometry": {"type": "Point", "coordinates": [48.41586788688786, -17.814011993472985]}, "type": "Feature"}, {"geometry": {"type": "Point", "coordinates": [3, 1]}, "type": "Feature"}]}'
     entity1 = Entity(self.dbm,
                      entity_type="Water Point",
                      location=[
                          'Madagascar', 'TOAMASINA', 'ALAOTRA MANGORO',
                          'AMBATONDRAZAKA', 'AMBATONDRAZAKA'
                      ],
                      short_code="WP002",
                      geometry={})
     entity2 = Entity(self.dbm,
                      entity_type="Water Point",
                      location=["India", "MH", "Pune"],
                      short_code="WP002",
                      geometry={
                          'type': 'Point',
                          'coordinates': [1, 3]
                      })
     entity_list = [entity1, entity2]
     self.assertEqual(expected_geojson,
                      helper.create_location_geojson(entity_list))
Exemplo n.º 17
0
    def test_should_add_email_to_datasender(self):
        email = "*****@*****.**"
        entity = Entity(self.manager,
                        entity_type=REPORTER_ENTITY_TYPE,
                        short_code="short_code")
        entity.save()
        delete_and_create_form_model(self.manager,
                                     GLOBAL_REGISTRATION_FORM_CODE)

        set_email_for_contact(self.manager, entity, email=email)

        entity_values = entity.data
        self.assertEquals(entity_values["email"]["value"], email)
Exemplo n.º 18
0
    def test_should_get_all_entities(self):
        e = Entity(self.dbm,
                   entity_type="clinic",
                   location=["India", "MH", "Mumbai"],
                   short_code='cli002')
        e.save()

        e = Entity(self.dbm,
                   entity_type="clinic",
                   location=["India", "MH", "Jalgaon"],
                   short_code='cli003')
        e.save()

        e = Entity(self.dbm,
                   entity_type="clinic",
                   location=["India", "MH", "Nasik"],
                   short_code='cli004')
        uuid = e.save()

        all_entities = get_all_entities(self.dbm)

        self.assertEqual(4, len(all_entities))
        self.assertEqual([uuid],
                         [e['id'] for e in all_entities if e['id'] == uuid])
Exemplo n.º 19
0
 def test_should_add_passed_in_hierarchy_path_on_create(self):
     e = Entity(self.dbm,
                entity_type=["HealthFacility", "Clinic"],
                location=["India", "MH", "Pune"],
                aggregation_paths={
                    "org": ["TW_Global", "TW_India", "TW_Pune"],
                    "levels":
                    ["Lead Consultant", "Sr. Consultant", "Consultant"]
                })
     uuid = e.save()
     saved = get(self.dbm, uuid)
     hpath = saved._doc.aggregation_paths
     self.assertEqual(hpath["org"], ["TW_Global", "TW_India", "TW_Pune"])
     self.assertEqual(hpath["levels"],
                      ["Lead Consultant", "Sr. Consultant", "Consultant"])
Exemplo n.º 20
0
    def test_should_create_with_bulk_upload(self):
        NUM_ENTITIES = 1000
        DATA_REC_PER_ENTITY = 10
        BATCH = (NUM_ENTITIES * DATA_REC_PER_ENTITY) / 1

        start = datetime.datetime.now()
        le = [
            Entity(self.dbm,
                   entity_type=["Health_Facility", "Clinic"],
                   location=['India', 'MH', 'Pune'])
            for x in range(0, NUM_ENTITIES)
        ]
        entity_docs = [x._doc for x in le]
        r = self.dbm.database.update(entity_docs)
        end = datetime.datetime.now()
        print "Updating entities took %s" % (end - start, )

        print "data records"
        for e in le:
            for i in range(0, DATA_REC_PER_ENTITY):
                e.add_data_bulk(
                    data=[("beds", 10,
                           self.beds_type), ("meds", 10, self.meds_type)])

        print "total bulk docs %s" % (len(self.dbm.bulk))
        print "bulk save !"

        start = datetime.datetime.now()

        for s in range(0, len(self.dbm.bulk))[::BATCH]:
            start = datetime.datetime.now()
            r = self.dbm.database.update(self.dbm.bulk[s:s + BATCH])
            end = datetime.datetime.now()

        print "Bulk updates took %s" % (end - start, )

        print "Firing view..."
        start = datetime.datetime.now()
        values = data.aggregate(self.dbm,
                                entity_type=["Health_Facility", "Clinic"],
                                aggregates={
                                    "beds": data.reduce_functions.LATEST,
                                    "meds": data.reduce_functions.COUNT
                                },
                                aggregate_on=EntityAggregration())
        end = datetime.datetime.now()
        print "views took %s" % (end - start, )
        print "Done!"
Exemplo n.º 21
0
    def test_get_associated_data_senders(self):
        entity = Entity(self.manager,
                        entity_type=["reporter"],
                        short_code="rep1")
        entity_id = entity.save()
        project = Project(dbm=self.manager,
                          name="TestDS",
                          goals="Testing",
                          devices=['web'],
                          form_code="ds_form",
                          fields=[])
        project.data_senders = ["rep1"]
        project.save()
        result = project.get_associated_datasenders(self.manager)

        self.assertEquals(result[0].short_code, entity.short_code)
        self.assertEquals(result[0].id, entity_id)
Exemplo n.º 22
0
 def test_should_return_data_types(self):
     med_type = DataDictType(self.dbm,
                             name='Medicines',
                             slug='meds',
                             primitive_type='number',
                             description='Number of medications',
                             tags=['med'])
     med_type.save()
     doctor_type = DataDictType(self.dbm,
                                name='Doctor',
                                slug='doc',
                                primitive_type='string',
                                description='Name of doctor',
                                tags=['doctor', 'med'])
     doctor_type.save()
     facility_type = DataDictType(self.dbm,
                                  name='Facility',
                                  slug='facility',
                                  primitive_type='string',
                                  description='Name of facility')
     facility_type.save()
     e = Entity(self.dbm, entity_type='foo')
     e.save()
     data_record = [('meds', 20, med_type), ('doc', "aroj", doctor_type),
                    ('facility', 'clinic', facility_type)]
     e.add_data(data_record)
     # med (tag in list)
     types = [typ.slug for typ in e.data_types(['med'])]
     self.assertTrue(med_type.slug in types)
     self.assertTrue(doctor_type.slug in types)
     self.assertTrue(facility_type.slug not in types)
     # doctor (tag as string)
     types = [typ.slug for typ in e.data_types('doctor')]
     self.assertTrue(doctor_type.slug in types)
     self.assertTrue(med_type.slug not in types)
     self.assertTrue(facility_type.slug not in types)
     # med and doctor (more than one tag)
     types = [typ.slug for typ in e.data_types(['med', 'doctor'])]
     self.assertTrue(doctor_type.slug in types)
     self.assertTrue(med_type.slug not in types)
     self.assertTrue(facility_type.slug not in types)
     # no tags
     types = [typ.slug for typ in e.data_types()]
     self.assertTrue(med_type.slug in types)
     self.assertTrue(doctor_type.slug in types)
     self.assertTrue(facility_type.slug in types)
Exemplo n.º 23
0
    def test_should_get_entity_by_short_code(self):
        reporter = Entity(self.dbm,
                          entity_type=["Reporter"],
                          location=["Pune", "India"],
                          short_code="repx")
        reporter.save()

        entity = get_by_short_code(self.dbm,
                                   short_code="repx",
                                   entity_type=["Reporter"])
        self.assertTrue(entity is not None)
        self.assertEqual("repx", entity.short_code)

        with self.assertRaises(DataObjectNotFound):
            entity = get_by_short_code(self.dbm,
                                       short_code="ABC",
                                       entity_type=["Waterpoint"])
Exemplo n.º 24
0
 def test_invalidate_data(self):
     e = Entity(self.dbm, entity_type='store', location=['nyc'])
     e.save()
     apple_type = DataDictType(self.dbm,
                               name='Apples',
                               slug='apples',
                               primitive_type='number')
     orange_type = DataDictType(self.dbm,
                                name='Oranges',
                                slug='oranges',
                                primitive_type='number')
     apple_type.save()
     orange_type.save()
     data = e.add_data([('apples', 20, apple_type),
                        ('oranges', 30, orange_type)])
     valid_doc = self.dbm._load_document(data)
     self.assertFalse(valid_doc.void)
     e.invalidate_data(data)
     invalid_doc = self.dbm._load_document(data)
     self.assertTrue(invalid_doc.void)
Exemplo n.º 25
0
 def test_should_validate_the_mobile_number_for_paid_account_if_unique(
         self):
     org = Mock(spec=Organization)
     type(org).in_trial_mode = PropertyMock(return_value=False)
     validator = MobileNumberValidater(org, "56789", "rep")
     manager = MagicMock(spec=DatabaseManager)
     with patch(
             "datawinners.accountmanagement.mobile_number_validater.find_reporters_by_from_number"
     ) as find_reporters_by_from_number:
         with patch(
                 "datawinners.accountmanagement.mobile_number_validater.get_database_manager_for_org"
         ) as get_db_manager:
             datasender = Entity(manager,
                                 REPORTER_ENTITY_TYPE,
                                 short_code='reporter_id')
             find_reporters_by_from_number.return_value = [datasender]
             manager = MagicMock(spec=DatabaseManager)
             manager.load_all_rows_in_view.return_value = []
             get_db_manager.return_value = manager
             valid, message = validator.validate()
             self.assertFalse(valid)
Exemplo n.º 26
0
    def test_subject_index_dict(self):
        dbm = Mock(spec=DatabaseManager)
        entity_type = ['entity_type']

        mock_entity = Entity(dbm=dbm, entity_type=entity_type)
        mock_entity.add_data(data=[('name1', 'ans1'), ('name2', 'ans2')])

        with patch.object(Entity, 'get') as get:
            get.return_value = mock_entity
            form_model = EntityFormModel(dbm=dbm, entity_type=entity_type)
            form_model.add_field(TextField('name1', 'code1', 'label1'))
            form_model.add_field(TextField('name2', 'code2', 'label2'))
            form_model._doc = Mock(form_code="abc", id='form_id')
            entity_doc = Mock()

            result = subject_dict(entity_type, entity_doc, dbm, form_model)

            expected = {
                'form_id_code1': 'ans1',
                'form_id_code2': 'ans2',
                'entity_type': ['entity_type'],
                'void': False
            }
            self.assertEquals(result, expected)
 def create_reporter(self):
     r = Entity(self.manager, entity_type=["reporter"], short_code='ashwin')
     r.add_data((('mobile_number', '2998288332'), ('name', 'ashwin')))
     r.save()
     return r
Exemplo n.º 28
0
 def test_should_not_raise_exception_if_no_location_or_geo_code_specified(
         self):
     entity = Entity(self.dbm,
                     entity_type="Water Point",
                     short_code="WP002")
     helper.create_location_geojson(entity_list=[entity])
Exemplo n.º 29
0
 def test_should_add_entity_type_on_create(self):
     e = Entity(self.dbm, entity_type=["healthfacility", "clinic"])
     uuid = e.save()
     saved = get(self.dbm, uuid)
     self.assertEqual(saved.type_path, ["healthfacility", "clinic"])
Exemplo n.º 30
0
 def test_should_add_entity_type_on_create_as_aggregation_tree(self):
     e = Entity(self.dbm, entity_type="health_facility")
     uuid = e.save()
     saved = get(self.dbm, uuid)
     self.assertEqual(saved.type_path, ["health_facility"])