def test_url_builder(): """should build correct urls""" requests = requests_util.Requests(URL, None) expected_url = URL + "/test?param=1" check.equal(requests.build_url("test", "param=1"), expected_url) check.equal(requests.build_url("test", "?param=1"), expected_url) check.equal(requests.build_url("/test", "?param=1"), expected_url)
def test_auth(): """header should be added""" authenticator = Authenticator() httpretty.register_uri( httpretty.GET, URL + "/", body=authenticator.request_callback ) requests = requests_util.Requests(URL, authenticator) requests.request("", "", {})
def test_upload(): """file should be uploaded correctly""" requests = requests_util.Requests(URL, None) httpretty.register_uri( httpretty.POST, URL + "/studies", body=request_callback ) assert requests.upload_dicom("./cloudBuild/dcms/1.dcm") == {'transferred': 8706,\ 'message': './cloudBuild/dcms/1.dcm uploaded as https://healthcare.googleapis.com/v1beta1/\ projects/healthcare/locations/europe-west2/datasets/exampl/dicomStores/store/dicomWeb/studies/1'}
def test_delete(self): """files should be deleted correctly""" requests = requests_util.Requests(URL, None) for url, status in DELETE_CASES.items(): httpretty.register_uri( httpretty.DELETE, URL + url, status=status ) if status == 200: requests.delete_dicom(url) else: with self.assertRaises(requests_util.NetworkError): requests.delete_dicom(url)
def test_download_dicom_by_ids(): """should download correct file by json based dict""" httpretty.register_uri( httpretty.GET, URL + "/studies/1/series/2/instances/6", body="idsData", adding_headers={ 'Content-Type': 'application/dicom'} ) requests = requests_util.Requests(URL, None) requests.download_dicom_by_ids({'study_id': '1', 'series_id': '2', 'instance_id': '6', }, "./testData", None) assert os.path.isfile("./testData/1/2/6.dcm") file = open("./testData/1/2/6.dcm", 'r') data = file.read() assert data == "idsData"
def test_download_path(): """should download correct file by path""" httpretty.register_uri( httpretty.GET, URL + "/studies/1/series/2/instances/3", body="3.dcm", adding_headers={ 'Content-Type': 'application/dicom'} ) requests = requests_util.Requests(URL, None) requests.download_dicom_by_ids(resources.ids_from_path( "studies/1/series/2/instances/3"), "./testData", None) assert os.path.isfile("./testData/1/2/3.dcm") file = open("./testData/1/2/3.dcm", 'r') data = file.read() assert data == "3.dcm"
def test_requests_handling(self): """url should be validated""" httpretty.register_uri( httpretty.GET, URL + "/", body='body' ) httpretty.register_uri( httpretty.GET, URL + "/not200", body='404', status=404 ) requests = requests_util.Requests(URL, None) assert requests.request("", "", {}).text == "body" with self.assertRaises(requests_util.NetworkError): print(requests.request("not200", "", {}))
def test_download_instance_json(): """should download correct file by json based dict""" httpretty.register_uri( httpretty.GET, URL + "/studies/3/series/4/instances/5", body="5.dcm", adding_headers={ 'Content-Type': 'application/dicom'} ) requests = requests_util.Requests(URL, None) requests.download_dicom_by_ids(resources.ids_from_json( {resources.STUDY_TAG: {"Value": ["3"]}, resources.SERIES_TAG: { "Value": ["4"]}, resources.INSTANCE_TAG: {"Value": ["5"]}}), "./testData", None) assert os.path.isfile("./testData/1/2/3.dcm") file = open("./testData/3/4/5.dcm", 'r') data = file.read() assert data == "5.dcm"
def test_download_multipart(): """should download correct file by json based dict""" mock_data = [b'1C\r\n--123 Content-Type:image/png\r\n4\r\ndata\r\n1C\r\n\ --123 Content-Type:image/png\r\n5\r\ndata2\r\n5\r\n--123\r\n0\r\n\r\n'] httpretty.register_uri( httpretty.GET, URL + "/studies/6/series/7/instances/8", body=(l for l in mock_data), adding_headers={ 'Content-Type': 'multipart/related; type="image/png"; boundary=123;', 'transfer-encoding': 'chunked'}, streaming=True ) requests = requests_util.Requests(URL, None) requests.download_dicom_by_ids(resources.ids_from_path( "studies/6/series/7/instances/8"), "./testData", "image/png") assert os.path.isfile("./testData/6/7/8_frame_1.png") assert os.path.isfile("./testData/6/7/8_frame_2.png") file = open("./testData/6/7/8_frame_2.png", 'r') data = file.read() assert data == 'data2'
def test_multipart_reader(): """should download correct file by json based dict""" mock_data = [b'1C\r\n--321 Content-Type:image/png\r\n4\r\ndata\r\n4\r\ndata\r\n1C\r\n\ --321 Content-Type:image/png\r\n5\r\ndata2\r\n5\r\n--321\r\n0\r\n\r\n'] httpretty.register_uri( httpretty.GET, URL + "/studies/6/series/7/instances/8", body=(l for l in mock_data), adding_headers={ 'Content-Type': 'multipart/related; type="image/png"; boundary=321;', 'transfer-encoding': 'chunked'}, streaming=True ) requests = requests_util.Requests(URL, None) response = requests.request("/studies/6/series/7/instances/8", "", {}, True) chunks = requests_util.MultipartChunksReader( response.iter_content(chunk_size=8192), bytes("--321", "utf-8")) chunks_expected = [(b'data', True), (b'data', False), (b'data2', True)] i = 0 for chunk in chunks.read_chunks(): assert chunk == chunks_expected[i] i += 1