Skip to content

Commit d02da49

Browse files
committed
Refactor download_url test into TestDownloadUrl class
1 parent 57fdd59 commit d02da49

File tree

2 files changed

+66
-31
lines changed

2 files changed

+66
-31
lines changed

tests/test_utils.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
nib, _ = optional_import("nibabel")
5858
http_error, has_req = optional_import("requests", name="HTTPError")
5959
file_url_error, has_gdown = optional_import("gdown.exceptions", name="FileURLRetrievalError")
60+
hf_http_error, has_hf_hub = optional_import("huggingface_hub.errors", name="HfHubHTTPError")
61+
hf_local_entry_error, _has_hf_local = optional_import("huggingface_hub.errors", name="LocalEntryNotFoundError")
6062

6163

6264
quick_test_var = "QUICKTEST"
@@ -70,6 +72,8 @@
7072
DOWNLOAD_EXCEPTS += (http_error,)
7173
if has_gdown:
7274
DOWNLOAD_EXCEPTS += (file_url_error,)
75+
if has_hf_hub:
76+
DOWNLOAD_EXCEPTS += (hf_http_error, hf_local_entry_error)
7377

7478
DOWNLOAD_FAIL_MSGS = (
7579
"unexpected EOF", # incomplete download
@@ -180,6 +184,37 @@ def skip_if_downloading_fails():
180184
raise rt_e
181185

182186

187+
SAMPLE_TIFF = "https://huggingface.co/datasets/MONAI/testing_data/resolve/main/CMU-1.tiff"
188+
SAMPLE_TIFF_HASH = "73a7e89bc15576587c3d68e55d9bf92f09690280166240b48ff4b48230b13bcd"
189+
SAMPLE_TIFF_HASH_TYPE = "sha256"
190+
191+
192+
class TestDownloadUrl(unittest.TestCase):
193+
"""Exercise ``download_url`` success and hash-mismatch paths."""
194+
195+
def test_download_url(self):
196+
"""Download a sample TIFF and validate hash handling.
197+
198+
Raises:
199+
RuntimeError: When the downloaded file's hash does not match.
200+
"""
201+
with tempfile.TemporaryDirectory() as tempdir:
202+
with skip_if_downloading_fails():
203+
download_url(
204+
url=SAMPLE_TIFF,
205+
filepath=os.path.join(tempdir, "model.tiff"),
206+
hash_val=SAMPLE_TIFF_HASH,
207+
hash_type=SAMPLE_TIFF_HASH_TYPE,
208+
)
209+
with self.assertRaises(RuntimeError):
210+
download_url(
211+
url=SAMPLE_TIFF,
212+
filepath=os.path.join(tempdir, "model_bad.tiff"),
213+
hash_val="0" * 64,
214+
hash_type=SAMPLE_TIFF_HASH_TYPE,
215+
)
216+
217+
183218
def test_pretrained_networks(network, input_param, device):
184219
with skip_if_downloading_fails():
185220
return network(**input_param).to(device)
Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,87 @@
11
{
22
"images": {
33
"wsi_generic_tiff": {
4-
"url": "https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/CMU-1.tiff",
4+
"url": "https://huggingface.co/datasets/MONAI/testing_data/resolve/main/CMU-1.tiff",
55
"hash_type": "sha256",
66
"hash_val": "73a7e89bc15576587c3d68e55d9bf92f09690280166240b48ff4b48230b13bcd"
77
},
88
"wsi_aperio_svs": {
9-
"url": "https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/Aperio-CMU-1.svs",
9+
"url": "https://huggingface.co/datasets/MONAI/testing_data/resolve/main/Aperio-CMU-1.svs",
1010
"hash_type": "sha256",
1111
"hash_val": "00a3d54482cd707abf254fe69dccc8d06b8ff757a1663f1290c23418c480eb30"
1212
},
1313
"favicon": {
14-
"url": "https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/favicon.ico.zip",
14+
"url": "https://huggingface.co/datasets/MONAI/testing_data/resolve/main/favicon.ico.zip",
1515
"hash_type": "sha256",
1616
"hash_val": "3a3635c8d8adb81feebc5926b4106e8eb643a24a4be2a69a9d35f9a578acadb5"
1717
},
1818
"icon": {
19-
"url": "https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/icon.tar.gz",
19+
"url": "https://huggingface.co/datasets/MONAI/testing_data/resolve/main/icon.tar.gz",
2020
"hash_type": "sha256",
2121
"hash_val": "90f24cd8f20f3932624da95190ce384302261acf0ea15b358f7832e3b6becac0"
2222
},
2323
"mednist": {
24-
"url": "https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/MedNIST.tar.gz",
24+
"url": "https://huggingface.co/datasets/MONAI/testing_data/resolve/main/MedNIST.tar.gz",
2525
"hash_type": "sha256",
2626
"hash_val": "f2f4881ff8799a170b10a403495f0ce0ad7486491901cde67a647e6627e7f916"
2727
},
2828
"Prostate_T2W_AX_1": {
29-
"url": "https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/Prostate_T2W_AX_1.nii",
29+
"url": "https://huggingface.co/datasets/MONAI/testing_data/resolve/main/Prostate_T2W_AX_1.nii",
3030
"hash_type": "sha256",
3131
"hash_val": "a14231f539c0f365a5f83f2a046969a9b9870e56ffd126fd8e7242364d25938a"
3232
},
3333
"0000_t2_tse_tra_4": {
34-
"url": "https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/ProstateX-0000_t2_tse_tra_4.nii.gz",
34+
"url": "https://huggingface.co/datasets/MONAI/testing_data/resolve/main/ProstateX-0000_t2_tse_tra_4.nii.gz",
3535
"hash_type": "md5",
3636
"hash_val": "adb3f1c4db66a6481c3e4a2a3033c7d5"
3737
},
3838
"0000_ep2d_diff_tra_7": {
39-
"url": "https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/ProstateX-0000_ep2d_diff_tra_7.nii.gz",
39+
"url": "https://huggingface.co/datasets/MONAI/testing_data/resolve/main/ProstateX-0000_ep2d_diff_tra_7.nii.gz",
4040
"hash_type": "md5",
4141
"hash_val": "f12a11ad0ebb0b1876e9e010564745d2"
4242
},
4343
"ref_avg152T1_LR": {
44-
"url": "https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/avg152T1_LR_nifti.nii.gz",
44+
"url": "https://huggingface.co/datasets/MONAI/testing_data/resolve/main/avg152T1_LR_nifti.nii.gz",
4545
"hash_type": "sha256",
4646
"hash_val": "c01a50caa7a563158ecda43d93a1466bfc8aa939bc16b06452ac1089c54661c8"
4747
},
4848
"ref_avg152T1_RL": {
49-
"url": "https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/avg152T1_RL_nifti.nii.gz",
49+
"url": "https://huggingface.co/datasets/MONAI/testing_data/resolve/main/avg152T1_RL_nifti.nii.gz",
5050
"hash_type": "sha256",
5151
"hash_val": "8a731128dac4de46ccb2cc60d972b98f75a52f21fb63ddb040ca96f0aed8b51a"
5252
},
5353
"MNI152_T1_2mm": {
54-
"url": "https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/MNI152_T1_2mm.nii.gz",
54+
"url": "https://huggingface.co/datasets/MONAI/testing_data/resolve/main/MNI152_T1_2mm.nii.gz",
5555
"hash_type": "sha256",
5656
"hash_val": "0585cd056bf5ccfb8bf97a5f6a66082d4e7caad525718fc11e40d80a827fcb92"
5757
},
5858
"MNI152_T1_2mm_strucseg": {
59-
"url": "https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/MNI152_T1_2mm_strucseg.nii.gz",
59+
"url": "https://huggingface.co/datasets/MONAI/testing_data/resolve/main/MNI152_T1_2mm_strucseg.nii.gz",
6060
"hash_type": "sha256",
6161
"hash_val": "eb4f1e596ca85aadaefc359d409fb9a3e27d733e6def04b996953b7c54bc26d4"
6262
},
6363
"copd1_highres_INSP_STD_COPD_img": {
64-
"url": "https://data.kitware.com/api/v1/file/62a0f067bddec9d0c4175c5a/download",
64+
"url": "https://huggingface.co/datasets/MONAI/testing_data/resolve/main/copd1_highres_INSP_STD_COPD_img.nii.gz",
6565
"hash_type": "sha512",
6666
"hash_val": "60193cd6ef0cf055c623046446b74f969a2be838444801bd32ad5bedc8a7eeecb343e8a1208769c9c7a711e101c806a3133eccdda7790c551a69a64b9b3701e9"
6767
},
6868
"copd1_highres_EXP_STD_COPD_img": {
69-
"url": "https://data.kitware.com/api/v1/item/62a0f045bddec9d0c4175c44/download",
69+
"url": "https://huggingface.co/datasets/MONAI/testing_data/resolve/main/copd1_highres_EXP_STD_COPD_img.nii.gz",
7070
"hash_type": "sha512",
7171
"hash_val": "841ef303958541474e66c2d1ccdc8b7ed17ba2f2681101307766b979a07979f2ec818ddf13791c3f1ac5a8ec3258d6ea45b692b4b4a838de9188602618972b6d"
7272
},
7373
"CT_2D_head_fixed": {
74-
"url": "https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/CT_2D_head_fixed.mha",
74+
"url": "https://huggingface.co/datasets/MONAI/testing_data/resolve/main/CT_2D_head_fixed.mha",
7575
"hash_type": "sha256",
7676
"hash_val": "06f2ce6fbf6a59f0874c735555fcf71717f631156b1b0697c1752442f7fc1cc5"
7777
},
7878
"CT_2D_head_moving": {
79-
"url": "https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/CT_2D_head_moving.mha",
79+
"url": "https://huggingface.co/datasets/MONAI/testing_data/resolve/main/CT_2D_head_moving.mha",
8080
"hash_type": "sha256",
8181
"hash_val": "a37c5fe388c38b3f4ac564f456277d09d3982eda58c4da05ead8ee2332360f47"
8282
},
8383
"DICOM_single": {
84-
"url": "https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/CT_DICOM_SINGLE.zip",
84+
"url": "https://huggingface.co/datasets/MONAI/testing_data/resolve/main/CT_DICOM_SINGLE.zip",
8585
"hash_type": "sha256",
8686
"hash_val": "a41f6e93d2e3d68956144f9a847273041d36441da12377d6a1d5ae610e0a7023"
8787
},
@@ -93,69 +93,69 @@
9393
},
9494
"videos": {
9595
"endovis": {
96-
"url": "https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/d1_im.mp4",
96+
"url": "https://huggingface.co/datasets/MONAI/testing_data/resolve/main/d1_im.mp4",
9797
"hash_type": "md5",
9898
"hash_val": "9b103c07326439b0ea376018d7189384"
9999
},
100100
"ultrasound": {
101-
"url": "https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/example_data_Ultrasound_Q000_04_tu_segmented_ultrasound_256.avi",
101+
"url": "https://huggingface.co/datasets/MONAI/testing_data/resolve/main/example_data_Ultrasound_Q000_04_tu_segmented_ultrasound_256.avi",
102102
"hash_type": "md5",
103103
"hash_val": "f0755960cc4a08a958561cda9a79a157"
104104
}
105105
},
106106
"models": {
107107
"senet154-c7b49a05": {
108-
"url": "https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/senet154-c7b49a05.pth",
108+
"url": "https://huggingface.co/datasets/MONAI/testing_data/resolve/main/senet154-c7b49a05.pth",
109109
"hash_type": "sha256",
110110
"hash_val": "c7b49a056b98b0bed65b0237c27acdead655e599669215573d357ad337460413"
111111
},
112112
"se_resnet101-7e38fcc6": {
113-
"url": "https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/se_resnet101-7e38fcc6.pth",
113+
"url": "https://huggingface.co/datasets/MONAI/testing_data/resolve/main/se_resnet101-7e38fcc6.pth",
114114
"hash_type": "sha256",
115115
"hash_val": "7e38fcc64eff3225a3ea4e6081efeb6087e8d5a61c204d94edc2ed1aab0b9d70"
116116
},
117117
"se_resnet152-d17c99b7": {
118-
"url": "https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/se_resnet152-d17c99b7.pth",
118+
"url": "https://huggingface.co/datasets/MONAI/testing_data/resolve/main/se_resnet152-d17c99b7.pth",
119119
"hash_type": "sha256",
120120
"hash_val": "d17c99b703dcca2d2507ddfb68f72625a2f7e23ee64396eb992f1b2cf7e6bdc1"
121121
},
122122
"se_resnet50-ce0d4300": {
123-
"url": "https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/se_resnet50-ce0d4300.pth",
123+
"url": "https://huggingface.co/datasets/MONAI/testing_data/resolve/main/se_resnet50-ce0d4300.pth",
124124
"hash_type": "sha256",
125125
"hash_val": "ce0d430017d3f4aa6b5658c72209f3bfffb060207fd26a2ef0b203ce592eba01"
126126
},
127127
"se_resnext101_32x4d-3b2fe3d8": {
128-
"url": "https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/se_resnext101_32x4d-3b2fe3d8.pth",
128+
"url": "https://huggingface.co/datasets/MONAI/testing_data/resolve/main/se_resnext101_32x4d-3b2fe3d8.pth",
129129
"hash_type": "sha256",
130130
"hash_val": "3b2fe3d8acb8de7d5976c4baf518f24a0237509272a69366e816682d3e57b989"
131131
},
132132
"se_resnext50_32x4d-a260b3a4": {
133-
"url": "https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/se_resnext50_32x4d-a260b3a4.pth",
133+
"url": "https://huggingface.co/datasets/MONAI/testing_data/resolve/main/se_resnext50_32x4d-a260b3a4.pth",
134134
"hash_type": "sha256",
135135
"hash_val": "a260b3a40f82dfe37c58d26a612bcf7bef0d27c6fed096226b0e4e9fb364168e"
136136
},
137137
"ssl_pretrained_weights": {
138-
"url": "https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/ssl_pretrained_weights.pth",
138+
"url": "https://huggingface.co/datasets/MONAI/testing_data/resolve/main/ssl_pretrained_weights.pth",
139139
"hash_type": "sha256",
140140
"hash_val": "c3564f40a6a051d3753a6d8fae5cc8eaf21ce8d82a9a3baf80748d15664055e8"
141141
},
142142
"decoder_only_transformer_monai_generative_weights": {
143-
"url": "https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/decoder_only_transformer.pth",
143+
"url": "https://huggingface.co/datasets/MONAI/testing_data/resolve/main/decoder_only_transformer.pth",
144144
"hash_type": "sha256",
145145
"hash_val": "f93de37d64d77cf91f3bde95cdf93d161aee800074c89a92aff9d5699120ec0d"
146146
},
147147
"diffusion_model_unet_monai_generative_weights": {
148-
"url": "https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/diffusion_model_unet.pth",
148+
"url": "https://huggingface.co/datasets/MONAI/testing_data/resolve/main/diffusion_model_unet.pth",
149149
"hash_type": "sha256",
150150
"hash_val": "0d2171b386902f5b4fd3e967b4024f63e353694ca45091b114970019d045beee"
151151
},
152152
"autoencoderkl_monai_generative_weights": {
153-
"url": "https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/autoencoderkl.pth",
153+
"url": "https://huggingface.co/datasets/MONAI/testing_data/resolve/main/autoencoderkl.pth",
154154
"hash_type": "sha256",
155155
"hash_val": "6e02c9540c51b16b9ba98b5c0c75d6b84b430afe9a3237df1d67a520f8d34184"
156156
},
157157
"controlnet_monai_generative_weights": {
158-
"url": "https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/controlnet.pth",
158+
"url": "https://huggingface.co/datasets/MONAI/testing_data/resolve/main/controlnet.pth",
159159
"hash_type": "sha256",
160160
"hash_val": "cd100d0c69f47569ae5b4b7df653a1cb19f5e02eff1630db3210e2646fb1ab2e"
161161
}
@@ -167,4 +167,4 @@
167167
"hash_val": "06954cad2cc5d3784e72077ac76f0fc8"
168168
}
169169
}
170-
}
170+
}

0 commit comments

Comments
 (0)