From 125b78a34822b1021bf4911e08fb2178eff2c1a6 Mon Sep 17 00:00:00 2001 From: Serene-Arc Date: Sat, 27 Feb 2021 10:35:43 +1000 Subject: [PATCH] Add tests for downloader Erome --- .../site_downloaders/erome.py | 6 +-- .../tests/downloaders/test_erome.py | 54 +++++++++++++++++++ 2 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 bulkredditdownloader/tests/downloaders/test_erome.py diff --git a/bulkredditdownloader/site_downloaders/erome.py b/bulkredditdownloader/site_downloaders/erome.py index 1220651..9f62738 100644 --- a/bulkredditdownloader/site_downloaders/erome.py +++ b/bulkredditdownloader/site_downloaders/erome.py @@ -23,13 +23,13 @@ class Erome(BaseDownloader): def find_resources(self, authenticator: Optional[SiteAuthenticator] = None) -> list[Resource]: try: - images = self._get_links(self.post.url) + images = set(self._get_links(self.post.url)) except urllib.error.HTTPError: raise NotADownloadableLinkError("Not a downloadable link") if len(images) == 1: - image = images[0] + image = images.pop() if not re.match(r'https?://.*', image): image = "https://" + image return [Resource(self.post, image)] @@ -39,7 +39,7 @@ class Erome(BaseDownloader): for i, image in enumerate(images): if not re.match(r'https?://.*', image): image = "https://" + image - out.append(Resource(self.post, image)) + out.append(Resource(self.post, image)) return out @staticmethod diff --git a/bulkredditdownloader/tests/downloaders/test_erome.py b/bulkredditdownloader/tests/downloaders/test_erome.py new file mode 100644 index 0000000..a0cba97 --- /dev/null +++ b/bulkredditdownloader/tests/downloaders/test_erome.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python3 +# coding=utf-8 + +from unittest.mock import Mock + +import pytest + +from bulkredditdownloader.resource import Resource +from bulkredditdownloader.site_downloaders.erome import Erome + + +@pytest.mark.online +@pytest.mark.parametrize(('test_url', 'expected_urls'), ( + ('https://www.erome.com/a/hzLCb2c5', + ('https://s2.erome.com/353/hzLCb2c5/8FNh4qa8.jpg', 'https://s2.erome.com/353/hzLCb2c5/8FNh4qa8_480p.mp4') + ), + ('https://www.erome.com/a/ORhX0FZz', + ('https://s4.erome.com/355/ORhX0FZz/9IYQocM9.jpg', + 'https://s4.erome.com/355/ORhX0FZz/9IYQocM9_480p.mp4', + 'https://s4.erome.com/355/ORhX0FZz/9eEDc8xm.jpg', + 'https://s4.erome.com/355/ORhX0FZz/9eEDc8xm_480p.mp4', + 'https://s4.erome.com/355/ORhX0FZz/EvApC7Rp.jpg', + 'https://s4.erome.com/355/ORhX0FZz/EvApC7Rp_480p.mp4', + 'https://s4.erome.com/355/ORhX0FZz/LruobtMs.jpg', + 'https://s4.erome.com/355/ORhX0FZz/LruobtMs_480p.mp4', + 'https://s4.erome.com/355/ORhX0FZz/TJNmSUU5.jpg', + 'https://s4.erome.com/355/ORhX0FZz/TJNmSUU5_480p.mp4', + 'https://s4.erome.com/355/ORhX0FZz/X11Skh6Z.jpg', + 'https://s4.erome.com/355/ORhX0FZz/X11Skh6Z_480p.mp4', + 'https://s4.erome.com/355/ORhX0FZz/bjlTkpn7.jpg', + 'https://s4.erome.com/355/ORhX0FZz/bjlTkpn7_480p.mp4') + ), +)) +def test_get_link(test_url: str, expected_urls: tuple[str]): + result = Erome. _get_links(test_url) + assert set(result) == set(expected_urls) + + +@pytest.mark.online +@pytest.mark.slow +@pytest.mark.parametrize(('test_url', 'expected_number_of_resources', 'expected_hashes'), ( + ('https://www.erome.com/a/hzLCb2c5', 2, + ('1b4b1703f81f2ad6a622f7319a4651c2', 'f24388a0f3443c1a27594e4af41c3e83') + ), +)) +def test_download_resource(test_url: str, expected_number_of_resources: int, expected_hashes: tuple[str]): + mock_submission = Mock + mock_submission.url = test_url + test_site = Erome(mock_submission) + resources = test_site.find_resources() + assert len(resources) == expected_number_of_resources + [res.download() for res in resources] + resource_hashes = [res.hash.hexdigest() for res in resources] + assert set(resource_hashes) == set(expected_hashes)