Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Organise unit tests to use pytest fixtures (implement remaining files) #178

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 36 additions & 41 deletions tests/test_fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,52 +13,47 @@

from benchcab.utils.fs import mkdir, next_path

pytest.skip(allow_module_level=True)


from .common import MOCK_CWD


def test_next_path():
class TestNextPath:
"""Tests for `next_path()`."""
pattern = "rev_number-*.log"

# Success case: get next path in 'empty' CWD
assert len(list(MOCK_CWD.glob(pattern))) == 0
ret = next_path(MOCK_CWD, pattern)
assert ret == "rev_number-1.log"
@pytest.fixture()
def pattern(self):
"""Return a file pattern for testing against."""
return "rev_number-*.log"

# Success case: get next path in 'non-empty' CWD
ret_path = MOCK_CWD / ret
ret_path.touch()
assert len(list(MOCK_CWD.glob(pattern))) == 1
ret = next_path(MOCK_CWD, pattern)
assert ret == "rev_number-2.log"
def test_next_path_in_empty_cwd(self, pattern, mock_cwd):
"""Success case: get next path in 'empty' CWD."""
assert next_path(mock_cwd, pattern) == "rev_number-1.log"

def test_next_path_in_non_empty_cwd(self, pattern, mock_cwd):
"""Success case: get next path in 'non-empty' CWD."""
(mock_cwd / next_path(mock_cwd, pattern)).touch()
assert next_path(mock_cwd, pattern) == "rev_number-2.log"

@pytest.mark.parametrize(
"test_path,kwargs",
[
(Path(MOCK_CWD, "test1"), {}),
(Path(MOCK_CWD, "test1/test2"), dict(parents=True)),
(Path(MOCK_CWD, "test1/test2"), dict(parents=True, exist_ok=True)),
],
)
def test_mkdir(test_path, kwargs):
"""Tests for `mkdir()`."""

# Success case: create a test directory
mkdir(test_path, **kwargs)
assert test_path.exists()
test_path.rmdir()


def test_mkdir_verbose():
"""Tests for verbose output of `mkdir()`."""
class TestMkdir:
"""Tests for `mkdir()`."""

# Success case: verbose output
test_path = Path(MOCK_CWD, "test1")
with contextlib.redirect_stdout(io.StringIO()) as buf:
mkdir(test_path, verbose=True)
assert buf.getvalue() == (f"Creating {test_path} directory\n")
test_path.rmdir()
@pytest.mark.parametrize(
("test_path", "kwargs"),
[
(Path("test1"), {}),
(Path("test1/test2"), dict(parents=True)),
(Path("test1/test2"), dict(parents=True, exist_ok=True)),
],
)
def test_mkdir(self, test_path, kwargs):
"""Success case: create a test directory."""
mkdir(test_path, **kwargs)
assert test_path.exists()
test_path.rmdir()

@pytest.mark.parametrize(
("verbosity", "expected"), [(False, ""), (True, "Creating test1 directory\n")]
)
def test_standard_output(self, verbosity, expected):
"""Success case: test standard output."""
with contextlib.redirect_stdout(io.StringIO()) as buf:
mkdir(Path("test1"), verbose=verbosity)
assert buf.getvalue() == expected
132 changes: 67 additions & 65 deletions tests/test_pbs.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
"""`pytest` tests for `utils/pbs.py`."""

import pytest

pytest.skip(allow_module_level=True)

from benchcab import internal
from benchcab.utils.pbs import render_job_script


def test_render_job_script():
class TestRenderJobScript:
"""Tests for `render_job_script()`."""
# Success case: test default job script generated is correct
assert render_job_script(
project="tm70",
config_path="/path/to/config.yaml",
modules=["foo", "bar", "baz"],
benchcab_path="/absolute/path/to/benchcab",
) == (
f"""#!/bin/bash

def test_default_job_script(self):
"""Success case: test default job script generated is correct."""
assert render_job_script(
project="tm70",
config_path="/path/to/config.yaml",
modules=["foo", "bar", "baz"],
benchcab_path="/absolute/path/to/benchcab",
) == (
f"""#!/bin/bash
#PBS -l wd
#PBS -l ncpus={internal.FLUXSITE_DEFAULT_PBS["ncpus"]}
#PBS -l mem={internal.FLUXSITE_DEFAULT_PBS["mem"]}
Expand All @@ -40,17 +38,18 @@ def test_render_job_script():
/absolute/path/to/benchcab fluxsite-bitwise-cmp --config=/path/to/config.yaml

"""
)

# Success case: test verbose flag is added to command line arguments
assert render_job_script(
project="tm70",
config_path="/path/to/config.yaml",
modules=["foo", "bar", "baz"],
verbose=True,
benchcab_path="/absolute/path/to/benchcab",
) == (
f"""#!/bin/bash
)

def test_verbose_flag_added_to_command_line_arguments(self):
"""Success case: test verbose flag is added to command line arguments."""
assert render_job_script(
project="tm70",
config_path="/path/to/config.yaml",
modules=["foo", "bar", "baz"],
verbose=True,
benchcab_path="/absolute/path/to/benchcab",
) == (
f"""#!/bin/bash
#PBS -l wd
#PBS -l ncpus={internal.FLUXSITE_DEFAULT_PBS["ncpus"]}
#PBS -l mem={internal.FLUXSITE_DEFAULT_PBS["mem"]}
Expand All @@ -73,17 +72,18 @@ def test_render_job_script():
/absolute/path/to/benchcab fluxsite-bitwise-cmp --config=/path/to/config.yaml -v

"""
)

# Success case: skip fluxsite-bitwise-cmp step
assert render_job_script(
project="tm70",
config_path="/path/to/config.yaml",
modules=["foo", "bar", "baz"],
skip_bitwise_cmp=True,
benchcab_path="/absolute/path/to/benchcab",
) == (
f"""#!/bin/bash
)

def test_skip_bitwise_comparison_step(self):
"""Success case: skip fluxsite-bitwise-cmp step."""
assert render_job_script(
project="tm70",
config_path="/path/to/config.yaml",
modules=["foo", "bar", "baz"],
skip_bitwise_cmp=True,
benchcab_path="/absolute/path/to/benchcab",
) == (
f"""#!/bin/bash
#PBS -l wd
#PBS -l ncpus={internal.FLUXSITE_DEFAULT_PBS["ncpus"]}
#PBS -l mem={internal.FLUXSITE_DEFAULT_PBS["mem"]}
Expand All @@ -104,23 +104,24 @@ def test_render_job_script():
/absolute/path/to/benchcab fluxsite-run-tasks --config=/path/to/config.yaml

"""
)

# Success case: specify parameters in pbs_config
assert render_job_script(
project="tm70",
config_path="/path/to/config.yaml",
modules=["foo", "bar", "baz"],
skip_bitwise_cmp=True,
benchcab_path="/absolute/path/to/benchcab",
pbs_config={
"ncpus": 4,
"mem": "16GB",
"walltime": "00:00:30",
"storage": ["gdata/foo"],
},
) == (
"""#!/bin/bash
)

def test_pbs_config_parameters(self):
"""Success case: specify parameters in pbs_config."""
assert render_job_script(
project="tm70",
config_path="/path/to/config.yaml",
modules=["foo", "bar", "baz"],
skip_bitwise_cmp=True,
benchcab_path="/absolute/path/to/benchcab",
pbs_config={
"ncpus": 4,
"mem": "16GB",
"walltime": "00:00:30",
"storage": ["gdata/foo"],
},
) == (
"""#!/bin/bash
#PBS -l wd
#PBS -l ncpus=4
#PBS -l mem=16GB
Expand All @@ -141,18 +142,19 @@ def test_render_job_script():
/absolute/path/to/benchcab fluxsite-run-tasks --config=/path/to/config.yaml

"""
)

# Success case: if the pbs_config is empty, use the default values
assert render_job_script(
project="tm70",
config_path="/path/to/config.yaml",
modules=["foo", "bar", "baz"],
skip_bitwise_cmp=True,
benchcab_path="/absolute/path/to/benchcab",
pbs_config={},
) == (
f"""#!/bin/bash
)

def test_default_pbs_config(self):
"""Success case: if the pbs_config is empty, use the default values."""
assert render_job_script(
project="tm70",
config_path="/path/to/config.yaml",
modules=["foo", "bar", "baz"],
skip_bitwise_cmp=True,
benchcab_path="/absolute/path/to/benchcab",
pbs_config={},
) == (
f"""#!/bin/bash
#PBS -l wd
#PBS -l ncpus={internal.FLUXSITE_DEFAULT_PBS["ncpus"]}
#PBS -l mem={internal.FLUXSITE_DEFAULT_PBS["mem"]}
Expand All @@ -173,4 +175,4 @@ def test_render_job_script():
/absolute/path/to/benchcab fluxsite-run-tasks --config=/path/to/config.yaml

"""
)
)
Loading