test_redgifs.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/usr/bin/env python3
  2. # coding=utf-8
  3. from unittest.mock import Mock
  4. import re
  5. import pytest
  6. from bdfr.resource import Resource
  7. from bdfr.site_downloaders.redgifs import Redgifs
  8. @pytest.mark.online
  9. @pytest.mark.parametrize(('test_url', 'expected'), (
  10. ('https://redgifs.com/watch/frighteningvictorioussalamander',
  11. {'FrighteningVictoriousSalamander.mp4'}),
  12. ('https://redgifs.com/watch/springgreendecisivetaruca',
  13. {'SpringgreenDecisiveTaruca.mp4'}),
  14. ('https://www.redgifs.com/watch/palegoldenrodrawhalibut',
  15. {'PalegoldenrodRawHalibut.mp4'}),
  16. ('https://redgifs.com/watch/hollowintentsnowyowl',
  17. {'HollowIntentSnowyowl-large.jpg'}),
  18. ('https://www.redgifs.com/watch/lustrousstickywaxwing',
  19. {'EntireEnchantingHypsilophodon-large.jpg',
  20. 'FancyMagnificentAdamsstaghornedbeetle-large.jpg',
  21. 'LustrousStickyWaxwing-large.jpg',
  22. 'ParchedWindyArmyworm-large.jpg',
  23. 'ThunderousColorlessErmine-large.jpg',
  24. 'UnripeUnkemptWoodpecker-large.jpg'}),
  25. ))
  26. def test_get_link(test_url: str, expected: set[str]):
  27. result = Redgifs._get_link(test_url)
  28. result = list(result)
  29. patterns = [r'https://thumbs\d\.redgifs\.com/' + e + r'.*' for e in expected]
  30. assert all([re.match(p, r) for p in patterns] for r in result)
  31. @pytest.mark.online
  32. @pytest.mark.parametrize(('test_url', 'expected_hashes'), (
  33. ('https://redgifs.com/watch/frighteningvictorioussalamander', {'4007c35d9e1f4b67091b5f12cffda00a'}),
  34. ('https://redgifs.com/watch/springgreendecisivetaruca', {'8dac487ac49a1f18cc1b4dabe23f0869'}),
  35. ('https://redgifs.com/watch/leafysaltydungbeetle', {'076792c660b9c024c0471ef4759af8bd'}),
  36. ('https://www.redgifs.com/watch/palegoldenrodrawhalibut', {'46d5aa77fe80c6407de1ecc92801c10e'}),
  37. ('https://redgifs.com/watch/hollowintentsnowyowl',
  38. {'5ee51fa15e0a58e98f11dea6a6cca771'}),
  39. ('https://www.redgifs.com/watch/lustrousstickywaxwing',
  40. {'b461e55664f07bed8d2f41d8586728fa',
  41. '30ba079a8ed7d7adf17929dc3064c10f',
  42. '0d4f149d170d29fc2f015c1121bab18b',
  43. '53987d99cfd77fd65b5fdade3718f9f1',
  44. 'fb2e7d972846b83bf4016447d3060d60',
  45. '44fb28f72ec9a5cca63fa4369ab4f672'}),
  46. ))
  47. def test_download_resource(test_url: str, expected_hashes: set[str]):
  48. mock_submission = Mock()
  49. mock_submission.url = test_url
  50. test_site = Redgifs(mock_submission)
  51. results = test_site.find_resources()
  52. assert all([isinstance(res, Resource) for res in results])
  53. [res.download() for res in results]
  54. hashes = set([res.hash.hexdigest() for res in results])
  55. assert hashes == set(expected_hashes)