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)
def _create_datadict_types(self): self.dd_types = { 'beds': DataDictType(self.manager, name='beds', slug='beds', primitive_type='number'), 'meds': DataDictType(self.manager, name='meds', slug='meds', primitive_type='number'), 'patients': DataDictType(self.manager, name='patients', slug='patients', primitive_type='number'), 'doctors': DataDictType(self.manager, name='doctors', slug='doctors', primitive_type='number'), 'director': DataDictType(self.manager, name='director', slug='director', primitive_type='string') } for label, dd_type in self.dd_types.items(): dd_type.save()
def _create_data_dict_type(self): med_type = DataDictType(self.dbm, name='Medicines', slug='meds', primitive_type='number', description='Number of medications') doctor_type = DataDictType(self.dbm, name='Doctor', slug='doc', primitive_type='string', description='Name of doctor') facility_type = DataDictType(self.dbm, name='Facility', slug='facility', primitive_type='string', description='Name of facility') opened_type = DataDictType(self.dbm, name='Opened on', slug='opened_on', primitive_type='datetime', description='Date of opening') med_type.save() doctor_type.save() facility_type.save() opened_type.save() return doctor_type, facility_type, med_type, opened_type
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 test_should_create_questionnaire_with_entity_question(self): NAME = "eid" LABEL = "Entity ID" SLUG = "entity_id" TYPE = "string" post = {"entity_type": "Water Point", "name": "Test Project"} dbm = Mock(spec=DatabaseManager) patcher = patch("datawinners.project.helper.generate_questionnaire_code") mock = patcher.start() mock.return_value = '001' expected_data_dict = DataDictType(dbm, NAME, SLUG, TYPE, LABEL) self.create_ddtype_mock.return_value = expected_data_dict with patch("datawinners.project.helper.get_datadict_type_by_slug") as get_datadict_type_by_slug_mock: get_datadict_type_by_slug_mock.side_effect = DataObjectNotFound("", "", "") form_model = helper.create_questionnaire(post, dbm) self.create_ddtype_mock.assert_called_twice_with(dbm=dbm, name=NAME, slug=SLUG, primitive_type=TYPE, description=LABEL) self.assertEqual(expected_data_dict, form_model.fields[0].ddtype) self.assertEqual(1, len(form_model.fields)) self.assertEqual(True, form_model.fields[0].is_entity_field) self.assertEqual(["Water Point"], form_model.entity_type) self.assertFalse(form_model.is_active()) patcher.stop()
def test_should_add_constraint_text_for_numeric_field_with_min(self): type = DataDictType(Mock(DatabaseManager), name="age type") constraint = NumericConstraint(min=10) field = IntegerField(name="What's in the age?", code="nam", label="naam", ddtype=type, range=constraint) preview = helper.get_preview_for_field(field) self.assertEqual("Minimum 10", preview["constraint"]) self.assertEqual("integer", preview["type"])
def test_should_return_choices(self): type = DataDictType(Mock(DatabaseManager), name="color type") field = SelectField(name="What's the color?", code="nam", label="naam", ddtype=type, options=[("Red", "a"), ("Green", "b"), ("Blue", "c")]) preview = helper.get_preview_for_field(field) self.assertEqual("select1", preview["type"]) self.assertEqual(["Red", "Green", "Blue"], preview["constraint"])
def _build_fixtures(self): entity_type = ["clinic"] default_ddtype = DataDictType(self.manager, name='default dd type', slug='string_default', primitive_type='string') entity_field = TextField('clinic', 'ID', 'clinic label', default_ddtype, entity_question_flag=True) beds_field = IntegerField('beds', 'BEDS', 'beds label', default_ddtype) doctor_field = TextField('beds', 'DOCTOR', 'doctor label', default_ddtype) meds_field = IntegerField('meds', 'MEDS', 'meds label', default_ddtype) FormModelBuilder(self.manager, entity_type, 'clf1').add_field(entity_field)\ .add_field(beds_field)\ .add_field(doctor_field)\ .add_field(meds_field)\ .build() [ EntityBuilder(self.manager, entity_type, 'cl00%d' % i).build() for i in range(1, 6) ]
def setUp(self): self.data = [ [ "What is the reporting period for the activity?\n\n Answer must be a date in the following format: day.month.year\n\n Example: 25.12.2011" ], ["12.12.2012"], ["11.11.2012"], ["12.10.2012"], ] self.user = Mock(User) self.dbm = Mock(spec=DatabaseManager) self.default_ddtype = DataDictType(self.dbm, name='Default String Datadict Type', slug='string_default', primitive_type='string') fields = \ [TextField(name="Q1", code="EID", label="What is the reporter ID?", entity_question_flag=True, ddtype=self.default_ddtype), TextField(name="Q2", code="DATE", label="What is the reporting period for the activity?", entity_question_flag=False, ddtype=self.default_ddtype)] self.form_model = FormModel(self.dbm, "abc", "abc", entity_type=["clinic"], form_code="cli001", fields=fields, type="survey") self.project = Mock(Project)
def _create_form_model_for_project(self, project): ddtype = DataDictType(self.dbm, name='Default String Datadict Type', slug='string_default', primitive_type='string') question1 = TextField(name="entity_question", code="ID", label="What is associated entity", language="eng", entity_question_flag=True, ddtype=ddtype) question2 = TextField(name="question1_Name", code="Q1", label="What is your name", defaultValue="some default value", language="eng", length=TextConstraint(5, 10), ddtype=ddtype) self.form_model = FormModel(self.dbm, name=self.project1.name, form_code="abc", fields=[question1, question2], entity_type=["Clinic"], state=attributes.INACTIVE_STATE) qid = self.form_model.save() project.qid = qid project.save(self.dbm)
def test_should_create_basic_fields_in_preview(self): type = DataDictType(Mock(DatabaseManager), name="Name type") field = TextField(name="What's in a name?", code="nam", label="naam", ddtype=type, instruction="please write more tests") preview = helper.get_preview_for_field(field) self.assertEquals("What's in a name?", preview["description"]) self.assertEquals("nam", preview["code"]) self.assertEquals("text", preview["type"]) self.assertEquals("please write more tests", preview["instruction"])
def test_should_return_geocode_format(self): type = DataDictType(Mock(DatabaseManager), name="date type") field = GeoCodeField(name="What is the place?", code="dat", label="naam", ddtype=type) preview = helper.get_preview_for_field(field) self.assertEqual("xx.xxxx yy.yyyy", preview["constraints"])
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)
def _create_default_dd_type(self): self.entity_type = ["HealthFacility", "Clinic"] define_type(self.manager, ["HealthFacility", "Clinic"]) self.default_ddtype = DataDictType(self.manager, name='Default String Datadict Type', slug='string_default', primitive_type='string') self.default_ddtype.save()
def test_should_add_constraint_text_for_numeric_field_without_constraint( self): type = DataDictType(Mock(DatabaseManager), name="age type") field = IntegerField(name="What's in the age?", code="nam", label="naam", ddtype=type) preview = helper.get_preview_for_field(field) self.assertEqual("", preview["constraints"])
def test_should_return_date_format(self): type = DataDictType(Mock(DatabaseManager), name="date type") field = DateField(name="What is the date?", code="dat", label="naam", ddtype=type, date_format="dd/mm/yyyy") preview = helper.get_preview_for_field(field) self.assertEqual("dd/mm/yyyy", preview["constraints"])
def create_ds_mapping(dbm, form_model): es = get_elasticsearch_handle() fields = form_model.fields fields.append( TextField(name="projects", code='projects', label='projects', ddtype=DataDictType(dbm))) es.put_mapping(dbm.database_name, REPORTER_ENTITY_TYPE[0], get_fields_mapping(form_model.form_code, fields))
def test_should_add_constraint_text_for_text_field_with_max(self): type = DataDictType(Mock(DatabaseManager), name="Name type") constraints = [TextLengthConstraint(max=100)] field = TextField(name="What's in a name?", code="nam", label="naam", ddtype=type, constraints=constraints) preview = helper.get_preview_for_field(field) self.assertEqual("Upto 100 characters", preview["constraints"])
def test_should_add_constraint_text_for_numeric_field_with_max(self): type = DataDictType(Mock(DatabaseManager), name="age type") constraint = NumericRangeConstraint(max=100) field = IntegerField(name="What's in the age?", code="nam", label="naam", ddtype=type, constraints=[constraint]) preview = helper.get_preview_for_field(field) self.assertEqual("Upto 100", preview["constraints"])
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)
def test_should_save_survey_with_uploaded_entrys_report_id_for_summary_project_when_user_is_logged_in( self): submission_quota_service = MagicMock() submission_quota_service.has_exceeded_quota_and_notify_users.return_value = False user_profile = MagicMock() expected_reporter_id = "rep_1" valid_row = {"eid": expected_reporter_id} valid_rows = [valid_row] with patch( "datawinners.project.submission.submission_import.SurveyResponseService" ) as SurveyResponseService: service, project, user, form_model = Mock(), MagicMock(), Mock( ), MagicMock() SurveyResponseService.return_value = service project.is_summary_project.return_value = True form_model.form_code = "form_code" default_ddtype = DataDictType(Mock(spec=DatabaseManager), name='Default String Datadict Type', slug='string_default', primitive_type='string') form_model.fields = [ TextField(name="Q1", code="EID", label="What is the reporter ID?", entity_question_flag=True, ddtype=default_ddtype) ] form_model.entity_question.code = "eid" submission_persister = SubmissionPersister( user, None, None, form_model, project, submission_quota_service) with patch( "datawinners.project.submission.submission_import.get_web_transport_info" ) as get_web_transport_info: with patch( "datawinners.project.submission.submission_import.get_feed_dictionary" ) as get_feed_dictionary: transport_info = None additional_feed_dictionary = None get_feed_dictionary.return_value = additional_feed_dictionary get_web_transport_info.return_value = transport_info submission_persister.save_submissions( True, user_profile, valid_rows) service.save_survey.assert_called_with( "form_code", valid_row, [], transport_info, valid_row, expected_reporter_id, additional_feed_dictionary)
def setUp(self): self.manager = get_db_manager('http://localhost:5984/', 'mangrove-test') define_type(self.manager, ["reporter"]) self.phone_number_type = DataDictType(self.manager, name='Telephone Number', slug='telephone_number', primitive_type='string') self.first_name_type = DataDictType(self.manager, name='First Name', slug='first_name', primitive_type='string') #Register Reporter self.register(self.manager, entity_type=["reporter"], data=[(MOBILE_NUMBER_FIELD, "1234567890", self.phone_number_type), (NAME_FIELD, "A", self.first_name_type)], location=[], source="sms", short_code="REP1") self.register(self.manager, entity_type=["reporter"], data=[(MOBILE_NUMBER_FIELD, "8888567890", self.phone_number_type), (NAME_FIELD, "B", self.first_name_type)], location=[], source="sms", short_code="rep5") self.register(self.manager, entity_type=["reporter"], data=[(MOBILE_NUMBER_FIELD, "1234567890", self.phone_number_type), (NAME_FIELD, "B", self.first_name_type)], location=[], source="sms", short_code="REP2")
def get_report_period_field(self): reporting_period_dict_type = DataDictType( self.dbm, name="rpd", slug="reporting_period", primitive_type="date", description="activity reporting period") reporting_period_question = DateField( name=self.report_period_question_name, code=self.report_period_question_name, label=self.report_period_question_name, ddtype=reporting_period_dict_type, date_format=self.datetime_format, event_time_field_flag=True) return reporting_period_question
def create_data_dictionary_entries(self): data_dict_types = [ { "name": 'open 24/7', "slug": "facility_open_247_yn", "primitive_type": "string", "description": 'Open twenty-four hours a day, seven days a week', }, { "name": 'All weather road', "slug": "all_weather_road_yn", "primitive_type": "string", "description": 'All weather road access to the facility.', }, { "name": 'Distance to referral facility (km).', "slug": "km_to_referral_facility", "primitive_type": "decimal", "description": 'Distance to referral facility (km).', }, { "name": "Ambulance transport availabel to referral facility.", "slug": "transport_to_referral_facility_ambulance", "primitive_type": "string", "description": "Ambulance transport availabel to referral facility.", }, { "name": 'Sufficient beds', "slug": "inpatient_care_enough_beds_yn", "primitive_type": "integer", "description": "Sufficient beds for inpatient care.", }, ] self.data_dict_types = {} for kwargs in data_dict_types: slug = kwargs['slug'] data_dict_type = DataDictType(self.dbm, **kwargs) self.data_dict_types[slug] = data_dict_type self.data_dict_types[slug].save()
def setUp(self): self.dbm = Mock(spec=DatabaseManager) self.default_ddtype = DataDictType(self.dbm, name='Default String Datadict Type', slug='string_default', primitive_type='string') fields = \ [DateField(name='q3', code='q3', label='Reporting date', date_format='dd.mm.yyyy', ddtype=Mock(spec=DataDictType)), TextField(name="Q1", code="EID", label="What is the reporting date?", entity_question_flag=True, ddtype=self.default_ddtype)] self.form_model = FormModel(self.dbm, "abc", "abc", entity_type=["clinic"], form_code="cli001", fields=fields, type="survey")
def test_should_create_result_with_facet_values(self): facet_results = [{ 'es_field_name': '0dab4170697411e3985908002738abcf_q1_value', 'facets': [{ 'count': 3, 'term': 'B+' }, { 'count': 2, 'term': 'O+' }], 'total': 6 }] form_model = MagicMock(spec=FormModel) form_model.id = '0dab4170697411e3985908002738abcf' form_model._get_field_by_code.return_value = SelectField( name="What is your blood group", code="BG", label="What is your blood group?", options=[{ "text": "O+" }, { "text": "B+" }], single_select_flag=False, ddtype=DataDictType(Mock(DatabaseManager)), required=False) analysis_response = create_statistics_response(facet_results, form_model) self.assertTrue("What is your blood group?" in analysis_response) facet_result = analysis_response["What is your blood group?"] self.assertEqual(facet_result['count'], 6) self.assertEqual(facet_result['data'], [{ 'term': 'B+', 'count': 3 }, { 'term': 'O+', 'count': 2 }]) self.assertEqual(facet_result['field_type'], 'select')
def _setup_datadict_types(self): # name=slug, primitive_type data_dict_types = [ ['beds', 'number'], ['meds', 'number'], ['patients', 'number'], ['doctors', 'number'], ] self.dd_types = {} for data_dict_type in data_dict_types: name_slug = data_dict_type[0] kwargs = { 'dbm': self.manager, 'name': name_slug, 'slug': name_slug, 'primitive_type': data_dict_type[1], } self.dd_types[name_slug] = DataDictType(**kwargs) self.dd_types[name_slug].save()
def test_should_create_location_question_with_implicit_ddtype(self): CODE = "lc3" LABEL = "what is your location" SLUG = "what_is_your_location" TYPE = "geocode" post = {"title": LABEL, "code": CODE, "type": TYPE, "is_entity_question": False} dbm = Mock(spec=DatabaseManager) expected_data_dict = DataDictType(dbm, CODE, SLUG, TYPE, LABEL) self.create_ddtype_mock.return_value = expected_data_dict with patch("datawinners.project.helper.get_datadict_type_by_slug") as get_datadict_type_by_slug_mock: get_datadict_type_by_slug_mock.side_effect = DataObjectNotFound("", "", "") location_question = helper.create_question(post, dbm) self.create_ddtype_mock.assert_called_once_with(dbm=dbm, name=CODE, slug=SLUG, primitive_type=TYPE, description=LABEL) self.assertEqual(expected_data_dict, location_question.ddtype)
def test_should_create_text_question_with_implicit_ddtype(self): post = {"title": "what is your name", "code": "qc1", "description": "desc1", "type": "text", "choices": [], "is_entity_question": True, "min_length": 1, "max_length": 15} dbm = Mock(spec=DatabaseManager) self.create_ddtype_mock.return_value = DataDictType(dbm, "qc1", "what_is_your_name", "text", "what is your name") with patch("datawinners.project.helper.get_datadict_type_by_slug") as get_datadict_type_by_slug_mock: get_datadict_type_by_slug_mock.side_effect = DataObjectNotFound("", "", "") text_question = helper.create_question(post, dbm) self.create_ddtype_mock.assert_called_once_with(dbm=dbm, name="qc1", slug="what_is_your_name", primitive_type="text", description="what is your name") self.assertEqual('qc1', text_question.ddtype.name) self.assertEqual("what is your name", text_question.ddtype.description) self.assertEqual("what_is_your_name", text_question.ddtype.slug) self.assertEqual("text", text_question.ddtype.primitive_type)
def test_should_create_integer_question_with_implicit_ddtype(self): post = {"title": "What is your age", "code": "age", "type": "integer", "choices": [], "is_entity_question": False, "range_min": 0, "range_max": 100} dbm = Mock(spec=DatabaseManager) self.create_ddtype_mock.return_value = DataDictType(dbm, "age", "what_is_your_age", "integer", "what is your age") with patch("datawinners.project.helper.get_datadict_type_by_slug") as get_datadict_type_by_slug_mock: get_datadict_type_by_slug_mock.side_effect = DataObjectNotFound("", "", "") integer_question = helper.create_question(post, dbm) self.create_ddtype_mock.assert_called_once_with(dbm=dbm, name="age", slug="what_is_your_age", primitive_type="integer", description="What is your age") self.assertEqual('age', integer_question.ddtype.name) self.assertEqual("what is your age", integer_question.ddtype.description) self.assertEqual("what_is_your_age", integer_question.ddtype.slug) self.assertEqual("integer", integer_question.ddtype.primitive_type)