diff --git a/tests/test_read_sequence_cli.py b/tests/test_read_sequence_cli.py new file mode 100644 index 0000000000000000000000000000000000000000..5006fb8f7b5bf3df093bfbe86f7c8ccbc71a7ec8 --- /dev/null +++ b/tests/test_read_sequence_cli.py @@ -0,0 +1,103 @@ +"""Test Functionality of Read Sequencing CLI. + +This test needs to be modified correctly. +""" + +import argparse +import importlib.util +import os +from pathlib import Path +import sys + +import pytest + +from src.read_sequencing.cli import ( + main, + parse_args, +) + +PACKAGE_DIR = Path(__file__).resolve().parents[1] / "src/read_sequencing" +TEST_FILE = Path.cwd() / 'tests/resources/test_terminal_fragments.txt', + + +class TestParseArgs: + """Test ``parse_args()`` function.""" + + def test_help_option(self, monkeypatch): + """Test help option.""" + monkeypatch.setattr( + sys, 'argv', [ + 'htsinfer', + '--help', + ] + ) + with pytest.raises(SystemExit) as exc: + assert parse_args() + assert exc.value.code == 0 + + def test_one_positional_arg(self, monkeypatch): + """Call with one positional arg.""" + monkeypatch.setattr( + sys, 'argv', [ + str(TEST_FILE), + ] + ) + ret_val = parse_args() + assert isinstance(ret_val, argparse.Namespace) + + def test_too_many_positional_args(self, monkeypatch): + """Call with two positional args.""" + monkeypatch.setattr( + sys, 'argv', [ + 'htsinfer', + str(TEST_FILE), str(TEST_FILE), + ] + ) + with pytest.raises(SystemExit) as exc: + parse_args() + assert exc.value.code != 0 + + +class TestMain: + """Test ``main()`` function.""" + + def test_with_too_many_args(self, monkeypatch): + """Call with too many positional args.""" + monkeypatch.setattr( + sys, 'argv', [ + 'read_sequencing', + str(TEST_FILE), str(TEST_FILE), + ] + ) + with pytest.raises(SystemExit) as exc: + main() + assert exc.value.code != 0 + + def test_keyboard_interrupt(self, monkeypatch): + """Test keyboard interrupt.""" + + class RaiseKeyboardInterrupt: + """Helper class to raise `ValueError`.""" + + def __init__(self, *args, **kwargs): + """Class constructor.""" + raise KeyboardInterrupt + + monkeypatch.setattr( + 'src.read_sequencing.cli.parse_args', + RaiseKeyboardInterrupt, + ) + with pytest.raises(SystemExit) as exc: + main() + assert exc.value.code >= 128 + + +def test_main_as_script(): + """Run as script.""" + main_file = PACKAGE_DIR / "cli.py" + fl = os.path.join(os.path.dirname(__file__), main_file) + spec = importlib.util.spec_from_file_location('__main__', fl) + module = importlib.util.module_from_spec(spec) + with pytest.raises(SystemExit) as exc: + spec.loader.exec_module(module) # type: ignore + assert exc.value.code != 0