Skip to content

Commit 2304f16

Browse files
committed
Add unit tests for command line interface
1 parent 82af7b0 commit 2304f16

File tree

3 files changed

+70
-10
lines changed

3 files changed

+70
-10
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
'Development Status :: 4 - Beta',
3636
],
3737
entry_points={
38-
'console_scripts': ['dicomweb_client = dicomweb_client.cli:main'],
38+
'console_scripts': ['dicomweb_client = dicomweb_client.cli:_main'],
3939
},
4040
include_package_data=True,
4141
packages=setuptools.find_packages('src'),

src/dicomweb_client/cli.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ def _print_pixel_data(pixels):
445445

446446

447447
def _create_headers(args):
448-
headers = None
448+
headers = {}
449449
if hasattr(args, "bearer_token"):
450450
headers = {
451451
"Authorization": "Bearer {}".format(args.bearer_token)
@@ -631,10 +631,14 @@ def _store_instances(client, args):
631631
client.store_instances(datasets)
632632

633633

634-
def main():
635-
'''Main entry point for the ``dicomweb_client`` command line program.'''
634+
def _main():
636635
parser = _get_parser()
637636
args = parser.parse_args()
637+
main(args)
638+
639+
640+
def main(args):
641+
'''Main entry point for the ``dicomweb_client`` command line program.'''
638642

639643
configure_logging(args.logging_verbosity)
640644

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

649653
try:
650654
session = add_certs_to_session(session, args.ca_bundle, args.cert)
651-
session = add_headers_to_session(session, headers=_create_headers(args))
655+
session.headers.update(_create_headers(args))
652656
client = DICOMwebClient(
653657
args.url,
654658
session=session,
@@ -662,8 +666,3 @@ def main():
662666
tb = traceback.format_exc()
663667
logger.error(tb)
664668
sys.exit(1)
665-
666-
667-
if __name__ == '__main__':
668-
669-
main()

tests/test_cli.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
import json
12
import tempfile
23

34
import pytest
45

6+
from dicomweb_client.api import load_json_dataset
7+
from dicomweb_client.cli import main
8+
59

610
def test_parse_search_studies(parser):
711
args = parser.parse_args([
@@ -592,3 +596,60 @@ def test_parse_retrieve_bulkdata_missing_argument(parser):
592596
parser.parse_args([
593597
'--url', 'http://localhost:8002', 'retrieve', 'bulkdata'
594598
])
599+
600+
601+
def test_search_for_studies(parser, httpserver, cache_dir, capsys):
602+
cache_filename = str(cache_dir.joinpath('search_for_studies.json'))
603+
with open(cache_filename, 'r') as f:
604+
content = f.read()
605+
headers = {'content-type': 'application/dicom+json'}
606+
httpserver.serve_content(content=content, code=200, headers=headers)
607+
args = parser.parse_args([
608+
'--url', httpserver.url, 'search', 'studies',
609+
])
610+
with pytest.raises(SystemExit) as exit:
611+
main(args)
612+
assert exit.value.code == 0
613+
stdout, stderr = capsys.readouterr()
614+
assert stdout == content
615+
616+
617+
def test_search_for_studies_dicomize(parser, httpserver, cache_dir, capsys):
618+
cache_filename = str(cache_dir.joinpath('search_for_studies.json'))
619+
with open(cache_filename, 'r') as f:
620+
content = f.read()
621+
parsed_content = json.loads(content)
622+
dicomized_content = '\n\n\n'.join([
623+
repr(load_json_dataset(instance))
624+
for instance in parsed_content
625+
])
626+
dicomized_content += '\n\n\n'
627+
headers = {'content-type': 'application/dicom+json'}
628+
httpserver.serve_content(content=content, code=200, headers=headers)
629+
args = parser.parse_args([
630+
'--url', httpserver.url, 'search', 'studies', '--dicomize'
631+
])
632+
with pytest.raises(SystemExit) as exit:
633+
main(args)
634+
assert exit.value.code == 0
635+
stdout, stderr = capsys.readouterr()
636+
assert stdout == dicomized_content
637+
638+
639+
def test_search_for_studies_prettify(parser, httpserver, cache_dir, capsys):
640+
cache_filename = str(cache_dir.joinpath('search_for_studies.json'))
641+
with open(cache_filename, 'r') as f:
642+
content = f.read()
643+
parsed_content = json.loads(content)
644+
prettified_content = json.dumps(parsed_content, indent=4, sort_keys=True)
645+
prettified_content += '\n'
646+
headers = {'content-type': 'application/dicom+json'}
647+
httpserver.serve_content(content=content, code=200, headers=headers)
648+
args = parser.parse_args([
649+
'--url', httpserver.url, 'search', 'studies', '--prettify'
650+
])
651+
with pytest.raises(SystemExit) as exit:
652+
main(args)
653+
assert exit.value.code == 0
654+
stdout, stderr = capsys.readouterr()
655+
assert stdout == prettified_content

0 commit comments

Comments
 (0)