Skip to content

Commit

Permalink
refactor(test): use data_class instead of dictionary (reanahub#724)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajclyall committed Aug 2, 2024
1 parent 0c51ddf commit 5d7224d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 50 deletions.
11 changes: 3 additions & 8 deletions reana_client/cli/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import sys
import click
from reana_client.cli.utils import add_access_token_options, check_connection

from reana_commons.gherkin_parser.parser import (
parse_and_run_tests,
AnalysisTestStatus,
Expand Down Expand Up @@ -138,12 +137,8 @@ def test(ctx, workflow, test_files, access_token):

click.secho(f"Summary of {test_file}:", bold=True)
for scenario in results[1]:
click.echo(f"Tested {scenario['scenario']}: ", nl=False)
click.echo(f"Tested {scenario.scenario}: ", nl=False)
click.secho(
f"{scenario['result'].name}",
fg=(
"green"
if scenario["result"] == AnalysisTestStatus.passed
else "red"
),
f"{scenario.result.name}",
fg=("green" if scenario.result == AnalysisTestStatus.passed else "red"),
)
81 changes: 39 additions & 42 deletions tests/test_cli_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,34 @@
from unittest.mock import Mock, patch

from pytest_reana.test_utils import make_mock_api_client
from reana_commons.gherkin_parser.parser import AnalysisTestStatus
from reana_commons.gherkin_parser.parser import AnalysisTestStatus, TestResult
from reana_commons.gherkin_parser.errors import FeatureFileError
from dataclasses import replace

passed_test = TestResult(
scenario="scenario1",
error_log=None,
result=AnalysisTestStatus.passed,
failed_testcase=None,
analysis_run_id="1618",
feature="Run Duration",
checked_at="2024-01-01T00:00:00.000000",
)

failed_test = TestResult(
scenario="scenario2",
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",
)


def test_test_workflow_not_found():
"""Test test command when workflow is not found."""
env = {"REANA_SERVER_URL": "localhost"}
runner = CliRunner(env=env)
runner = CliRunner()
with runner.isolated_filesystem():
result = runner.invoke(
cli,
Expand Down Expand Up @@ -50,9 +70,7 @@ def test_test_workflow_not_found():
)
def test_test_workflow_not_finished(mock_api_client):
"""Test test command when workflow is not finished."""

env = {"REANA_SERVER_URL": "localhost"}
runner = CliRunner(env=env)
runner = CliRunner()
with runner.isolation():
result = runner.invoke(
cli,
Expand Down Expand Up @@ -81,8 +99,7 @@ def test_test_workflow_not_finished(mock_api_client):
)
def test_test_workflow_deleted(mock_api_client):
"""Test test command when workflow is deleted."""
env = {"REANA_SERVER_URL": "localhost"}
runner = CliRunner(env=env)
runner = CliRunner()
with runner.isolation():
result = runner.invoke(
cli,
Expand All @@ -103,8 +120,7 @@ def test_test_workflow_deleted(mock_api_client):
)
def test_test_no_test_files(mock_api_client):
"""Test test command when no test files are specified."""
env = {"REANA_SERVER_URL": "localhost"}
runner = CliRunner(env=env)
runner = CliRunner()
with runner.isolation():
result = runner.invoke(cli, ["test", "-w", "myanalysis", "-t", "000000"])
assert (
Expand All @@ -122,8 +138,7 @@ def test_test_no_test_files(mock_api_client):
)
def test_test_no_test_files_with_test_file_option(mock_api_client):
"""Test test command when no test files are specified in reana.yml and when the test file option is provided."""
env = {"REANA_SERVER_URL": "localhost"}
runner = CliRunner(env=env)
runner = CliRunner()
with runner.isolation():
result = runner.invoke(
cli,
Expand Down Expand Up @@ -155,8 +170,7 @@ def test_test_multiple_test_files_with_test_file_option(mock_api_client):
"""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.
"""
env = {"REANA_SERVER_URL": "localhost"}
runner = CliRunner(env=env)
runner = CliRunner()
with runner.isolation():
result = runner.invoke(
cli,
Expand Down Expand Up @@ -190,9 +204,7 @@ def test_test_multiple_test_files_with_test_file_option(mock_api_client):
"reana_client.cli.test.parse_and_run_tests",
return_value=(
"myanalysis",
[
{"scenario": "scenario1", "result": AnalysisTestStatus.passed},
],
[passed_test],
),
)
def test_test_files_from_spec(mock_api_client, mock_parse_and_run_tests):
Expand Down Expand Up @@ -220,8 +232,7 @@ def test_test_files_from_spec(mock_api_client, mock_parse_and_run_tests):
)
def test_test_parser_error(mock_api_client, mock_parse_and_run_tests):
"""Test test command when parser error occurs."""
env = {"REANA_SERVER_URL": "localhost"}
runner = CliRunner(env=env)
runner = CliRunner()
with runner.isolation():
result = runner.invoke(
cli,
Expand Down Expand Up @@ -251,8 +262,7 @@ def test_test_parser_error(mock_api_client, mock_parse_and_run_tests):
)
def test_test_test_file_not_found(mock_api_client, mock_parse_and_run_tests):
"""Test test command when parser error occurs."""
env = {"REANA_SERVER_URL": "localhost"}
runner = CliRunner(env=env)
runner = CliRunner()
with runner.isolation():
result = runner.invoke(
cli,
Expand Down Expand Up @@ -280,13 +290,12 @@ def test_test_test_file_not_found(mock_api_client, mock_parse_and_run_tests):
"reana_client.cli.test.parse_and_run_tests",
return_value=(
"myanalysis",
[{"scenario": "scenario1", "result": AnalysisTestStatus.passed}],
[passed_test],
),
)
def test_test_multiple_test_files(mock_api_client, mock_parse_and_run_tests):
"""Test test command when multiple test files are specified."""
env = {"REANA_SERVER_URL": "localhost"}
runner = CliRunner(env=env)
runner = CliRunner()
with runner.isolation():
result = runner.invoke(
cli,
Expand Down Expand Up @@ -316,16 +325,12 @@ def test_test_multiple_test_files(mock_api_client, mock_parse_and_run_tests):
"reana_client.cli.test.parse_and_run_tests",
return_value=(
"myanalysis",
[
{"scenario": "scenario1", "result": AnalysisTestStatus.passed},
{"scenario": "scenario2", "result": AnalysisTestStatus.passed},
],
[passed_test, replace(passed_test, scenario="scenario2")],
),
)
def test_test_all_scenarios_pass(mock_api_client, mock_parse_and_run_tests):
"""Test test command when tests pass."""
env = {"REANA_SERVER_URL": "localhost"}
runner = CliRunner(env=env)
runner = CliRunner()
with runner.isolated_filesystem():
result = runner.invoke(
cli,
Expand All @@ -345,16 +350,12 @@ def test_test_all_scenarios_pass(mock_api_client, mock_parse_and_run_tests):
"reana_client.cli.test.parse_and_run_tests",
return_value=(
"myanalysis",
[
{"scenario": "scenario1", "result": AnalysisTestStatus.failed},
{"scenario": "scenario2", "result": AnalysisTestStatus.failed},
],
[replace(failed_test, scenario="scenario1"), failed_test],
),
)
def test_test_all_scenarios_fail(mock_api_client, mock_parse_and_run_tests):
"""Test test command when tests fail."""
env = {"REANA_SERVER_URL": "localhost"}
runner = CliRunner(env=env)
runner = CliRunner()
with runner.isolation():
result = runner.invoke(
cli,
Expand All @@ -374,16 +375,12 @@ def test_test_all_scenarios_fail(mock_api_client, mock_parse_and_run_tests):
"reana_client.cli.test.parse_and_run_tests",
return_value=(
"myanalysis",
[
{"scenario": "scenario1", "result": AnalysisTestStatus.passed},
{"scenario": "scenario2", "result": AnalysisTestStatus.failed},
],
[passed_test, failed_test],
),
)
def test_test_some_scenarios_pass(mock_api_client, mock_parse_and_run_tests):
"""Test test command when some tests pass and some fail."""
env = {"REANA_SERVER_URL": "localhost"}
runner = CliRunner(env=env)
runner = CliRunner()
with runner.isolated_filesystem():
result = runner.invoke(
cli,
Expand Down

0 comments on commit 5d7224d

Please sign in to comment.