def test_task_create(self): """Test Task object""" api = mock.create_autospec(BatchAppsApi) with self.assertRaises(TypeError): task = Task(None, None) task = Task(api, None) self.assertEqual(task._job, 'None') self.assertIsNone(task.status) task = Task(api, "job_id", status='Complete', cores='8', instance='sample_0') self.assertEqual(task.id, 0) self.assertEqual(task._job, 'job_id') self.assertEqual(task.status, 'Complete') self.assertIsNone(task.deployment) task = Task(api, None, outputs=[{}, {}]) self.assertEqual(task.outputs, [{ 'name': None, 'link': None, 'type': None }, { 'name': None, 'link': None, 'type': None }])
def test_task_list_outputs(self): """Test list_task_outputs""" resp = mock.create_autospec(Response) resp.success = False resp.result = RestCallException(None, "test", None) api = mock.create_autospec(BatchAppsApi) api.list_task_outputs.return_value = resp task = Task(api, "job", id="5") with self.assertRaises(RestCallException): task.list_outputs() api.list_task_outputs.assert_called_with("job", 5) resp.success = True outputs = task.list_outputs() self.assertEqual(outputs, resp.result)
def test_task_get_thumbnail(self, mock_get): """Test get_thumbnail""" _callback = mock.Mock() resp = mock.create_autospec(Response) resp.success = False resp.result = RestCallException(None, "test", None) api = mock.create_autospec(BatchAppsApi) mock_get.return_value = resp task = Task(api, None) with self.assertRaises(FileDownloadException): task.get_thumbnail() task = Task(api, None, outputs=[{'kind':'TaskPreview'}]) with self.assertRaises(RestCallException): task.get_thumbnail() self.assertTrue(mock_get.called) resp.success = True thumb = task.get_thumbnail(download_dir="dir", filename="name", overwrite=False) mock_get.assert_called_with({'name':'name', 'type':'TaskPreview', 'link':None}, "dir", False, callback=None) self.assertEqual(thumb, "dir\\name") task = Task(api, None, outputs=[{'kind':'TaskPreview', 'name':'thumb.png'}]) thumb = task.get_thumbnail(download_dir="dir", overwrite=False, callback=_callback) mock_get.assert_called_with({'name':'thumb.png', 'type':'TaskPreview', 'link':None}, "dir", False, callback=_callback) self.assertEqual(thumb, "dir\\thumb.png") thumb = task.get_thumbnail(download_dir="dir", filename="name", overwrite=False) mock_get.assert_called_with({'name':'name', 'type':'TaskPreview', 'link':None}, "dir", False, callback=None) self.assertEqual(thumb, "dir\\name")
def test_task_cancel(self): """Test cancel_task""" resp = mock.create_autospec(Response) resp.success = False resp.result = RestCallException(None, "test", None) api = mock.create_autospec(BatchAppsApi) api.cancel_task.return_value = resp task = Task(api, "abc") cancelled = task.cancel() api.cancel_task.assert_called_with("abc", 0) self.assertFalse(cancelled) resp.result = RestCallException(TypeError, "Boom!", None) with self.assertRaises(RestCallException): task.cancel() resp.success = True cancelled = task.cancel() self.assertTrue(cancelled)
def test_task_get_output(self, mock_get): """Test get_output""" _callback = mock.Mock() resp = mock.create_autospec(Response) resp.success = False resp.result = RestCallException(None, "test", None) api = mock.create_autospec(BatchAppsApi) mock_get.return_value = resp task = Task(api, None) with self.assertRaises(RestCallException): task.get_output(None, None) resp.success = True output = task.get_output({}, "dir") mock_get.assert_called_with({}, "dir", False, callback=None, block=4096) self.assertEqual(output, "dir\\") output = task.get_output({'name':'test.txt'}, "dir", overwrite=True, callback=_callback, block=111) mock_get.assert_called_with({'name':'test.txt'}, "dir", True, callback=_callback, block=111) self.assertEqual(output, "dir\\test.txt")
def test_task_get_output(self, mock_get): """Test get_output""" _callback = mock.Mock() resp = mock.create_autospec(Response) resp.success = False resp.result = RestCallException(None, "test", None) api = mock.create_autospec(BatchAppsApi) mock_get.return_value = resp task = Task(api, None) with self.assertRaises(RestCallException): task.get_output(None, None) resp.success = True output = task.get_output({}, "dir") mock_get.assert_called_with({}, "dir", False, callback=None, block=4096) self.assertEqual(output, "dir\\") output = task.get_output({'name': 'test.txt'}, "dir", overwrite=True, callback=_callback, block=111) mock_get.assert_called_with({'name': 'test.txt'}, "dir", True, callback=_callback, block=111) self.assertEqual(output, "dir\\test.txt")
def test_task_get_files(self): """Test _get_file""" _callback = mock.Mock() resp = mock.create_autospec(Response) resp.success = False resp.result = RestCallException(None, "test", None) api = mock.create_autospec(BatchAppsApi) api.props_output_file.return_value = resp api.get_output_file.return_value = resp task = Task(api, None) with self.assertRaises(RestCallException): task._get_file({}, "dir", False) api.props_output_file.assert_called_with(url=None) resp.success = True resp.result = 42 task._get_file({}, "dir", False) api.props_output_file.assert_called_with(url=None) api.get_output_file.assert_called_with("dir", 42, False, fname=None, url=None, callback=None, block=4096) api.props_output_file.called = False task._get_file( { 'type': 'TaskPreview', 'link': 'http://', 'name': 'file.txt' }, "dir", True, callback=_callback, block=111) self.assertFalse(api.props_output_file.called) api.get_output_file.assert_called_with("dir", None, True, fname="file.txt", url="http://", callback=_callback, block=111)
def test_task_get_files(self): """Test _get_file""" _callback = mock.Mock() resp = mock.create_autospec(Response) resp.success = False resp.result = RestCallException(None, "test", None) api = mock.create_autospec(BatchAppsApi) api.props_output_file.return_value = resp api.get_output_file.return_value = resp task = Task(api, None) with self.assertRaises(RestCallException): task._get_file({}, "dir", False) api.props_output_file.assert_called_with(url=None) resp.success = True resp.result = 42 task._get_file({}, "dir", False) api.props_output_file.assert_called_with(url=None) api.get_output_file.assert_called_with("dir", 42, False, fname=None, url=None, callback=None, block=4096) api.props_output_file.called = False task._get_file({'type':'TaskPreview', 'link':'http://', 'name':'file.txt'}, "dir", True, callback=_callback, block=111) self.assertFalse(api.props_output_file.called) api.get_output_file.assert_called_with("dir", None, True, fname="file.txt", url="http://", callback=_callback, block=111)
def test_task_get_thumbnail(self, mock_get): """Test get_thumbnail""" _callback = mock.Mock() resp = mock.create_autospec(Response) resp.success = False resp.result = RestCallException(None, "test", None) api = mock.create_autospec(BatchAppsApi) mock_get.return_value = resp task = Task(api, None) with self.assertRaises(FileDownloadException): task.get_thumbnail() task = Task(api, None, outputs=[{'kind': 'TaskPreview'}]) with self.assertRaises(RestCallException): task.get_thumbnail() self.assertTrue(mock_get.called) resp.success = True thumb = task.get_thumbnail(download_dir="dir", filename="name", overwrite=False) mock_get.assert_called_with( { 'name': 'name', 'type': 'TaskPreview', 'link': None }, "dir", False, callback=None) self.assertEqual(thumb, "dir\\name") task = Task(api, None, outputs=[{ 'kind': 'TaskPreview', 'name': 'thumb.png' }]) thumb = task.get_thumbnail(download_dir="dir", overwrite=False, callback=_callback) mock_get.assert_called_with( { 'name': 'thumb.png', 'type': 'TaskPreview', 'link': None }, "dir", False, callback=_callback) self.assertEqual(thumb, "dir\\thumb.png") thumb = task.get_thumbnail(download_dir="dir", filename="name", overwrite=False) mock_get.assert_called_with( { 'name': 'name', 'type': 'TaskPreview', 'link': None }, "dir", False, callback=None) self.assertEqual(thumb, "dir\\name")