def _get_cascade_questionnaire(self, cascades): fields = [] for cascade in cascades: fields.append( SelectField(cascade, cascade, "Please select", cascades[cascade], is_cascade=True)) doc = ProjectDocument() doc.xform = self._build_xform(fields=fields, field_type="cascade", cascades=cascades) questionnaire = Project.new_from_doc(DatabaseManagerStub(), doc) questionnaire.name = "q1" questionnaire.form_code = "007" questionnaire.fields.append( FieldSet(code="group_outer", name="group_outer", label="Enter the outer group details", field_set=[ FieldSet(code="repeat_outer", name="repeat_outer", label="Enter the details you wanna repeat", field_set=fields) ])) return questionnaire
def test_generate_elastic_index_for_a_unique_id_field_within_group_in_repeat( self, lookup_entity_mock): unique_id_field = UniqueIdField('clinic', 'my_unique_id', 'my_unique_id', 'My Unique ID') group_field = FieldSet('group_name', 'group_name', 'My Label', field_set=[unique_id_field], fieldset_type='group') repeat_field = FieldSet('repeat_name', 'repeat_name', 'My Label', field_set=[group_field], fieldset_type='repeat') self.form_model.fields = [repeat_field] value = { 'repeat_name': [{ 'group_name': [{ 'my_unique_id': 'cli001' }] }, { 'group_name': [{ 'my_unique_id': 'cli002' }] }] } submission = SurveyResponseDocument(values=value, status='success') lookup_entity_mock.return_value = {'q2': 'my_clinic'} search_dict = {} with patch( 'datawinners.search.submission_index.get_by_short_code_include_voided' ) as get_by_short_code_include_voided_mock: get_by_short_code_include_voided_mock.side_effect = get_by_short_code_include_voided_mock_func _update_with_form_model_fields(Mock(spec=DatabaseManager), submission, search_dict, self.form_model) self.assertDictEqual( search_dict, { '1212_repeat_name': '[{"group_name": [{"my_unique_id": "my_clinic", "my_unique_id_unique_code": "cli001"}]}, ' '{"group_name": [{"my_unique_id": "my_clinic", "my_unique_id_unique_code": "cli002"}]}]', 'is_anonymous': False, 'media': {}, 'void': False, })
def test_should_update_search_dict_with_select_field_in_group_within_repeat( self): search_dict = {} self.form_model.fields = [ FieldSet(name="repeat", code="repeat-code", label="repeat-label", fieldset_type="repeat", field_set=[self.group_question]) ] values = { 'repeat-code': [{ 'group-code': [{ 'q4': 'a b', 'single-select': 'a' }] }] } submission_doc = SurveyResponseDocument(values=values, status="success") _update_with_form_model_fields(Mock(spec=DatabaseManager), submission_doc, search_dict, self.form_model) self.assertEquals( { '1212_repeat-code': '[{"group-code": [{"single-select": "one", "q4": ["one", "two"]}]}]', 'is_anonymous': False, 'void': False }, search_dict)
def test_should_return_unique_entries_when_multiple_unique_id_fields_are_present_in_group( self): group_fields = [ UniqueIdField("chicken", "What chicken are you reporting on?", "q3", "What chicken are you reporting on?", parent_field_code='group'), UniqueIdField("goats", "What goat are you reporting on?", "q4", "What goat are you reporting on?", parent_field_code='group'), ] fields = [ TextField("Some word question", "q1", "Some word question"), UniqueIdField("goats", "What goat are you reporting on?", "q2", "What goat are you reporting on?"), FieldSet('group', 'group', 'animal group', field_set=group_fields) ] fields_array = get_filterable_fields(fields, []) print[r for r in fields_array] self.assertEqual(len(fields_array), 2) self.assertDictEqual(fields_array[0], { 'type': 'unique_id', 'code': 'q2', 'entity_type': 'goats' }) self.assertDictEqual(fields_array[1], { 'type': 'unique_id', 'code': 'group----q3', 'entity_type': 'chicken' })
def test_generate_elastic_index_for_a_unique_id_field_within_group(self): search_dict = {} unique_id_field = UniqueIdField('clinic', 'my_unique_id', 'my_unique_id', 'My Unique ID') group_field = FieldSet('group_name', 'group_name', 'My Label', field_set=[unique_id_field]) self.form_model.fields = [group_field] value = {'group_name': [{'my_unique_id': 'cli001'}]} submission = SurveyResponseDocument(values=value, status='success') with patch('datawinners.search.submission_index.lookup_entity' ) as lookup_entity: lookup_entity.return_value = {'q2': 'my_clinic'} _update_with_form_model_fields(Mock(spec=DatabaseManager), submission, search_dict, self.form_model) self.assertEqual( search_dict, { '1212_group_name-my_unique_id_details': { 'q2': 'my_clinic' }, '1212_group_name-my_unique_id': 'my_clinic', '1212_group_name-my_unique_id_unique_code': 'cli001', 'is_anonymous': False, 'media': {}, 'void': False })
def test_get_media_fields_in_a_repeat_and_update_values(self): values = [{ "group": [{ "image": "image.png", "name": "something" }, { "image": "image.png", "name": "something2" }, { "image": "image.png", "name": "something3" }] }] counter = count_generator() field1 = PhotoField('image', 'image', 'image') field2 = TextField(name='name', code='name', label='wat is ur name') field_set = FieldSet('group', 'group', 'group', field_set=[field1, field2]) with patch( "mangrove.transport.services.MediaSubmissionService.MediaSubmissionService.create_media_details_document" ) as document_created: media_files = self.media_submission_service._get_media_fields_and_update_values( [field_set], values, counter) expected_files = { "1-image.png": self.image, "2-image.png": self.image, "3-image.png": self.image } self.assertDictEqual(expected_files, media_files)
def test_generate_elastic_index_for_a_unique_id_field_within_group_in_group( self): unique_id_field = UniqueIdField('clinic', 'my_unique_id', 'my_unique_id', 'My Unique ID') group1_field = FieldSet('group1_name', 'group1_name', 'My Label', field_set=[unique_id_field], fieldset_type='group') group2_field = FieldSet('group2_name', 'group2_name', 'My Label', field_set=[group1_field], fieldset_type='group') self.form_model.fields = [group2_field] value = { 'group2_name': [{ 'group1_name': [{ 'my_unique_id': 'cli001' }] }] } submission = SurveyResponseDocument(values=value, status='success') search_dict = {} with patch('datawinners.search.submission_index.lookup_entity' ) as lookup_entity: with patch( 'datawinners.search.submission_index.get_by_short_code_include_voided' ) as get_by_short_code_include_voided_mock: lookup_entity.return_value = {'q2': 'test'} get_by_short_code_include_voided_mock.side_effect = get_by_short_code_include_voided_mock_func _update_with_form_model_fields(Mock(spec=DatabaseManager), submission, search_dict, self.form_model) self.assertDictEqual( search_dict, { '1212_group1_name-my_unique_id_unique_code': 'cli001', 'is_anonymous': False, 'media': {}, 'void': False, '1212_group1_name-my_unique_id_details': { 'q2': 'test' }, '1212_group1_name-my_unique_id': 'test' })
def setUp(self): self.field1 = TextField('text', 'q1', 'Enter Text') self.field2 = IntegerField('integer', 'q2', 'Enter a Number') self.field3 = UniqueIdField('clinic', 'unique_id_field', 'q3', 'Which clinic are you reporting on') self.field4 = UniqueIdField('school', 'unique_id_field2', 'q4', 'Which school are you reporting on') self.repeat_field = FieldSet('repeat','repeat', 'repeat label', field_set=[self.field1, self.field4]) self.form_model = MagicMock(spec=FormModel) self.form_model.id = 'form_model_id'
def _create_field_set_question(self, post_dict, code): fields = post_dict.get("fields") sub_form_fields = [self.create_question(f, f['code']) for i, f in enumerate(fields)] return FieldSet(name=self._get_name(post_dict), code=code, label=post_dict["title"], instruction=post_dict.get("instruction"), required=post_dict.get("required"), field_set=sub_form_fields, fieldset_type=post_dict.get("fieldset_type"), parent_field_code=post_dict.get('parent_field_code'))
def setUp(self): self.field1 = TextField('text', 'q1', 'Enter Text') self.field2 = IntegerField('integer', 'q2', 'Enter a Number') self.field3 = UniqueIdField('clinic', 'unique_id_field', 'q3', 'Which clinic are you reporting on') self.field4 = UniqueIdField('school', 'unique_id_field2', 'q4', 'Which school are you reporting on') self.repeat_field = FieldSet('repeat','repeat', 'repeat label', field_set=[self.field1, self.field4]) self.form_model = MagicMock(spec=Project) self.form_model.id = 'form_model_id' self.patch_get_entity_type_info = patch('datawinners.entity.import_data.get_entity_type_info') self.get_entity_type_info_mock = self.patch_get_entity_type_info.start()
def _get_questionnaire(self, group_label="Enter the outer group details", group_name="group_outer", field_label="Name please", field_name="text2", hint=None, constraint_message=None, appearance=None, default=None, required=False, xform_constraint=None, relevant=None, field_choices=None): field = SelectField(field_name, field_name, field_label, field_choices) if field_choices is not None \ else Field(code=field_name, name=field_name, label=field_label, parent_field_code=group_name, hint=hint, constraint_message=constraint_message, appearance=appearance, default=default, required=required, xform_constraint=xform_constraint, relevant=relevant) repeat = FieldSet(code="repeat_outer", name="repeat_outer", label="Enter the details you wanna repeat", field_set=[field]) xform = self._build_xform(group_label=group_label, fields=[field], field_type=field.type) if field.relevant: xform = _replace_node_name_with_xpath(field.relevant, xform) elif field.xform_constraint: xform = _replace_node_name_with_xpath(field.xform_constraint, xform) doc = ProjectDocument() doc.xform = xform questionnaire = Project.new_from_doc(DatabaseManagerStub(), doc) questionnaire.name = "q1" questionnaire.form_code = "007" questionnaire.fields.append( FieldSet(code=group_name, name=group_name, label=group_label, field_set=[repeat])) return questionnaire
def setUp(self): self.form_model = MagicMock(spec=FormModel, id="1212") self.field1 = UniqueIdField(unique_id_type='clinic', name='unique_id', code="q1", label='which clinic') self.field2 = TextField('text', "q2", "enter text") self.field3 = GeoCodeField('gps', "q3", "enter gps") self.field4 = SelectField('select', 'q4', 'select multple options', [{ 'text': 'one', 'val': 'a' }, { 'text': 'two', 'val': 'b' }], single_select_flag=False) self.field5 = DateField('date', 'q5', 'enter date', 'mm.dd.yyyy') self.field6 = SelectField('select', 'single-select', 'select one option', [{ 'text': 'one', 'val': 'a' }, { 'text': 'two', 'val': 'b' }], single_select_flag=True) self.repeat_question = FieldSet(name="repeat", code="repeat-code", label="repeat-label", fieldset_type="repeat", field_set=[self.field4, self.field6]) self.group_question = FieldSet(name="group", code="group-code", label="group-label", fieldset_type="group", field_set=[self.field4, self.field6]) self.dbm = MagicMock(spec=DatabaseManager)
def test_should_create_xform_for_datetime_field_inside_repeat(self): field_set_fields = [DateTimeField('date_time', 'date_time', 'label', parent_field_code='field_set'), TextField('text','text','label', parent_field_code='field_set')] field_set = FieldSet('field_set', 'field_set', 'field_set', field_set=field_set_fields, fieldset_type='repeat') survey_response_values = {'field_set': [{'date_time': '12.12.2212 12:12:12', 'text': 'text1'}, {'date_time': '12.12.3212 12:12:12', 'text': 'text2'}]} submissionProcessor = XFormSubmissionProcessor() expected_xml = '<project-name-01><field_set><date_time>2212-12-12T12:12:12</date_time><text>text1</text></field_set>' \ '<field_set><date_time>3212-12-12T12:12:12</date_time><text>text2</text></field_set>' \ '<form_code>form_code-01</form_code></project-name-01>' instance_node_xml = submissionProcessor.get_model_edit_str([field_set], survey_response_values, 'project-name-01', 'form_code-01') self.assertEqual(expected_xml, instance_node_xml)
def _get_questionnaire_with_field_removed( self, group_label="Enter the outer group details", group_name="group_outer", field_type=None): repeat = FieldSet(code="repeat_outer", name="repeat_outer", label="Enter the details you wanna repeat", field_set=[]) doc = ProjectDocument() doc.xform = self._build_xform(group_label=group_label, field_type=field_type) questionnaire = Project.new_from_doc(DatabaseManagerStub(), doc) questionnaire.name = "q1" questionnaire.form_code = "007" questionnaire.fields.append( FieldSet(code=group_name, name=group_name, label=group_label, field_set=[repeat])) return questionnaire
def test_should_create_image_file_names_string(self): img_field = PhotoField('img1', 'img1', 'land image') land_address = TextField('address','address', 'Address') owner_name = TextField('name','name', 'Name') owner_img_field = PhotoField('owner_img', 'owner_img', 'owner image') owners_field_set = FieldSet('owners_info', 'owners_info', 'Owners Information', field_set=[owner_name, owner_img_field], fieldset_type='repeat') pet_breed = TextField('breed', 'breed', 'Breed') pet_field_set = FieldSet('pets', 'pets', 'Pet details', field_set=[pet_breed], fieldset_type='repeat') form_fields = [land_address, img_field, owners_field_set, pet_field_set] survey_response_values = {'address': 'some address', 'img1': 'img1.jgp', 'owners_info': [{'name':'name a', 'owner_img':'owner1.jpg'}, {'name': 'name b', 'owner_img':'owner2.jpg'}], 'pets' :[{'breed':'German goat'}]} expected = 'img1.jgp,owner1.jpg,owner2.jpg' imageProcessor = XFormImageProcessor() image_file_names_string = imageProcessor.get_media_files_str(form_fields, survey_response_values) self.assertEqual(expected, image_file_names_string)
def test_should_format_repeat_with_skippable_group_question(self): field1 = TextField(name='name', code='name', label='wat is ur name') field2 = IntegerField(name='age', code='age', label='wat is ur age') field3 = SelectField(name='languages', code='lang', label='wat languages do you kno', options=[("English US", "eng"), ("French", "fre"), ("German", "ger"), ("Spanish", "spa")], single_select_flag=False) group_field = FieldSet('group', 'group', 'group', field_set=[field1, field2, field3]) multi_field = FieldSet('student_details', 'student_details', 'Enter Student details', field_set=[group_field]) entry = u'[{}]' formatted_values = _format_fieldset_values_for_representation( entry, multi_field, "id") result = '<span class="repeat_ans">"<span class="repeat_qtn_label">group</span>: ";<br><br></span>' self.assertEqual(formatted_values, result)
def test_should_format_repeat_with_unique_id_question(self): field1 = TextField(name='name', code='name', label='wat is ur name') field2 = UniqueIdField(name='unique_id', code='unique_id', label='wat is ur unique_id', unique_id_type='student') repeat_field = FieldSet('group', 'group', 'group', field_set=[field1, field2]) multi_field = FieldSet('student_details', 'student_details', 'Enter Student details', field_set=[repeat_field]) entry = u'[{"group":[{"name": "messi", "unique_id":"messi", "unique_id_unique_code": "stu1" }]},{"group": [{"name": "ronaldo", "unique_id":"ronaldo", "unique_id_unique_code": "stu2" }]}]' actual_formatted_values = _format_fieldset_values_for_representation( entry, multi_field, "id") expected_value = '<span class="repeat_ans">"<span class="repeat_qtn_label">group</span>: "<span class="repeat_qtn_label">wat is ur name</span>: messi",' \ ' "<span class="repeat_qtn_label">wat is ur unique_id</span>: messi (stu1)";";<br><br>"<span class="repeat_qtn_label">group</span>:' \ ' "<span class="repeat_qtn_label">wat is ur name</span>: ronaldo", "<span class="repeat_qtn_label">wat is ur unique_id</span>: ronaldo (stu2)";";<br><br></span>' self.assertEqual(actual_formatted_values, expected_value)
def create_test_fields_and_survey(self): #change this to reporter #entity_field = TextField('clinic', 'ID', 'clinic label', entity_question_flag=True) city_field = TextField('city', 'city', 'What is the City name?') name_field = TextField('centername', 'centername', 'Center Name?') area_field = TextField('area', 'area', 'Area?') center_field_set = FieldSet('center', 'center', 'Center Information', field_set=[name_field, area_field]) form_fields = [#entity_field, city_field, center_field_set] survey_response_values = {'city': 'Bhopal', 'center': [{'centername': 'Boot', 'area': 'New Market'}, {'centername': 'Weene', 'area': 'Bgh'}], 'eid': 'rep276'} return form_fields, survey_response_values
def _create_field_set_question(self, post_dict, code): fields = post_dict.get("fields") sub_form_fields = [self.create_question(f, f['code']) for i, f in enumerate(fields)] return FieldSet(name=self._get_name(post_dict, code), code=code, label=post_dict["title"], instruction=post_dict.get("instruction"), required=post_dict.get("required"), field_set=sub_form_fields, fieldset_type=post_dict.get("fieldset_type"), parent_field_code=post_dict.get('parent_field_code'), hint=post_dict.get('hint'), constraint_message=post_dict.get('constraint_message'), appearance=post_dict.get('appearance'), default=post_dict.get('default'), xform_constraint=post_dict.get('xform_constraint'), relevant=post_dict.get('relevant'))
def test_should_format_repeat_with_media_question(self): field1 = TextField(name='name', code='name', label='wat is ur name') field2 = PhotoField(name='img', code='img', label='wat is ur img') field3 = VideoField(name='video', code='video', label='wat is ur video') repeat_field = FieldSet('group', 'group', 'group', field_set=[field1, field2, field3]) multi_field = FieldSet('student_details', 'student_details', 'Enter Student details', field_set=[repeat_field]) entry = u'[{"group":[{"name": "messi", "img": "img.png", "video": "video.mp4" }]},{"group": [{"name": "ronaldo", "img": "img2.png", "video": "video1.mp4"}]}]' actual_formatted_values = _format_fieldset_values_for_representation( entry, multi_field, "id") expected_value = '<span class="repeat_ans">"<span class="repeat_qtn_label">group</span>: "<span class="repeat_qtn_label">wat is ur name</span>: messi", ' \ '"<span class="repeat_qtn_label">wat is ur img</span>: <img src=\'/download/attachment/id/preview_img.png\' alt=\'\'/> <a href=\'/download/attachment/id/img.png\'>img.png</a>",' \ ' "<span class="repeat_qtn_label">wat is ur video</span>: <a href=\'/download/attachment/id/video.mp4\'>video.mp4</a>";";<br><br>"<span class="repeat_qtn_label">group</span>: "' \ '<span class="repeat_qtn_label">wat is ur name</span>: ronaldo", "<span class="repeat_qtn_label">wat is ur img</span>: <img src=\'/download/attachment/id/preview_img2.png\' alt=\'\'/> ' \ ' <a href=\'/download/attachment/id/img2.png\'>img2.png</a>", "<span class="repeat_qtn_label">wat is ur video</span>: <a href=\'/download/attachment/id/video1.mp4\'>video1.mp4</a>";";<br>' \ '<br></span>' self.assertEqual(actual_formatted_values, expected_value)
def test_should_format_repeat_with_group_question(self): field1 = TextField(name='name', code='name', label='wat is ur name') field2 = IntegerField(name='age', code='age', label='wat is ur age') field3 = SelectField(name='languages', code='lang', label='wat languages do you kno', options=[("English US", "eng"), ("French", "fre"), ("German", "ger"), ("Spanish", "spa")], single_select_flag=False) group_field = FieldSet('group', 'group', 'group', field_set=[field1, field2, field3]) multi_field = FieldSet('student_details', 'student_details', 'Enter Student details', field_set=[group_field]) entry = u'[{"group":[{"name": "messi", "age": "24", "lang": []}]},{"group": [{"name": "ronaldo", "age": "28", "lang": ["English US", "French", "Spanish"]}]},{"group": [{"name": "mueller", "age": "22", "lang": ["German"]}]}]' formatted_values = _format_fieldset_values_for_representation( entry, multi_field, "id") result = '<span class="repeat_ans">"<span class="repeat_qtn_label">group</span>: "<span class="repeat_qtn_label">wat is ur name</span>: messi", "<span class="repeat_qtn_label">wat is ur age</span>: 24", "<span class="repeat_qtn_label">wat languages do you kno</span>: ";";<br><br>' \ '"<span class="repeat_qtn_label">group</span>: "<span class="repeat_qtn_label">wat is ur name</span>: ronaldo", "<span class="repeat_qtn_label">wat is ur age</span>: 28", "<span class="repeat_qtn_label">wat languages do you kno</span>: (English US, French, Spanish)";";<br><br>' \ '"<span class="repeat_qtn_label">group</span>: "<span class="repeat_qtn_label">wat is ur name</span>: mueller", "<span class="repeat_qtn_label">wat is ur age</span>: 22", "<span class="repeat_qtn_label">wat languages do you kno</span>: German";";<br><br></span>' self.assertEqual(formatted_values, result)
def test_should_get_field_from_field_set(self): field1 = TextField('text', 'text', 'text label', parent_field_code='field_set_code') field2 = TextField('text1', 'text1', 'text label1', parent_field_code='field_set_code') fieldset_field = FieldSet('field_set', 'field_set_code', 'field set label', field_set=[field1, field2]) form_model = FormModel(Mock(spec=DatabaseManager)) form_model._form_fields = [fieldset_field] self.assertEqual( field1, form_model.get_field_by_code_in_fieldset('text', 'field_set_code'))
def test_should_accept_submission_with_repeat_fields(self): submission_data = u''' <hindimai-055> <familyname>singh</familyname> <family> <name>नाम asd</name> <age>12</age> </family> <family> <name>tommy</name> <age>15</age> </family> <form_code>055</form_code> </hindimai-055> ''' expected_values = { 'familyname': 'singh', 'family': [{ 'name': u'नाम asd', 'age': '12' }, { 'name': 'tommy', 'age': '15' }] } with patch("mangrove.transport.player.parser.get_form_model_by_code" ) as mock_get_form_model: mock_form_model = Mock() mock_form_model.fields = [ TextField('', 'familyname', ''), FieldSet('', 'family', '', field_set=[ TextField('', 'name', ''), IntegerField('', 'age', '') ]) ] mock_get_form_model.return_value = mock_form_model self.assertEquals(self.parser.parse(submission_data), ('055', expected_values))
def create_test_fields_and_survey_for_all_fields_type(self): name = TextField('name', 'name' ,'What is your name?') degree = TextField('degree', 'degree' ,'Degree name') completed_on = DateField('completed_on', 'completed_on','Degree completion year', 'dd.mm.yyyy') education = FieldSet('education', 'education', 'Education', field_set=[degree,completed_on]) age = IntegerField('age', 'age' ,'What is your age?') opt_fav_col = [('red','Red'), ('blue','Blue'),('c','Green')] fav_col = SelectField('fav_color', 'fav_color', 'Which colors you like?', opt_fav_col) opt_pizza_col = [('yes', 'Yes'),('no','No')] pizza_fan = SelectField('pizza_fan', 'pizza_fan', 'Do you like pizza?', opt_pizza_col) other = TextField('other', 'other' ,'What else you like?') pizza_type = TextField('pizza_type', 'pizza_type' ,'Which pizza type you like?') location = GeoCodeField('location', 'location' ,'Your location?') form_fields = [name, education, age, fav_col, pizza_fan, other, pizza_type, location] # todo how required will be handled survey_response_values = {'name': 'Santa', 'pizza_type': None, 'age': '30', 'other': 'Samosa', 'location': '4.9158,11.9531', 'education': [{'completed_on': u'10.02.2014', 'degree': 'SantaSSC'}], 'pizza_fan': 'yes', 'fav_color': 'red blue'} return form_fields, survey_response_values