Skip to content

Commit

Permalink
Add requested changes from review
Browse files Browse the repository at this point in the history
Use pytest's `parametrize()` feature for testing different levels of
verbosity.

Remove usage of `mock_cwd` fixture unless it is required for a test to
pass.
  • Loading branch information
SeanBryan51 committed Oct 10, 2023
1 parent cd99c72 commit 19eff9e
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 166 deletions.
21 changes: 13 additions & 8 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,23 @@

@pytest.fixture()
def mock_cwd():
"""Fixture for creating and changing into a mock CWD directory."""
"""Create and return a unique temporary directory to use as the CWD.
The return value is the path of the directory.
"""
return Path(tempfile.mkdtemp(prefix="benchcab_tests"))


@pytest.fixture(autouse=True)
def _run_around_tests(mock_cwd):
"""Change into the `mock_cwd` directory."""
prevdir = Path.cwd()
_mock_cwd = Path(tempfile.mkdtemp(prefix="benchcab_tests"))
if _mock_cwd.exists():
shutil.rmtree(_mock_cwd)
_mock_cwd.mkdir()
os.chdir(_mock_cwd.expanduser())
os.chdir(mock_cwd.expanduser())

yield _mock_cwd
yield

os.chdir(prevdir)
shutil.rmtree(_mock_cwd)
shutil.rmtree(mock_cwd)


@pytest.fixture()
Expand Down
112 changes: 60 additions & 52 deletions tests/test_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,27 @@

import contextlib
import io
from pathlib import Path

import pytest

from benchcab import internal
from benchcab.comparison import ComparisonTask

FILE_NAME_A, FILE_NAME_B = "file_a.nc", "file_b.nc"
TASK_NAME = "mock_comparison_task_name"


@pytest.fixture()
def files(mock_cwd):
def files():
"""Return mock file paths used for comparison."""
return mock_cwd / "file_a.nc", mock_cwd / "file_b.nc"
return Path(FILE_NAME_A), Path(FILE_NAME_B)


@pytest.fixture()
def comparison_task(files, mock_cwd, mock_subprocess_handler):
"""Returns a mock `ComparisonTask` instance for testing against."""
_comparison_task = ComparisonTask(
files=files, task_name="mock_comparison_task_name"
)
_comparison_task = ComparisonTask(files=files, task_name=TASK_NAME)
_comparison_task.subprocess_handler = mock_subprocess_handler
_comparison_task.root_dir = mock_cwd
return _comparison_task
Expand All @@ -30,37 +32,36 @@ class TestRun:
"""Tests for `ComparisonTask.run()`."""

@pytest.fixture()
def bitwise_cmp_dir(self, mock_cwd):
def bitwise_cmp_dir(self):
"""Create and return the fluxsite bitwise comparison directory."""
_bitwise_cmp_dir = mock_cwd / internal.FLUXSITE_BITWISE_CMP_DIR
_bitwise_cmp_dir.mkdir(parents=True)
return _bitwise_cmp_dir
internal.FLUXSITE_BITWISE_CMP_DIR.mkdir(parents=True)
return internal.FLUXSITE_BITWISE_CMP_DIR

def test_nccmp_execution(self, comparison_task, files, mock_subprocess_handler):
"""Success case: test nccmp is executed."""
file_a, file_b = files
comparison_task.run()
assert f"nccmp -df {file_a} {file_b}" in mock_subprocess_handler.commands

def test_default_standard_output(self, comparison_task, files):
"""Success case: test default standard output."""
file_a, file_b = files
with contextlib.redirect_stdout(io.StringIO()) as buf:
comparison_task.run()
assert (
buf.getvalue()
== f"Success: files {file_a.name} {file_b.name} are identical\n"
)

def test_verbose_standard_output(self, comparison_task, files):
"""Success case: test verbose standard output."""
file_a, file_b = files
@pytest.mark.parametrize(
("verbosity", "expected"),
[
(
False,
f"Success: files {FILE_NAME_A} {FILE_NAME_B} are identical\n",
),
(
True,
f"Comparing files {FILE_NAME_A} and {FILE_NAME_B} bitwise...\n"
f"Success: files {FILE_NAME_A} {FILE_NAME_B} are identical\n",
),
],
)
def test_standard_output(self, comparison_task, verbosity, expected):
"""Success case: test standard output."""
with contextlib.redirect_stdout(io.StringIO()) as buf:
comparison_task.run(verbose=True)
assert buf.getvalue() == (
f"Comparing files {file_a.name} and {file_b.name} bitwise...\n"
f"Success: files {file_a.name} {file_b.name} are identical\n"
)
comparison_task.run(verbose=verbosity)
assert buf.getvalue() == expected

def test_failed_comparison_check(
self, comparison_task, mock_subprocess_handler, bitwise_cmp_dir
Expand All @@ -72,31 +73,38 @@ def test_failed_comparison_check(
with stdout_file.open("r", encoding="utf-8") as file:
assert file.read() == mock_subprocess_handler.stdout

def test_default_standard_output_on_failure(
self, comparison_task, files, mock_subprocess_handler, bitwise_cmp_dir
):
"""Failure case: test non-verbose standard output on failure."""
file_a, file_b = files
stdout_file = bitwise_cmp_dir / f"{comparison_task.task_name}.txt"
mock_subprocess_handler.error_on_call = True
with contextlib.redirect_stdout(io.StringIO()) as buf:
comparison_task.run()
assert buf.getvalue() == (
f"Failure: files {file_a.name} {file_b.name} differ. Results of diff "
f"have been written to {stdout_file}\n"
)

def test_verbose_standard_output_on_failure(
self, comparison_task, files, mock_subprocess_handler, bitwise_cmp_dir
# TODO(Sean) fix for issue https://github.com/CABLE-LSM/benchcab/issues/162
@pytest.mark.skip(
reason="""This will always fail since `parametrize()` parameters are
dependent on the `mock_cwd` fixture."""
)
@pytest.mark.parametrize(
("verbosity", "expected"),
[
(
False,
f"Failure: files {FILE_NAME_A} {FILE_NAME_B} differ. Results of "
"diff have been written to "
f"{internal.FLUXSITE_BITWISE_CMP_DIR}/{TASK_NAME}\n",
),
(
True,
f"Comparing files {FILE_NAME_A} and {FILE_NAME_B} bitwise...\n"
f"Failure: files {FILE_NAME_A} {FILE_NAME_B} differ. Results of "
"diff have been written to "
f"{internal.FLUXSITE_BITWISE_CMP_DIR}/{TASK_NAME}\n",
),
],
)
def test_standard_output_on_failure(
self,
comparison_task,
mock_subprocess_handler,
verbosity,
expected,
):
"""Failure case: test verbose standard output on failure."""
file_a, file_b = files
"""Failure case: test standard output on failure."""
mock_subprocess_handler.error_on_call = True
stdout_file = bitwise_cmp_dir / f"{comparison_task.task_name}.txt"
with contextlib.redirect_stdout(io.StringIO()) as buf:
comparison_task.run(verbose=True)
assert buf.getvalue() == (
f"Comparing files {file_a.name} and {file_b.name} bitwise...\n"
f"Failure: files {file_a.name} {file_b.name} differ. Results of diff "
f"have been written to {stdout_file}\n"
)
comparison_task.run(verbose=verbosity)
assert buf.getvalue() == expected
Loading

0 comments on commit 19eff9e

Please sign in to comment.