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.º 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_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.º 4
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)
Exemplo n.º 5
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"])
    def CreateSchool(self, data, id, short_code):
        school = Entity(self.manager, self.entity_type, data, id=id, short_code=short_code)
        school_id = school.save()
        school.add_data(data=[("SchoolID", id, self.default_ddtype),
                            ("SchollName", short_code, self.default_ddtype),
                            ("SchoolLocation", data, self.default_ddtype)])

        return school_id
Exemplo n.º 7
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.º 8
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.º 9
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.º 10
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.º 11
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.º 12
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.º 13
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.º 14
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.º 15
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.º 16
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.º 17
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.º 18
0
def find_reporter_entity(dbm, from_number):
    reporter_list = find_reporters_by_from_number(dbm, from_number)
    if len(reporter_list) > 1:
        raise MultipleReportersForANumberException(from_number)
    reporter_entity_id = reporter_list[0].keys()[0]
    reporter_name = reporter_list[0][reporter_entity_id].get(NAME_FIELD)
    return Entity.get(dbm, reporter_entity_id),reporter_name
Exemplo n.º 19
0
def lookup_entity_by_uid(dbm, uid):
    try:
        if uid:
            entity = Entity.get(dbm, uid)
            return entity.value('name'), entity.short_code
    except Exception:
        pass
    return UNKNOWN, UNKNOWN
Exemplo n.º 20
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.º 21
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.º 22
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)
def get_data_sender(manager, submission):
    if submission.owner_uid:
        try:
            data_sender_entity = Entity.get(manager, submission.owner_uid)
            return data_sender_entity.value("name"), data_sender_entity.short_code, data_sender_entity.id
        except Exception as e:
            pass #ignore and sending unknown datasender for backward compatibility.
    return NOT_AVAILABLE_DS, NOT_AVAILABLE
Exemplo n.º 24
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.º 25
0
def _find_reporter_name(dbm, row):
    try:
       if row.value["owner_uid"]:
           data_sender_entity = Entity.get(dbm, row.value["owner_uid"])
           name = data_sender_entity.value('name')
           return name
    except Exception:
        pass
    return ""
Exemplo n.º 26
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.º 27
0
 def get_associated_datasenders(self, dbm):
     keys = [([REPORTER], short_code) for short_code in self.data_senders]
     rows = dbm.view.by_short_codes(reduce=False,
                                    include_docs=True,
                                    keys=keys)
     return [
         Entity.new_from_doc(dbm,
                             Entity.__document_class__.wrap(row.get('doc')))
         for row in rows
     ]
Exemplo n.º 28
0
def void_data_records(database_name):
    dbm = get_db_manager(database_name)
    entities = dbm.load_all_rows_in_view('entity_by_short_code', include_docs=True)
    for entity in entities:
        if entity['doc']['document_type'] == 'Entity':
            entity = Entity.new_from_doc(dbm, EntityDocument.wrap(entity['doc']))
            if entity.is_void():
                data_records = dbm.load_all_rows_in_view('entity_data', key=entity.id)
                for record in data_records:
                    dbm.invalidate(record.id)
Exemplo n.º 29
0
def _entity_dict(entity_type, entity_doc, dbm, form_model):
    entity = Entity.get(dbm, entity_doc.id)
    fields, labels, codes = get_entity_type_fields(dbm, form_model.form_code)
    data = tabulate_data(entity, form_model, codes)
    dictionary = OrderedDict()
    for index in range(0, len(fields)):
        dictionary.update({fields[index]: data['cols'][index]})
    dictionary.update({"entity_type": entity_type})
    dictionary.update({"void": entity.is_void()})
    return dictionary
Exemplo n.º 30
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.º 31
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.º 32
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.º 33
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.º 34
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])
    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.º 36
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.º 37
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)
Exemplo n.º 38
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.º 39
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.º 40
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.º 41
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.º 42
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)
 def CreateReporter(self):
     reporter = Entity(self.manager, location=["China", "Beijing"], short_code="rpt_bj", id="Reporter001",
         entity_type=self.reporter_type)
     reporter.add_data(data=[(MOBILE_NUMBER_FIELD, "1234567890", self.phone_number_type),(NAME_FIELD, "001s", self.first_name_type)])
     reporter_id = reporter.save()
     return reporter_id, reporter
Exemplo n.º 44
0
                           location=[country, state],
                           centroid=centroid,
                           gr_id=gr_id)
                locations[(country, state)] = e.save()
                states[state] = e.id
                data = [(name_type.slug, state, name_type)]
                e.add_data(data, event_time=datetime.datetime(2011, 03, 01, tzinfo=UTC))
                num_rows += 1
                print "[%s]...(%s, %s) -- %s" % (num_rows, country, state, e.id)
            gr_id = lga_gr_id
            feature = get_feature_by_id(gr_id)
#            geometry = feature['geometry']
            centroid = json.loads(feature['properties']['geometry_centroid'])
            e = Entity(dbm,
                       entity_type=["Location", "LGA"],
                       location=[country, state, lga],
                       centroid=centroid,
                       gr_id=gr_id)
            locations[location] = e.save()
            geo_id_dict[geo_id] = e
            data = [(name_type.slug, lga, name_type)]
            e.add_data(data, event_time=datetime.datetime(2011, 03, 01, tzinfo=UTC))
            data = [(geo_id_type.slug, geo_id, geo_id_type)]
            e.add_data(data, event_time=datetime.datetime(2011, 03, 01, tzinfo=UTC))

            num_rows += 1
            print "[%s]...(%s, %s, %s) -- %s" % (num_rows, country, state, lga, e.id)
            if cgs:
                num_cgs += 1
                e.add_data(data=[(cgs_type.slug, cgs, cgs_type)])
Exemplo n.º 45
0
 def setUp(self):
     self.dbm = get_db_manager(database='mangrove-test')
     e = Entity(self.dbm, entity_type="clinic", location=["India", "MH", "Pune"])
     self.uuid = e.save()
Exemplo n.º 46
0
 def test_should_create_entity_from_document(self):
     existing = self.dbm.get(self.uuid, Entity)
     e = Entity.new_from_doc(self.dbm, existing._doc)
     self.assertTrue(e._doc is not None)
     self.assertEqual(e.id, existing.id)
     self.assertEqual(e.type_path, existing.type_path)
Exemplo n.º 47
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"])
Exemplo n.º 48
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.º 49
0
 def test_should_return_empty_list_if_location_path_is_not_stored(self):
     e = Entity(self.dbm, entity_type="clinic")
     uuid = e.save()
     saved = get(self.dbm, uuid)
     self.assertEqual(saved.location_path, [])
Exemplo n.º 50
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)
Exemplo n.º 51
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.º 52
0
    def _create_entities_and_data_records(self):
        ENTITY_TYPE = ["Health_Facility", "Clinic"]
        self.entity_type = ENTITY_TYPE
        FEB = datetime.datetime(2011, 02, 01, tzinfo=UTC)
        MARCH = datetime.datetime(2011, 03, 01, tzinfo=UTC)

        # Entities for State 1: Maharashtra
        e = Entity(self.manager, entity_type=ENTITY_TYPE, location=['India', 'MH', 'Pune'])
        e.save()

        e.add_data(data=[("beds", 300, self.dd_types['beds']), ("meds", 20, self.dd_types['meds']),
                         ("director", "Dr. A", self.dd_types['director']), ("patients", 10, self.dd_types['patients'])],
                   event_time=FEB)
        e.add_data(data=[("beds", 500, self.dd_types['beds']), ("meds", 20, self.dd_types['meds']),
                         ("patients", 20, self.dd_types['patients'])],
                   event_time=MARCH)

        e = Entity(self.manager, entity_type=ENTITY_TYPE, location=['India', 'MH', 'Pune'])
        e.save()
        e.add_data(data=[("beds", 100, self.dd_types['beds']), ("meds", 10, self.dd_types['meds']),
                         ("director", "Dr. AA", self.dd_types['director']), ("patients", 50, self.dd_types['patients'])],
                   event_time=FEB)
        e.add_data(data=[("beds", 200, self.dd_types['beds']), ("meds", 20, self.dd_types['meds']),
                         ("patients", 20, self.dd_types['patients'])],
                   event_time=MARCH)

        e = Entity(self.manager, entity_type=ENTITY_TYPE, location=['India', 'MH', 'Mumbai'])
        e.save()
        e.add_data(data=[("beds", 100, self.dd_types['beds']), ("meds", 10, self.dd_types['meds']),
                         ("director", "Dr. AAA", self.dd_types['director']), ("patients", 50, self.dd_types['patients'])],
                   event_time=FEB)
        e.add_data(data=[("beds", 200, self.dd_types['beds']), ("meds", 20, self.dd_types['meds']),
                         ("patients", 50, self.dd_types['patients'])],
                   event_time=MARCH)

        # Entities for State 2: karnataka
        e = Entity(self.manager, entity_type=ENTITY_TYPE, location=['India', 'Karnataka', 'Bangalore'])
        e.save()
        e.add_data(data=[("beds", 100, self.dd_types['beds']), ("meds", 250, self.dd_types['meds']),
                         ("director", "Dr. B1", self.dd_types['director']), ("patients", 50, self.dd_types['patients'])],
                   event_time=FEB)
        e.add_data(data=[("beds", 200, self.dd_types['beds']), ("meds", 400, self.dd_types['meds']),
                         ("director", "Dr. B2", self.dd_types['director']), ("patients", 20, self.dd_types['patients'])],
                   event_time=MARCH)
        e = Entity(self.manager, entity_type=ENTITY_TYPE, location=['India', 'Karnataka', 'Hubli'])
        e.save()
        e.add_data(data=[("beds", 100, self.dd_types['beds']), ("meds", 250, self.dd_types['meds']),
                         ("director", "Dr. B1", self.dd_types['director']), ("patients", 50, self.dd_types['patients'])],
                   event_time=FEB)
        e.add_data(data=[("beds", 200, self.dd_types['beds']), ("meds", 400, self.dd_types['meds']),
                         ("director", "Dr. B2", self.dd_types['director']), ("patients", 20, self.dd_types['patients'])],
                   event_time=MARCH)
        # Entities for State 3: Kerala
        e = Entity(self.manager, entity_type=ENTITY_TYPE, location=['India', 'Kerala', 'Kochi'])
        e.save()
        e.add_data(data=[("beds", 200, self.dd_types['beds']), ("meds", 50, self.dd_types['meds']),
                         ("director", "Dr. C", self.dd_types['director']), ("patients", 12, self.dd_types['patients'])],
                   event_time=MARCH)
Exemplo n.º 53
0
    def _import_data(self, server, database, max_facilities_to_import=DEFAULT_FACILITIES_TO_IMPORT,
                     max_mdg_to_import=DEFAULT_MDG_TO_IMPORT):
        from mangrove.datastore.database import DatabaseManager
        from mangrove.datastore.entity import Entity, get_entities_by_value
        from mangrove.datastore.datadict import DataDictType, get_datadict_type, create_datadict_type
        from mangrove.utils import GoogleSpreadsheetsClient
        from mangrove.utils.google_spreadsheets import get_string, get_number, get_boolean, get_list
        from mangrove.utils.spreadsheets import CsvReader
        from mangrove.utils.helpers import slugify
        from mangrove.georegistry.api import get_feature_by_id
        import os
        import datetime
        import json
        from pytz import UTC

        print "Loading 'NIMS Data'..."

        print "\tServer: %s" % server
        print "\tDatabase: %s" % database

        dbm = DatabaseManager(server=server, database=database)

        user_spreadsheets = GoogleSpreadsheetsClient(settings.GMAIL_USERNAME, settings.GMAIL_PASSWORD)
        nims_data = user_spreadsheets['NIMS Data Deux']

        load_population = True
        load_other = True
        load_mdg = True
        load_health = True
        load_water = True
        load_education = True

        countries = {}
        states = {}
        locations = {}
        num_cgs = 0
        datadict_types = {}
        geo_id_dict = {}

        cgs_type = create_datadict_type(
            dbm,
            slug='cgs',
            name='CGS',
            primitive_type='boolean'
        )
        datadict_types['cgs'] = cgs_type.id

        geo_id_type = create_datadict_type(
            dbm,
            slug='geo_id',
            name='Geographic ID',
            primitive_type='string'
        )
        datadict_types['geo_id'] = geo_id_type.id

        name_type = create_datadict_type(
            dbm,
            slug='name',
            name='Name',
            primitive_type='string'
        )
        datadict_types['name'] = name_type.id

        mdg_type = create_datadict_type(
            dbm,
            slug='mdg',
            name='MDG',
            primitive_type='string'
        )
        datadict_types['mdg'] = mdg_type.id

        country_geo_id = {}
        for row in nims_data['Nigeria Country ALL']:
            country_geo_id[row['name']] = row['grid']
        state_geo_ids = {}
        for row in nims_data['Nigeria States ALL']:
            state_geo_ids[row['name']] = row['grid']

        num_rows = 0
        print "Importing location entities from 'Nigeria LGAs ALL' worksheet"
        for row in nims_data['Nigeria LGAs ALL']:
            country = get_string('country', row)
            state = get_string('state', row)
            lga = get_string('lga', row)
            cgs = get_boolean('cgs', row)
            geo_id = get_string('geoid', row)
            lga_gr_id = get_string('grid', row)
            location = (country, state, lga)
            if country not in countries:
                gr_id = country_geo_id[country]
                feature = get_feature_by_id(gr_id)
#                geometry = feature['geometry']
                centroid = json.loads(feature['properties']['geometry_centroid'])
                e = Entity(dbm,
                           entity_type=["Location", "Country"],
                           location=[country],
                           centroid=centroid,
                           gr_id=gr_id)
                locations[(country,)] = e.save()
                countries[country] = e.id
                data = [(name_type.slug, country, name_type)]
                e.add_data(data, event_time=datetime.datetime(2011, 03, 01, tzinfo=UTC))
                num_rows += 1
                print "[%s]...(%s) -- %s" % (num_rows, country, e.id)
Exemplo n.º 54
0
def _get_entity_for_row(manager, row, type):
    short_code = row['doc']['short_code']
    entity = Entity.new_from_doc(dbm=manager, doc=Entity.__document_class__.wrap(row.get('doc')))
    return entity, short_code