test_imgur.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #!/usr/bin/env python3
  2. # coding=utf-8
  3. from unittest.mock import Mock
  4. import pytest
  5. from bdfr.exceptions import SiteDownloaderError
  6. from bdfr.resource import Resource
  7. from bdfr.site_downloaders.imgur import Imgur
  8. @pytest.mark.online
  9. @pytest.mark.parametrize(('test_url', 'expected_gen_dict', 'expected_image_dict'), (
  10. (
  11. 'https://imgur.com/a/xWZsDDP',
  12. {'num_images': '1', 'id': 'xWZsDDP', 'hash': 'xWZsDDP'},
  13. [
  14. {'hash': 'ypa8YfS', 'title': '', 'ext': '.png', 'animated': False}
  15. ]
  16. ),
  17. (
  18. 'https://imgur.com/gallery/IjJJdlC',
  19. {'num_images': 1, 'id': 384898055, 'hash': 'IjJJdlC'},
  20. [
  21. {'hash': 'CbbScDt',
  22. 'description': 'watch when he gets it',
  23. 'ext': '.gif',
  24. 'animated': True,
  25. 'has_sound': False
  26. }
  27. ],
  28. ),
  29. (
  30. 'https://imgur.com/a/dcc84Gt',
  31. {'num_images': '4', 'id': 'dcc84Gt', 'hash': 'dcc84Gt'},
  32. [
  33. {'hash': 'ylx0Kle', 'ext': '.jpg', 'title': ''},
  34. {'hash': 'TdYfKbK', 'ext': '.jpg', 'title': ''},
  35. {'hash': 'pCxGbe8', 'ext': '.jpg', 'title': ''},
  36. {'hash': 'TSAkikk', 'ext': '.jpg', 'title': ''},
  37. ]
  38. ),
  39. (
  40. 'https://m.imgur.com/a/py3RW0j',
  41. {'num_images': '1', 'id': 'py3RW0j', 'hash': 'py3RW0j', },
  42. [
  43. {'hash': 'K24eQmK', 'has_sound': False, 'ext': '.jpg'}
  44. ],
  45. ),
  46. ))
  47. def test_get_data_album(test_url: str, expected_gen_dict: dict, expected_image_dict: list[dict]):
  48. result = Imgur._get_data(test_url)
  49. assert all([result.get(key) == expected_gen_dict[key] for key in expected_gen_dict.keys()])
  50. # Check if all the keys from the test dict are correct in at least one of the album entries
  51. assert any([all([image.get(key) == image_dict[key] for key in image_dict.keys()])
  52. for image_dict in expected_image_dict for image in result['album_images']['images']])
  53. @pytest.mark.online
  54. @pytest.mark.parametrize(('test_url', 'expected_image_dict'), (
  55. (
  56. 'https://i.imgur.com/dLk3FGY.gifv',
  57. {'hash': 'dLk3FGY', 'title': '', 'ext': '.mp4', 'animated': True}
  58. ),
  59. (
  60. 'https://imgur.com/65FqTpT.gifv',
  61. {
  62. 'hash': '65FqTpT',
  63. 'title': '',
  64. 'description': '',
  65. 'animated': True,
  66. 'mimetype': 'video/mp4'
  67. },
  68. ),
  69. ))
  70. def test_get_data_gif(test_url: str, expected_image_dict: dict):
  71. result = Imgur._get_data(test_url)
  72. assert all([result.get(key) == expected_image_dict[key] for key in expected_image_dict.keys()])
  73. @pytest.mark.parametrize('test_extension', (
  74. '.gif',
  75. '.png',
  76. '.jpg',
  77. '.mp4'
  78. ))
  79. def test_imgur_extension_validation_good(test_extension: str):
  80. result = Imgur._validate_extension(test_extension)
  81. assert result == test_extension
  82. @pytest.mark.parametrize('test_extension', (
  83. '.jpeg',
  84. 'bad',
  85. '.avi',
  86. '.test',
  87. '.flac',
  88. ))
  89. def test_imgur_extension_validation_bad(test_extension: str):
  90. with pytest.raises(SiteDownloaderError):
  91. Imgur._validate_extension(test_extension)
  92. @pytest.mark.online
  93. @pytest.mark.parametrize(('test_url', 'expected_hashes'), (
  94. (
  95. 'https://imgur.com/a/xWZsDDP',
  96. ('f551d6e6b0fef2ce909767338612e31b',)
  97. ),
  98. (
  99. 'https://imgur.com/gallery/IjJJdlC',
  100. ('740b006cf9ec9d6f734b6e8f5130bdab',),
  101. ),
  102. (
  103. 'https://imgur.com/a/dcc84Gt',
  104. (
  105. 'cf1158e1de5c3c8993461383b96610cf',
  106. '28d6b791a2daef8aa363bf5a3198535d',
  107. '248ef8f2a6d03eeb2a80d0123dbaf9b6',
  108. '029c475ce01b58fdf1269d8771d33913',
  109. ),
  110. ),
  111. (
  112. 'https://imgur.com/a/eemHCCK',
  113. (
  114. '9cb757fd8f055e7ef7aa88addc9d9fa5',
  115. 'b6cb6c918e2544e96fb7c07d828774b5',
  116. 'fb6c913d721c0bbb96aa65d7f560d385',
  117. ),
  118. ),
  119. (
  120. 'https://i.imgur.com/lFJai6i.gifv',
  121. ('01a6e79a30bec0e644e5da12365d5071',),
  122. ),
  123. (
  124. 'https://i.imgur.com/ywSyILa.gifv?',
  125. ('56d4afc32d2966017c38d98568709b45',),
  126. ),
  127. (
  128. 'https://imgur.com/ubYwpbk.GIFV',
  129. ('d4a774aac1667783f9ed3a1bd02fac0c',),
  130. ),
  131. (
  132. 'https://i.imgur.com/j1CNCZY.gifv',
  133. ('58e7e6d972058c18b7ecde910ca147e3',),
  134. ),
  135. (
  136. 'https://i.imgur.com/uTvtQsw.gifv',
  137. ('46c86533aa60fc0e09f2a758513e3ac2',),
  138. ),
  139. (
  140. 'https://i.imgur.com/OGeVuAe.giff',
  141. ('77389679084d381336f168538793f218',),
  142. ),
  143. (
  144. 'https://i.imgur.com/OGeVuAe.gift',
  145. ('77389679084d381336f168538793f218',),
  146. ),
  147. (
  148. 'https://i.imgur.com/3SKrQfK.jpg?1',
  149. ('aa299e181b268578979cad176d1bd1d0',),
  150. ),
  151. (
  152. 'https://i.imgur.com/cbivYRW.jpg?3',
  153. ('7ec6ceef5380cb163a1d498c359c51fd',),
  154. ),
  155. (
  156. 'http://i.imgur.com/s9uXxlq.jpg?5.jpg',
  157. ('338de3c23ee21af056b3a7c154e2478f',),
  158. ),
  159. ))
  160. def test_find_resources(test_url: str, expected_hashes: list[str]):
  161. mock_download = Mock()
  162. mock_download.url = test_url
  163. downloader = Imgur(mock_download)
  164. results = downloader.find_resources()
  165. assert all([isinstance(res, Resource) for res in results])
  166. [res.download() for res in results]
  167. hashes = set([res.hash.hexdigest() for res in results])
  168. assert hashes == set(expected_hashes)