Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

test_internet.py 2.3 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
  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. import pathlib
  8. from urllib.error import URLError
  9. import pytest
  10. import torchvision.datasets.utils as utils
  11. class TestDatasetUtils:
  12. @pytest.mark.parametrize("use_pathlib", (True, False))
  13. def test_download_url(self, tmpdir, use_pathlib):
  14. if use_pathlib:
  15. tmpdir = pathlib.Path(tmpdir)
  16. url = "http://github.com/pytorch/vision/archive/master.zip"
  17. try:
  18. utils.download_url(url, tmpdir)
  19. assert len(os.listdir(tmpdir)) != 0
  20. except URLError:
  21. pytest.skip(f"could not download test file '{url}'")
  22. @pytest.mark.parametrize("use_pathlib", (True, False))
  23. def test_download_url_retry_http(self, tmpdir, use_pathlib):
  24. if use_pathlib:
  25. tmpdir = pathlib.Path(tmpdir)
  26. url = "https://github.com/pytorch/vision/archive/master.zip"
  27. try:
  28. utils.download_url(url, tmpdir)
  29. assert len(os.listdir(tmpdir)) != 0
  30. except URLError:
  31. pytest.skip(f"could not download test file '{url}'")
  32. @pytest.mark.parametrize("use_pathlib", (True, False))
  33. def test_download_url_dont_exist(self, tmpdir, use_pathlib):
  34. if use_pathlib:
  35. tmpdir = pathlib.Path(tmpdir)
  36. url = "http://github.com/pytorch/vision/archive/this_doesnt_exist.zip"
  37. with pytest.raises(URLError):
  38. utils.download_url(url, tmpdir)
  39. @pytest.mark.parametrize("use_pathlib", (True, False))
  40. def test_download_url_dispatch_download_from_google_drive(self, mocker, tmpdir, use_pathlib):
  41. if use_pathlib:
  42. tmpdir = pathlib.Path(tmpdir)
  43. url = "https://drive.google.com/file/d/1GO-BHUYRuvzr1Gtp2_fqXRsr9TIeYbhV/view"
  44. id = "1GO-BHUYRuvzr1Gtp2_fqXRsr9TIeYbhV"
  45. filename = "filename"
  46. md5 = "md5"
  47. mocked = mocker.patch("torchvision.datasets.utils.download_file_from_google_drive")
  48. utils.download_url(url, tmpdir, filename, md5)
  49. mocked.assert_called_once_with(id, os.path.expanduser(tmpdir), filename, md5)
  50. if __name__ == "__main__":
  51. pytest.main([__file__])
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...