test_gallery.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/usr/bin/env python3
  2. # coding=utf-8
  3. import praw
  4. import pytest
  5. from bdfr.exceptions import SiteDownloaderError
  6. from bdfr.site_downloaders.gallery import Gallery
  7. @pytest.mark.online
  8. @pytest.mark.parametrize(('test_ids', 'expected'), (
  9. ([
  10. {'media_id': '18nzv9ch0hn61'},
  11. {'media_id': 'jqkizcch0hn61'},
  12. {'media_id': 'k0fnqzbh0hn61'},
  13. {'media_id': 'm3gamzbh0hn61'},
  14. ], {
  15. 'https://i.redd.it/18nzv9ch0hn61.jpg',
  16. 'https://i.redd.it/jqkizcch0hn61.jpg',
  17. 'https://i.redd.it/k0fnqzbh0hn61.jpg',
  18. 'https://i.redd.it/m3gamzbh0hn61.jpg'
  19. }),
  20. ([
  21. {'media_id': '04vxj25uqih61'},
  22. {'media_id': '0fnx83kpqih61'},
  23. {'media_id': '7zkmr1wqqih61'},
  24. {'media_id': 'u37k5gxrqih61'},
  25. ], {
  26. 'https://i.redd.it/04vxj25uqih61.png',
  27. 'https://i.redd.it/0fnx83kpqih61.png',
  28. 'https://i.redd.it/7zkmr1wqqih61.png',
  29. 'https://i.redd.it/u37k5gxrqih61.png'
  30. }),
  31. ))
  32. def test_gallery_get_links(test_ids: list[dict], expected: set[str]):
  33. results = Gallery._get_links(test_ids)
  34. assert set(results) == expected
  35. @pytest.mark.online
  36. @pytest.mark.reddit
  37. @pytest.mark.parametrize(('test_submission_id', 'expected_hashes'), (
  38. ('m6lvrh', {
  39. '5c42b8341dd56eebef792e86f3981c6a',
  40. '8f38d76da46f4057bf2773a778e725ca',
  41. 'f5776f8f90491c8b770b8e0a6bfa49b3',
  42. 'fa1a43c94da30026ad19a9813a0ed2c2',
  43. }),
  44. ('ljyy27', {
  45. '359c203ec81d0bc00e675f1023673238',
  46. '79262fd46bce5bfa550d878a3b898be4',
  47. '808c35267f44acb523ce03bfa5687404',
  48. 'ec8b65bdb7f1279c4b3af0ea2bbb30c3',
  49. }),
  50. ('obkflw', {
  51. '65163f685fb28c5b776e0e77122718be',
  52. '2a337eb5b13c34d3ca3f51b5db7c13e9',
  53. }),
  54. ('rb3ub6', { # patreon post
  55. '748a976c6cedf7ea85b6f90e7cb685c7',
  56. '839796d7745e88ced6355504e1f74508',
  57. 'bcdb740367d0f19f97a77e614b48a42d',
  58. '0f230b8c4e5d103d35a773fab9814ec3',
  59. 'e5192d6cb4f84c4f4a658355310bf0f9',
  60. '91cbe172cd8ccbcf049fcea4204eb979',
  61. })
  62. ))
  63. def test_gallery_download(test_submission_id: str, expected_hashes: set[str], reddit_instance: praw.Reddit):
  64. test_submission = reddit_instance.submission(id=test_submission_id)
  65. gallery = Gallery(test_submission)
  66. results = gallery.find_resources()
  67. [res.download() for res in results]
  68. hashes = [res.hash.hexdigest() for res in results]
  69. assert set(hashes) == expected_hashes
  70. @pytest.mark.parametrize('test_id', (
  71. 'n0pyzp',
  72. 'nxyahw',
  73. ))
  74. def test_gallery_download_raises_right_error(test_id: str, reddit_instance: praw.Reddit):
  75. test_submission = reddit_instance.submission(id=test_id)
  76. gallery = Gallery(test_submission)
  77. with pytest.raises(SiteDownloaderError):
  78. gallery.find_resources()