def test_should_format_image_question(self): form_model = MagicMock(spec=FormModel) form_model.is_media_type_fields_present = True form_model.media_fields = [PhotoField("photo", "img", "img")] required_field_names = [ 'some_question', 'ds_id', 'ds_name', 'form_model_id_img' ] results = Response({ "_hits": [ Result({ '_type': "form_model_id", '_id': 'index_id', '_source': { 'ds_id': 'his_id', 'ds_name': 'his_name', 'form_model_id_q1': 'sub_last_name', 'form_model_id_img': 'img2.png', 'some_question': 'answer for it' } }) ] }) form_model.id = 'form_model_id' local_time_delta = ('+', 2, 0) submissions = SubmissionQueryResponseCreator( form_model, local_time_delta).create_response(required_field_names, results) expected = [[ 'index_id', 'answer for it', ["his_name<span class='small_grey'> his_id</span>"], '<img src=\'/download/attachment/index_id/preview_img2.png\' alt=\'\'/> <a href=\'/download/attachment/index_id/img2.png\'>img2.png</a>' ]] self.assertEqual(submissions, expected)
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_should_accept_submission_with_media_fields(self): submission_data = u''' <hindimai-055> <image type="file">image.png</image> <audio type="file">audio.mp3</audio> <video type="file">video.mp4</video> <form_code>055</form_code> </hindimai-055> ''' expected_values = { 'image': "image.png", 'audio': "audio.mp3", 'video': "video.mp4" } with patch("mangrove.transport.player.parser.get_form_model_by_code" ) as mock_get_form_model: mock_form_model = Mock() mock_form_model.fields = [ PhotoField('', 'image', ''), AudioField('', 'audio', ''), VideoField('', 'video', '') ] mock_get_form_model.return_value = mock_form_model self.assertEquals(self.parser.parse(submission_data), ('055', expected_values))
def test_get_media_fields_and_update_values(self): values = [{u'image': u'image.png'}] counter = count_generator() photo_field = PhotoField('image', 'image', 'image') 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( [photo_field], values, counter) expected_files = {"1-image.png": self.image} self.assertDictEqual(expected_files, media_files)
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_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)