Skip to content

Commit db6b130

Browse files
authored
Refactor download_url test into TestDownloadUrl class
Refactor test for download_url to use a class-based structure and improve hash validation. Signed-off-by: Enoch Mok <65853622+e-mny@users.noreply.github.com>
1 parent 0d1890d commit db6b130

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

tests/test_utils.py

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -183,32 +183,35 @@ def skip_if_downloading_fails():
183183

184184
raise rt_e
185185

186-
187-
SAMPLE_TIFF = (
188-
https://huggingface.co/datasets/MONAI/testing_data/resolve/main/CMU-1.tiff
189-
)
190-
186+
SAMPLE_TIFF = "https://huggingface.co/datasets/MONAI/testing_data/resolve/main/CMU-1.tiff"
191187
SAMPLE_TIFF_HASH = "73a7e89bc15576587c3d68e55d9bf92f09690280166240b48ff4b48230b13bcd"
192188
SAMPLE_TIFF_HASH_TYPE = "sha256"
193189

194-
def test_download_url():
195-
with tempfile.TemporaryDirectory() as tempdir:
196-
# successful download with correct hash
197-
download_url(
198-
url=SAMPLE_TIFF,
199-
filepath=os.path.join(tempdir, "model.tiff"),
200-
hash_val=SAMPLE_TIFF_HASH,
201-
hash_type=SAMPLE_TIFF_HASH_TYPE,
202-
)
203190

204-
# fails with wrong hash
205-
with unittest.assertRaises(RuntimeError):
206-
download_url(
207-
url=SAMPLE_TIFF,
208-
filepath=os.path.join(tempdir, "model_bad.tiff"),
209-
hash_val="wrong_hash",
210-
hash_type=SAMPLE_TIFF_HASH_TYPE,
211-
)
191+
class TestDownloadUrl(unittest.TestCase):
192+
"""Exercise ``download_url`` success and hash-mismatch paths."""
193+
194+
def test_download_url(self):
195+
"""Download a sample TIFF and validate hash handling.
196+
197+
Raises:
198+
RuntimeError: When the downloaded file's hash does not match.
199+
"""
200+
with tempfile.TemporaryDirectory() as tempdir:
201+
with skip_if_downloading_fails():
202+
download_url(
203+
url=SAMPLE_TIFF,
204+
filepath=os.path.join(tempdir, "model.tiff"),
205+
hash_val=SAMPLE_TIFF_HASH,
206+
hash_type=SAMPLE_TIFF_HASH_TYPE,
207+
)
208+
with self.assertRaises(RuntimeError):
209+
download_url(
210+
url=SAMPLE_TIFF,
211+
filepath=os.path.join(tempdir, "model_bad.tiff"),
212+
hash_val="0" * 64,
213+
hash_type=SAMPLE_TIFF_HASH_TYPE,
214+
)
212215

213216
def test_pretrained_networks(network, input_param, device):
214217
with skip_if_downloading_fails():

0 commit comments

Comments
 (0)