Skip to content

Commit

Permalink
test(r-c-test): fix tests after removing analysis_run_id (#724)
Browse files Browse the repository at this point in the history
Also avoided mocking api client in favour of the functions directly.
  • Loading branch information
ajclyall committed Aug 22, 2024
1 parent d052838 commit dceb6ba
Showing 1 changed file with 70 additions and 81 deletions.
151 changes: 70 additions & 81 deletions tests/test_cli_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@

from click.testing import CliRunner
from reana_client.cli import cli
from unittest.mock import Mock, patch
from unittest.mock import patch

from pytest_reana.test_utils import make_mock_api_client
from reana_commons.gherkin_parser.parser import AnalysisTestStatus, TestResult
from reana_commons.gherkin_parser.errors import FeatureFileError
from dataclasses import replace
Expand All @@ -22,7 +21,6 @@
error_log=None,
result=AnalysisTestStatus.passed,
failed_testcase=None,
analysis_run_id="1618",
feature="Run Duration",
checked_at="2024-01-01T00:00:00.000000",
)
Expand All @@ -32,7 +30,6 @@
error_log="Test designed to fail",
result=AnalysisTestStatus.failed,
failed_testcase="Scenario to fail",
analysis_run_id="3000",
feature="Run Duration",
checked_at="2024-01-01T00:00:00.000000",
)
Expand All @@ -59,16 +56,10 @@ def test_test_workflow_not_found():


@patch(
"reana_client.api.client.current_rs_api_client",
new_callable=lambda: make_mock_api_client("reana-server")(
{
"status": "running",
"id": "31415",
},
Mock(status_code=200),
),
"reana_client.cli.test.get_workflow_status",
return_value={"status": "running", "name": "myanalysis"},
)
def test_test_workflow_not_finished(mock_api_client):
def test_test_workflow_not_finished(mock_get_workflow_status):
"""Test test command when workflow is not finished."""
runner = CliRunner()
with runner.isolation():
Expand All @@ -92,12 +83,10 @@ def test_test_workflow_not_finished(mock_api_client):


@patch(
"reana_client.api.client.current_rs_api_client",
new_callable=lambda: make_mock_api_client("reana-server")(
{"status": "deleted", "id": "31415"}, Mock(status_code=200)
),
"reana_client.cli.test.get_workflow_status",
return_value={"status": "deleted", "name": "myanalysis"},
)
def test_test_workflow_deleted(mock_api_client):
def test_test_workflow_deleted(mock_get_workflow_status):
"""Test test command when workflow is deleted."""
runner = CliRunner()
with runner.isolation():
Expand All @@ -113,12 +102,14 @@ def test_test_workflow_deleted(mock_api_client):


@patch(
"reana_client.api.client.current_rs_api_client",
new_callable=lambda: make_mock_api_client("reana-server")(
{"status": "finished", "id": "1111"}, Mock(status_code=200)
),
"reana_client.cli.test.get_workflow_status",
return_value={"status": "finished", "name": "myanalysis"},
)
def test_test_no_test_files(mock_api_client):
@patch(
"reana_client.cli.test.get_workflow_specification",
return_value={"specification": {"inputs": {"directories": ["data"]}}},
)
def test_test_no_test_files(mock_get_workflow_status, mock_get_workflow_specification):
"""Test test command when no test files are specified."""
runner = CliRunner()
with runner.isolation():
Expand All @@ -131,12 +122,16 @@ def test_test_no_test_files(mock_api_client):


@patch(
"reana_client.api.client.current_rs_api_client",
new_callable=lambda: make_mock_api_client("reana-server")(
{"status": "finished", "id": "496"}, Mock(status_code=200)
),
"reana_client.cli.test.get_workflow_status",
return_value={"status": "finished", "name": "myanalysis"},
)
def test_test_no_test_files_with_test_file_option(mock_api_client):
@patch(
"reana_client.cli.test.get_workflow_specification",
return_value={"specification": {"inputs": {"directories": ["data"]}}},
)
def test_test_no_test_files_with_test_file_option(
mock_get_workflow_status, mock_get_workflow_specification
):
"""Test test command when no test files are specified in reana.yml and when the test file option is provided."""
runner = CliRunner()
with runner.isolation():
Expand All @@ -156,17 +151,20 @@ def test_test_no_test_files_with_test_file_option(mock_api_client):


@patch(
"reana_client.api.client.current_rs_api_client",
new_callable=lambda: make_mock_api_client("reana-server")(
{
"status": "finished",
"id": "496",
"specification": {"tests": {"files": ["test1.feature", "test2.feature"]}},
},
Mock(status_code=200),
),
"reana_client.cli.test.get_workflow_status",
return_value={"status": "finished", "name": "myanalysis"},
)
@patch(
"reana_client.cli.test.get_workflow_specification",
return_value={
"specification": {
"tests": {"files": ["test_analysis.feature", "test_analysis2.feature"]}
}
},
)
def test_test_multiple_test_files_with_test_file_option(mock_api_client):
def test_test_multiple_test_files_with_test_file_option(
mock_get_workflow_status, mock_get_workflow_specification
):
"""Test test command when multiple test files are specified in reana.yml and test file option is provided.
In this case, the test-file option should be used instead of the test files specified in reana.yml.
"""
Expand All @@ -190,15 +188,14 @@ def test_test_multiple_test_files_with_test_file_option(mock_api_client):


@patch(
"reana_client.api.client.current_rs_api_client",
new_callable=lambda: make_mock_api_client("reana-server")(
{
"status": "finished",
"id": "496",
"specification": {"tests": {"files": ["use-me.feature", "me-too.feature"]}},
},
Mock(status_code=200),
),
"reana_client.cli.test.get_workflow_status",
return_value={"status": "finished", "name": "myanalysis"},
)
@patch(
"reana_client.cli.test.get_workflow_specification",
return_value={
"specification": {"tests": {"files": ["use-me.feature", "me-too.feature"]}}
},
)
@patch(
"reana_client.cli.test.parse_and_run_tests",
Expand All @@ -207,7 +204,9 @@ def test_test_multiple_test_files_with_test_file_option(mock_api_client):
[passed_test],
),
)
def test_test_files_from_spec(mock_api_client, mock_parse_and_run_tests):
def test_test_files_from_spec(
mock_get_workflow_status, get_workflow_specification, mock_parse_and_run_tests
):
"""Test test command when test files are specified in reana.yml."""
env = {"REANA_SERVER_URL": "localhost"}
runner = CliRunner(env=env)
Expand All @@ -221,16 +220,14 @@ def test_test_files_from_spec(mock_api_client, mock_parse_and_run_tests):


@patch(
"reana_client.api.client.current_rs_api_client",
new_callable=lambda: make_mock_api_client("reana-server")(
{"status": "finished", "id": "28"}, Mock(status_code=200)
),
"reana_client.cli.test.get_workflow_status",
return_value={"status": "finished", "name": "myanalysis"},
)
@patch(
"reana_client.cli.test.parse_and_run_tests",
side_effect=FeatureFileError,
)
def test_test_parser_error(mock_api_client, mock_parse_and_run_tests):
def test_test_parser_error(mock_get_workflow_status, mock_parse_and_run_tests):
"""Test test command when parser error occurs."""
runner = CliRunner()
with runner.isolation():
Expand All @@ -251,17 +248,17 @@ def test_test_parser_error(mock_api_client, mock_parse_and_run_tests):


@patch(
"reana_client.api.client.current_rs_api_client",
new_callable=lambda: make_mock_api_client("reana-server")(
{"status": "finished", "id": "28"}, Mock(status_code=200)
),
"reana_client.cli.test.get_workflow_status",
return_value={"status": "finished", "name": "myanalysis"},
)
@patch(
"reana_client.cli.test.parse_and_run_tests",
side_effect=FileNotFoundError,
)
def test_test_test_file_not_found(mock_api_client, mock_parse_and_run_tests):
"""Test test command when parser error occurs."""
def test_test_feature_file_not_found(
mock_get_workflow_status, mock_parse_and_run_tests
):
"""Test test command when a feature file is not found."""
runner = CliRunner()
with runner.isolation():
result = runner.invoke(
Expand All @@ -281,10 +278,8 @@ def test_test_test_file_not_found(mock_api_client, mock_parse_and_run_tests):


@patch(
"reana_client.api.client.current_rs_api_client",
new_callable=lambda: make_mock_api_client("reana-server")(
{"status": "finished", "id": "1618"}, Mock(status_code=200)
),
"reana_client.cli.test.get_workflow_status",
return_value={"status": "finished", "name": "myanalysis"},
)
@patch(
"reana_client.cli.test.parse_and_run_tests",
Expand All @@ -293,7 +288,7 @@ def test_test_test_file_not_found(mock_api_client, mock_parse_and_run_tests):
[passed_test],
),
)
def test_test_multiple_test_files(mock_api_client, mock_parse_and_run_tests):
def test_test_multiple_test_files(mock_workflow_status, mock_parse_and_run_tests):
"""Test test command when multiple test files are specified."""
runner = CliRunner()
with runner.isolation():
Expand All @@ -316,10 +311,8 @@ def test_test_multiple_test_files(mock_api_client, mock_parse_and_run_tests):


@patch(
"reana_client.api.client.current_rs_api_client",
new_callable=lambda: make_mock_api_client("reana-server")(
{"status": "finished", "id": "1618"}, Mock(status_code=200)
),
"reana_client.cli.test.get_workflow_status",
return_value={"status": "finished", "name": "myanalysis"},
)
@patch(
"reana_client.cli.test.parse_and_run_tests",
Expand All @@ -328,7 +321,7 @@ def test_test_multiple_test_files(mock_api_client, mock_parse_and_run_tests):
[passed_test, replace(passed_test, scenario="scenario2")],
),
)
def test_test_all_scenarios_pass(mock_api_client, mock_parse_and_run_tests):
def test_test_all_scenarios_pass(mock_workflow_status, mock_parse_and_run_tests):
"""Test test command when tests pass."""
runner = CliRunner()
with runner.isolated_filesystem():
Expand All @@ -341,10 +334,8 @@ def test_test_all_scenarios_pass(mock_api_client, mock_parse_and_run_tests):


@patch(
"reana_client.api.client.current_rs_api_client",
new_callable=lambda: make_mock_api_client("reana-server")(
{"status": "finished", "id": "2998"}, Mock(status_code=200)
),
"reana_client.cli.test.get_workflow_status",
return_value={"status": "finished", "name": "myanalysis"},
)
@patch(
"reana_client.cli.test.parse_and_run_tests",
Expand All @@ -353,7 +344,7 @@ def test_test_all_scenarios_pass(mock_api_client, mock_parse_and_run_tests):
[replace(failed_test, scenario="scenario1"), failed_test],
),
)
def test_test_all_scenarios_fail(mock_api_client, mock_parse_and_run_tests):
def test_test_all_scenarios_fail(mock_workflow_status, mock_parse_and_run_tests):
"""Test test command when tests fail."""
runner = CliRunner()
with runner.isolation():
Expand All @@ -362,14 +353,12 @@ def test_test_all_scenarios_fail(mock_api_client, mock_parse_and_run_tests):
["test", "-w", "myanalysis", "-n", "test_analysis.feature", "-t", "000000"],
)
assert "failed" in result.output and "passed" not in result.output
assert result.exit_code == 0
assert result.exit_code == 1


@patch(
"reana_client.api.client.current_rs_api_client",
new_callable=lambda: make_mock_api_client("reana-server")(
{"status": "finished", "id": "8128"}, Mock(status_code=200)
),
"reana_client.cli.test.get_workflow_status",
return_value={"status": "finished", "name": "myanalysis"},
)
@patch(
"reana_client.cli.test.parse_and_run_tests",
Expand All @@ -378,7 +367,7 @@ def test_test_all_scenarios_fail(mock_api_client, mock_parse_and_run_tests):
[passed_test, failed_test],
),
)
def test_test_some_scenarios_pass(mock_api_client, mock_parse_and_run_tests):
def test_test_some_scenarios_pass(mock_workflow_status, mock_parse_and_run_tests):
"""Test test command when some tests pass and some fail."""
runner = CliRunner()
with runner.isolated_filesystem():
Expand All @@ -387,4 +376,4 @@ def test_test_some_scenarios_pass(mock_api_client, mock_parse_and_run_tests):
["test", "-w", "myanalysis", "-n", "test_analysis.feature", "-t", "000000"],
)
assert "passed" in result.output and "failed" in result.output
assert result.exit_code == 0
assert result.exit_code == 1

0 comments on commit dceb6ba

Please sign in to comment.