def test_should_create_entity_with_short_code(self): with self.assertRaises(AssertionError): entity = create_entity(self.dbm, entity_type=["reporter"], short_code=None) with self.assertRaises(AssertionError): entity = create_entity(self.dbm, entity_type=["reporter"], short_code="") with self.assertRaises(AssertionError): entity = create_entity(self.dbm, entity_type="Reporter", short_code="BLAH") with self.assertRaises(AssertionError): entity = create_entity(self.dbm, entity_type=[], short_code="BLAH") with self.assertRaises(AssertionError): entity = create_entity(self.dbm, entity_type=("reporter"), short_code="BLAH") entity = create_entity(self.dbm, entity_type=["reporter"], short_code="ABC") saved_entity = get_by_short_code(self.dbm, short_code="ABC", entity_type=["reporter"]) self.assertEqual(saved_entity.id, entity.id) with self.assertRaises(DataObjectAlreadyExists): create_entity(self.dbm, entity_type=["reporter"], short_code="ABC") with self.assertRaises(EntityTypeDoesNotExistsException): create_entity(self.dbm, entity_type=["Dog"], short_code="ABC")
def setUp(self): self.dbm = get_db_manager(database='mangrove-test') initializer.run(self.dbm) define_type(self.dbm, ["dog"]) self.entity_type = ["healthfacility", "clinic"] define_type(self.dbm, self.entity_type) self.name_type = DataDictType(self.dbm, name='Name', slug='name', primitive_type='string') self.telephone_number_type = DataDictType(self.dbm, name='telephone_number', slug='telephone_number', primitive_type='string') self.entity_id_type = DataDictType(self.dbm, name='Entity Id Type', slug='entity_id', primitive_type='string') self.stock_type = DataDictType(self.dbm, name='Stock Type', slug='stock', primitive_type='integer') self.color_type = DataDictType(self.dbm, name='Color Type', slug='color', primitive_type='string') self.name_type.save() self.telephone_number_type.save() self.stock_type.save() self.color_type.save() self.entity = create_entity(self.dbm, entity_type=self.entity_type, location=["India", "Pune"], aggregation_paths=None, short_code="cli1", ) self.data_record_id = self.entity.add_data(data=[("Name", "Ruby", self.name_type)], submission=dict(submission_id="1")) self.reporter = create_entity(self.dbm, entity_type=["reporter"], location=["India", "Pune"], aggregation_paths=None, short_code="rep1", ) self.reporter.add_data(data=[(MOBILE_NUMBER_FIELD, '1234', self.telephone_number_type), (NAME_FIELD, "Test_reporter", self.name_type)], submission=dict(submission_id="2")) question1 = TextField(name="entity_question", code="EID", label="What is associated entity", language="eng", entity_question_flag=True, ddtype=self.entity_id_type) question2 = TextField(name="Name", code="NAME", label="Clinic Name", defaultValue="some default value", language="eng", length=TextConstraint(4, 15), ddtype=self.name_type) question3 = IntegerField(name="Arv stock", code="ARV", label="ARV Stock", range=NumericConstraint(min=15, max=120), ddtype=self.stock_type) question4 = SelectField(name="Color", code="COL", label="Color", options=[("RED", 1), ("YELLOW", 2)], ddtype=self.color_type) self.form_model = FormModel(self.dbm, entity_type=self.entity_type, name="aids", label="Aids form_model", form_code="clinic", type='survey', fields=[question1, question2, question3]) self.form_model.add_field(question4) self.form_model__id = self.form_model.save() self.submission_handler = SubmissionHandler(self.dbm) self.sms_player = SMSPlayer(self.dbm, self.submission_handler, LocationTree())
def make_user_as_a_datasender(manager, organization, current_user_name, mobile_number): total_entity = get_entity_count_for_type(manager, [REPORTER]) reporter_short_code = 'rep' + str(total_entity + 1) entity = create_entity(dbm=manager, entity_type=REPORTER_ENTITY_TYPE, short_code=reporter_short_code, location=[organization.country_name()]) mobile_number_type = get_or_create_data_dict(manager, name='Mobile Number Type', slug='mobile_number', primitive_type='string') name_type = get_or_create_data_dict(manager, name='Name', slug='name', primitive_type='string') data = [(MOBILE_NUMBER_FIELD, mobile_number, mobile_number_type), (NAME_FIELD, current_user_name, name_type)] entity.add_data(data=data) if organization.in_trial_mode: data_sender = DataSenderOnTrialAccount.objects.model( mobile_number=mobile_number, organization=organization) data_sender.save() return entity.short_code
def make_user_as_a_datasender(manager, organization, current_user_name, mobile_number, email=None): total_entity = get_entity_count_for_type(manager, [REPORTER]) reporter_id = None offset = 1 location = [organization.country_name()] while not reporter_id: reporter_short_code = 'rep' + str(total_entity + offset) try: entity = create_entity(dbm=manager, entity_type=REPORTER_ENTITY_TYPE, short_code=reporter_short_code, location=location) reporter_id = entity.short_code except DataObjectAlreadyExists: offset += 1 data = _create_entity_data(manager, current_user_name, email, location, mobile_number) entity.add_data(data=data) entity.save() if organization.in_trial_mode: data_sender = DataSenderOnTrialAccount.objects.model( mobile_number=mobile_number, organization=organization) data_sender.save(force_insert=True) return entity.short_code
def create_or_update_entity(manager, entity_type, location, aggregation_paths, short_code, geometry=None): try: entity = get_by_short_code(manager, short_code, entity_type) entity.delete() except DataObjectNotFound: pass return create_entity(manager, entity_type, short_code, location, aggregation_paths, geometry)
def create_entity(self, dbm): location_hierarchy, processed_geometry = Location(self.location_tree, self.form_model).process_entity_creation( self.cleaned_data) return entity.create_entity(dbm=dbm, entity_type=self.entity_type, location=location_hierarchy, short_code=self.short_code, geometry=processed_geometry)
def to_entity(self, dbm): if self.form_model.is_registration_form(): location = self.cleaned_data.get(LOCATION_TYPE_FIELD_CODE) return entity.create_entity(dbm=dbm, entity_type=self.entity_type, location=location, short_code=self.short_code, geometry=convert_to_geometry(self.cleaned_data.get(GEO_CODE))) return entity.get_by_short_code(dbm, self.short_code, self.entity_type)
def create_or_update_entity(self, manager, entity_type, location, aggregation_paths, short_code, geometry=None): try: entity = get_by_short_code(manager, short_code, entity_type) entity.delete() except DataObjectNotFound: pass if entity_type == [REPORTER]: return create_contact(manager, short_code, location, aggregation_paths, geometry) return create_entity(manager, entity_type, short_code, location, aggregation_paths, geometry)
def create_datasender(self): ds = create_entity(self.manager, entity_type=["reporter"], location=["India", "Pune"], aggregation_paths=None, short_code=''.join( random.sample('abcdefghijklmnopqrs', 6))) ds.add_data(data=[("entity_type", "reporter")]) return ds
def _create_entities(self): self.entity_type = ['clinic'] define_type(self.dbm, self.entity_type) define_type(self.dbm, ['reporter']) self.name_type = DataDictType(self.dbm, name='Name', slug='name', primitive_type='string') self.telephone_number_type = DataDictType(self.dbm, name='telephone_number', slug='telephone_number', primitive_type='string') rep1 = create_entity(self.dbm, ['reporter'], 'rep1') rep1.add_data(data=[(MOBILE_NUMBER_FIELD, '1234', self.telephone_number_type), (NAME_FIELD, "Test_reporter", self.name_type)], submission=dict(submission_id="2"))
def to_entity(self, dbm): if self.form_model.is_registration_form(): location = self.cleaned_data.get(LOCATION_TYPE_FIELD_CODE) return entity.create_entity(dbm=dbm, entity_type=self.entity_type, location=location, short_code=self.short_code, geometry=convert_to_geometry( self.cleaned_data.get(GEO_CODE))) return entity.get_by_short_code(dbm, self.short_code, self.entity_type)
def _create_couchdb_datasender(manager, organization, current_user_name, mobile_number): total_entity = get_entity_count_for_type(manager, [REPORTER]) reporter_short_code = 'rep' + str(total_entity + 1) entity = create_entity(dbm=manager, entity_type=REPORTER_ENTITY_TYPE, short_code=reporter_short_code, location=[organization.country_name()]) mobile_number_type = get_or_create_data_dict(manager, name='Mobile Number Type', slug='mobile_number', primitive_type='string') name_type = get_or_create_data_dict(manager, name='Name', slug='name', primitive_type='string') data = [(MOBILE_NUMBER_FIELD, mobile_number, mobile_number_type), (NAME_FIELD, current_user_name, name_type)] entity.add_data(data=data) return reporter_short_code
def _create_couchdb_datasender(manager, organization, current_user_name, mobile_number): total_entity = get_entity_count_for_type(manager, [REPORTER]) reporter_short_code = 'rep' + str(total_entity + 1) entity = create_entity(dbm=manager, entity_type=REPORTER_ENTITY_TYPE, short_code=reporter_short_code, location=[organization.country_name()]) data = [(MOBILE_NUMBER_FIELD, mobile_number), (NAME_FIELD, current_user_name)] entity.add_data(data=data) return reporter_short_code
def _create_entities(self): self.entity_type = ['clinic'] define_type(self.manager, self.entity_type) create_registration_form(self.manager, self.entity_type) self.entity_type = ['waterpoint'] define_type(self.manager, self.entity_type) create_registration_form(self.manager, self.entity_type) define_type(self.manager, ['reporter']) self.name_type = DataDictType(self.manager, name='Name', slug='name', primitive_type='string') self.telephone_number_type = DataDictType(self.manager, name='telephone_number', slug='telephone_number', primitive_type='string') rep1 = create_entity(self.manager, ['reporter'], 'rep1') rep1.add_data(data=[(MOBILE_NUMBER_FIELD, '1234', self.telephone_number_type), (NAME_FIELD, "Test_reporter", self.name_type)], submission=dict(submission_id="2"))
def create_default_reporter(manager): try: entity = get_by_short_code(manager, REPORTER_SHORT_CODE, REPORTER_ENTITY_TYPE) entity.delete() except DataObjectNotFound: pass entity = create_entity(dbm=manager, entity_type=REPORTER_ENTITY_TYPE, short_code=REPORTER_SHORT_CODE, location=DEFAULT_LOCATION) mobile_number_type = get_or_create_data_dict(manager, name='Mobile Number Type', slug='mobile_number', primitive_type='string') name_type = get_or_create_data_dict(manager, name='Name', slug='name', primitive_type='string') data = [(MOBILE_NUMBER_FIELD, TEST_REPORTER_MOBILE_NUMBER,mobile_number_type),(NAME_FIELD,'TEST',name_type)] entity.add_data(data=data)
def create_default_reporter(manager): try: entity = get_by_short_code_include_voided(manager, REPORTER_SHORT_CODE, REPORTER_ENTITY_TYPE) entity.delete() except DataObjectNotFound: pass entity = create_entity(dbm=manager, entity_type=REPORTER_ENTITY_TYPE, short_code=REPORTER_SHORT_CODE, location=DEFAULT_LOCATION) data = [(MOBILE_NUMBER_FIELD, TEST_REPORTER_MOBILE_NUMBER), (NAME_FIELD, 'TEST')] entity.add_data(data=data)
def test_should_return_error_dict_if_mobile_number_already_exists_for_a_different_reporter( self): reporter1 = create_entity(self.manager, [REPORTER], ut_reporter_id()) reporter1.add_data(data=[("mobile_number", "123"), ("name", "aname")], event_time=datetime.datetime(2010, 02, 01, tzinfo=UTC), submission=dict(submission_id='1', form_code='reg')) values = dict(t='reporter', m='123', s='rep_test2', l='test_location', g='1 1') cleaned_data, errors = self.reg_form.validate_submission(values) self.assertIn('m', errors)
def register(self, manager, entity_type, data, location, source, aggregation_paths=None, short_code=None): # manager = get_db_manager() e = create_entity(manager, entity_type=entity_type, location=location, aggregation_paths=aggregation_paths, short_code=short_code) e.add_data(data=data) return e
def test_should_return_error_if_mobile_number_comes_in_epsilon_format_from_excel_file( self): reporter1 = create_entity(self.manager, [REPORTER], ut_reporter_id()) reporter1.add_data(data=[("mobile_number", "266123321435")], event_time=datetime.datetime(2010, 02, 01, tzinfo=UTC), submission=dict(submission_id='1', form_code='reg')) values = dict(t='reporter', m='2.66123321435e+11', s='rep_test2', l='test_location', g='1 1', n='Test Reporter') cleaned_data, errors = self.reg_form.validate_submission(values) self.assertTrue('m' in errors)
def create_default_reporter(manager): try: entity = get_by_short_code(manager, REPORTER_SHORT_CODE, REPORTER_ENTITY_TYPE) entity.delete() except DataObjectNotFound: pass entity = create_entity(dbm=manager, entity_type=REPORTER_ENTITY_TYPE, short_code=REPORTER_SHORT_CODE, location=DEFAULT_LOCATION) mobile_number_type = get_or_create_data_dict(manager, name='Mobile Number Type', slug='mobile_number', primitive_type='string') name_type = get_or_create_data_dict(manager, name='Name', slug='name', primitive_type='string') data = [(MOBILE_NUMBER_FIELD, TEST_REPORTER_MOBILE_NUMBER, mobile_number_type), (NAME_FIELD, 'TEST', name_type)] entity.add_data(data=data)
def test_get_entity_count_for_type_as_string(self): entity_type = "Clinic" define_type(self.manager, [entity_type]) create_entity(self.manager, [entity_type], "1").save() self.assertEqual(1, get_entity_count_for_type(self.manager, entity_type))
def test_get_entity_count_for_type(self): entity_type = ["Clinic"] define_type(self.manager, entity_type) create_entity(self.manager, entity_type, "1").save() self.assertEqual(1, get_entity_count_for_type(self.manager, entity_type))
def setUp(self): MangroveTestCase.setUp(self) initializer.run(self.manager) define_type(self.manager, ["dog"]) self.entity_type = ["clinic"] define_type(self.manager, self.entity_type) self.entity = create_entity( self.manager, entity_type=self.entity_type, location=["India", "Pune"], aggregation_paths=None, short_code="cli1", ) self.data_record_id = self.entity.add_data( data=[("Name", "Ruby")], submission=dict(submission_id="1")) self.reporter = create_entity( self.manager, entity_type=["reporter"], location=["India", "Pune"], aggregation_paths=None, short_code="rep1", ) self.reporter.add_data(data=[(MOBILE_NUMBER_FIELD, '1234'), (NAME_FIELD, "Test_reporter")], submission=dict(submission_id="2")) #Web submission Form Model question1 = UniqueIdField(unique_id_type='clinic', name="entity_question", code="EID", label="What is associated entity") question2 = TextField(name="Name", code="NAME", label="Clinic Name", defaultValue="some default value", constraints=[TextLengthConstraint(4, 15)], required=False) question3 = IntegerField( name="Arv stock", code="ARV", label="ARV Stock", constraints=[NumericRangeConstraint(min=15, max=120)], required=False) question4 = SelectField(name="Color", code="COL", label="Color", options=[("RED", 1), ("YELLOW", 2)], required=False) self.form_model = FormModel( self.manager, name="aids", label="Aids form_model", form_code="clinic", fields=[question1, question2, question3, question4]) self.form_model.save() #Activity Report Form Model question1 = UniqueIdField(unique_id_type='reporter', name="entity_question", code="EID", label="What is associated entity") question2 = TextField(name="Name", code="NAME", label="Clinic Name", defaultValue="some default value", constraints=[TextLengthConstraint(4, 15)]) question3 = IntegerField( name="Arv stock", code="ARV", label="ARV Stock", constraints=[NumericRangeConstraint(min=15, max=120)]) activity_report = FormModel(self.manager, name="report", label="reporting form_model", form_code="acp", fields=[question1, question2, question3]) activity_report.save() self.web_player = WebPlayerV2(self.manager)
def setUpClass(cls): cls.dbm = create_db(uniq('mangrove-test')) initializer.initial_data_setup(cls.dbm) cls.entity_type = ["healthfacility", "clinic"] safe_define_type(cls.dbm, cls.entity_type) cls.entity_short_code = "cli" + str(int(random.random() * 10000)) cls.entity = create_entity( cls.dbm, entity_type=cls.entity_type, location=["India", "Pune"], aggregation_paths=None, short_code=cls.entity_short_code, ) cls.entity.save() cls.reporter_id = "rep" + str(int(random.random() * 10000)) cls.reporter = create_contact(cls.dbm, location=["India", "Pune"], aggregation_paths=None, short_code=cls.reporter_id) cls.reporter.save() cls.phone_number = str(int(random.random() * 10000000)) cls.reporter.add_data(data=[(MOBILE_NUMBER_FIELD, cls.phone_number), (NAME_FIELD, "Test_reporter")], submission=dict(submission_id="2")) question1 = ShortCodeField( name="entity_question", code="EID", label="What is associated entity", constraints=[TextLengthConstraint(min=1, max=20)]) question2 = TextField(name="Name", code="NAME", label="Clinic Name", defaultValue="some default value", constraints=[TextLengthConstraint(4, 15)], required=False) question3 = IntegerField( name="Arv stock", code="ARV", label="ARV Stock", constraints=[NumericRangeConstraint(min=15, max=120)], required=False) question4 = SelectField(name="Color", code="COL", label="Color", options=[("RED", 'a'), ("YELLOW", 'a')], required=False) try: cls.form_model = get_form_model_by_code(cls.dbm, "clinic") except FormModelDoesNotExistsException: cls.form_model = EntityFormModel( cls.dbm, entity_type=cls.entity_type, name="aids", label="Aids form_model", form_code="clinic", fields=[question1, question2, question3], is_registration_model=True) cls.form_model.add_field(question4) cls.form_model.save() cls.sms_player = SMSPlayer(cls.dbm, LocationTree()) cls.sms_ordered_message_player = SMSPlayer(cls.dbm, LocationTree())
def setUp(self): self.dbm = get_db_manager(database='mangrove-test') initializer.run(self.dbm) define_type(self.dbm, ["dog"]) self.entity_type = ["healthfacility", "clinic"] define_type(self.dbm, self.entity_type) self.name_type = DataDictType(self.dbm, name='Name', slug='name', primitive_type='string') self.telephone_number_type = DataDictType(self.dbm, name='telephone_number', slug='telephone_number', primitive_type='string') self.entity_id_type = DataDictType(self.dbm, name='Entity Id Type', slug='entity_id', primitive_type='string') self.stock_type = DataDictType(self.dbm, name='Stock Type', slug='stock', primitive_type='integer') self.color_type = DataDictType(self.dbm, name='Color Type', slug='color', primitive_type='string') self.name_type.save() self.telephone_number_type.save() self.stock_type.save() self.color_type.save() self.entity = create_entity( self.dbm, entity_type=self.entity_type, location=["India", "Pune"], aggregation_paths=None, short_code="cli1", ) self.data_record_id = self.entity.add_data( data=[("Name", "Ruby", self.name_type)], submission=dict(submission_id="1")) self.reporter = create_entity( self.dbm, entity_type=["reporter"], location=["India", "Pune"], aggregation_paths=None, short_code="rep1", ) self.reporter.add_data(data=[ (MOBILE_NUMBER_FIELD, '1234', self.telephone_number_type), (NAME_FIELD, "Test_reporter", self.name_type) ], submission=dict(submission_id="2")) question1 = TextField(name="entity_question", code="EID", label="What is associated entity", language="eng", entity_question_flag=True, ddtype=self.entity_id_type) question2 = TextField(name="Name", code="NAME", label="Clinic Name", defaultValue="some default value", language="eng", length=TextConstraint(4, 15), ddtype=self.name_type) question3 = IntegerField(name="Arv stock", code="ARV", label="ARV Stock", range=NumericConstraint(min=15, max=120), ddtype=self.stock_type) question4 = SelectField(name="Color", code="COL", label="Color", options=[("RED", 1), ("YELLOW", 2)], ddtype=self.color_type) self.form_model = FormModel(self.dbm, entity_type=self.entity_type, name="aids", label="Aids form_model", form_code="clinic", type='survey', fields=[question1, question2, question3]) self.form_model.add_field(question4) self.form_model__id = self.form_model.save() self.submission_handler = SubmissionHandler(self.dbm) self.sms_player = SMSPlayer(self.dbm, self.submission_handler, LocationTree())
def create_water_point_entity(self): water_point_type = ["waterpoint"] safe_define_type(self.manager, water_point_type) create_entity(self.manager, entity_type=water_point_type, short_code=uniq("4"))
def setUp(self): MangroveTestCase.setUp(self) initializer.run(self.manager) self.entity_type = ["reporter"] self.telephone_number_type = DataDictType(self.manager, name='telephone_number', slug='telephone_number', primitive_type='string') self.entity_id_type = DataDictType(self.manager, name='Entity Id Type', slug='entity_id', primitive_type='string') self.name_type = DataDictType(self.manager, name='Name', slug='name', primitive_type='string') self.telephone_number_type.save() self.name_type.save() self.reporter = create_entity(self.manager, entity_type=self.entity_type, location=["India", "Pune"], aggregation_paths=None, short_code="rep1", ) self.reporter.add_data(data=[(MOBILE_NUMBER_FIELD, '1234', self.telephone_number_type), (NAME_FIELD, "Test_reporter", self.name_type)], submission=dict(submission_id="1")) question1 = TextField(name="entity_question", code="EID", label="What is associated entity", entity_question_flag=True, ddtype=self.entity_id_type) question2 = TextField(name="Name", code="NAME", label="Clinic Name", defaultValue="some default value", ddtype=self.name_type, required=False) self.form_model = FormModel(self.manager, entity_type=self.entity_type, name="Dengue", label="Dengue form_model", form_code="clinic", type='survey', fields=[question1,question2]) self.form_model.save() self.csv_data_for_activity_report = """ FORM_CODE,EID,NAME clinic,rep1,XYZ """ self.csv_data_about_reporter = """ FORM_CODE,t,n,l,d,m REG,"reporter",Dr. A,Pune,"Description",201 """ self.csv_data_with_same_mobile_number = """ FORM_CODE,t,n,l,d,m REG,"reporter",Dr. A,Pune,"Description",201 REG,"reporter",Dr. B,Pune,"Description",201 """ self.csv_data_with_exception = """ FORM_CODE,t,n,l,d,m REG,"reporter",Dr. A,Pune,"Description",201 REG,"reporter",Dr. B,Pune,"Description",201 REG,"reporter",Dr. C,Pune,"Description",202 """ self.csv_data_with_missing_name = """ FORM_CODE,t,n,l,d,m REG,"reporter",,Pune,"Description",201 """ self.csv_data_with_missing_type = """ FORM_CODE,t,n,l,d,m REG,,Dr. A,Pune,"Description",201 """ self.csv_data_with_incorrect_mobile_number = """ FORM_CODE,t,n,l,d,m REG,"reporter",Dr. A,Pune,"Description",2014678447676512 REG,"reporter",Dr. A,Pune,"Description",~!@#$%^&*()+|} REG,"reporter",Dr. A,Pune,"Description", """ self.csv_data_with_incorrect_GPS = """ FORM_CODE,t,n,g,d,m REG,"reporter",Dr. A,18,"Description",201 """ self.csv_data_with_out_of_range_GPS_value = """ FORM_CODE,t,n,g,d,m REG,"reporter",Dr. A,-95 48,"Description",201 REG,"reporter",Dr. A,-18 184,"Description",201 """ self.csv_data_without_form_code= """ FORM_CODE,t,n,g,d,m ,"reporter",Dr. A,-95 48,"Description",201 ABC,"reporter",Dr. A,-95 48,"Description",201 """ self.parser = CsvParser() self.file_player = FilePlayer(self.manager,self.parser, Channel.CSV, LocationBridge(DummyLocationTree(),dummy_get_location_hierarchy))