test_internet.py 1.96 KB
Newer Older
1
2
3
4
5
6
7
8
"""This file should contain all tests that need access to the internet (apart
from the ones in test_datasets_download.py)

We want to bundle all internet-related tests in one file, so the file can be
cleanly ignored in FB internal test infra.
"""

import os
9
import pytest
10
11
12
13
14
15
16
import warnings
from urllib.error import URLError

import torchvision.datasets.utils as utils
from common_utils import get_tmp_dir


17
class TestDatasetUtils:
18
19
20
21
22
    def test_download_url(self):
        with get_tmp_dir() as temp_dir:
            url = "http://github.com/pytorch/vision/archive/master.zip"
            try:
                utils.download_url(url, temp_dir)
23
                assert len(os.listdir(temp_dir)) != 0
24
            except URLError:
25
                pytest.skip(f"could not download test file '{url}'")
26
27
28
29
30
31

    def test_download_url_retry_http(self):
        with get_tmp_dir() as temp_dir:
            url = "https://github.com/pytorch/vision/archive/master.zip"
            try:
                utils.download_url(url, temp_dir)
32
                assert len(os.listdir(temp_dir)) != 0
33
            except URLError:
34
                pytest.skip(f"could not download test file '{url}'")
35
36
37
38

    def test_download_url_dont_exist(self):
        with get_tmp_dir() as temp_dir:
            url = "http://github.com/pytorch/vision/archive/this_doesnt_exist.zip"
39
            with pytest.raises(URLError):
40
41
                utils.download_url(url, temp_dir)

42
    def test_download_url_dispatch_download_from_google_drive(self, mocker):
43
44
45
46
47
48
        url = "https://drive.google.com/file/d/1hbzc_P1FuxMkcabkgn9ZKinBwW683j45/view"

        id = "1hbzc_P1FuxMkcabkgn9ZKinBwW683j45"
        filename = "filename"
        md5 = "md5"

49
        mocked = mocker.patch('torchvision.datasets.utils.download_file_from_google_drive')
50
51
52
        with get_tmp_dir() as root:
            utils.download_url(url, root, filename, md5)

53
54
55
56
57
        mocked.assert_called_once_with(id, root, filename, md5)


if __name__ == '__main__':
    pytest.main([__file__])