Skip to content

Commit

Permalink
Add unit tests for command line interface
Browse files Browse the repository at this point in the history
  • Loading branch information
hackermd committed Apr 16, 2020
1 parent 82af7b0 commit 2304f16
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 10 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
'Development Status :: 4 - Beta',
],
entry_points={
'console_scripts': ['dicomweb_client = dicomweb_client.cli:main'],
'console_scripts': ['dicomweb_client = dicomweb_client.cli:_main'],
},
include_package_data=True,
packages=setuptools.find_packages('src'),
Expand Down
17 changes: 8 additions & 9 deletions src/dicomweb_client/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ def _print_pixel_data(pixels):


def _create_headers(args):
headers = None
headers = {}
if hasattr(args, "bearer_token"):
headers = {
"Authorization": "Bearer {}".format(args.bearer_token)
Expand Down Expand Up @@ -631,10 +631,14 @@ def _store_instances(client, args):
client.store_instances(datasets)


def main():
'''Main entry point for the ``dicomweb_client`` command line program.'''
def _main():
parser = _get_parser()
args = parser.parse_args()
main(args)


def main(args):
'''Main entry point for the ``dicomweb_client`` command line program.'''

configure_logging(args.logging_verbosity)

Expand All @@ -648,7 +652,7 @@ def main():

try:
session = add_certs_to_session(session, args.ca_bundle, args.cert)
session = add_headers_to_session(session, headers=_create_headers(args))
session.headers.update(_create_headers(args))
client = DICOMwebClient(
args.url,
session=session,
Expand All @@ -662,8 +666,3 @@ def main():
tb = traceback.format_exc()
logger.error(tb)
sys.exit(1)


if __name__ == '__main__':

main()
61 changes: 61 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import json
import tempfile

import pytest

from dicomweb_client.api import load_json_dataset
from dicomweb_client.cli import main


def test_parse_search_studies(parser):
args = parser.parse_args([
Expand Down Expand Up @@ -592,3 +596,60 @@ def test_parse_retrieve_bulkdata_missing_argument(parser):
parser.parse_args([
'--url', 'http://localhost:8002', 'retrieve', 'bulkdata'
])


def test_search_for_studies(parser, httpserver, cache_dir, capsys):
cache_filename = str(cache_dir.joinpath('search_for_studies.json'))
with open(cache_filename, 'r') as f:
content = f.read()
headers = {'content-type': 'application/dicom+json'}
httpserver.serve_content(content=content, code=200, headers=headers)
args = parser.parse_args([
'--url', httpserver.url, 'search', 'studies',
])
with pytest.raises(SystemExit) as exit:
main(args)
assert exit.value.code == 0
stdout, stderr = capsys.readouterr()
assert stdout == content


def test_search_for_studies_dicomize(parser, httpserver, cache_dir, capsys):
cache_filename = str(cache_dir.joinpath('search_for_studies.json'))
with open(cache_filename, 'r') as f:
content = f.read()
parsed_content = json.loads(content)
dicomized_content = '\n\n\n'.join([
repr(load_json_dataset(instance))
for instance in parsed_content
])
dicomized_content += '\n\n\n'
headers = {'content-type': 'application/dicom+json'}
httpserver.serve_content(content=content, code=200, headers=headers)
args = parser.parse_args([
'--url', httpserver.url, 'search', 'studies', '--dicomize'
])
with pytest.raises(SystemExit) as exit:
main(args)
assert exit.value.code == 0
stdout, stderr = capsys.readouterr()
assert stdout == dicomized_content


def test_search_for_studies_prettify(parser, httpserver, cache_dir, capsys):
cache_filename = str(cache_dir.joinpath('search_for_studies.json'))
with open(cache_filename, 'r') as f:
content = f.read()
parsed_content = json.loads(content)
prettified_content = json.dumps(parsed_content, indent=4, sort_keys=True)
prettified_content += '\n'
headers = {'content-type': 'application/dicom+json'}
httpserver.serve_content(content=content, code=200, headers=headers)
args = parser.parse_args([
'--url', httpserver.url, 'search', 'studies', '--prettify'
])
with pytest.raises(SystemExit) as exit:
main(args)
assert exit.value.code == 0
stdout, stderr = capsys.readouterr()
assert stdout == prettified_content

0 comments on commit 2304f16

Please sign in to comment.