tests.test_pycli
Test the CLI provided by dicom_echo
.
1"""Test the CLI provided by `dicom_echo`.""" 2 3from __future__ import annotations 4 5import subprocess 6import sys 7from typing import TYPE_CHECKING 8 9import rich.emoji 10from typer.testing import CliRunner 11 12import dicom_echo as echo 13from dicom_echo.cli import app, command 14 15if TYPE_CHECKING: # pragma: no cover 16 from unittest.mock import MagicMock 17 18runner = CliRunner() 19 20 21def test_help() -> None: 22 """Test the response of the `--help` flag.""" 23 result = runner.invoke(app, ['--help']) 24 25 assert 0 == result.exit_code 26 assert 'Usage' in result.stdout 27 assert 'Arguments' in result.stdout 28 assert 'Options' in result.stdout 29 assert 'DICOM Options' in result.stdout 30 31 32def test_success(scpserver: str) -> None: 33 """Test the CLI output when the DICOM SCP server is accepting associations.""" 34 result = runner.invoke(app, [scpserver]) 35 36 assert 0 == result.exit_code 37 assert '✅ Success\n' == result.stdout 38 39 40def test_failure() -> None: 41 """Test the CLI when the DICOM SCP server is unreachable.""" 42 result = runner.invoke(app, ['dummy:1234']) 43 44 assert 1 == result.exit_code 45 assert ConnectionError is result.exception.__class__ 46 47 48def test_version() -> None: 49 """Test the CLI output when the `--version` flag is provided.""" 50 result = runner.invoke(app, ['--version']) 51 52 assert 0 == result.exit_code 53 assert command in result.stdout 54 assert echo.__version__ in result.stdout 55 56 57def test_version_called_as_module() -> None: 58 """Test the CLI output when the utility is invoked as a Python module.""" 59 out = subprocess.check_output([sys.executable, '-m', 'dicom_echo', '--version'], text=True) 60 61 assert 'dicom_echo' in out 62 assert echo.__version__ in out 63 64 65def test_host_aetitle(mock_send_rc0: MagicMock) -> None: 66 """Verify the called AE title may be passed with the target address.""" 67 # Arrange 68 called_ae_title = 'dummy' 69 70 # Act 71 runner.invoke(app, [f'{called_ae_title}@localhost:1234']) 72 73 # Assert 74 mock_send_rc0.assert_called_once() 75 assert mock_send_rc0.call_args.kwargs['called_ae_title'] == 'dummy' 76 77 78def test_nonzero_response(mock_send_rc1: MagicMock) -> None: 79 """Test the CLI output when the DICOM SCP returns a non-zero status code.""" 80 result = runner.invoke(app, ['dummy:1234']) 81 82 assert mock_send_rc1.return_value == result.exit_code 83 assert 'Warning:' in result.stdout 84 assert rich.emoji.Emoji.replace(':warning:') in result.stdout 85 assert str(mock_send_rc1.return_value) in result.stdout
runner =
<typer.testing.CliRunner object>
def
test_help() -> None:
22def test_help() -> None: 23 """Test the response of the `--help` flag.""" 24 result = runner.invoke(app, ['--help']) 25 26 assert 0 == result.exit_code 27 assert 'Usage' in result.stdout 28 assert 'Arguments' in result.stdout 29 assert 'Options' in result.stdout 30 assert 'DICOM Options' in result.stdout
Test the response of the --help
flag.
def
test_success(scpserver: str) -> None:
33def test_success(scpserver: str) -> None: 34 """Test the CLI output when the DICOM SCP server is accepting associations.""" 35 result = runner.invoke(app, [scpserver]) 36 37 assert 0 == result.exit_code 38 assert '✅ Success\n' == result.stdout
Test the CLI output when the DICOM SCP server is accepting associations.
def
test_failure() -> None:
41def test_failure() -> None: 42 """Test the CLI when the DICOM SCP server is unreachable.""" 43 result = runner.invoke(app, ['dummy:1234']) 44 45 assert 1 == result.exit_code 46 assert ConnectionError is result.exception.__class__
Test the CLI when the DICOM SCP server is unreachable.
def
test_version() -> None:
49def test_version() -> None: 50 """Test the CLI output when the `--version` flag is provided.""" 51 result = runner.invoke(app, ['--version']) 52 53 assert 0 == result.exit_code 54 assert command in result.stdout 55 assert echo.__version__ in result.stdout
Test the CLI output when the --version
flag is provided.
def
test_version_called_as_module() -> None:
58def test_version_called_as_module() -> None: 59 """Test the CLI output when the utility is invoked as a Python module.""" 60 out = subprocess.check_output([sys.executable, '-m', 'dicom_echo', '--version'], text=True) 61 62 assert 'dicom_echo' in out 63 assert echo.__version__ in out
Test the CLI output when the utility is invoked as a Python module.
def
test_host_aetitle(mock_send_rc0: unittest.mock.MagicMock) -> None:
66def test_host_aetitle(mock_send_rc0: MagicMock) -> None: 67 """Verify the called AE title may be passed with the target address.""" 68 # Arrange 69 called_ae_title = 'dummy' 70 71 # Act 72 runner.invoke(app, [f'{called_ae_title}@localhost:1234']) 73 74 # Assert 75 mock_send_rc0.assert_called_once() 76 assert mock_send_rc0.call_args.kwargs['called_ae_title'] == 'dummy'
Verify the called AE title may be passed with the target address.
def
test_nonzero_response(mock_send_rc1: unittest.mock.MagicMock) -> None:
79def test_nonzero_response(mock_send_rc1: MagicMock) -> None: 80 """Test the CLI output when the DICOM SCP returns a non-zero status code.""" 81 result = runner.invoke(app, ['dummy:1234']) 82 83 assert mock_send_rc1.return_value == result.exit_code 84 assert 'Warning:' in result.stdout 85 assert rich.emoji.Emoji.replace(':warning:') in result.stdout 86 assert str(mock_send_rc1.return_value) in result.stdout
Test the CLI output when the DICOM SCP returns a non-zero status code.