def test_dds_job_input_files_post(self, mock_requests): mock_response = Mock(status_code=200) mock_response.json.return_value = 'dds-job-input-file1' mock_requests.post.return_value = mock_response api = BespinApi(config=self.mock_config, user_agent_str=self.mock_user_agent_str) dds_input_file = api.dds_job_input_files_post( project_id='123', file_id='456', destination_path='data.txt', sequence_group=1, sequence=2, dds_user_credentials=4, stage_group_id=5, size=1000) self.assertEqual(dds_input_file, 'dds-job-input-file1') expected_json = { 'project_id': '123', 'file_id': '456', 'destination_path': 'data.txt', 'sequence_group': 1, 'sequence': 2, 'dds_user_credentials': 4, 'stage_group': 5, 'size': 1000, } mock_requests.post.assert_called_with('someurl/dds-job-input-files/', headers=self.expected_headers, json=expected_json)
def test_workflow_versions_post(self, mock_requests): mock_response = Mock(status_code=200) mock_response.json.return_value = 'worflow_version1' mock_requests.post.return_value = mock_response api = BespinApi(config=self.mock_config, user_agent_str=self.mock_user_agent_str) # def workflow_versions_post(self, workflow, version, workflow_type, description, workflow_path, url, version_info_url, fields): worflow_version = api.workflow_versions_post( workflow=1, version='v2.1.0', workflow_type='zipped', description='my desc', workflow_path='dir/workflow.cwl', url='https://example.com/v2.1.0.zip', version_info_url='https://example.com/info.md', fields=['field1']) self.assertEqual(worflow_version, 'worflow_version1') expected_post_payload = { 'workflow': 1, 'version': 'v2.1.0', 'description': 'my desc', 'url': 'https://example.com/v2.1.0.zip', 'type': 'zipped', 'workflow_path': 'dir/workflow.cwl', 'version_info_url': 'https://example.com/info.md', 'fields': ['field1'] } mock_requests.post.assert_called_with( 'someurl/admin/workflow-versions/', headers=self.expected_headers, json=expected_post_payload)
def test_workflow_configurations_post(self, mock_requests): mock_response = Mock(status_code=200) mock_response.json.return_value = 'workflowconfiguration1' mock_requests.post.return_value = mock_response api = BespinApi(config=self.mock_config, user_agent_str=self.mock_user_agent_str) workflow_configuration = api.workflow_configurations_post( tag='myconfig', workflow=1, share_group=2, default_job_strategy=3, system_job_order={}) self.assertEqual(workflow_configuration, 'workflowconfiguration1') expected_post_payload = { 'tag': 'myconfig', 'workflow': 1, 'share_group': 2, 'default_job_strategy': 3, 'system_job_order': {} } mock_requests.post.assert_called_with( 'someurl/admin/workflow-configurations/', headers=self.expected_headers, json=expected_post_payload)
def test_delete_job(self, mock_requests): mock_response = Mock(status_code=200) mock_requests.delete.return_value = mock_response api = BespinApi(config=self.mock_config, user_agent_str=self.mock_user_agent_str) api.delete_job(job_id=123) mock_requests.delete.assert_called_with('someurl/jobs/123', headers=self.expected_headers)
def test_delete_connection_error(self, mock_requests): mock_requests.exceptions.ConnectionError = ValueError mock_requests.delete.side_effect = ValueError("Some Error") api = BespinApi(config=self.mock_config, user_agent_str=self.mock_user_agent_str) with self.assertRaises(BespinException) as raised_exception: api._delete_request('test') self.assertEqual( str(raised_exception.exception).strip(), 'Failed to connect to someurl\nSome Error')
def test_workflow_configurations_list_no_filtering(self, mock_requests): mock_response = Mock(status_code=200) mock_response.json.return_value = ['workflowconfig1'] mock_requests.get.return_value = mock_response api = BespinApi(config=self.mock_config, user_agent_str=self.mock_user_agent_str) items = api.workflow_configurations_list() mock_requests.get.assert_called_with( 'someurl/workflow-configurations/', headers=self.expected_headers) self.assertEqual(items, ['workflowconfig1'])
def test_vm_strategy_get(self, mock_requests): mock_response = Mock(status_code=200) mock_response.json.return_value = 'jobstrategy1' mock_requests.get.return_value = mock_response api = BespinApi(config=self.mock_config, user_agent_str=self.mock_user_agent_str) response = api.job_strategy_get(123) self.assertEqual(response, 'jobstrategy1') mock_requests.get.assert_called_with('someurl/job-strategies/123/', headers=self.expected_headers)
def test_share_group_get_for_name(self, mock_requests): mock_response = Mock(status_code=200) mock_response.json.return_value = ['sharegroup1'] mock_requests.get.return_value = mock_response api = BespinApi(config=self.mock_config, user_agent_str=self.mock_user_agent_str) response = api.share_group_get_for_name(name='myname') self.assertEqual(response, 'sharegroup1') mock_requests.get.assert_called_with( 'someurl/share-groups/?name=myname', headers=self.expected_headers)
def test_jobs_list(self, mock_requests): mock_response = Mock(status_code=200) mock_response.json.return_value = ['job1', 'job2'] mock_requests.get.return_value = mock_response api = BespinApi(config=self.mock_config, user_agent_str=self.mock_user_agent_str) jobs = api.jobs_list() self.assertEqual(jobs, ['job1', 'job2']) mock_requests.get.assert_called_with('someurl/jobs/', headers=self.expected_headers)
def test_workflows_list_with_filter(self, mock_requests): mock_response = Mock(status_code=200) mock_response.json.return_value = ['workflow1', 'workflow2'] mock_requests.get.return_value = mock_response api = BespinApi(config=self.mock_config, user_agent_str=self.mock_user_agent_str) workflows = api.workflows_list(tag="mytag") self.assertEqual(workflows, ['workflow1', 'workflow2']) mock_requests.get.assert_called_with('someurl/workflows/?tag=mytag', headers=self.expected_headers)
def test_dds_user_credentials_list(self, mock_requests): mock_response = Mock(status_code=200) mock_response.json.return_value = ['agentcred1'] mock_requests.get.return_value = mock_response api = BespinApi(config=self.mock_config, user_agent_str=self.mock_user_agent_str) items = api.dds_user_credentials_list() self.assertEqual(items, ['agentcred1']) mock_requests.get.assert_called_with('someurl/dds-user-credentials/', headers=self.expected_headers)
def test_workflow_get(self, mock_requests): mock_response = Mock(status_code=200) mock_response.json.return_value = 'workflow1' mock_requests.get.return_value = mock_response api = BespinApi(config=self.mock_config, user_agent_str=self.mock_user_agent_str) workflow = api.workflow_get('12') self.assertEqual(workflow, 'workflow1') mock_requests.get.assert_called_with('someurl/workflows/12/', headers=self.expected_headers)
def test_workflow_version_get(self, mock_requests): mock_response = Mock(status_code=200) mock_response.json.return_value = 'workflowversion1' mock_requests.get.return_value = mock_response api = BespinApi(config=self.mock_config, user_agent_str=self.mock_user_agent_str) item = api.workflow_version_get(workflow_version=123) self.assertEqual(item, 'workflowversion1') mock_requests.get.assert_called_with('someurl/workflow-versions/123/', headers=self.expected_headers)
def test_job_strategies_list(self, mock_requests): mock_response = Mock(status_code=200) mock_response.json.return_value = ['jobstrategy1'] mock_requests.get.return_value = mock_response api = BespinApi(config=self.mock_config, user_agent_str=self.mock_user_agent_str) response = api.job_strategies_list(name='somename') self.assertEqual(response, ['jobstrategy1']) mock_requests.get.assert_called_with( 'someurl/job-strategies/?name=somename', headers=self.expected_headers)
def test_job_templates_create_job(self, mock_requests): mock_response = Mock(status_code=200) mock_response.json.return_value = 'job_template_filled_in' mock_requests.post.return_value = mock_response api = BespinApi(config=self.mock_config, user_agent_str=self.mock_user_agent_str) result = api.job_templates_create_job(job_file_payload={'a': '1'}) self.assertEqual(result, 'job_template_filled_in') mock_requests.post.assert_called_with( 'someurl/job-templates/create-job/', headers=self.expected_headers, json={'a': '1'})
def test_job_templates_init(self, mock_requests): mock_response = Mock(status_code=200) mock_response.json.return_value = 'job_template1' mock_requests.post.return_value = mock_response api = BespinApi(config=self.mock_config, user_agent_str=self.mock_user_agent_str) result = api.job_templates_init(tag="exome/v1/human") self.assertEqual(result, 'job_template1') expected_post_payload = {'tag': 'exome/v1/human'} mock_requests.post.assert_called_with('someurl/job-templates/init/', headers=self.expected_headers, json=expected_post_payload)
def test_restart_job(self, mock_requests): mock_response = Mock(status_code=200) mock_response.json.return_value = 'job1' mock_requests.post.return_value = mock_response api = BespinApi(config=self.mock_config, user_agent_str=self.mock_user_agent_str) item = api.restart_job(job_id=123) self.assertEqual(item, 'job1') mock_requests.post.assert_called_with('someurl/jobs/123/restart/', headers=self.expected_headers, json={})
def test_stage_group_post(self, mock_requests): mock_response = Mock(status_code=200) mock_response.json.return_value = 'stagegroup1' mock_requests.post.return_value = mock_response api = BespinApi(config=self.mock_config, user_agent_str=self.mock_user_agent_str) stage_group = api.stage_group_post() self.assertEqual(stage_group, 'stagegroup1') mock_requests.post.assert_called_with('someurl/job-file-stage-groups/', headers=self.expected_headers, json={})
def test_workflow_version_find_by_tag_version(self, mock_requests): mock_response = Mock(status_code=200) mock_response.json.return_value = [ 'filtered', ] mock_requests.get.return_value = mock_response api = BespinApi(config=self.mock_config, user_agent_str=self.mock_user_agent_str) item = api.workflow_version_find_by_tag_version('exomeseq', 'v3') self.assertEqual(item, 'filtered') mock_requests.get.assert_called_with( 'someurl/workflow-versions/?workflow__tag=exomeseq&version=v3', headers=self.expected_headers)
def test_workflow_versions_list_with_filter(self, mock_requests): mock_response = Mock(status_code=200) mock_response.json.return_value = [ 'workflowversion1', 'workflowversion2' ] mock_requests.get.return_value = mock_response api = BespinApi(config=self.mock_config, user_agent_str=self.mock_user_agent_str) items = api.workflow_versions_list(workflow_tag='exomeseq') self.assertEqual(items, ['workflowversion1', 'workflowversion2']) mock_requests.get.assert_called_with( 'someurl/workflow-versions/?workflow__tag=exomeseq', headers=self.expected_headers)
def test_workflow_version_find_by_tag_version_raises_empty( self, mock_requests): mock_response = Mock(status_code=200) mock_response.json.return_value = [] mock_requests.get.return_value = mock_response api = BespinApi(config=self.mock_config, user_agent_str=self.mock_user_agent_str) with self.assertRaises(WorkflowNotFound) as context: api.workflow_version_find_by_tag_version('exomeseq', 'v3') self.assertIn('No workflow version found matching exomeseq/v3', str(context.exception)) mock_requests.get.assert_called_with( 'someurl/workflow-versions/?workflow__tag=exomeseq&version=v3', headers=self.expected_headers)
def test_workflow_post(self, mock_requests): mock_response = Mock(status_code=200) mock_response.json.return_value = 'workflow1' mock_requests.post.return_value = mock_response api = BespinApi(config=self.mock_config, user_agent_str=self.mock_user_agent_str) workflow = api.workflow_post(name="myname", tag="mytag") self.assertEqual(workflow, 'workflow1') mock_requests.post.assert_called_with('someurl/admin/workflows/', headers=self.expected_headers, json={ 'name': 'myname', 'tag': 'mytag' })
def test_workflow_version_tool_details_post(self, mock_requests): mock_response = Mock(status_code=201) mock_response.json.return_value = 'details1' mock_requests.post.return_value = mock_response workflow_version_id = '3' contents = [{'docker_image': 'ubuntu:latest'}] api = BespinApi(config=self.mock_config, user_agent_str=self.mock_user_agent_str) tool_details = api.workflow_version_tool_details_post( workflow_version_id, contents) self.assertEqual(tool_details, 'details1') mock_requests.post.assert_called_with( 'someurl/admin/workflow-version-tool-details/', headers=self.expected_headers, json={ 'workflow_version': '3', 'details': contents })
def _create_api(self): config = ConfigFile().read_or_create_config() return BespinApi(config, user_agent_str=self.user_agent_str)
def test_check_response_raising_exceptions(self): api = BespinApi(config=self.mock_config, user_agent_str=self.mock_user_agent_str) mock_response = Mock() mock_response.json.return_value = {'field1': ['bad']} mock_response.raise_for_status.side_effect = requests.HTTPError() mock_response.status_code = 400 with self.assertRaises(BespinClientErrorException): api._check_response(mock_response) mock_response.status_code = 401 with self.assertRaises(BespinClientErrorException): api._check_response(mock_response) mock_response.status_code = 404 with self.assertRaises(NotFoundException): api._check_response(mock_response) mock_response = Mock() mock_response.json.return_value = {} mock_response.raise_for_status.return_value = None mock_response.status_code = 200 api._check_response(mock_response) mock_response.status_code = 201 api._check_response(mock_response)