def test_should_not_throw_exception_when_unique_id_field_reordered(self): new_fields = [ UniqueIdField(unique_id_type='clinic', name="Q1", code="EID", label="What is the clinic id?"), DateField(name='q3', code='q3', label='Reporting date', date_format='dd.mm.yyyy') ] es_mock = MagicMock() latest_form_model = FormModel(self.dbm, "abc", "abc", form_code="cli001", fields=new_fields) with patch( "datawinners.search.submission_index.get_elasticsearch_handle" ) as get_elasticsearch_handle_mock: get_elasticsearch_handle_mock.return_value = es_mock SubmissionSearchStore(self.dbm, latest_form_model, self.form_model)._verify_unique_id_change() self.assertFalse(self.es.put_mapping.called)
def create_form_for_entity_type(self): string_data_type = get_or_create_data_dict(self.manager, name='Name', slug='name', primitive_type='string') school_name_field = TextField(name="name", code="q1", label="What's the name?", ddtype=string_data_type) address_field = TextField(name="address", code="q2", label="Where is the clinic?", ddtype=string_data_type) unique_id_field = TextField( name="unique_id", code="q3", label="What is the clinic's Unique ID Number?", ddtype=string_data_type, entity_question_flag=True) form_model = FormModel( self.manager, "clinic", form_code=FORM_CODE, entity_type=["clinic"], is_registration_model=True, fields=[school_name_field, address_field, unique_id_field]) form_model.save()
def create_questionnaire(post, dbm): reporting_period_dict_type = get_or_create_data_dict( dbm=dbm, name="rpd", slug="reporting_period", primitive_type="date", description="activity reporting period") entity_type = [post["entity_type"]] if is_string( post["entity_type"]) else post["entity_type"] entity_id_question = create_entity_id_question(dbm) activity_report_question = DateField( name="What is the reporting period for the activity?", code="rpd", label="Period being reported on", ddtype=reporting_period_dict_type, date_format="dd.mm.yyyy") fields = [entity_id_question] if entity_type == [REPORTER]: fields = [entity_id_question, activity_report_question] return FormModel(dbm, entity_type=entity_type, name=post["name"], fields=fields, form_code=generate_questionnaire_code(dbm), type='survey', state=attributes.INACTIVE_STATE)
def _get_form_model(self, is_registration_form=False): return FormModel(dbm=self.dbm, form_code=self.form_code, name="abc", fields=[], is_registration_model=is_registration_form, entity_type=["entity_type"])
def test_should_throw_exception_when_unique_id_type_field_code_changes( self): new_fields = [ DateField(name='q3', code='q3', label='Reporting date', date_format='dd.mm.yyyy'), UniqueIdField(unique_id_type='clinic', name="Q1", code="EID new", label="What is the clinic id?") ] latest_form_model = FormModel(self.dbm, "abc", "abc", form_code="cli001", fields=new_fields) es_mock = MagicMock() with patch( "datawinners.search.submission_index.get_elasticsearch_handle" ) as get_elasticsearch_handle_mock: with self.assertRaises(FieldTypeChangeException) as e: get_elasticsearch_handle_mock.return_value = es_mock SubmissionSearchStore( self.dbm, latest_form_model, self.form_model)._verify_unique_id_change()
def test_subject_index_dict(self): dbm = Mock(spec=DatabaseManager) entity_type = ['entity_type'] with patch('datawinners.search.index_utils.get_entity_type_fields' ) as get_entity_type: with patch('datawinners.search.index_utils.tabulate_data' ) as tabulate_data: get_entity_type.return_value = ['name1', 'name2' ], ['label1', 'label2' ], ['code1', 'code2'] mock_entity = Mock(spec=Entity) mock_entity.is_void.return_value = False tabulate_data.return_value = {'cols': ['ans1', 'ans2']} with patch.object(Entity, 'get') as get: get.return_value = mock_entity form_model = FormModel(dbm=dbm, entity_type=entity_type, is_registration_model=True) 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 test_should_update_submission_index_date_field_with_current_format( self): dd_type = Mock(spec=DataDictType) fields = [ TextField(name="entity_question", code="EID", label="What is associated entity", entity_question_flag=True, ddtype=dd_type), DateField("date", "date", "Date", "dd.mm.yyyy", dd_type) ] form_model = FormModel(dbm=Mock(spec=DatabaseManager), form_code="001", type="survey", name="form", entity_type=["clinic"], fields=fields) form_model._doc.entity_type = ["clinic"] values = {'eid': 'cid005', 'date': '12.21.2012'} submission_doc = SurveyResponseDocument(values=values, status="success", form_model_revision="rev1") search_dict = {} form_model._doc = Mock(spec=FormModelDocument) form_model._doc.rev = "rev2" form_model._snapshots = { "rev1": [DateField("date", "date", "Date", "mm.dd.yyyy", dd_type)] } with patch('datawinners.search.submission_index.lookup_entity_name' ) as lookup_entity_name: lookup_entity_name.return_value = 'Test' search_dict = _update_with_form_model_fields( Mock(spec=DatabaseManager), submission_doc, search_dict, form_model) self.assertEquals("21.12.2012", search_dict.get("date"))
def _create_summary_form_model(self): question1 = TextField( name="question1_Name", code="Q1", label="What is your name", defaultValue="some default value", constraints=[TextLengthConstraint(5, 10), RegexConstraint("\w+")], ddtype=self.default_ddtype) question2 = IntegerField( name="Father's age", code="Q2", label="What is your Father's Age", constraints=[NumericRangeConstraint(min=15, max=120)], ddtype=self.default_ddtype) question3 = SelectField(name="Color", code="Q3", label="What is your favourite color", options=[("RED", 1), ("YELLOW", 2)], ddtype=self.default_ddtype) self.summary_form_model = FormModel( self.manager, entity_type=["reporter"], name="aids", label="Aids form_model", form_code=FORM_CODE_2, type="survey", fields=[question1, question2, question3]) self.summary_form_model__id = self.summary_form_model.save()
def _create_data_submission_form(self): question1 = UniqueIdField('clinic',name="entity_question", code="ID", label="What is associated entity") question2 = TextField(name="Name", code="Q1", label="What is your name", defaultValue="some default value") return FormModel(self.dbm, name="aids", label="Aids form_model", form_code="AIDS", fields=[question1, question2])
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 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_for_entity_type(self): school_name_field = TextField(name="name", code="q1", label="What's the name?") address_field = TextField(name="address", code="q2", label="Where is the clinic?") unique_id_field = UniqueIdField('clinic',name="unique_id", code="q3", label="What is the clinic's Unique ID Number?") form_model = FormModel(self.manager, "clinic", form_code=FORM_CODE, entity_type=["clinic"], is_registration_model=True, fields=[school_name_field, address_field, unique_id_field]) form_model.save()
def _get_form_model(self, is_registration_form=False): self.dbm = Mock(spec=DatabaseManager) self.form_code = "form_code" return FormModel(dbm=self.dbm, form_code=self.form_code, name="abc", fields=[], is_registration_model=is_registration_form)
def build(self): if not entity_type_already_defined(self._manager, self._entity_type): define_type(self._manager, self._entity_type) self.form_model = FormModel(self._manager, name=self._name, label=self._label, form_code=self._form_code, fields=self._fields, is_registration_model=self._is_reg) form_model_id = self.form_model.save() return FormModel.get(self._manager, form_model_id)
def form_model_with_gps_question(self): return FormModel(self.database_manager, name="AIDS", label="Aids form_model", form_code="cli002", type='survey', fields=[self.eid_field, self.gps_field], entity_type=["clinic"])
def form_model(self): question1 = UniqueIdField(unique_id_type='clinic', name="entity_question", code="q1", label="What is associated entity", ) question2 = IntegerField(name="question1_Name", code="q2", label="What is your name", constraints=[NumericRangeConstraint(min=10, max=100)]) return FormModel(self.dbm, name="aids", label="Aids form_model", form_code="aids", fields=[question1, question2])
def test_should_get_header_information_for_submission_excel(self): fields = [{ "name": "first name", "code": 'q1', "label": 'What is your name', "type": "text" }, { "name": "age", "code": 'q2', "label": 'What is your age', "type": "integer", "constraints": [["range", { "max": "15", "min": "12" }]] }, { "name": "reporting date", "code": 'q3', "label": 'What is the reporting date', "date_format": "dd.mm.yyyy", "type": "date" }, { "name": "choices", "code": 'q5', "label": 'Your choices', "type": "select" }] form_model_fields = [ TextField("first_name", "q1", "What is your name"), IntegerField("age", "q2", "What is your age"), DateField("reporting date", "q3", "What is the reporting date", "dd.mm.yyyy") ] form_model = FormModel(Mock(spec=DatabaseManager), name="some_name", form_code="cli00_mp", fields=form_model_fields) headers = get_submission_headers(fields, form_model) headers_text = self._get_header_component(headers, 0) self.assertEqual([ "What is your name", "What is your age", "What is the reporting date", "Your choices" ], headers_text) header_instructions = self._get_header_component(headers, 1) self.assertEqual([ "\n\nAnswer must be a word", "\n\nEnter a number between 12-15.", "\n\nAnswer must be a date in the following format: day.month.year", "\n\nEnter 1 or more answers from the list." ], header_instructions) header_examples = self._get_header_component(headers, 2) self.assertEqual([ "\n\n", "\n\n", "\n\nExample: 25.12.2011", "\n\nExample: a or ab" ], header_examples)
def create_questionnaire(post, manager, entity_type, name, language): entity_type = [entity_type] if is_string(entity_type) else entity_type questionnaire_code = post['questionnaire-code'].lower() json_string = post['question-set'] question_set = json.loads(json_string) form_model = FormModel(manager, entity_type=entity_type, name=name, type='survey', state=post['project_state'], fields=[], form_code=questionnaire_code, language=language) QuestionnaireBuilder(form_model, manager).update_questionnaire_with_questions(question_set) return form_model
def setUp(self): self.dbm = Mock(spec=DatabaseManager) fields = [DateField(name='q3', code='q3', label='Reporting date', date_format='dd.mm.yyyy'), UniqueIdField(unique_id_type='clinic',name="Q1", code="EID", label="What is the clinic id?")] self.form_model = FormModel(self.dbm, "abc", "abc", form_code="cli001", fields=fields) self.dbm = MagicMock(spec=DatabaseManager) dbm_view = MagicMock() self.dbm.database_name = 'somedb' self.dbm.view = dbm_view self.es = Mock()
def test_hidden_form_code_field_created(self): entity_field = UniqueIdField('clinic', "reporting on", "rep_on", "rep") form_model = FormModel(self.dbm, 'some form', 'some', 'form_code_1', fields=[entity_field]) form = WebForm(form_model, None) self.assertEquals(len(form.fields), 1) self.assertEquals(type(form.fields['form_code'].widget), HiddenInput)
def summary_form_model_with_rp(self): return FormModel(self.database_manager, name="AIDS", label="Aids form_model", form_code="cli002", type='survey', fields=[ self.rp_field, self.eid_field, self.symptoms_field, self.blood_type_field ], entity_type=["reporter"])
def test_edit_survey_response_form_for_previously_errored_survey_response(self): question1 = UniqueIdField('patient',name="entity_question", code="q1", label="What is associated entity") question2 = TextField(name="question1_Name", code="q2", label="What is your name", defaultValue="some default value") form_model = FormModel(self.dbm, name="aids", label="Aids form_model", form_code="clinic", fields=[question1, question2]) errored_survey_response = self.errored_survey_response() form = EditSurveyResponseForm(self.dbm, errored_survey_response, form_model, {'q1': 'a1', 'q2': 'a2'}) form.save() self.assertTrue(form.is_valid)
def subject_form_model_without_rp(self): return FormModel(self.database_manager, name="AIDS", label="Aids form_model", form_code="cli002", type='survey', fields=[ self.eid_field, self.symptoms_field, self.blood_type_field ], entity_type=["clinic"])
def form_model(self, form_code="cli002"): return FormModel(self.database_manager, name="AIDS", label="Aids form_model", form_code=form_code, type='survey', fields=[ self.eid_field, self.rp_field, self.symptoms_field, self.blood_type_field ], entity_type=["clinic"])
def setUp(self): self.dbm = Mock(spec=DatabaseManager) self.datadict_patcher = patch( "mangrove.form_model.form_model.get_or_create_data_dict") self.datadict_mock = self.datadict_patcher.start() self.ddtype_mock = Mock(spec=DataDictType) self.datadict_mock.return_value = self.ddtype_mock q1 = TextField(name="entity_question", code="ID", label="What is associated entity", language="eng", entity_question_flag=True, ddtype=self.ddtype_mock) q2 = TextField(name="question1_Name", code="Q1", label="What is your name", defaultValue="some default value", language="eng", length=TextConstraint(5, 10), ddtype=self.ddtype_mock) q3 = IntegerField(name="Father's age", code="Q2", label="What is your Father's Age", range=NumericConstraint(min=15, max=120), ddtype=self.ddtype_mock) q4 = SelectField(name="Color", code="Q3", label="What is your favourite color", options=[("RED", 1), ("YELLOW", 2)], ddtype=self.ddtype_mock) q5 = TextField(name="Desc", code="Q4", label="Description", ddtype=self.ddtype_mock) self.form_model = FormModel( self.dbm, entity_type=["XYZ"], name="aids", label="Aids form_model", form_code="1", type='survey', fields=[ # expected_short_code = "dog3" # self.assertEqual(response.short_code, expected_short_code) # b = get_by_short_code(self.dbm, expected_short_code, ["dog"]) # self.assertEqual(b.short_code, expected_short_code) q1, q2, q3, q4, q5 ])
def test_should_give_back_unique_id_field(self): question1 = UniqueIdField('entity_type', name="question1_Name", code="Q1", label="What is your name", defaultValue="some default value") activity_report = FormModel(self.dbm, name="aids", label="Aids form_model", form_code="1", fields=[question1]) self.assertIsNotNone(activity_report.entity_questions)
def test_should_save_questionnaire_from_post(self): post = [{"title": "q1", "code": "qc1", "type": "text", "choices": [], "is_entity_question": True, "min_length": 1, "max_length": ""}, {"title": "q2", "code": "qc2", "type": "integer", "choices": [], "is_entity_question": False, "range_min": 0, "range_max": 100}, {"title": "q3", "code": "qc3", "type": "select", "choices": [{"value": "c1"}, {"value": "c2"}], "is_entity_question": False} ] q1 = helper.create_question(post[0], self.dbm) form_model = FormModel(self.dbm, "test", "test", "test", [q1], ["test"], "test") questionnaire = helper.update_questionnaire_with_questions(form_model, post, self.dbm) self.assertEqual(3, len(questionnaire.fields))
def test_should_raise_exception_if_form_code_already_exists_on_creation( self): question1 = UniqueIdField('clinic', name="entity_question", code="ID", label="What is associated entity") form_model = FormModel(self.manager, name="aids", label="Aids form_model", form_code="1", fields=[question1]) with self.assertRaises(DataObjectAlreadyExists): form_model.save()
def test_should_not_raise_exception_if_form_code_is_updated(self): question1 = UniqueIdField('clinic', name="entity_question", code="ID", label="What is associated entity") form_model2 = FormModel(self.manager, name="aids", label="Aids form_model", form_code="2", fields=[question1]) form_model2.save() form_model2.form_code = "2" form_model2.save()
def test_should_call_submission_mapping_if_not_registration_form_model( self): dbm = Mock(spec=DatabaseManager) form_model_document = Mock(spec=FormModelDocument) with patch("mangrove.form_model.form_model.FormModel.new_from_doc" ) as new_from_doc: with patch("datawinners.search.mapping.create_submission_mapping" ) as create_submission_mapping: form_model = FormModel(dbm, form_code='clinic') form_model._doc = form_model_document new_from_doc.return_value = form_model form_model_change_handler(form_model_document, dbm) assert create_submission_mapping.called