1
0
Fork 0
mirror of synced 2024-06-28 19:10:41 +12:00

Add some tests for base_download

This commit is contained in:
Serene-Arc 2021-02-07 16:38:30 +10:00 committed by Ali Parlakci
parent f573038a21
commit 5ef58f147f
4 changed files with 45 additions and 2 deletions

View file

@ -96,8 +96,9 @@ class BaseDownloader(ABC):
@staticmethod
def _get_extension(url: str) -> str:
pattern = re.compile(r'(\.(jpg|jpeg|png|mp4|webm|gif))')
if len(results := re.search(pattern, url).groups()) > 1:
return results[1]
if results := re.search(pattern, url):
if len(results.groups()) > 1:
return results[0]
if "v.redd.it" not in url:
return '.jpg'
else:

View file

View file

@ -0,0 +1,42 @@
#!/uasr/bin/env python3
# coding=utf-8
from pathlib import Path
import pytest
from bulkredditdownloader.downloaders.base_downloader import BaseDownloader
@pytest.mark.parametrize(('test_bytes', 'expected'), ((b'test', '098f6bcd4621d373cade4e832627b4f6'),
(b'test2', 'ad0234829205b9033196ba818f7a872b')))
def test_create_hash(test_bytes: bytes, expected: str):
result = BaseDownloader._create_hash(test_bytes)
assert result == expected
@pytest.mark.parametrize(('test_url', 'expected'), (('test.png', '.png'),
('random.jpg', '.jpg'),
('http://random.com/test.png', '.png'),
('https://example.net/picture.jpg', '.jpg'),
('https://v.redd.it/picture', '.mp4'),
('https://v.redd.it/picture.jpg', '.jpg'),
('https:/random.url', '.jpg')
))
def test_get_extension(test_url: str, expected: str):
result = BaseDownloader._get_extension(test_url)
assert result == expected
@pytest.mark.skip
@pytest.mark.parametrize(('test_url', 'expected_hash'), (('https://www.iana.org/_img/2013.1/iana-logo-header.svg', ''),
('', '')
))
def test_download_resource(test_url: str, expected_hash: str, tmp_path: Path):
test_file = tmp_path / 'test'
BaseDownloader._download_resource(test_file, tmp_path, test_url)
assert test_file.exists()
with open(test_file, 'rb') as file:
content = file.read()
hash_result = BaseDownloader._create_hash(content)
assert hash_result == expected_hash