1
0
Fork 0
mirror of synced 2024-06-26 10:00:20 +12:00

Add tests for SelfPost

This commit is contained in:
Serene-Arc 2021-02-28 09:40:42 +10:00 committed by Ali Parlakci
parent b699639b5c
commit be68d4eb1c
3 changed files with 31 additions and 4 deletions

View file

@ -43,10 +43,13 @@ class Resource:
content = self.retry_download(self.url, 0)
if content:
self.content = content
self.hash = hashlib.md5(self.content)
self.create_hash()
else:
raise BulkDownloaderException('Could not download resource')
def create_hash(self):
self.hash = hashlib.md5(self.content)
def _determine_extension(self) -> str:
extension_pattern = r'.*(\..{3,5})$'
match = re.search(extension_pattern, self.url)

View file

@ -5,8 +5,8 @@ from typing import Optional
from praw.models import Submission
from bulkredditdownloader.site_authenticator import SiteAuthenticator
from bulkredditdownloader.resource import Resource
from bulkredditdownloader.site_authenticator import SiteAuthenticator
from bulkredditdownloader.site_downloaders.base_downloader import BaseDownloader
logger = logging.getLogger(__name__)
@ -18,8 +18,9 @@ class SelfPost(BaseDownloader):
def find_resources(self, authenticator: Optional[SiteAuthenticator] = None) -> list[Resource]:
out = Resource(self.post, self.post.url)
out.content = self.export_to_string()
return out
out.content = self.export_to_string().encode('utf-8')
out.create_hash()
return [out]
def export_to_string(self) -> str:
"""Self posts are formatted here"""

View file

@ -0,0 +1,23 @@
#!/usr/bin/env python3
# coding=utf-8
import praw
import pytest
from bulkredditdownloader.resource import Resource
from bulkredditdownloader.site_downloaders.self_post import SelfPost
@pytest.mark.online
@pytest.mark.reddit
@pytest.mark.parametrize(('test_submission_id', 'expected_hash'), (
('ltmivt', '7d2c9e4e989e5cf2dca2e55a06b1c4f6'),
('ltoaan', '221606386b614d6780c2585a59bd333f'),
))
def test_find_resource(test_submission_id: str, expected_hash: str, reddit_instance: praw.Reddit):
submission = reddit_instance.submission(id=test_submission_id)
downloader = SelfPost(submission)
results = downloader.find_resources()
assert len(results) == 1
assert isinstance(results[0], Resource)
assert results[0].hash.hexdigest() == expected_hash