"vscode:/vscode.git/clone" did not exist on "b3c2cfceb35f8c962bf596f81ea6c5dcc47ed39a"
test_internet.py 1.73 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
import warnings
from urllib.error import URLError

import torchvision.datasets.utils as utils


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

    def test_download_url_retry_http(self, tmpdir):
        url = "https://github.com/pytorch/vision/archive/master.zip"
        try:
            utils.download_url(url, tmpdir)
            assert len(os.listdir(tmpdir)) != 0
        except URLError:
            pytest.skip(f"could not download test file '{url}'")

    def test_download_url_dont_exist(self, tmpdir):
        url = "http://github.com/pytorch/vision/archive/this_doesnt_exist.zip"
        with pytest.raises(URLError):
            utils.download_url(url, tmpdir)

    def test_download_url_dispatch_download_from_google_drive(self, mocker, tmpdir):
39
40
41
42
43
44
        url = "https://drive.google.com/file/d/1hbzc_P1FuxMkcabkgn9ZKinBwW683j45/view"

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

45
        mocked = mocker.patch('torchvision.datasets.utils.download_file_from_google_drive')
46
        utils.download_url(url, tmpdir, filename, md5)
47

48
        mocked.assert_called_once_with(id, tmpdir, filename, md5)
49
50
51
52


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