From e45b34e81cf07a7de520176515edec194ab8e01f Mon Sep 17 00:00:00 2001 From: Alyssa Travitz <31974495+atravitz@users.noreply.github.com> Date: Tue, 28 Jan 2025 13:48:12 -0800 Subject: [PATCH] 1089 fix tests for missing gather data (#1090) * add assert_click_success helper * fixing xfail tests so they pass * add type annotations * organize into a class * update docstring * adding no-cov for test utils * removing failing pooch test --- openfecli/tests/commands/test_gather.py | 80 ++++++++++--------------- openfecli/tests/utils.py | 15 +++++ 2 files changed, 48 insertions(+), 47 deletions(-) create mode 100644 openfecli/tests/utils.py diff --git a/openfecli/tests/commands/test_gather.py b/openfecli/tests/commands/test_gather.py index 8949e48ea..84483f951 100644 --- a/openfecli/tests/commands/test_gather.py +++ b/openfecli/tests/commands/test_gather.py @@ -5,6 +5,7 @@ import pathlib import pytest import pooch +from ..utils import assert_click_success from openfecli.commands.gather import ( gather, format_estimate_uncertainty, _get_column, @@ -135,24 +136,24 @@ def test_get_column(val, col): """ @pytest.fixture() -def results_dir_serial(tmpdir): +def results_dir_serial(tmpdir)->str: """Example output data, with replicates run in serial (3 replicates per results JSON).""" with tmpdir.as_cwd(): with resources.files('openfecli.tests.data') as d: - t = tarfile.open(d / 'rbfe_results.tar.gz', mode='r') - t.extractall('.') + tar = tarfile.open(d / 'rbfe_results.tar.gz', mode='r') + tar.extractall('.') - return os.path.abspath(t.getnames()[0]) + return os.path.abspath(tar.getnames()[0]) @pytest.fixture() -def results_dir_parallel(tmpdir): +def results_dir_parallel(tmpdir)->str: """Example output data, with replicates run in serial (3 replicates per results JSON).""" with tmpdir.as_cwd(): with resources.files('openfecli.tests.data') as d: - t = tarfile.open(d / 'rbfe_results_parallel.tar.gz', mode='r') - t.extractall('.') + tar = tarfile.open(d / 'rbfe_results_parallel.tar.gz', mode='r') + tar.extractall('.') - return os.path.abspath(t.getnames()[0]) + return os.path.abspath(tar.getnames()[0]) @pytest.mark.parametrize('data_fixture', ['results_dir_serial', 'results_dir_parallel']) @pytest.mark.parametrize('report', ["", "dg", "ddg", "raw"]) @@ -194,54 +195,39 @@ def test_generate_bad_legs_error_message(include): for string in expected: assert string in msg +class TestGatherFailedEdges: + @pytest.fixture() + def results_dir_serial_missing_leg(self, tmpdir)->str: + """Example output data, with replicates run in serial and one deleted results JSON.""" + with tmpdir.as_cwd(): + with resources.files('openfecli.tests.data') as d: + tar = tarfile.open(d / 'rbfe_results.tar.gz', mode='r') + tar.extractall('.') -@pytest.mark.xfail -def test_missing_leg_error(results_dir_serial): - file_to_remove = "easy_rbfe_lig_ejm_31_complex_lig_ejm_42_complex.json" - (pathlib.Path("results") / file_to_remove).unlink() + results_dir_path = os.path.abspath(tar.getnames()[0]) + file_to_remove = "easy_rbfe_lig_ejm_31_complex_lig_ejm_42_complex.json" + (pathlib.Path(results_dir_path)/ file_to_remove).unlink() + return results_dir_path - runner = CliRunner() - result = runner.invoke(gather, ['results'] + ['-o', '-']) - assert result.exit_code == 1 - assert isinstance(result.exception, RuntimeError) - assert "Unable to determine" in str(result.exception) - assert "'lig_ejm_31'" in str(result.exception) - assert "'lig_ejm_42'" in str(result.exception) + def test_missing_leg_error(self, results_dir_serial_missing_leg: str): + runner = CliRunner() + result = runner.invoke(gather, [results_dir_serial_missing_leg] + ['-o', '-']) + assert result.exit_code == 1 + assert isinstance(result.exception, RuntimeError) + assert "Unable to determine" in str(result.exception) + assert "'lig_ejm_31'" in str(result.exception) + assert "'lig_ejm_42'" in str(result.exception) -@pytest.mark.xfail -def test_missing_leg_allow_partial(results_dir_serial): - file_to_remove = "easy_rbfe_lig_ejm_31_complex_lig_ejm_42_complex.json" - (pathlib.Path("results") / file_to_remove).unlink() - runner = CliRunner() - result = runner.invoke(gather, - ['results'] + ['--allow-partial', '-o', '-']) - assert result.exit_code == 0 + def test_missing_leg_allow_partial(self, results_dir_serial_missing_leg: str): + runner = CliRunner() + result = runner.invoke(gather, [results_dir_serial_missing_leg] + ['--allow-partial', '-o', '-']) + assert_click_success(result) RBFE_RESULTS = pooch.create( pooch.os_cache('openfe'), base_url="doi:10.6084/m9.figshare.25148945", registry={"results.tar.gz": "bf27e728935b31360f95188f41807558156861f6d89b8a47854502a499481da3"}, ) - - -@pytest.fixture -def rbfe_results(): - # fetches rbfe results from online - # untars into local directory and returns path to this - d = RBFE_RESULTS.fetch('results.tar.gz', processor=pooch.Untar()) - - return os.path.join(pooch.os_cache('openfe'), 'results.tar.gz.untar', 'results') - - -@pytest.mark.download -@pytest.mark.xfail -def test_rbfe_results(rbfe_results): - runner = CliRunner() - - result = runner.invoke(gather, ['--report', 'raw', rbfe_results]) - - assert result.exit_code == 0 - assert result.stdout_bytes == _EXPECTED_RAW diff --git a/openfecli/tests/utils.py b/openfecli/tests/utils.py new file mode 100644 index 000000000..7857806a2 --- /dev/null +++ b/openfecli/tests/utils.py @@ -0,0 +1,15 @@ +"""Helper utilities for CLI tests""" + +import click +import traceback + + +def assert_click_success(result: click.testing.Result): # -no-cov- + """Pass through error message if a click test fails. + Taken from https://github.com/openpathsampling/openpathsampling-cli/blob/main/paths_cli/commands/pathsampling.py + """ + if result.exit_code != 0: + print(result.output) + traceback.print_tb(result.exc_info[2]) + print(result.exc_info[0], result.exc_info[1]) + assert result.exit_code == 0