Skip to content

Commit

Permalink
AI suggestions: spelling and extra unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tovrstra committed Jul 14, 2024
1 parent f23aca2 commit aa2cc20
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
4 changes: 2 additions & 2 deletions iodata/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ def parse_args():
"--allow-changes",
default=False,
action="store_true",
help="Allow reorganizing the input data to make it comatible with the output format. "
"Warnings will be emitted for all changes made.",
help="Allow (not trivially reversible) conversion the input data to make it compatible "
"with the output format. Warnings will be emitted for all changes made.",
)
parser.add_argument(
"-m",
Expand Down
33 changes: 26 additions & 7 deletions iodata/test/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@

from ..__main__ import convert as convfn
from ..api import load_many, load_one
from ..utils import PrepareDumpError, PrepareDumpWarning
from ..utils import FileFormatError, PrepareDumpError, PrepareDumpWarning


def _convscript(infn: str, outfn: str, many: bool, infmt: str, outfmt: str, allow_changes: bool):
"""Simulate the convert function by calling iodata-convert in a subprocess."""
args = [sys.executable, "-m", "iodata.__main__", infn, outfn]
if many:
args.append("-m")
Expand All @@ -43,12 +44,16 @@ def _convscript(infn: str, outfn: str, many: bool, infmt: str, outfmt: str, allo
args.append(f"--outfmt={outfmt}")
if allow_changes:
args.append("-c")
try:
subprocess.run(args, check=True)
except subprocess.CalledProcessError as exc:
raise PrepareDumpError("Translated error") from exc
if allow_changes:
warn(PrepareDumpWarning("Some message"), stacklevel=2)
cp = subprocess.run(args, capture_output=True, check=False, encoding="utf8")
if cp.returncode == 0:
if allow_changes and "PrepareDumpWarning" in cp.stderr:
warn(PrepareDumpWarning(cp.stderr), stacklevel=2)
else:
if "PrepareDumpError" in cp.stderr:
raise PrepareDumpError(cp.stderr)
if "FileFormatError" in cp.stderr:
raise FileFormatError(cp.stderr)
raise RuntimeError(f"Failure not processed.\n{cp.stderr}")


def _check_convert_one(myconvert, tmpdir):
Expand Down Expand Up @@ -93,6 +98,20 @@ def test_convert_one_manfmt(tmpdir, convert):
_check_convert_one(myconvert, tmpdir)


@pytest.mark.parametrize("convert", [convfn, _convscript])
def test_convert_one_nonexisting_infmt(tmpdir, convert):
myconvert = partial(convert, many=False, infmt="blablabla", outfmt="xyz")
with pytest.raises(FileFormatError):
_check_convert_one(myconvert, tmpdir)


@pytest.mark.parametrize("convert", [convfn, _convscript])
def test_convert_one_nonexisting_outfmt(tmpdir, convert):
myconvert = partial(convert, many=False, infmt="fchk", outfmt="blablabla")
with pytest.raises(FileFormatError):
_check_convert_one(myconvert, tmpdir)


@pytest.mark.parametrize("convert", [convfn, _convscript])
def test_convert_one_manfmt_changes(tmpdir, convert):
myconvert = partial(convert, many=False, infmt="fchk", outfmt="molekel")
Expand Down

0 comments on commit aa2cc20

Please sign in to comment.