def test_calling_tracker_with_payload_indicating_final_status_for_only_remaining_nonfinal_export_of_extraction_order_advertises_downloads( self, emissary_class_mock, *args, **mocks): self.export.conversion_service_job_id = 1 self.export.save() for other_export in self.export.extraction_order.exports.exclude(id=self.export.id): other_export.status = FAILED other_export.save() emissary_mock = emissary_class_mock() factory = APIRequestFactory() request = factory.get( reverse('job_progress:tracker', kwargs=dict(export_id=self.export.id)), data=dict(status='finished', job='http://localhost:8901/api/conversion_job/1/') ) resulting_file = tempfile.NamedTemporaryFile() resulting_file.write(b'dummy file') resulting_file.seek(0) requests_mock = mocks['requests'] requests_mock.get( 'http://localhost:8901/api/conversion_job/1/', json=dict( resulting_file_path=resulting_file.name ) ) views.tracker(request, export_id=str(self.export.id)) expected_body = '\n'.join( [ 'The extraction order #{order_id} "Neverland" has been processed.', 'Results available for download:', '- Esri File Geodatabase (http://testserver{download_url})', '', 'The following exports have failed:', '- SpatiaLite', 'Please order them anew if you need them. ' 'If there are repeated failures please inform the administrators.', '', 'View the complete order at http://testserver/exports/', ] ).format( order_id=self.export.extraction_order.id, download_url=self.export.output_file.file.url, ) assert_that( emissary_mock.mock_calls, contains_in_any_order( call.success( 'Export #{export_id} "Neverland" to Esri File Geodatabase has finished.'.format( export_id=self.export.id, ), ), call.inform_mail( subject='Extraction Order #{order_id} "Neverland": 1 Export finished successfully, 1 failed'.format( order_id=self.export.extraction_order.id, ), mail_body=expected_body, ), ) )
def test_calling_tracker_with_payload_indicating_status_finished_downloads_result(self, *args, **mocks): self.export.conversion_service_job_id = 1 self.export.save() factory = APIRequestFactory() request = factory.get( reverse('job_progress:tracker', kwargs=dict(export_id=self.export.id)), data=dict(status='finished', job='http://localhost:8901/api/conversion_job/1/') ) resulting_file = tempfile.NamedTemporaryFile() resulting_file.write(b'dummy file') resulting_file.seek(0) requests_mock = mocks['requests'] requests_mock.get( 'http://localhost:8901/api/conversion_job/1/', json=dict( resulting_file_path=resulting_file.name ) ) requests_mock.get( 'http://localhost:8901/api/conversion_job/1/conversion_result.zip', body=BytesIO('dummy file'.encode()) ) views.tracker(request, export_id=str(self.export.id)) with self.export.output_file.file as f: file_content = f.read() assert file_content.decode() == 'dummy file'
def test_calling_tracker_with_payload_indicating_started_updates_export_status(self, *args): factory = APIRequestFactory() request = factory.get( reverse('job_progress:tracker', kwargs=dict(export_id=self.export.id)), data=dict(status='started', job='http://localhost:8901/api/conversion_job/1/') ) views.tracker(request, export_id=str(self.export.id)) self.export.refresh_from_db() self.assertEqual(self.export.status, STARTED)
def test_calling_tracker_with_payload_indicating_unchanged_status_does_not_inform_user( self, emissary_class_mock, *args, **mocks): self.export.status = 'started' self.export.save() emissary_mock = emissary_class_mock() factory = APIRequestFactory() request = factory.get( reverse('job_progress:tracker', kwargs=dict(export_id=self.export.id)), data=dict(status='started', job='http://localhost:8901/api/conversion_job/1/') ) views.tracker(request, export_id=str(self.export.id)) assert emissary_mock.mock_calls == []
def test_calling_tracker_when_status_query_indicates_started_updates_extraction_order_state(self, *args, **mocks): requests_mock = mocks['requests'] requests_mock.get( 'http://localhost:8901/api/conversion_result/53880847-faa9-43eb-ae84-dd92f3803a28/', text=self.fgdb_started_and_spatialite_queued_response ) factory = APIRequestFactory() request = factory.get( reverse('job_progress:tracker', kwargs=dict(order_id=self.extraction_order.id)), data=dict(status='http://localhost:8901/api/conversion_result/53880847-faa9-43eb-ae84-dd92f3803a28/') ) views.tracker(request, order_id=str(self.extraction_order.id)) self.extraction_order.refresh_from_db() self.assertEqual(self.extraction_order.state, ExtractionOrderState.PROCESSING)
def test_calling_tracker_with_payload_indicating_queued_informs_user( self, emissary_class_mock, *args, **mocks): emissary_mock = emissary_class_mock() factory = APIRequestFactory() request = factory.get( reverse('job_progress:tracker', kwargs=dict(export_id=self.export.id)), data=dict(status='queued', job='http://localhost:8901/api/conversion_job/1/')) views.tracker(request, export_id=str(self.export.id)) assert_that( emissary_mock.mock_calls, contains_in_any_order( call.info( 'Export #{export_id} "Neverland" to Esri File Geodatabase has been queued.' .format(export_id=self.export.id), ), ))
def test_calling_tracker_when_status_query_indicates_started_informs_user(self, info_mock, *args, **mocks): requests_mock = mocks['requests'] requests_mock.get( 'http://localhost:8901/api/conversion_result/53880847-faa9-43eb-ae84-dd92f3803a28/', text=self.fgdb_started_and_spatialite_queued_response ) factory = APIRequestFactory() request = factory.get( reverse('job_progress:tracker', kwargs=dict(order_id=self.extraction_order.id)), data=dict(status='http://localhost:8901/api/conversion_result/53880847-faa9-43eb-ae84-dd92f3803a28/') ) views.tracker(request, order_id=str(self.extraction_order.id)) info_mock.assert_called_with( 'Extraction order {order_id} is now PROCESSING.'.format( order_id=self.extraction_order.id ) )
def test_calling_tracker_when_status_query_indicates_error_informs_user( self, emissary_info_mock, emissary_warn_mock, emissary_error_mock, *args, **mocks ): requests_mock = mocks['requests'] requests_mock.get( 'http://localhost:8901/api/conversion_result/53880847-faa9-43eb-ae84-dd92f3803a28/', text=json.dumps({ "rq_job_id": "53880847-faa9-43eb-ae84-dd92f3803a28", "status": "error", "progress": "error", "gis_formats": [ { "format": "fgdb", "progress": "error", "result_url": None }, { "format": "spatialite", "progress": "error", "result_url": None } ] }) ) factory = APIRequestFactory() request = factory.get( reverse('job_progress:tracker', kwargs=dict(order_id=self.extraction_order.id)), data=dict(status='http://localhost:8901/api/conversion_result/53880847-faa9-43eb-ae84-dd92f3803a28/') ) views.tracker(request, order_id=str(self.extraction_order.id)) self.extraction_order.refresh_from_db() self.assertEqual(self.extraction_order.state, ExtractionOrderState.FAILED) emissary_info_mock.assert_not_called() emissary_warn_mock.assert_not_called() emissary_error_mock.assert_called_with('The extraction order "1" has failed. Please try again later.')
def test_calling_tracker_with_payload_indicating_final_status_for_only_remaining_nonfinal_export_of_extraction_order_advertises_downloads( self, emissary_class_mock, *args, **mocks): self.export.conversion_service_job_id = 1 self.export.save() for other_export in self.export.extraction_order.exports.exclude( id=self.export.id): other_export.status = status.FAILED other_export.save() emissary_mock = emissary_class_mock() factory = APIRequestFactory() request = factory.get( reverse('job_progress:tracker', kwargs=dict(export_id=self.export.id)), data=dict(status='finished', job='http://*****:*****@hsr.ch', ]).format( order_id=self.export.extraction_order.id, download_url=self.export.output_file.file.url, ) assert_that( emissary_mock.mock_calls, contains_in_any_order( call.success( 'Export #{export_id} "Neverland" to Esri File Geodatabase has finished.' .format(export_id=self.export.id, ), ), call.inform_mail( subject=expected_subject, mail_body=expected_body, ), ))