test_internet.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. """This file should contain all tests that need access to the internet (apart
  2. from the ones in test_datasets_download.py)
  3. We want to bundle all internet-related tests in one file, so the file can be
  4. cleanly ignored in FB internal test infra.
  5. """
  6. import os
  7. from urllib.error import URLError
  8. import pytest
  9. import torchvision.datasets.utils as utils
  10. class TestDatasetUtils:
  11. def test_download_url(self, tmpdir):
  12. url = "http://github.com/pytorch/vision/archive/master.zip"
  13. try:
  14. utils.download_url(url, tmpdir)
  15. assert len(os.listdir(tmpdir)) != 0
  16. except URLError:
  17. pytest.skip(f"could not download test file '{url}'")
  18. def test_download_url_retry_http(self, tmpdir):
  19. url = "https://github.com/pytorch/vision/archive/master.zip"
  20. try:
  21. utils.download_url(url, tmpdir)
  22. assert len(os.listdir(tmpdir)) != 0
  23. except URLError:
  24. pytest.skip(f"could not download test file '{url}'")
  25. def test_download_url_dont_exist(self, tmpdir):
  26. url = "http://github.com/pytorch/vision/archive/this_doesnt_exist.zip"
  27. with pytest.raises(URLError):
  28. utils.download_url(url, tmpdir)
  29. def test_download_url_dispatch_download_from_google_drive(self, mocker, tmpdir):
  30. url = "https://drive.google.com/file/d/1GO-BHUYRuvzr1Gtp2_fqXRsr9TIeYbhV/view"
  31. id = "1GO-BHUYRuvzr1Gtp2_fqXRsr9TIeYbhV"
  32. filename = "filename"
  33. md5 = "md5"
  34. mocked = mocker.patch("torchvision.datasets.utils.download_file_from_google_drive")
  35. utils.download_url(url, tmpdir, filename, md5)
  36. mocked.assert_called_once_with(id, tmpdir, filename, md5)
  37. if __name__ == "__main__":
  38. pytest.main([__file__])