|
| 1 | +# Copyright (c) MONAI Consortium |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# Unless required by applicable law or agreed to in writing, software |
| 7 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 8 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 9 | +# See the License for the specific language governing permissions and |
| 10 | +# limitations under the License. |
| 11 | + |
| 12 | +from __future__ import annotations |
| 13 | + |
| 14 | +import os |
| 15 | +import tempfile |
| 16 | +import unittest |
| 17 | +from urllib.error import HTTPError |
| 18 | + |
| 19 | +from monai.apps.utils import download_url |
| 20 | + |
| 21 | +YANDEX_MODEL_URL = ( |
| 22 | + "https://cloud-api.yandex.net/v1/disk/public/resources/download?" |
| 23 | + "public_key=https%3A%2F%2Fdisk.yandex.ru%2Fd%2Fxs0gzlj2_irgWA" |
| 24 | +) |
| 25 | +YANDEX_MODEL_FLAWED_URL = ( |
| 26 | + "https://cloud-api.yandex.net/v1/disk/public/resources/download?" |
| 27 | + "public_key=https%3A%2F%2Fdisk.yandex.ru%2Fd%2Fxs0gzlj2_irgWA-url-with-error" |
| 28 | +) |
| 29 | + |
| 30 | + |
| 31 | +class TestDownloadUrlYandex(unittest.TestCase): |
| 32 | + def test_verify(self): |
| 33 | + with tempfile.TemporaryDirectory() as tempdir: |
| 34 | + download_url(url=YANDEX_MODEL_URL, filepath=os.path.join(tempdir, "model.pt")) |
| 35 | + |
| 36 | + def test_verify_error(self): |
| 37 | + with tempfile.TemporaryDirectory() as tempdir: |
| 38 | + with self.assertRaises(HTTPError): |
| 39 | + download_url(url=YANDEX_MODEL_FLAWED_URL, filepath=os.path.join(tempdir, "model.pt")) |
| 40 | + |
| 41 | + |
| 42 | +if __name__ == "__main__": |
| 43 | + unittest.main() |
0 commit comments