diff --git a/.coveragerc b/.coveragerc index 48e74e12..40aa900c 100644 --- a/.coveragerc +++ b/.coveragerc @@ -1,3 +1,5 @@ [run] -# omit third party code -omit = shipyard_airflow/plugins/rest_api_plugin.py \ No newline at end of file +# omit third party code and unit tests +omit = + shipyard_airflow/plugins/rest_api_plugin.py + shipyard_client/tests/* diff --git a/shipyard_airflow/control/af_monitoring/workflows_api.py b/shipyard_airflow/control/af_monitoring/workflows_api.py index 5fd19d21..8e80df38 100644 --- a/shipyard_airflow/control/af_monitoring/workflows_api.py +++ b/shipyard_airflow/control/af_monitoring/workflows_api.py @@ -76,7 +76,7 @@ class WorkflowIdResource(BaseResource): """ Retrieve a workflow by id, :param helper: The WorkflowHelper constructed for this invocation - :param workflow_id: a string in {dag_id}T{execution_date} format + :param workflow_id: a string in {dag_id}__{execution_date} format identifying a workflow :returns: a workflow detail dictionary including steps """ diff --git a/tests/unit/control/af_monitoring/test_workflow_api.py b/tests/unit/control/af_monitoring/test_workflow_api.py index 88e06cee..5fd49bf9 100644 --- a/tests/unit/control/af_monitoring/test_workflow_api.py +++ b/tests/unit/control/af_monitoring/test_workflow_api.py @@ -12,54 +12,89 @@ # See the License for the specific language governing permissions and # limitations under the License. from mock import patch - import pytest from shipyard_airflow.control.af_monitoring.workflow_helper import ( - WorkflowHelper -) + WorkflowHelper) from shipyard_airflow.control.af_monitoring.workflows_api import ( - WorkflowIdResource, - WorkflowResource -) - + WorkflowResource, WorkflowIdResource) from shipyard_airflow.errors import ApiError +from tests.unit.control import common -def test_get_all_workflows(): - """ - test that get_all_workflows invokes the helper properly - """ - wr = WorkflowResource() - with patch.object(WorkflowHelper, 'get_workflow_list') as mock_method: - helper = WorkflowHelper('') - wr.get_all_workflows(helper, None) - mock_method.assert_called_once_with(since_iso8601=None) +class TestWorkflowResource(): + @patch.object(WorkflowResource, 'get_all_workflows', common.str_responder) + def test_on_get(self, api_client): + """Validate the on_get method returns 200 on success""" + result = api_client.simulate_get( + "/api/v1.0/workflows", headers=common.AUTH_HEADERS) + assert result.status_code == 200 + + def test_get_all_workflows(self): + """ + test that get_all_workflows invokes the helper properly + """ + wr = WorkflowResource() + with patch.object(WorkflowHelper, 'get_workflow_list') as mock_method: + helper = WorkflowHelper('') + wr.get_all_workflows(helper, None) + mock_method.assert_called_once_with(since_iso8601=None) -def test_get_workflow_detail(): - """ - test that get_workflow_detail properly invokes the helper - """ - wir = WorkflowIdResource() - with patch.object(WorkflowHelper, 'get_workflow') as mock_method: - helper = WorkflowHelper('') - wir.get_workflow_detail(helper, - 'deploy_site__1972-04-03T10:00:00.20123') - mock_method.assert_called_once_with( - workflow_id='deploy_site__1972-04-03T10:00:00.20123' - ) +class TestWorkflowIdResource(): + @patch.object(WorkflowIdResource, 'get_workflow_detail', + common.str_responder) + def test_on_get(self, api_client): + """Validate the on_get method returns 200 on success""" + result = api_client.simulate_get( + "/api/v1.0/workflows/123456789", headers=common.AUTH_HEADERS) + assert result.status_code == 200 - with patch.object(WorkflowHelper, 'get_workflow') as mock_method: - helper = WorkflowHelper('') - with pytest.raises(ApiError): - wir.get_workflow_detail(helper, None) - with pytest.raises(ApiError): - wir.get_workflow_detail(helper, 'this is a bad id') - with pytest.raises(ApiError): - wir.get_workflow_detail(helper, 'dag_idTexecution_date') - with pytest.raises(ApiError): - wir.get_workflow_detail(helper, 'dag_id_execution_date') + def test_get_workflow_detail_success(self): + """ + test that get_workflow_detail properly invokes the helper + """ + wir = WorkflowIdResource() + with patch.object(WorkflowHelper, 'get_workflow') as mock_method: + helper = WorkflowHelper('') + wir.get_workflow_detail(helper, + 'deploy_site__1972-04-03T10:00:00.20123') + mock_method.assert_called_once_with( + workflow_id='deploy_site__1972-04-03T10:00:00.20123') - wir.get_workflow_detail(helper, 'dag_id__execution_date') - mock_method.assert_called_once_with(workflow_id='dag_id__execution_date') + def test_get_workflow_detail_invalid_format(self): + """ + Assert ApiError 'Invalid Workflow ID specified' is raised when + workflow ID is not a valid format, otherwise the error is not raised. + """ + wir = WorkflowIdResource() + with patch.object(WorkflowHelper, 'get_workflow') as mock_method: + helper = WorkflowHelper('') + with pytest.raises(ApiError) as expected_exc: + wir.get_workflow_detail(helper, None) + assert "Invalid Workflow ID specified" in str(expected_exc) + with pytest.raises(ApiError) as expected_exc: + wir.get_workflow_detail(helper, 'this is a bad id') + assert "Invalid Workflow ID specified" in str(expected_exc) + with pytest.raises(ApiError) as expected_exc: + wir.get_workflow_detail(helper, 'dag_idTexecution_date') + assert "Invalid Workflow ID specified" in str(expected_exc) + with pytest.raises(ApiError) as expected_exc: + wir.get_workflow_detail(helper, 'dag_id_execution_date') + assert "Invalid Workflow ID specified" in str(expected_exc) + wir.get_workflow_detail(helper, 'dag_id__execution_date') + mock_method.assert_called_once_with( + workflow_id='dag_id__execution_date') + + def test_get_workflow_detail_not_found(self): + """ + Assert ApiError 'Workflow not found' is raised when get_workflow + returns None. + """ + wir = WorkflowIdResource() + with patch.object(WorkflowHelper, 'get_workflow') as mock_method: + helper = WorkflowHelper('') + mock_method.return_value = None + with pytest.raises(ApiError) as expected_exc: + wir.get_workflow_detail(helper, 'dag_id__execution_date') + assert "Workflow not found" in str(expected_exc) diff --git a/tox.ini b/tox.ini index 4c454ed2..f0b9cbcf 100644 --- a/tox.ini +++ b/tox.ini @@ -50,5 +50,6 @@ commands = --cov-branch \ --cov-report term-missing:skip-covered \ --cov-config .coveragerc \ + --cov=shipyard_airflow \ --cov=shipyard_client \ - --cov=shipyard_airflow + --cov-report=html