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