test_resource.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. #!/usr/bin/env python3
  2. # coding=utf-8
  3. from unittest.mock import MagicMock
  4. import pytest
  5. from bdfr.resource import Resource
  6. @pytest.mark.parametrize(('test_url', 'expected'), (
  7. ('test.png', '.png'),
  8. ('another.mp4', '.mp4'),
  9. ('test.jpeg', '.jpeg'),
  10. ('http://www.random.com/resource.png', '.png'),
  11. ('https://www.resource.com/test/example.jpg', '.jpg'),
  12. ('hard.png.mp4', '.mp4'),
  13. ('https://preview.redd.it/7zkmr1wqqih61.png?width=237&format=png&auto=webp&s=19de214e634cbcad99', '.png'),
  14. ('test.jpg#test', '.jpg'),
  15. ('test.jpg?width=247#test', '.jpg'),
  16. ('https://www.test.com/test/test2/example.png?random=test#thing', '.png'),
  17. ))
  18. def test_resource_get_extension(test_url: str, expected: str):
  19. test_resource = Resource(MagicMock(), test_url, lambda: None)
  20. result = test_resource._determine_extension()
  21. assert result == expected
  22. @pytest.mark.online
  23. @pytest.mark.parametrize(('test_url', 'expected_hash'), (
  24. ('https://www.iana.org/_img/2013.1/iana-logo-header.svg', '426b3ac01d3584c820f3b7f5985d6623'),
  25. ))
  26. def test_download_online_resource(test_url: str, expected_hash: str):
  27. test_resource = Resource(MagicMock(), test_url, Resource.retry_download(test_url))
  28. test_resource.download()
  29. assert test_resource.hash.hexdigest() == expected_hash