def test_task_on_failure(self,): shp_export_task = ShpExportTask() celery_uid = str(uuid.uuid4()) # assume task is running running_task = ExportTask.objects.create( run=self.run, celery_uid=celery_uid, status='RUNNING', name=shp_export_task.name ) exc = None exc_info = None try: raise ValueError('some unexpected error') except ValueError as e: exc = e exc_info = sys.exc_info() einfo = ExceptionInfo(exc_info=exc_info) shp_export_task.on_failure(exc, task_id=celery_uid, einfo=einfo, args={}, kwargs={'run_uid': str(self.run.uid)}) task = ExportTask.objects.get(celery_uid=celery_uid) self.assertIsNotNone(task) exception = task.exceptions.all()[0] exc_info = cPickle.loads(str(exception.exception)).exc_info error_type, msg, tb = exc_info[0], exc_info[1], exc_info[2] self.assertEquals(error_type, ValueError) self.assertEquals('some unexpected error', str(msg))
def test_task_on_success(self, os_stat, shutil_copy, exists, mkdirs): exists.return_value = False # download dir doesn't exist osstat = os_stat.return_value type(osstat).st_size = PropertyMock(return_value=1234567890) shp_export_task = ShpExportTask() celery_uid = str(uuid.uuid4()) # assume task is running running_task = ExportTask.objects.create( run=self.run, celery_uid=celery_uid, status='RUNNING', name=shp_export_task.name ) shp_export_task = ShpExportTask() download_url = '/exports/' + str(self.run.uid) + '/file.shp' download_root = settings.EXPORT_DOWNLOAD_ROOT run_dir = '{0}{1}'.format(download_root, str(self.run.uid)) shp_export_task.on_success(retval={'result': download_url}, task_id=celery_uid, args={}, kwargs={'run_uid': str(self.run.uid)}) os_stat.assert_called_once_with(download_url) exists.assert_called_once_with(run_dir) mkdirs.assert_called_once_with(run_dir) task = ExportTask.objects.get(celery_uid=celery_uid) self.assertIsNotNone(task) result = task.result self.assertIsNotNone(result) self.assertEqual(task, result.task) self.assertEquals('SUCCESS', task.status) self.assertEquals('Default Shapefile Export', task.name) # pull out the result and test result = ExportTaskResult.objects.get(task__celery_uid=celery_uid) self.assertIsNotNone(result) self.assertEquals(download_url, result.download_url)
def test_task_on_failure(self, ): shp_export_task = ShpExportTask() celery_uid = str(uuid.uuid4()) # assume task is running running_task = ExportTask.objects.create(run=self.run, celery_uid=celery_uid, status='RUNNING', name=shp_export_task.name) exc = None exc_info = None try: raise ValueError('some unexpected error') except ValueError as e: exc = e exc_info = sys.exc_info() einfo = ExceptionInfo(exc_info=exc_info) shp_export_task.on_failure(exc, task_id=celery_uid, einfo=einfo, args={}, kwargs={'run_uid': str(self.run.uid)}) task = ExportTask.objects.get(celery_uid=celery_uid) self.assertIsNotNone(task) exception = task.exceptions.all()[0] exc_info = cPickle.loads(str(exception.exception)).exc_info error_type, msg, tb = exc_info[0], exc_info[1], exc_info[2] self.assertEquals(error_type, ValueError) self.assertEquals('some unexpected error', str(msg))
def test_run_shp_export_task(self, mock, mock_request): task = ShpExportTask() celery_uid = str(uuid.uuid4()) type(mock_request).id = PropertyMock(return_value=celery_uid) sqlite_to_shp = mock.return_value job_name = self.job.name.lower() sqlite_to_shp.convert.return_value = '/path/to/' + job_name + '.shp' stage_dir = settings.EXPORT_STAGING_ROOT + str(self.run.uid) saved_export_task = ExportTask.objects.create(run=self.run, status='PENDING', name=task.name) result = task.run(run_uid=str(self.run.uid), stage_dir=stage_dir, job_name=job_name) sqlite_to_shp.convert.assert_called_once() self.assertEquals('/path/to/' + job_name + '.shp', result['result']) # test tasks update_task_state method run_task = ExportTask.objects.get(celery_uid=celery_uid) self.assertIsNotNone(run_task) self.assertEquals('RUNNING', run_task.status)
def test_task_on_success(self, os_stat, shutil_copy, exists, mkdirs): exists.return_value = False # download dir doesn't exist osstat = os_stat.return_value type(osstat).st_size = PropertyMock(return_value=1234567890) shp_export_task = ShpExportTask() celery_uid = str(uuid.uuid4()) # assume task is running running_task = ExportTask.objects.create(run=self.run, celery_uid=celery_uid, status='RUNNING', name=shp_export_task.name) shp_export_task = ShpExportTask() download_url = '/downloads/' + str(self.run.uid) + '/file.shp' download_root = settings.EXPORT_DOWNLOAD_ROOT run_dir = '{0}{1}'.format(download_root, str(self.run.uid)) shp_export_task.on_success(retval={'result': download_url}, task_id=celery_uid, args={}, kwargs={'run_uid': str(self.run.uid)}) os_stat.assert_called_once_with(download_url) exists.assert_called_once_with(run_dir) mkdirs.assert_called_once_with(run_dir) task = ExportTask.objects.get(celery_uid=celery_uid) self.assertIsNotNone(task) result = task.result self.assertIsNotNone(result) self.assertEqual(task, result.task) self.assertEquals('SUCCESS', task.status) self.assertEquals('Default Shapefile Export', task.name) # pull out the result and test result = ExportTaskResult.objects.get(task__celery_uid=celery_uid) self.assertIsNotNone(result) self.assertEquals(download_url, result.download_url)