|
| 1 | +import json |
1 | 2 | import tempfile |
2 | 3 |
|
3 | 4 | import pytest |
4 | 5 |
|
| 6 | +from dicomweb_client.api import load_json_dataset |
| 7 | +from dicomweb_client.cli import main |
| 8 | + |
5 | 9 |
|
6 | 10 | def test_parse_search_studies(parser): |
7 | 11 | args = parser.parse_args([ |
@@ -592,3 +596,60 @@ def test_parse_retrieve_bulkdata_missing_argument(parser): |
592 | 596 | parser.parse_args([ |
593 | 597 | '--url', 'http://localhost:8002', 'retrieve', 'bulkdata' |
594 | 598 | ]) |
| 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