Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .circleci/unittest/linux/scripts/environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ channels:
dependencies:
- pytest
- pytest-cov
- pytest-mock
- pip
- libpng
# NOTE: Pinned to fix issues with size_t on Windows
Expand Down
1 change: 1 addition & 0 deletions .circleci/unittest/windows/scripts/environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ channels:
dependencies:
- pytest
- pytest-cov
- pytest-mock
- pip
- libpng
# NOTE: Pinned to fix issues with size_t on Windows
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ python setup.py develop
# MACOSX_DEPLOYMENT_TARGET=10.9 CC=clang CXX=clang++ python setup.py develop
# for C++ debugging, please use DEBUG=1
# DEBUG=1 python setup.py develop
pip install flake8 typing mypy pytest scipy
pip install flake8 typing mypy pytest pytest-mock scipy
```
You may also have to install `libpng-dev` and `libjpeg-turbo8-dev` libraries:
```bash
Expand Down
31 changes: 15 additions & 16 deletions test/test_internet.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@
"""

import os
import unittest
import unittest.mock
import pytest
import warnings
from urllib.error import URLError

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


class DatasetUtilsTester(unittest.TestCase):
class TestDatasetUtils:

def test_get_redirect_url(self):
url = "http://www.vision.caltech.edu/visipedia-data/CUB-200-2011/CUB_200_2011.tgz"
Expand All @@ -26,46 +25,46 @@ def test_get_redirect_url(self):

def test_get_redirect_url_max_hops_exceeded(self):
url = "http://www.vision.caltech.edu/visipedia-data/CUB-200-2011/CUB_200_2011.tgz"
with self.assertRaises(RecursionError):
with pytest.raises(RecursionError):
utils._get_redirect_url(url, max_hops=0)

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)
self.assertFalse(len(os.listdir(temp_dir)) == 0)
assert len(os.listdir(temp_dir)) != 0
except URLError:
msg = "could not download test file '{}'".format(url)
warnings.warn(msg, RuntimeWarning)
raise unittest.SkipTest(msg)
pytest.skip(f"could not download test file '{url}'")

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)
self.assertFalse(len(os.listdir(temp_dir)) == 0)
assert len(os.listdir(temp_dir)) != 0
except URLError:
msg = "could not download test file '{}'".format(url)
warnings.warn(msg, RuntimeWarning)
raise unittest.SkipTest(msg)
pytest.skip(f"could not download test file '{url}'")

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"
with self.assertRaises(URLError):
with pytest.raises(URLError):
utils.download_url(url, temp_dir)

@unittest.mock.patch("torchvision.datasets.utils.download_file_from_google_drive")
def test_download_url_dispatch_download_from_google_drive(self, mock):
def test_download_url_dispatch_download_from_google_drive(self, mocker):
url = "https://drive.google.com/file/d/1hbzc_P1FuxMkcabkgn9ZKinBwW683j45/view"

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

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

mock.assert_called_once_with(id, root, filename, md5)
mocked.assert_called_once_with(id, root, filename, md5)


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