test_download.py 4.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest

17
from paddle.utils.download import get_weights_path_from_url
S
Steffy-zxf 已提交
18
from paddle.utils.download import get_path_from_url
19 20 21 22 23 24 25


class TestDownload(unittest.TestCase):
    def download(self, url, md5sum):
        get_weights_path_from_url(url, md5sum)

    def test_download_model(self):
26 27
        url = 'https://paddle-hapi.bj.bcebos.com/unittest/single_file.pdparams'
        md5sum = 'd41d8cd98f00b204e9800998ecf8427e'
28 29 30
        self.download(url, md5sum)

    def test_exist_download(self):
31 32
        url = 'https://paddle-hapi.bj.bcebos.com/unittest/single_file.pdparams'
        md5sum = 'd41d8cd98f00b204e9800998ecf8427e'
33 34 35
        self.download(url, md5sum)

    def test_download_without_md5sum(self):
36
        url = 'https://paddle-hapi.bj.bcebos.com/unittest/single_file.pdparams'
37 38 39 40
        self.download(url, None)

    def test_download_errors(self):
        with self.assertRaises(RuntimeError):
41
            url = 'https://paddle-hapi.bj.bcebos.com/unittest/single_file.pdparams'
42 43 44 45 46 47 48
            md5sum = '8ff74f291f72533f2a7956a4eftttttt'
            self.download(url, md5sum)

        with self.assertRaises(RuntimeError):
            url = 'https://paddle-hapi.bj.bcebos.com/models/mobilenet_v2_x1.0t.pdparams'
            self.download(url, None)

L
LielinJiang 已提交
49 50 51 52 53 54 55 56 57 58 59 60
    def test_download_and_uncompress(self):
        urls = [
            "https://paddle-hapi.bj.bcebos.com/unittest/files.tar",
            "https://paddle-hapi.bj.bcebos.com/unittest/files.zip",
            "https://paddle-hapi.bj.bcebos.com/unittest/single_dir.tar",
            "https://paddle-hapi.bj.bcebos.com/unittest/single_dir.zip",
            "https://paddle-hapi.bj.bcebos.com/unittest/single_file.tar",
            "https://paddle-hapi.bj.bcebos.com/unittest/single_file.zip",
        ]
        for url in urls:
            self.download(url, None)

S
Steffy-zxf 已提交
61 62 63 64 65 66 67 68 69 70 71 72
    def test_get_path_from_url(self):
        urls = [
            "https://paddle-hapi.bj.bcebos.com/unittest/files.tar",
            "https://paddle-hapi.bj.bcebos.com/unittest/files.zip",
            "https://paddle-hapi.bj.bcebos.com/unittest/single_dir.tar",
            "https://paddle-hapi.bj.bcebos.com/unittest/single_dir.zip",
            "https://paddle-hapi.bj.bcebos.com/unittest/single_file.tar",
            "https://paddle-hapi.bj.bcebos.com/unittest/single_file.zip",
        ]
        for url in urls:
            get_path_from_url(url, root_dir='./test')

73 74 75 76 77 78 79
    def test_retry_exception(self, ):
        with self.assertRaises(RuntimeError):
            from paddle.utils.download import _download
            _download(
                'www.baidu.com',
                './test', )

W
Wenyu 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    def test_wget_download_error(self, ):
        with self.assertRaises(RuntimeError):
            from paddle.utils.download import _download
            _download('www.baidu', './test', method='wget')

    def test_download_methods(self, ):
        urls = [
            "https://paddle-hapi.bj.bcebos.com/unittest/files.tar",
            "https://paddle-hapi.bj.bcebos.com/unittest/files.zip",
        ]

        import sys
        from paddle.utils.download import _download
        if sys.platform == 'linux':
            methods = ['wget', 'get']
        else:
            methods = ['get']

        for url in urls:
            for method in methods:
                _download(
                    url,
                    path='./test',
                    method=method, )

105 106 107

if __name__ == '__main__':
    unittest.main()