1
0
Fork 0
mirror of synced 2024-06-29 03:21:19 +12:00

Refactor and optimise some tests (#245)

* Rename to follow actual directory name

* Remove unnecessary Reddit call from test

* Refactor test to reduce duplication

* Parameterise some tests

* Standardise formatting
This commit is contained in:
Serene 2021-04-04 03:25:58 +10:00 committed by Ali Parlakci
parent a05fa1a965
commit 2385867afb
19 changed files with 189 additions and 133 deletions

View file

@ -22,7 +22,11 @@ def test_get_comments(test_submission_id: str, min_comments: int, reddit_instanc
@pytest.mark.online @pytest.mark.online
@pytest.mark.reddit @pytest.mark.reddit
@pytest.mark.parametrize(('test_submission_id', 'expected_dict'), ( @pytest.mark.parametrize(('test_submission_id', 'expected_dict'), (
('m3reby', {'author': 'sinjen-tos', 'id': 'm3reby', 'link_flair_text': 'image'}), ('m3reby', {
'author': 'sinjen-tos',
'id': 'm3reby',
'link_flair_text': 'image',
}),
('m3kua3', {'author': 'DELETED'}), ('m3kua3', {'author': 'DELETED'}),
)) ))
def test_get_post_details(test_submission_id: str, expected_dict: dict, reddit_instance: praw.Reddit): def test_get_post_details(test_submission_id: str, expected_dict: dict, reddit_instance: praw.Reddit):

View file

@ -21,7 +21,8 @@ from bulkredditdownloader.site_downloaders.youtube import Youtube
@pytest.mark.parametrize(('test_submission_url', 'expected_class'), ( @pytest.mark.parametrize(('test_submission_url', 'expected_class'), (
('https://v.redd.it/9z1dnk3xr5k61', VReddit), ('https://v.redd.it/9z1dnk3xr5k61', VReddit),
('https://www.reddit.com/r/TwoXChromosomes/comments/lu29zn/i_refuse_to_live_my_life_in_anything_but_comfort/', SelfPost), ('https://www.reddit.com/r/TwoXChromosomes/comments/lu29zn/i_refuse_to_live_my_life'
'_in_anything_but_comfort/', SelfPost),
('https://i.imgur.com/bZx1SJQ.jpg', Direct), ('https://i.imgur.com/bZx1SJQ.jpg', Direct),
('https://i.redd.it/affyv0axd5k61.png', Direct), ('https://i.redd.it/affyv0axd5k61.png', Direct),
('https://i.imgur.com/BuzvZwb.gifv', Imgur), ('https://i.imgur.com/BuzvZwb.gifv', Imgur),

View file

@ -10,16 +10,18 @@ from bulkredditdownloader.site_downloaders.erome import Erome
@pytest.mark.online @pytest.mark.online
@pytest.mark.parametrize(('test_url', 'expected_urls'), ( @pytest.mark.parametrize(('test_url', 'expected_urls'), (
('https://www.erome.com/a/vqtPuLXh', ('https://s11.erome.com/365/vqtPuLXh/KH2qBT99_480p.mp4',)), ('https://www.erome.com/a/vqtPuLXh', (
('https://www.erome.com/a/ORhX0FZz', 'https://s11.erome.com/365/vqtPuLXh/KH2qBT99_480p.mp4',
('https://s4.erome.com/355/ORhX0FZz/9IYQocM9_480p.mp4', )),
'https://s4.erome.com/355/ORhX0FZz/9eEDc8xm_480p.mp4', ('https://www.erome.com/a/ORhX0FZz', (
'https://s4.erome.com/355/ORhX0FZz/EvApC7Rp_480p.mp4', 'https://s4.erome.com/355/ORhX0FZz/9IYQocM9_480p.mp4',
'https://s4.erome.com/355/ORhX0FZz/LruobtMs_480p.mp4', 'https://s4.erome.com/355/ORhX0FZz/9eEDc8xm_480p.mp4',
'https://s4.erome.com/355/ORhX0FZz/TJNmSUU5_480p.mp4', 'https://s4.erome.com/355/ORhX0FZz/EvApC7Rp_480p.mp4',
'https://s4.erome.com/355/ORhX0FZz/X11Skh6Z_480p.mp4', 'https://s4.erome.com/355/ORhX0FZz/LruobtMs_480p.mp4',
'https://s4.erome.com/355/ORhX0FZz/bjlTkpn7_480p.mp4') 'https://s4.erome.com/355/ORhX0FZz/TJNmSUU5_480p.mp4',
), 'https://s4.erome.com/355/ORhX0FZz/X11Skh6Z_480p.mp4',
'https://s4.erome.com/355/ORhX0FZz/bjlTkpn7_480p.mp4'
)),
)) ))
def test_get_link(test_url: str, expected_urls: tuple[str]): def test_get_link(test_url: str, expected_urls: tuple[str]):
result = Erome. _get_links(test_url) result = Erome. _get_links(test_url)
@ -29,8 +31,12 @@ def test_get_link(test_url: str, expected_urls: tuple[str]):
@pytest.mark.online @pytest.mark.online
@pytest.mark.slow @pytest.mark.slow
@pytest.mark.parametrize(('test_url', 'expected_hashes'), ( @pytest.mark.parametrize(('test_url', 'expected_hashes'), (
('https://www.erome.com/a/vqtPuLXh', {'5da2a8d60d87bed279431fdec8e7d72f'}), ('https://www.erome.com/a/vqtPuLXh', {
('https://www.erome.com/i/ItASD33e', {'b0d73fedc9ce6995c2f2c4fdb6f11eff'}), '5da2a8d60d87bed279431fdec8e7d72f'
}),
('https://www.erome.com/i/ItASD33e', {
'b0d73fedc9ce6995c2f2c4fdb6f11eff'
}),
('https://www.erome.com/a/lGrcFxmb', { ('https://www.erome.com/a/lGrcFxmb', {
'0e98f9f527a911dcedde4f846bb5b69f', '0e98f9f527a911dcedde4f846bb5b69f',
'25696ae364750a5303fc7d7dc78b35c1', '25696ae364750a5303fc7d7dc78b35c1',

View file

@ -38,14 +38,18 @@ def test_gallery_get_links(test_url: str, expected: set[str]):
@pytest.mark.online @pytest.mark.online
@pytest.mark.reddit @pytest.mark.reddit
@pytest.mark.parametrize(('test_submission_id', 'expected_hashes'), ( @pytest.mark.parametrize(('test_submission_id', 'expected_hashes'), (
('m6lvrh', {'6c8a892ae8066cbe119218bcaac731e1', ('m6lvrh', {
'93ce177f8cb7994906795f4615114d13', '6c8a892ae8066cbe119218bcaac731e1',
'9a293adf19354f14582608cf22124574', '93ce177f8cb7994906795f4615114d13',
'b73e2c3daee02f99404644ea02f1ae65'}), '9a293adf19354f14582608cf22124574',
('ljyy27', {'1bc38bed88f9c4770e22a37122d5c941', 'b73e2c3daee02f99404644ea02f1ae65'
'2539a92b78f3968a069df2dffe2279f9', }),
'37dea50281c219b905e46edeefc1a18d', ('ljyy27', {
'ec4924cf40549728dcf53dd40bc7a73c'}), '1bc38bed88f9c4770e22a37122d5c941',
'2539a92b78f3968a069df2dffe2279f9',
'37dea50281c219b905e46edeefc1a18d',
'ec4924cf40549728dcf53dd40bc7a73c'
}),
)) ))
def test_gallery_download(test_submission_id: str, expected_hashes: set[str], reddit_instance: praw.Reddit): def test_gallery_download(test_submission_id: str, expected_hashes: set[str], reddit_instance: praw.Reddit):
test_submission = reddit_instance.submission(id=test_submission_id) test_submission = reddit_instance.submission(id=test_submission_id)

View file

@ -51,17 +51,24 @@ def test_get_data_gif(test_url: str, expected_image_dict: dict):
assert all([result.get(key) == expected_image_dict[key] for key in expected_image_dict.keys()]) assert all([result.get(key) == expected_image_dict[key] for key in expected_image_dict.keys()])
@pytest.mark.parametrize('test_extension', @pytest.mark.parametrize('test_extension', (
('.gif', '.png', '.jpg', '.mp4') '.gif',
) '.png',
'.jpg',
'.mp4'
))
def test_imgur_extension_validation_good(test_extension: str): def test_imgur_extension_validation_good(test_extension: str):
result = Imgur._validate_extension(test_extension) result = Imgur._validate_extension(test_extension)
assert result == test_extension assert result == test_extension
@pytest.mark.parametrize('test_extension', @pytest.mark.parametrize('test_extension', (
('.jpeg', '.avi', 'bad', '.test', '.flac') '.jpeg',
) 'bad',
'.avi',
'.test',
'.flac'
))
def test_imgur_extension_validation_bad(test_extension: str): def test_imgur_extension_validation_bad(test_extension: str):
with pytest.raises(SiteDownloaderError): with pytest.raises(SiteDownloaderError):
Imgur._validate_extension(test_extension) Imgur._validate_extension(test_extension)
@ -69,13 +76,18 @@ def test_imgur_extension_validation_bad(test_extension: str):
@pytest.mark.online @pytest.mark.online
@pytest.mark.parametrize(('test_url', 'expected_hashes'), ( @pytest.mark.parametrize(('test_url', 'expected_hashes'), (
('https://imgur.com/a/xWZsDDP', ('f551d6e6b0fef2ce909767338612e31b',)), ('https://imgur.com/a/xWZsDDP', (
('https://imgur.com/gallery/IjJJdlC', ('7227d4312a9779b74302724a0cfa9081',)), 'f551d6e6b0fef2ce909767338612e31b',
('https://imgur.com/a/dcc84Gt', )),
('cf1158e1de5c3c8993461383b96610cf', ('https://imgur.com/gallery/IjJJdlC', (
'28d6b791a2daef8aa363bf5a3198535d', '7227d4312a9779b74302724a0cfa9081',
'248ef8f2a6d03eeb2a80d0123dbaf9b6', )),
'029c475ce01b58fdf1269d8771d33913')), ('https://imgur.com/a/dcc84Gt', (
'cf1158e1de5c3c8993461383b96610cf',
'28d6b791a2daef8aa363bf5a3198535d',
'248ef8f2a6d03eeb2a80d0123dbaf9b6',
'029c475ce01b58fdf1269d8771d33913'
)),
)) ))
def test_find_resources(test_url: str, expected_hashes: list[str]): def test_find_resources(test_url: str, expected_hashes: list[str]):
mock_download = Mock() mock_download = Mock()

View file

@ -1,7 +1,8 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# coding=utf-8 # coding=utf-8
import praw from unittest.mock import MagicMock
import pytest import pytest
from bulkredditdownloader.resource import Resource from bulkredditdownloader.resource import Resource
@ -9,14 +10,14 @@ from bulkredditdownloader.site_downloaders.youtube import Youtube
@pytest.mark.online @pytest.mark.online
@pytest.mark.reddit
@pytest.mark.slow @pytest.mark.slow
@pytest.mark.parametrize(('test_submission_id', 'expected_hash'), ( @pytest.mark.parametrize(('test_url', 'expected_hash'), (
('ltnoqp', '468136300a106c67f1463a7011a6db4a'), ('https://www.youtube.com/watch?v=uSm2VDgRIUs', '3c79a62898028987f94161e0abccbddf'),
('m2l5oo', 'a70512f7782f13922258297bb12055d9'), ('https://www.youtube.com/watch?v=m-tKnjFwleU', '61651cc6f53782af50030c0a7dd0b6f6'),
)) ))
def test_find_resources(test_submission_id: str, expected_hash: str, reddit_instance: praw.Reddit): def test_find_resources(test_url: str, expected_hash: str):
test_submission = reddit_instance.submission(id=test_submission_id) test_submission = MagicMock()
test_submission.url = test_url
downloader = Youtube(test_submission) downloader = Youtube(test_submission)
resources = downloader.find_resources() resources = downloader.find_resources()
assert len(resources) == 1 assert len(resources) == 1

View file

@ -10,7 +10,10 @@ from bulkredditdownloader.configuration import Configuration
@pytest.mark.parametrize('arg_dict', ( @pytest.mark.parametrize('arg_dict', (
{'directory': 'test_dir'}, {'directory': 'test_dir'},
{'directory': 'test_dir', 'no_dupes': True}, {
'directory': 'test_dir',
'no_dupes': True,
},
)) ))
def test_process_click_context(arg_dict: dict): def test_process_click_context(arg_dict: dict):
test_config = Configuration() test_config = Configuration()

View file

@ -11,43 +11,47 @@ def download_filter() -> DownloadFilter:
return DownloadFilter(['mp4', 'mp3'], ['test.com', 'reddit.com']) return DownloadFilter(['mp4', 'mp3'], ['test.com', 'reddit.com'])
@pytest.mark.parametrize(('test_url', 'expected'), (('test.mp4', False), @pytest.mark.parametrize(('test_url', 'expected'), (
('test.avi', True), ('test.mp4', False),
('test.random.mp3', False) ('test.avi', True),
)) ('test.random.mp3', False),
))
def test_filter_extension(test_url: str, expected: bool, download_filter: DownloadFilter): def test_filter_extension(test_url: str, expected: bool, download_filter: DownloadFilter):
result = download_filter._check_extension(test_url) result = download_filter._check_extension(test_url)
assert result == expected assert result == expected
@pytest.mark.parametrize(('test_url', 'expected'), (('test.mp4', True), @pytest.mark.parametrize(('test_url', 'expected'), (
('http://reddit.com/test.mp4', False), ('test.mp4', True),
('http://reddit.com/test.gif', False), ('http://reddit.com/test.mp4', False),
('https://www.example.com/test.mp4', True), ('http://reddit.com/test.gif', False),
('https://www.example.com/test.png', True), ('https://www.example.com/test.mp4', True),
)) ('https://www.example.com/test.png', True),
))
def test_filter_domain(test_url: str, expected: bool, download_filter: DownloadFilter): def test_filter_domain(test_url: str, expected: bool, download_filter: DownloadFilter):
result = download_filter._check_domain(test_url) result = download_filter._check_domain(test_url)
assert result == expected assert result == expected
@pytest.mark.parametrize(('test_url', 'expected'), (('test.mp4', False), @pytest.mark.parametrize(('test_url', 'expected'), (
('test.gif', True), ('test.mp4', False),
('https://www.example.com/test.mp4', False), ('test.gif', True),
('https://www.example.com/test.png', True), ('https://www.example.com/test.mp4', False),
('http://reddit.com/test.mp4', False), ('https://www.example.com/test.png', True),
('http://reddit.com/test.gif', False), ('http://reddit.com/test.mp4', False),
)) ('http://reddit.com/test.gif', False),
))
def test_filter_all(test_url: str, expected: bool, download_filter: DownloadFilter): def test_filter_all(test_url: str, expected: bool, download_filter: DownloadFilter):
result = download_filter.check_url(test_url) result = download_filter.check_url(test_url)
assert result == expected assert result == expected
@pytest.mark.parametrize('test_url', ('test.mp3', @pytest.mark.parametrize('test_url', (
'test.mp4', 'test.mp3',
'http://reddit.com/test.mp4', 'test.mp4',
't', 'http://reddit.com/test.mp4',
)) 't',
))
def test_filter_empty_filter(test_url: str): def test_filter_empty_filter(test_url: str):
download_filter = DownloadFilter() download_filter = DownloadFilter()
result = download_filter.check_url(test_url) result = download_filter.check_url(test_url)

View file

@ -1,7 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# coding=utf-8 # coding=utf-8
import argparse
import re import re
from pathlib import Path from pathlib import Path
from typing import Iterator from typing import Iterator
@ -15,7 +14,7 @@ from bulkredditdownloader.__main__ import setup_logging
from bulkredditdownloader.configuration import Configuration from bulkredditdownloader.configuration import Configuration
from bulkredditdownloader.download_filter import DownloadFilter from bulkredditdownloader.download_filter import DownloadFilter
from bulkredditdownloader.downloader import RedditDownloader, RedditTypes from bulkredditdownloader.downloader import RedditDownloader, RedditTypes
from bulkredditdownloader.exceptions import BulkDownloaderException, RedditAuthenticationError, RedditUserError from bulkredditdownloader.exceptions import BulkDownloaderException
from bulkredditdownloader.file_name_formatter import FileNameFormatter from bulkredditdownloader.file_name_formatter import FileNameFormatter
from bulkredditdownloader.site_authenticator import SiteAuthenticator from bulkredditdownloader.site_authenticator import SiteAuthenticator
@ -53,7 +52,7 @@ def test_determine_directories(tmp_path: Path, downloader_mock: MagicMock):
@pytest.mark.parametrize(('skip_extensions', 'skip_domains'), ( @pytest.mark.parametrize(('skip_extensions', 'skip_domains'), (
([], []), ([], []),
(['.test'], ['test.com']), (['.test'], ['test.com'],),
)) ))
def test_create_download_filter(skip_extensions: list[str], skip_domains: list[str], downloader_mock: MagicMock): def test_create_download_filter(skip_extensions: list[str], skip_domains: list[str], downloader_mock: MagicMock):
downloader_mock.args.skip = skip_extensions downloader_mock.args.skip = skip_extensions
@ -244,25 +243,18 @@ def test_get_user_submissions(test_user: str, limit: int, downloader_mock: Magic
@pytest.mark.online @pytest.mark.online
@pytest.mark.reddit @pytest.mark.reddit
@pytest.mark.authenticated @pytest.mark.authenticated
def test_get_user_upvoted(downloader_mock: MagicMock, authenticated_reddit_instance: praw.Reddit): @pytest.mark.parametrize('test_flag', (
'upvoted',
'saved',
))
def test_get_user_authenticated_lists(
test_flag: str,
downloader_mock: MagicMock,
authenticated_reddit_instance: praw.Reddit,
):
downloader_mock.args.__dict__[test_flag] = True
downloader_mock.reddit_instance = authenticated_reddit_instance downloader_mock.reddit_instance = authenticated_reddit_instance
downloader_mock.args.user = 'me' downloader_mock.args.user = 'me'
downloader_mock.args.upvoted = True
downloader_mock.args.limit = 10
downloader_mock._determine_sort_function.return_value = praw.models.Subreddit.hot
downloader_mock.sort_filter = RedditTypes.SortType.HOT
RedditDownloader._resolve_user_name(downloader_mock)
results = RedditDownloader._get_user_data(downloader_mock)
assert_all_results_are_submissions(10, results)
@pytest.mark.online
@pytest.mark.reddit
@pytest.mark.authenticated
def test_get_user_saved(downloader_mock: MagicMock, authenticated_reddit_instance: praw.Reddit):
downloader_mock.reddit_instance = authenticated_reddit_instance
downloader_mock.args.user = 'me'
downloader_mock.args.saved = True
downloader_mock.args.limit = 10 downloader_mock.args.limit = 10
downloader_mock._determine_sort_function.return_value = praw.models.Subreddit.hot downloader_mock._determine_sort_function.return_value = praw.models.Subreddit.hot
downloader_mock.sort_filter = RedditTypes.SortType.HOT downloader_mock.sort_filter = RedditTypes.SortType.HOT
@ -299,7 +291,8 @@ def test_download_submission_file_exists(
downloader_mock: MagicMock, downloader_mock: MagicMock,
reddit_instance: praw.Reddit, reddit_instance: praw.Reddit,
tmp_path: Path, tmp_path: Path,
capsys: pytest.CaptureFixture): capsys: pytest.CaptureFixture
):
setup_logging(3) setup_logging(3)
downloader_mock.reddit_instance = reddit_instance downloader_mock.reddit_instance = reddit_instance
downloader_mock.download_filter.check_url.return_value = True downloader_mock.download_filter.check_url.return_value = True
@ -317,11 +310,17 @@ def test_download_submission_file_exists(
@pytest.mark.online @pytest.mark.online
@pytest.mark.reddit @pytest.mark.reddit
@pytest.mark.parametrize(('test_submission_id', 'test_hash'), (
('m1hqw6', 'a912af8905ae468e0121e9940f797ad7'),
))
def test_download_submission_hash_exists( def test_download_submission_hash_exists(
test_submission_id: str,
test_hash: str,
downloader_mock: MagicMock, downloader_mock: MagicMock,
reddit_instance: praw.Reddit, reddit_instance: praw.Reddit,
tmp_path: Path, tmp_path: Path,
capsys: pytest.CaptureFixture): capsys: pytest.CaptureFixture
):
setup_logging(3) setup_logging(3)
downloader_mock.reddit_instance = reddit_instance downloader_mock.reddit_instance = reddit_instance
downloader_mock.download_filter.check_url.return_value = True downloader_mock.download_filter.check_url.return_value = True
@ -329,8 +328,8 @@ def test_download_submission_hash_exists(
downloader_mock.args.no_dupes = True downloader_mock.args.no_dupes = True
downloader_mock.file_name_formatter = RedditDownloader._create_file_name_formatter(downloader_mock) downloader_mock.file_name_formatter = RedditDownloader._create_file_name_formatter(downloader_mock)
downloader_mock.download_directory = tmp_path downloader_mock.download_directory = tmp_path
downloader_mock.master_hash_list = {'a912af8905ae468e0121e9940f797ad7': None} downloader_mock.master_hash_list = {test_hash: None}
submission = downloader_mock.reddit_instance.submission(id='m1hqw6') submission = downloader_mock.reddit_instance.submission(id=test_submission_id)
RedditDownloader._download_submission(downloader_mock, submission) RedditDownloader._download_submission(downloader_mock, submission)
folder_contents = list(tmp_path.iterdir()) folder_contents = list(tmp_path.iterdir())
output = capsys.readouterr() output = capsys.readouterr()
@ -373,15 +372,23 @@ def test_split_subreddit_entries(test_subreddit_entries: list[str], expected: se
@pytest.mark.online @pytest.mark.online
@pytest.mark.reddit @pytest.mark.reddit
def test_mark_hard_link(downloader_mock: MagicMock, tmp_path: Path, reddit_instance: praw.Reddit): @pytest.mark.parametrize('test_submission_id', (
'm1hqw6',
))
def test_mark_hard_link(
test_submission_id: str,
downloader_mock: MagicMock,
tmp_path: Path,
reddit_instance: praw.Reddit
):
downloader_mock.reddit_instance = reddit_instance downloader_mock.reddit_instance = reddit_instance
downloader_mock.args.make_hard_links = True downloader_mock.args.make_hard_links = True
downloader_mock.download_directory = tmp_path downloader_mock.download_directory = tmp_path
downloader_mock.args.folder_scheme = '' downloader_mock.args.folder_scheme = ''
downloader_mock.args.file_scheme = '{POSTID}' downloader_mock.args.file_scheme = '{POSTID}'
downloader_mock.file_name_formatter = RedditDownloader._create_file_name_formatter(downloader_mock) downloader_mock.file_name_formatter = RedditDownloader._create_file_name_formatter(downloader_mock)
submission = downloader_mock.reddit_instance.submission(id='m1hqw6') submission = downloader_mock.reddit_instance.submission(id=test_submission_id)
original = Path(tmp_path, 'm1hqw6.png') original = Path(tmp_path, f'{test_submission_id}.png')
RedditDownloader._download_submission(downloader_mock, submission) RedditDownloader._download_submission(downloader_mock, submission)
assert original.exists() assert original.exists()
@ -390,7 +397,7 @@ def test_mark_hard_link(downloader_mock: MagicMock, tmp_path: Path, reddit_insta
downloader_mock.file_name_formatter = RedditDownloader._create_file_name_formatter(downloader_mock) downloader_mock.file_name_formatter = RedditDownloader._create_file_name_formatter(downloader_mock)
RedditDownloader._download_submission(downloader_mock, submission) RedditDownloader._download_submission(downloader_mock, submission)
test_file_1_stats = original.stat() test_file_1_stats = original.stat()
test_file_2_inode = Path(tmp_path, 'test2_m1hqw6.png').stat().st_ino test_file_2_inode = Path(tmp_path, f'test2_{test_submission_id}.png').stat().st_ino
assert test_file_1_stats.st_nlink == 2 assert test_file_1_stats.st_nlink == 2
assert test_file_1_stats.st_ino == test_file_2_inode assert test_file_1_stats.st_ino == test_file_2_inode

View file

@ -31,15 +31,16 @@ def reddit_submission(reddit_instance: praw.Reddit) -> praw.models.Submission:
return reddit_instance.submission(id='lgilgt') return reddit_instance.submission(id='lgilgt')
@pytest.mark.parametrize(('format_string', 'expected'), (('{SUBREDDIT}', 'randomreddit'), @pytest.mark.parametrize(('format_string', 'expected'), (
('{REDDITOR}', 'person'), ('{SUBREDDIT}', 'randomreddit'),
('{POSTID}', '12345'), ('{REDDITOR}', 'person'),
('{UPVOTES}', '1000'), ('{POSTID}', '12345'),
('{FLAIR}', 'test_flair'), ('{UPVOTES}', '1000'),
('{DATE}', '123456789'), ('{FLAIR}', 'test_flair'),
('{REDDITOR}_{TITLE}_{POSTID}', 'person_name_12345'), ('{DATE}', '123456789'),
('{RANDOM}', '{RANDOM}'), ('{REDDITOR}_{TITLE}_{POSTID}', 'person_name_12345'),
)) ('{RANDOM}', '{RANDOM}'),
))
def test_format_name_mock(format_string: str, expected: str, submission: MagicMock): def test_format_name_mock(format_string: str, expected: str, submission: MagicMock):
result = FileNameFormatter._format_name(submission, format_string) result = FileNameFormatter._format_name(submission, format_string)
assert result == expected assert result == expected
@ -61,14 +62,14 @@ def test_check_format_string_validity(test_string: str, expected: bool):
@pytest.mark.online @pytest.mark.online
@pytest.mark.reddit @pytest.mark.reddit
@pytest.mark.parametrize(('format_string', 'expected'), @pytest.mark.parametrize(('format_string', 'expected'), (
(('{SUBREDDIT}', 'Mindustry'), ('{SUBREDDIT}', 'Mindustry'),
('{REDDITOR}', 'Gamer_player_boi'), ('{REDDITOR}', 'Gamer_player_boi'),
('{POSTID}', 'lgilgt'), ('{POSTID}', 'lgilgt'),
('{FLAIR}', 'Art'), ('{FLAIR}', 'Art'),
('{SUBREDDIT}_{TITLE}', 'Mindustry_Toxopid that is NOT humane >:('), ('{SUBREDDIT}_{TITLE}', 'Mindustry_Toxopid that is NOT humane >:('),
('{REDDITOR}_{TITLE}_{POSTID}', 'Gamer_player_boi_Toxopid that is NOT humane >:(_lgilgt') ('{REDDITOR}_{TITLE}_{POSTID}', 'Gamer_player_boi_Toxopid that is NOT humane >:(_lgilgt')
)) ))
def test_format_name_real(format_string: str, expected: str, reddit_submission: praw.models.Submission): def test_format_name_real(format_string: str, expected: str, reddit_submission: praw.models.Submission):
result = FileNameFormatter._format_name(reddit_submission, format_string) result = FileNameFormatter._format_name(reddit_submission, format_string)
assert result == expected assert result == expected
@ -76,13 +77,23 @@ def test_format_name_real(format_string: str, expected: str, reddit_submission:
@pytest.mark.online @pytest.mark.online
@pytest.mark.reddit @pytest.mark.reddit
@pytest.mark.parametrize(('format_string_directory', 'format_string_file', 'expected'), @pytest.mark.parametrize(('format_string_directory', 'format_string_file', 'expected'), (
(('{SUBREDDIT}', '{POSTID}', 'test/Mindustry/lgilgt.png'), (
('{SUBREDDIT}', '{TITLE}_{POSTID}', '{SUBREDDIT}',
'test/Mindustry/Toxopid that is NOT humane >:(_lgilgt.png'), '{POSTID}',
('{SUBREDDIT}', '{REDDITOR}_{TITLE}_{POSTID}', 'test/Mindustry/lgilgt.png',
'test/Mindustry/Gamer_player_boi_Toxopid that is NOT humane >:(_lgilgt.png') ),
)) (
'{SUBREDDIT}',
'{TITLE}_{POSTID}',
'test/Mindustry/Toxopid that is NOT humane >:(_lgilgt.png',
),
(
'{SUBREDDIT}',
'{REDDITOR}_{TITLE}_{POSTID}',
'test/Mindustry/Gamer_player_boi_Toxopid that is NOT humane >:(_lgilgt.png',
),
))
def test_format_full( def test_format_full(
format_string_directory: str, format_string_directory: str,
format_string_file: str, format_string_file: str,
@ -112,13 +123,12 @@ def test_format_full_conform(
@pytest.mark.online @pytest.mark.online
@pytest.mark.reddit @pytest.mark.reddit
@pytest.mark.parametrize(('format_string_directory', 'format_string_file', 'index', 'expected'), @pytest.mark.parametrize(('format_string_directory', 'format_string_file', 'index', 'expected'), (
(('{SUBREDDIT}', '{POSTID}', None, 'test/Mindustry/lgilgt.png'), ('{SUBREDDIT}', '{POSTID}', None, 'test/Mindustry/lgilgt.png'),
('{SUBREDDIT}', '{POSTID}', 1, 'test/Mindustry/lgilgt_1.png'), ('{SUBREDDIT}', '{POSTID}', 1, 'test/Mindustry/lgilgt_1.png'),
('{SUBREDDIT}', '{POSTID}', 2, 'test/Mindustry/lgilgt_2.png'), ('{SUBREDDIT}', '{POSTID}', 2, 'test/Mindustry/lgilgt_2.png'),
('{SUBREDDIT}', '{TITLE}_{POSTID}', 2, ('{SUBREDDIT}', '{TITLE}_{POSTID}', 2, 'test/Mindustry/Toxopid that is NOT humane >:(_lgilgt_2.png'),
'test/Mindustry/Toxopid that is NOT humane >:(_lgilgt_2.png'), ))
))
def test_format_full_with_index_suffix( def test_format_full_with_index_suffix(
format_string_directory: str, format_string_directory: str,
format_string_file: str, format_string_file: str,
@ -218,7 +228,10 @@ def test_strip_emojies(test_string: str, expected: str):
@pytest.mark.online @pytest.mark.online
@pytest.mark.reddit @pytest.mark.reddit
@pytest.mark.parametrize(('test_submission_id', 'expected'), ( @pytest.mark.parametrize(('test_submission_id', 'expected'), (
('mfuteh', {'title': 'Why Do Interviewers Ask Linked List Questions?', 'redditor': 'mjgardner'}), ('mfuteh', {
'title': 'Why Do Interviewers Ask Linked List Questions?',
'redditor': 'mjgardner',
}),
)) ))
def test_generate_dict_for_submission(test_submission_id: str, expected: dict, reddit_instance: praw.Reddit): def test_generate_dict_for_submission(test_submission_id: str, expected: dict, reddit_instance: praw.Reddit):
test_submission = reddit_instance.submission(id=test_submission_id) test_submission = reddit_instance.submission(id=test_submission_id)

View file

@ -21,12 +21,12 @@ def example_config() -> configparser.ConfigParser:
@pytest.mark.online @pytest.mark.online
@pytest.mark.parametrize('test_scopes', ( @pytest.mark.parametrize('test_scopes', (
('history',), {'history', },
('history', 'creddits'), {'history', 'creddits'},
('account', 'flair'), {'account', 'flair'},
('*',), {'*', },
)) ))
def test_check_scopes(test_scopes: list[str]): def test_check_scopes(test_scopes: set[str]):
OAuth2Authenticator._check_scopes(test_scopes) OAuth2Authenticator._check_scopes(test_scopes)
@ -54,7 +54,7 @@ def test_check_scopes_bad(test_scopes: set[str]):
def test_token_manager_read(example_config: configparser.ConfigParser): def test_token_manager_read(example_config: configparser.ConfigParser):
mock_authoriser = MagicMock() mock_authoriser = MagicMock()
mock_authoriser.refresh_token = None mock_authoriser.refresh_token = None
test_manager = OAuth2TokenManager(example_config, None) test_manager = OAuth2TokenManager(example_config, MagicMock())
test_manager.pre_refresh_callback(mock_authoriser) test_manager.pre_refresh_callback(mock_authoriser)
assert mock_authoriser.refresh_token == example_config.get('DEFAULT', 'user_token') assert mock_authoriser.refresh_token == example_config.get('DEFAULT', 'user_token')

View file

@ -2,6 +2,7 @@
# coding=utf-8 # coding=utf-8
import pytest import pytest
from unittest.mock import MagicMock
from bulkredditdownloader.resource import Resource from bulkredditdownloader.resource import Resource
@ -16,7 +17,7 @@ from bulkredditdownloader.resource import Resource
('https://preview.redd.it/7zkmr1wqqih61.png?width=237&format=png&auto=webp&s=19de214e634cbcad99', '.png'), ('https://preview.redd.it/7zkmr1wqqih61.png?width=237&format=png&auto=webp&s=19de214e634cbcad99', '.png'),
)) ))
def test_resource_get_extension(test_url: str, expected: str): def test_resource_get_extension(test_url: str, expected: str):
test_resource = Resource(None, test_url) test_resource = Resource(MagicMock(), test_url)
result = test_resource._determine_extension() result = test_resource._determine_extension()
assert result == expected assert result == expected
@ -26,6 +27,6 @@ def test_resource_get_extension(test_url: str, expected: str):
('https://www.iana.org/_img/2013.1/iana-logo-header.svg', '426b3ac01d3584c820f3b7f5985d6623'), ('https://www.iana.org/_img/2013.1/iana-logo-header.svg', '426b3ac01d3584c820f3b7f5985d6623'),
)) ))
def test_download_online_resource(test_url: str, expected_hash: str): def test_download_online_resource(test_url: str, expected_hash: str):
test_resource = Resource(None, test_url) test_resource = Resource(MagicMock(), test_url)
test_resource.download() test_resource.download()
assert test_resource.hash.hexdigest() == expected_hash assert test_resource.hash.hexdigest() == expected_hash