2021-02-11 12:08:47 +13:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# coding=utf-8
|
|
|
|
|
|
|
|
from pathlib import Path
|
|
|
|
from unittest.mock import Mock
|
|
|
|
|
|
|
|
import praw.models
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from bulkredditdownloader.file_name_formatter import FileNameFormatter
|
|
|
|
from bulkredditdownloader.resource import Resource
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture()
|
|
|
|
def submission() -> Mock:
|
|
|
|
test = Mock()
|
|
|
|
test.title = 'name'
|
|
|
|
test.subreddit.display_name = 'randomreddit'
|
|
|
|
test.author.name = 'person'
|
|
|
|
test.id = '12345'
|
|
|
|
test.score = 1000
|
|
|
|
test.link_flair_text = 'test_flair'
|
|
|
|
test.created_utc = 123456789
|
|
|
|
return test
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture()
|
2021-02-15 22:16:51 +13:00
|
|
|
def reddit_submission(reddit_instance) -> praw.models.Submission:
|
|
|
|
return reddit_instance.submission(id='lgilgt')
|
2021-02-11 12:08:47 +13:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(('format_string', 'expected'), (('{SUBREDDIT}', 'randomreddit'),
|
|
|
|
('{REDDITOR}', 'person'),
|
|
|
|
('{POSTID}', '12345'),
|
|
|
|
('{UPVOTES}', '1000'),
|
|
|
|
('{FLAIR}', 'test_flair'),
|
|
|
|
('{DATE}', '123456789'),
|
|
|
|
('{REDDITOR}_{TITLE}_{POSTID}', 'person_name_12345')
|
|
|
|
))
|
|
|
|
def test_format_name_mock(format_string: str, expected: str, submission: Mock):
|
|
|
|
result = FileNameFormatter._format_name(submission, format_string)
|
|
|
|
assert result == expected
|
|
|
|
|
|
|
|
|
2021-03-02 17:06:21 +13:00
|
|
|
@pytest.mark.parametrize(('test_string', 'expected'), (
|
|
|
|
('', False),
|
|
|
|
('test', False),
|
|
|
|
('{POSTID}', True),
|
|
|
|
('POSTID', False),
|
|
|
|
('{POSTID}_test', True),
|
|
|
|
('test_{TITLE}', True),
|
|
|
|
('TITLE_POSTID', False),
|
|
|
|
))
|
|
|
|
def test_check_format_string_validity(test_string: str, expected: bool):
|
|
|
|
result = FileNameFormatter.validate_string(test_string)
|
|
|
|
assert result == expected
|
|
|
|
|
|
|
|
|
2021-02-26 22:09:25 +13:00
|
|
|
@pytest.mark.online
|
2021-02-26 22:19:12 +13:00
|
|
|
@pytest.mark.reddit
|
2021-02-11 12:08:47 +13:00
|
|
|
@pytest.mark.parametrize(('format_string', 'expected'),
|
|
|
|
(('{SUBREDDIT}', 'Mindustry'),
|
|
|
|
('{REDDITOR}', 'Gamer_player_boi'),
|
|
|
|
('{POSTID}', 'lgilgt'),
|
|
|
|
('{FLAIR}', 'Art'),
|
|
|
|
('{SUBREDDIT}_{TITLE}', 'Mindustry_Toxopid that is NOT humane >:('),
|
|
|
|
('{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):
|
|
|
|
result = FileNameFormatter._format_name(reddit_submission, format_string)
|
|
|
|
assert result == expected
|
|
|
|
|
|
|
|
|
2021-02-26 22:09:25 +13:00
|
|
|
@pytest.mark.online
|
2021-02-26 22:19:12 +13:00
|
|
|
@pytest.mark.reddit
|
2021-02-11 12:08:47 +13:00
|
|
|
@pytest.mark.parametrize(('format_string_directory', 'format_string_file', 'expected'),
|
|
|
|
(('{SUBREDDIT}', '{POSTID}', 'test/Mindustry/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(
|
|
|
|
format_string_directory: str,
|
|
|
|
format_string_file: str,
|
|
|
|
expected: str,
|
|
|
|
reddit_submission: praw.models.Submission):
|
2021-02-26 22:09:25 +13:00
|
|
|
test_resource = Resource(reddit_submission, 'i.reddit.com/blabla.png')
|
2021-02-11 12:08:47 +13:00
|
|
|
test_formatter = FileNameFormatter(format_string_file, format_string_directory)
|
|
|
|
result = test_formatter.format_path(test_resource, Path('test'))
|
|
|
|
assert str(result) == expected
|