From caaedeb6c892e72c03dbd7d3ca216f1b0371108c Mon Sep 17 00:00:00 2001 From: Nicolas Hug Date: Wed, 17 Mar 2021 11:44:21 +0000 Subject: [PATCH 1/4] Put internet tests in single file --- CONTRIBUTING.md | 3 +++ test/test_datasets_utils.py | 53 ------------------------------------- test/test_videoapi.py | 20 -------------- 3 files changed, 3 insertions(+), 73 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index a825a3c7172..7150427caf6 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -99,6 +99,9 @@ If you would like to run all tests: pytest test -vvv ``` +Tests that require internet access should be in +`test/test_requires_internet.py`. + ### Documentation Torchvision uses [Google style](http://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html) diff --git a/test/test_datasets_utils.py b/test/test_datasets_utils.py index be9299483fc..949026d31cb 100644 --- a/test/test_datasets_utils.py +++ b/test/test_datasets_utils.py @@ -37,18 +37,6 @@ def test_check_integrity(self): self.assertTrue(utils.check_integrity(existing_fpath)) self.assertFalse(utils.check_integrity(nonexisting_fpath)) - def test_get_redirect_url(self): - url = "http://www.vision.caltech.edu/visipedia-data/CUB-200-2011/CUB_200_2011.tgz" - expected = "https://drive.google.com/file/d/1hbzc_P1FuxMkcabkgn9ZKinBwW683j45/view" - - actual = utils._get_redirect_url(url) - assert actual == expected - - 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): - utils._get_redirect_url(url, max_hops=0) - def test_get_google_drive_file_id(self): url = "https://drive.google.com/file/d/1hbzc_P1FuxMkcabkgn9ZKinBwW683j45/view" expected = "1hbzc_P1FuxMkcabkgn9ZKinBwW683j45" @@ -61,47 +49,6 @@ def test_get_google_drive_file_id_invalid_url(self): assert utils._get_google_drive_file_id(url) is None - 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) - except URLError: - msg = "could not download test file '{}'".format(url) - warnings.warn(msg, RuntimeWarning) - raise unittest.SkipTest(msg) - - 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) - except URLError: - msg = "could not download test file '{}'".format(url) - warnings.warn(msg, RuntimeWarning) - raise unittest.SkipTest(msg) - - 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): - 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): - url = "https://drive.google.com/file/d/1hbzc_P1FuxMkcabkgn9ZKinBwW683j45/view" - - id = "1hbzc_P1FuxMkcabkgn9ZKinBwW683j45" - filename = "filename" - md5 = "md5" - - with get_tmp_dir() as root: - utils.download_url(url, root, filename, md5) - - mock.assert_called_once_with(id, root, filename, md5) - def test_detect_file_type(self): for file, expected in [ ("foo.tar.xz", (".tar.xz", ".tar", ".xz")), diff --git a/test/test_videoapi.py b/test/test_videoapi.py index da73c7cd17d..df5bfb041eb 100644 --- a/test/test_videoapi.py +++ b/test/test_videoapi.py @@ -5,7 +5,6 @@ import torch import torchvision from torchvision.io import _HAS_VIDEO_OPT, VideoReader -from torchvision.datasets.utils import download_url from common_utils import PY39_SKIP @@ -23,17 +22,6 @@ CheckerConfig = ["duration", "video_fps", "audio_sample_rate"] GroundTruth = collections.namedtuple("GroundTruth", " ".join(CheckerConfig)) - -def fate(name, path="."): - """Download and return a path to a sample from the FFmpeg test suite. - See the `FFmpeg Automated Test Environment `_ - """ - - file_name = name.split("/")[1] - download_url("http://fate.ffmpeg.org/fate-suite/" + name, path, file_name) - return os.path.join(path, file_name) - - test_videos = { "RATRACE_wave_f_nm_np1_fr_goo_37.avi": GroundTruth( duration=2.0, video_fps=30.0, audio_sample_rate=None @@ -187,14 +175,6 @@ def test_accurateseek_middle(self): ub = duration / 2 + 1 / md[stream]["fps"][0] self.assertTrue((lb <= frame["pts"]) & (ub >= frame["pts"])) - def test_fate_suite(self): - video_path = fate("sub/MovText_capability_tester.mp4", VIDEO_DIR) - vr = VideoReader(video_path) - metadata = vr.get_metadata() - - self.assertTrue(metadata["subtitles"]["duration"] is not None) - os.remove(video_path) - if __name__ == "__main__": unittest.main() From f27974395251e3b1aac742a697cc2cbcab8ddaf1 Mon Sep 17 00:00:00 2001 From: Nicolas Hug Date: Wed, 17 Mar 2021 11:46:34 +0000 Subject: [PATCH 2/4] adding the file --- test/test_internet.py | 99 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 test/test_internet.py diff --git a/test/test_internet.py b/test/test_internet.py new file mode 100644 index 00000000000..d6ad4a0b546 --- /dev/null +++ b/test/test_internet.py @@ -0,0 +1,99 @@ +"""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 +import unittest +import unittest.mock +import warnings +from urllib.error import URLError + +import torch +from torchvision import models +import torchvision.datasets.utils as utils +from torchvision.io import _HAS_VIDEO_OPT, VideoReader +from common_utils import get_tmp_dir +from common_utils import PY39_SKIP + + +class DatasetUtilsTester(unittest.TestCase): + + def test_get_redirect_url(self): + url = "http://www.vision.caltech.edu/visipedia-data/CUB-200-2011/CUB_200_2011.tgz" + expected = "https://drive.google.com/file/d/1hbzc_P1FuxMkcabkgn9ZKinBwW683j45/view" + + actual = utils._get_redirect_url(url) + assert actual == expected + + 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): + 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) + except URLError: + msg = "could not download test file '{}'".format(url) + warnings.warn(msg, RuntimeWarning) + raise unittest.SkipTest(msg) + + 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) + except URLError: + msg = "could not download test file '{}'".format(url) + warnings.warn(msg, RuntimeWarning) + raise unittest.SkipTest(msg) + + 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): + 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): + url = "https://drive.google.com/file/d/1hbzc_P1FuxMkcabkgn9ZKinBwW683j45/view" + + id = "1hbzc_P1FuxMkcabkgn9ZKinBwW683j45" + filename = "filename" + md5 = "md5" + + with get_tmp_dir() as root: + utils.download_url(url, root, filename, md5) + + mock.assert_called_once_with(id, root, filename, md5) + + +@unittest.skipIf(_HAS_VIDEO_OPT is False, "Didn't compile with ffmpeg") +@PY39_SKIP +class VideoAPITester(unittest.TestCase): + + VIDEO_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "assets", "videos") + + def _fate(self, name, path="."): + """Download and return a path to a sample from the FFmpeg test suite. + See the `FFmpeg Automated Test Environment `_ + """ + + file_name = name.split("/")[1] + utils.download_url("http://fate.ffmpeg.org/fate-suite/" + name, path, file_name) + return os.path.join(path, file_name) + + def test_fate_suite(self): + video_path = self._fate("sub/MovText_capability_tester.mp4", self.VIDEO_DIR) + vr = VideoReader(video_path) + metadata = vr.get_metadata() + + self.assertTrue(metadata["subtitles"]["duration"] is not None) + os.remove(video_path) From d81a32028c46e81ac2242954155224530e37a205 Mon Sep 17 00:00:00 2001 From: Nicolas Hug Date: Wed, 17 Mar 2021 11:55:10 +0000 Subject: [PATCH 3/4] edit contirbuting guide --- CONTRIBUTING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 7150427caf6..3fd20df6ca1 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -100,7 +100,7 @@ pytest test -vvv ``` Tests that require internet access should be in -`test/test_requires_internet.py`. +`test/test_internet.py`. ### Documentation From fac95ec986d5325458efc4d752e8fb9e284024e1 Mon Sep 17 00:00:00 2001 From: Nicolas Hug Date: Wed, 17 Mar 2021 13:01:09 +0000 Subject: [PATCH 4/4] restore videoapi test --- test/test_internet.py | 28 ---------------------------- test/test_videoapi.py | 20 ++++++++++++++++++++ 2 files changed, 20 insertions(+), 28 deletions(-) diff --git a/test/test_internet.py b/test/test_internet.py index d6ad4a0b546..05496752c7f 100644 --- a/test/test_internet.py +++ b/test/test_internet.py @@ -11,12 +11,8 @@ import warnings from urllib.error import URLError -import torch -from torchvision import models import torchvision.datasets.utils as utils -from torchvision.io import _HAS_VIDEO_OPT, VideoReader from common_utils import get_tmp_dir -from common_utils import PY39_SKIP class DatasetUtilsTester(unittest.TestCase): @@ -73,27 +69,3 @@ def test_download_url_dispatch_download_from_google_drive(self, mock): utils.download_url(url, root, filename, md5) mock.assert_called_once_with(id, root, filename, md5) - - -@unittest.skipIf(_HAS_VIDEO_OPT is False, "Didn't compile with ffmpeg") -@PY39_SKIP -class VideoAPITester(unittest.TestCase): - - VIDEO_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "assets", "videos") - - def _fate(self, name, path="."): - """Download and return a path to a sample from the FFmpeg test suite. - See the `FFmpeg Automated Test Environment `_ - """ - - file_name = name.split("/")[1] - utils.download_url("http://fate.ffmpeg.org/fate-suite/" + name, path, file_name) - return os.path.join(path, file_name) - - def test_fate_suite(self): - video_path = self._fate("sub/MovText_capability_tester.mp4", self.VIDEO_DIR) - vr = VideoReader(video_path) - metadata = vr.get_metadata() - - self.assertTrue(metadata["subtitles"]["duration"] is not None) - os.remove(video_path) diff --git a/test/test_videoapi.py b/test/test_videoapi.py index df5bfb041eb..da73c7cd17d 100644 --- a/test/test_videoapi.py +++ b/test/test_videoapi.py @@ -5,6 +5,7 @@ import torch import torchvision from torchvision.io import _HAS_VIDEO_OPT, VideoReader +from torchvision.datasets.utils import download_url from common_utils import PY39_SKIP @@ -22,6 +23,17 @@ CheckerConfig = ["duration", "video_fps", "audio_sample_rate"] GroundTruth = collections.namedtuple("GroundTruth", " ".join(CheckerConfig)) + +def fate(name, path="."): + """Download and return a path to a sample from the FFmpeg test suite. + See the `FFmpeg Automated Test Environment `_ + """ + + file_name = name.split("/")[1] + download_url("http://fate.ffmpeg.org/fate-suite/" + name, path, file_name) + return os.path.join(path, file_name) + + test_videos = { "RATRACE_wave_f_nm_np1_fr_goo_37.avi": GroundTruth( duration=2.0, video_fps=30.0, audio_sample_rate=None @@ -175,6 +187,14 @@ def test_accurateseek_middle(self): ub = duration / 2 + 1 / md[stream]["fps"][0] self.assertTrue((lb <= frame["pts"]) & (ub >= frame["pts"])) + def test_fate_suite(self): + video_path = fate("sub/MovText_capability_tester.mp4", VIDEO_DIR) + vr = VideoReader(video_path) + metadata = vr.get_metadata() + + self.assertTrue(metadata["subtitles"]["duration"] is not None) + os.remove(video_path) + if __name__ == "__main__": unittest.main()