"vscode:/vscode.git/clone" did not exist on "a1c6fe2d2428cb8a1b4a9e11a9a5075a4bccecd1"
test_internet.py 2.29 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
limm's avatar
limm committed
9
import pathlib
10
11
from urllib.error import URLError

limm's avatar
limm committed
12
import pytest
13
14
15
import torchvision.datasets.utils as utils


limm's avatar
limm committed
16
17
18
19
20
21
22
23
24
25
26
class TestDatasetUtils:
    @pytest.mark.parametrize("use_pathlib", (True, False))
    def test_download_url(self, tmpdir, use_pathlib):
        if use_pathlib:
            tmpdir = pathlib.Path(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}'")
27

limm's avatar
limm committed
28
29
30
31
32
33
34
35
36
37
    @pytest.mark.parametrize("use_pathlib", (True, False))
    def test_download_url_retry_http(self, tmpdir, use_pathlib):
        if use_pathlib:
            tmpdir = pathlib.Path(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}'")
38

limm's avatar
limm committed
39
40
41
42
43
44
45
    @pytest.mark.parametrize("use_pathlib", (True, False))
    def test_download_url_dont_exist(self, tmpdir, use_pathlib):
        if use_pathlib:
            tmpdir = pathlib.Path(tmpdir)
        url = "http://github.com/pytorch/vision/archive/this_doesnt_exist.zip"
        with pytest.raises(URLError):
            utils.download_url(url, tmpdir)
46

limm's avatar
limm committed
47
48
49
50
51
    @pytest.mark.parametrize("use_pathlib", (True, False))
    def test_download_url_dispatch_download_from_google_drive(self, mocker, tmpdir, use_pathlib):
        if use_pathlib:
            tmpdir = pathlib.Path(tmpdir)
        url = "https://drive.google.com/file/d/1GO-BHUYRuvzr1Gtp2_fqXRsr9TIeYbhV/view"
52

limm's avatar
limm committed
53
        id = "1GO-BHUYRuvzr1Gtp2_fqXRsr9TIeYbhV"
54
55
56
        filename = "filename"
        md5 = "md5"

limm's avatar
limm committed
57
58
59
60
61
        mocked = mocker.patch("torchvision.datasets.utils.download_file_from_google_drive")
        utils.download_url(url, tmpdir, filename, md5)

        mocked.assert_called_once_with(id, os.path.expanduser(tmpdir), filename, md5)

62

limm's avatar
limm committed
63
64
if __name__ == "__main__":
    pytest.main([__file__])