def test_submittedjob_get_thumbnail(self, mock_prev): """Test get_thumbnail""" resp = mock.create_autospec(Response) resp.success = False resp.result = RestCallException(None, "test", None) mock_prev.return_value = resp api = mock.create_autospec(BatchAppsApi) job = SubmittedJob(api, "abc", None, None) with self.assertRaises(FileDownloadException): job.get_thumbnail() self.assertFalse(mock_prev.called) job = SubmittedJob(api, "abc", None, None, previewLink={'href': 'http://'}) with self.assertRaises(RestCallException): job.get_thumbnail() self.assertTrue(mock_prev.called) resp.success = True thumb = job.get_thumbnail(filename="thumb.png") mock_prev.assert_called_with(tempfile.gettempdir(), "thumb.png", True) thumb = job.get_thumbnail(download_dir="dir", filename="thumb.png", overwrite=False) mock_prev.assert_called_with("dir", "thumb.png", False) self.assertEqual(thumb, "dir\\thumb.png")
def test_submittedjob_get_final_preview(self): """Test _get_final_preview""" _callback = mock.Mock() resp = mock.create_autospec(Response) api = mock.create_autospec(BatchAppsApi) api.get_output.return_value = resp job = SubmittedJob(api, None, None, None) output = job._get_final_preview("dir", "name", True) api.get_output.assert_called_with("dir", 0, "name", True, url=None, callback=None, block=4096) self.assertEqual(output, resp) job = SubmittedJob(api, None, None, None, previewLink={'href':'http://thumb'}) output = job._get_final_preview("dir", "name", False, callback=_callback, block=111) api.get_output.assert_called_with("dir", 0, "name", False, url='http://thumb', callback=_callback, block=111) self.assertEqual(output, resp)
def test_submittedjob_get_thumbnail(self, mock_prev): """Test get_thumbnail""" resp = mock.create_autospec(Response) resp.success = False resp.result = RestCallException(None, "test", None) mock_prev.return_value = resp api = mock.create_autospec(BatchAppsApi) job = SubmittedJob(api, "abc", None, None) with self.assertRaises(FileDownloadException): job.get_thumbnail() self.assertFalse(mock_prev.called) job = SubmittedJob(api, "abc", None, None, previewLink={'href':'http://'}) with self.assertRaises(RestCallException): job.get_thumbnail() self.assertTrue(mock_prev.called) resp.success = True thumb = job.get_thumbnail(filename="thumb.png") mock_prev.assert_called_with(tempfile.gettempdir(), "thumb.png", True) thumb = job.get_thumbnail(download_dir="dir", filename="thumb.png", overwrite=False) mock_prev.assert_called_with("dir", "thumb.png", False) self.assertEqual(thumb, "dir\\thumb.png")
def test_submittedjob_list_all_outputs(self): """Test list_all_outputs""" resp = mock.create_autospec(Response) resp.success = False resp.result = RestCallException(None, "test", None) api = mock.create_autospec(BatchAppsApi) api.list_output_files.return_value = resp job = SubmittedJob(api, "abc", None, None) with self.assertRaises(RestCallException): job.list_all_outputs() resp.success = True outputs = job.list_all_outputs() api.list_output_files.assert_called_with("abc") self.assertEqual(outputs, resp.result)
def test_submittedjob_get_output(self, mock_final, mock_int): """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_final.return_value = resp mock_int.return_value = resp job = SubmittedJob(api, "abc", None, None) with self.assertRaises(FileDownloadException): job.get_output("dir") job = SubmittedJob(api, "abc", None, None, outputLink={'href':'http://'}, outputFileName="filename") with self.assertRaises(RestCallException): output = job.get_output("dir") mock_final.assert_called_with("dir", False, callback=None, block=4096) self.assertFalse(mock_int.called) resp.success = True output = job.get_output("dir") self.assertEqual(output, "dir\\filename") mock_final.called = False output = job.get_output("dir", output={'name':'test'}, overwrite=True, callback=_callback, block=1) self.assertFalse(mock_final.called) mock_int.assert_called_with({'name':'test'}, "dir", True, callback=_callback, block=1) self.assertEqual(output, "dir\\test")
def test_submittedjob_get_logs(self): """Test get_logs""" resp = mock.create_autospec(Response) resp.success = False resp.result = RestCallException(None, "test", None) api = mock.create_autospec(BatchAppsApi) api.get_log.return_value = resp job = SubmittedJob(api, "abc", None, None) logs = job.get_logs() api.get_log.assert_called_with("abc", None, 100) self.assertIsNone(logs) resp.success = True logs = job.get_logs(start=10, max_lines=None) api.get_log.assert_called_with("abc", 10, None) self.assertEqual(logs, resp.result)
def test_submittedjob_get_tasks(self, mock_task): """Test get_tasks""" resp = mock.create_autospec(Response) resp.success = False resp.result = RestCallException(None, "test", None) api = mock.create_autospec(BatchAppsApi) api.list_tasks.return_value = resp job = SubmittedJob(api, "abc", None, None) with self.assertRaises(RestCallException): job.get_tasks() api.list_tasks.assert_called_once_with(job_id="abc") resp.success = True resp.result = [{"a":1, "b":2, "c":3}] job.get_tasks() api.list_tasks.assert_called_with(job_id="abc") mock_task.assert_called_once_with(api, "abc", **{"a":1, "b":2, "c":3})
def test_submittedjob_get_intermediate_output(self): """Test _get_intermediate_output""" _callback = mock.Mock() resp_a = mock.create_autospec(Response) resp_a.success = False resp_a.result = RestCallException(None, "test", None) resp_b = mock.create_autospec(Response) api = mock.create_autospec(BatchAppsApi) api.props_output_file.return_value = resp_a api.get_output_file.return_value = resp_b job = SubmittedJob(api, None, None, None) output = job._get_intermediate_output({}, "dir", True) api.props_output_file.assert_called_with(url=None) self.assertFalse(api.get_output_file.called) self.assertEqual(output, resp_a) job = SubmittedJob(api, None, None, None) output = job._get_intermediate_output({'link': 'http://output'}, "dir", True) api.props_output_file.assert_called_with(url='http://output') self.assertFalse(api.get_output_file.called) self.assertEqual(output, resp_a) resp_a.success = True resp_a.result = 42 output = job._get_intermediate_output({'link': 'http://output'}, "dir", True) api.props_output_file.assert_called_with(url='http://output') api.get_output_file.assert_called_with("dir", 42, True, url='http://output', fname=None, callback=None, block=4096) self.assertEqual(output, resp_b) output = job._get_intermediate_output({'link': 'http://output'}, "dir", True, callback=_callback, block=1) api.get_output_file.assert_called_with("dir", 42, True, url='http://output', fname=None, callback=_callback, block=1)
def test_submittedjob_create(self): """Test __getattr__""" api = mock.create_autospec(BatchAppsApi) job = SubmittedJob(api, None, None, None) self.assertIsNone(job.name) self.assertIsNone(job.percentage) self.assertEqual(job.status, 'NotStarted') job = SubmittedJob(api, "abc", "test", "type", status="InProgress", instanceCount="5", outputFileName="output", settings="some_data") self.assertEqual(job.submission['status'], "InProgress") self.assertEqual(job.submission['requested_instances'], 5) self.assertEqual(job.submission['output_filename'], "output") self.assertEqual(job.submission['xml_settings'], "some_data") self.assertEqual(job.status, "InProgress") self.assertEqual(job.requested_instances, 5) self.assertEqual(job.output_filename, "output") self.assertEqual(job.xml_settings, "some_data") with self.assertRaises(AttributeError): print(job.endpoint) with self.assertRaises(AttributeError): del job.start with self.assertRaises(ValueError): job.status = 'Complete' with self.assertRaises(ValueError): del job.status self.assertEqual(job.name, "test")
def test_submittedjob_update(self, mock_format): """Test update""" resp = mock.create_autospec(Response) resp.success = False resp.result = RestCallException(None, "test", None) api = mock.create_autospec(BatchAppsApi) api.get_job.return_value = resp job = SubmittedJob(api, "abc", None, None) mock_format.called = False with self.assertRaises(RestCallException): updated = job.update() api.get_job.assert_called_with("abc") self.assertFalse(mock_format.called) resp.success = True resp.result = {'status': 'Complete'} updated = job.update() self.assertTrue(updated) mock_format.assert_called_with({'status': 'Complete'})
def test_submittedjob_reprocess(self): """Test reprocess""" resp = mock.create_autospec(Response) resp.success = False resp.result = RestCallException(None, "test", None) api = mock.create_autospec(BatchAppsApi) api.reprocess.return_value = resp job = SubmittedJob(api, "abc", None, None) working = job.reprocess() api.reprocess.assert_called_with("abc") self.assertFalse(working) resp.result = RestCallException(TypeError, "Boom!", None) with self.assertRaises(RestCallException): job.reprocess() resp.success = True working = job.reprocess() self.assertTrue(working)
def test_submittedjob_cancel(self): """Test cancel""" resp = mock.create_autospec(Response) resp.success = False resp.result = RestCallException(None, "test", None) api = mock.create_autospec(BatchAppsApi) api.cancel.return_value = resp job = SubmittedJob(api, "abc", None, None) cancelled = job.cancel() api.cancel.assert_called_with("abc") self.assertFalse(cancelled) resp.result = RestCallException(TypeError, "Boom!", None) with self.assertRaises(RestCallException): job.cancel() resp.success = True cancelled = job.cancel() self.assertTrue(cancelled)
def test_submittedjob_update(self, mock_format): """Test update""" resp = mock.create_autospec(Response) resp.success = False resp.result = RestCallException(None, "test", None) api = mock.create_autospec(BatchAppsApi) api.get_job.return_value = resp job = SubmittedJob(api, "abc", None, None) mock_format.called = False with self.assertRaises(RestCallException): updated = job.update() api.get_job.assert_called_with("abc") self.assertFalse(mock_format.called) resp.success = True resp.result = {'status':'Complete'} updated = job.update() self.assertTrue(updated) mock_format.assert_called_with({'status':'Complete'})
def test_submittedjob_get_output(self, mock_final, mock_int): """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_final.return_value = resp mock_int.return_value = resp job = SubmittedJob(api, "abc", None, None) with self.assertRaises(FileDownloadException): job.get_output("dir") job = SubmittedJob(api, "abc", None, None, outputLink={'href': 'http://'}, outputFileName="filename") with self.assertRaises(RestCallException): output = job.get_output("dir") mock_final.assert_called_with("dir", False, callback=None, block=4096) self.assertFalse(mock_int.called) resp.success = True output = job.get_output("dir") self.assertEqual(output, "dir\\filename") mock_final.called = False output = job.get_output("dir", output={'name': 'test'}, overwrite=True, callback=_callback, block=1) self.assertFalse(mock_final.called) mock_int.assert_called_with({'name': 'test'}, "dir", True, callback=_callback, block=1) self.assertEqual(output, "dir\\test")
def test_submittedjob_get_final_output(self): """Test _get_final_output""" _callback = mock.Mock() resp_a = mock.create_autospec(Response) resp_a.success = False resp_a.result = RestCallException(None, "test", None) resp_b = mock.create_autospec(Response) resp_b.success = False resp_b.result = RestCallException(None, "test", None) api = mock.create_autospec(BatchAppsApi) api.props_output.return_value = resp_a api.get_output.return_value = resp_b job = SubmittedJob(api, None, None, None) output = job._get_final_output("", True) api.props_output.assert_called_with(url=None) self.assertFalse(api.get_output.called) job = SubmittedJob(api, None, None, None, outputLink={'href': 'http://output'}) output = job._get_final_output("", True) api.props_output.assert_called_with(url='http://output') self.assertFalse(api.get_output.called) resp_a.success = True resp_a.result = 42 output = job._get_final_output("", True) api.props_output.assert_called_with(url='http://output') api.get_output.assert_called_with("", 42, None, True, url='http://output', callback=None, block=4096) self.assertEqual(output, resp_b) output = job._get_final_output("", True, callback=_callback, block=111) api.get_output.assert_called_with("", 42, None, True, url='http://output', callback=_callback, block=111)
def test_submittedjob_get_intermediate_output(self): """Test _get_intermediate_output""" _callback = mock.Mock() resp_a = mock.create_autospec(Response) resp_a.success = False resp_a.result = RestCallException(None, "test", None) resp_b = mock.create_autospec(Response) api = mock.create_autospec(BatchAppsApi) api.props_output_file.return_value = resp_a api.get_output_file.return_value = resp_b job = SubmittedJob(api, None, None, None) output = job._get_intermediate_output({}, "dir", True) api.props_output_file.assert_called_with(url=None) self.assertFalse(api.get_output_file.called) self.assertEqual(output, resp_a) job = SubmittedJob(api, None, None, None) output = job._get_intermediate_output({'link':'http://output'}, "dir", True) api.props_output_file.assert_called_with(url='http://output') self.assertFalse(api.get_output_file.called) self.assertEqual(output, resp_a) resp_a.success = True resp_a.result = 42 output = job._get_intermediate_output({'link':'http://output'}, "dir", True) api.props_output_file.assert_called_with(url='http://output') api.get_output_file.assert_called_with("dir", 42, True, url='http://output', fname=None, callback=None, block=4096) self.assertEqual(output, resp_b) output = job._get_intermediate_output({'link':'http://output'}, "dir", True, callback=_callback, block=1) api.get_output_file.assert_called_with("dir", 42, True, url='http://output', fname=None, callback=_callback, block=1)
def test_submittedjob_get_final_output(self): """Test _get_final_output""" _callback = mock.Mock() resp_a = mock.create_autospec(Response) resp_a.success = False resp_a.result = RestCallException(None, "test", None) resp_b = mock.create_autospec(Response) resp_b.success = False resp_b.result = RestCallException(None, "test", None) api = mock.create_autospec(BatchAppsApi) api.props_output.return_value = resp_a api.get_output.return_value = resp_b job = SubmittedJob(api, None, None, None) output = job._get_final_output("", True) api.props_output.assert_called_with(url=None) self.assertFalse(api.get_output.called) job = SubmittedJob(api, None, None, None, outputLink={'href':'http://output'}) output = job._get_final_output("", True) api.props_output.assert_called_with(url='http://output') self.assertFalse(api.get_output.called) resp_a.success = True resp_a.result = 42 output = job._get_final_output("", True) api.props_output.assert_called_with(url='http://output') api.get_output.assert_called_with("", 42, None, True, url='http://output', callback=None, block=4096) self.assertEqual(output, resp_b) output = job._get_final_output("", True, callback=_callback, block=111) api.get_output.assert_called_with("", 42, None, True, url='http://output', callback=_callback, block=111)
def test_submittedjob_get_tasks(self, mock_task): """Test get_tasks""" resp = mock.create_autospec(Response) resp.success = False resp.result = RestCallException(None, "test", None) api = mock.create_autospec(BatchAppsApi) api.list_tasks.return_value = resp job = SubmittedJob(api, "abc", None, None) with self.assertRaises(RestCallException): job.get_tasks() api.list_tasks.assert_called_once_with(job_id="abc") resp.success = True resp.result = [{"a": 1, "b": 2, "c": 3}] job.get_tasks() api.list_tasks.assert_called_with(job_id="abc") mock_task.assert_called_once_with(api, "abc", **{ "a": 1, "b": 2, "c": 3 })
def test_submittedjob_get_final_preview(self): """Test _get_final_preview""" _callback = mock.Mock() resp = mock.create_autospec(Response) api = mock.create_autospec(BatchAppsApi) api.get_output.return_value = resp job = SubmittedJob(api, None, None, None) output = job._get_final_preview("dir", "name", True) api.get_output.assert_called_with("dir", 0, "name", True, url=None, callback=None, block=4096) self.assertEqual(output, resp) job = SubmittedJob(api, None, None, None, previewLink={'href': 'http://thumb'}) output = job._get_final_preview("dir", "name", False, callback=_callback, block=111) api.get_output.assert_called_with("dir", 0, "name", False, url='http://thumb', callback=_callback, block=111) self.assertEqual(output, resp)