def test_format_full_conform( format_string_directory: str, format_string_file: str, reddit_submission: praw.models.Submission): test_resource = Resource(reddit_submission, 'i.reddit.com/blabla.png', lambda: None) test_formatter = FileNameFormatter(format_string_file, format_string_directory, 'ISO') test_formatter.format_path(test_resource, Path('test'))
def test_format_full(format_string_directory: str, format_string_file: str, expected: str, reddit_submission: praw.models.Submission): test_resource = Resource(reddit_submission, 'i.reddit.com/blabla.png') test_formatter = FileNameFormatter(format_string_file, format_string_directory) result = test_formatter.format_path(test_resource, Path('test')) assert str(result) == expected
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_formatter = FileNameFormatter('{TITLE}', '', 'ISO') result = test_formatter._generate_name_dict_from_submission( test_submission) assert all([result.get(key) == expected[key] for key in expected.keys()])
def test_format_full( format_string_directory: str, format_string_file: str, expected: str, reddit_submission: praw.models.Submission): test_resource = Resource(reddit_submission, 'i.reddit.com/blabla.png', lambda: None) test_formatter = FileNameFormatter(format_string_file, format_string_directory, 'ISO') result = test_formatter.format_path(test_resource, Path('test')) assert do_test_path_equality(result, expected)
def test_shorten_filename_real(submission: MagicMock, tmp_path: Path): submission.title = 'A' * 500 submission.author.name = 'test' submission.subreddit.display_name = 'test' submission.id = 'BBBBBB' test_resource = Resource(submission, 'www.example.com/empty', lambda: None, '.jpeg') test_formatter = FileNameFormatter('{REDDITOR}_{TITLE}_{POSTID}', '{SUBREDDIT}', 'ISO') result = test_formatter.format_path(test_resource, tmp_path) result.parent.mkdir(parents=True) result.touch()
def test_name_submission( test_reddit_id: str, test_downloader: type(BaseDownloader), expected_names: set[str], reddit_instance: praw.reddit.Reddit, ): test_submission = reddit_instance.submission(id=test_reddit_id) test_resources = test_downloader(test_submission).find_resources() test_formatter = FileNameFormatter('{TITLE}', '', '') results = test_formatter.format_resource_paths(test_resources, Path('.')) results = set([r[0].name for r in results]) assert expected_names == results
def test_format_full_with_index_suffix( format_string_directory: str, format_string_file: str, index: Optional[int], expected: str, reddit_submission: praw.models.Submission, ): test_resource = Resource(reddit_submission, 'i.reddit.com/blabla.png') test_formatter = FileNameFormatter(format_string_file, format_string_directory) result = test_formatter.format_path(test_resource, Path('test'), index) assert str(result) == expected
def test_format_archive_entry_comment( test_file_scheme: str, test_folder_scheme: str, test_comment_id: str, expected_name: str, tmp_path: Path, reddit_instance: praw.Reddit, ): test_comment = reddit_instance.comment(id=test_comment_id) test_formatter = FileNameFormatter(test_file_scheme, test_folder_scheme, 'ISO') test_entry = Resource(test_comment, '', lambda: None, '.json') result = test_formatter.format_path(test_entry, tmp_path) assert do_test_string_equality(result, expected_name)
def test_format_archive_entry_comment( test_file_scheme: str, test_folder_scheme: str, test_comment_id: str, expected_name: str, tmp_path: Path, reddit_instance: praw.Reddit, ): test_comment = reddit_instance.comment(id=test_comment_id) test_formatter = FileNameFormatter(test_file_scheme, test_folder_scheme) test_entry = Resource(test_comment, '', '.json') result = test_formatter.format_path(test_entry, tmp_path) assert result.name == expected_name
def test_format_multiple_resources(): mocks = [] for i in range(1, 5): new_mock = MagicMock() new_mock.url = 'https://example.com/test.png' new_mock.extension = '.png' new_mock.source_submission.title = 'test' new_mock.source_submission.__class__ = praw.models.Submission mocks.append(new_mock) test_formatter = FileNameFormatter('{TITLE}', '') results = test_formatter.format_resource_paths(mocks, Path('.')) results = set([str(res[0]) for res in results]) assert results == {'test_1.png', 'test_2.png', 'test_3.png', 'test_4.png'}
def test_multilevel_folder_scheme( test_folder_scheme: str, expected: str, tmp_path: Path, submission: MagicMock, ): test_formatter = FileNameFormatter('{POSTID}', test_folder_scheme, 'ISO') test_resource = MagicMock() test_resource.source_submission = submission test_resource.extension = '.png' result = test_formatter.format_path(test_resource, tmp_path) result = result.relative_to(tmp_path) assert do_test_path_equality(result.parent, expected) assert len(result.parents) == (len(expected.split('/')) + 1)
def do_test_path_equality(result: Path, expected: str) -> bool: if platform.system() == 'Windows': expected = expected.split('/') expected = [FileNameFormatter._format_for_windows(part) for part in expected] expected = Path(*expected) else: expected = Path(expected) return str(result).endswith(str(expected))
def test_preserve_id_append_when_shortening(test_filename: str, test_ending: str, expected_end: str): result = FileNameFormatter._limit_file_name_length(test_filename, test_ending) assert len(result) <= 255 assert len(result.encode('utf-8')) <= 255 assert isinstance(result, str) assert result.endswith(expected_end)
def test_shorten_path(test_name: str, test_ending: str, tmp_path: Path): result = FileNameFormatter.limit_file_name_length(test_name, test_ending, tmp_path) assert len(str(result.name)) <= 255 assert len(str(result.name).encode('UTF-8')) <= 255 assert len(str(result.name).encode('cp1252')) <= 255 assert len(str(result)) <= FileNameFormatter.find_max_path_length()
def test_windows_max_path(tmp_path: Path): with unittest.mock.patch('platform.system', return_value='Windows'): with unittest.mock.patch('bdfr.file_name_formatter.FileNameFormatter.find_max_path_length', return_value=260): result = FileNameFormatter.limit_file_name_length('test' * 100, '_1.png', tmp_path) assert len(str(result)) <= 260 assert len(result.name) <= (260 - len(str(tmp_path)))
def test_get_max_path_length(): result = FileNameFormatter.find_max_path_length() assert result in (4096, 260, 1024)
def test_time_string_formats(test_time_format: str, expected: str): test_time = datetime(2021, 5, 2, 13, 33) test_formatter = FileNameFormatter('{TITLE}', '', test_time_format) result = test_formatter._convert_timestamp(test_time.timestamp()) assert result == expected
def test_convert_timestamp(test_datetime: datetime, expected: str): test_timestamp = test_datetime.timestamp() test_formatter = FileNameFormatter('{POSTID}', '', 'ISO') result = test_formatter._convert_timestamp(test_timestamp) assert result == expected
def test_convert_unicode_escapes(test_string: str, expected: str): result = FileNameFormatter._convert_unicode_escapes(test_string) assert result == expected
def test_preserve_emojis(test_name_string: str, expected: str, submission: MagicMock): submission.title = test_name_string test_formatter = FileNameFormatter('{TITLE}', '', 'ISO') result = test_formatter._format_name(submission, '{TITLE}') assert do_test_string_equality(result, expected)
def do_test_string_equality(result: [Path, str], expected: str) -> bool: if platform.system() == 'Windows': expected = FileNameFormatter._format_for_windows(expected) return str(result).endswith(expected)
def test_format_name_mock(format_string: str, expected: str, submission: MagicMock): result = FileNameFormatter._format_name(submission, format_string) assert result == expected
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
def test_check_format_string_validity(test_string: str, expected: bool): result = FileNameFormatter.validate_string(test_string) assert result == expected
def test_limit_filename_length(test_filename: str, test_ending: str): result = FileNameFormatter.limit_file_name_length(test_filename, test_ending, Path('.')) assert len(result.name) <= 255 assert len(result.name.encode('utf-8')) <= 255 assert len(str(result)) <= FileNameFormatter.find_max_path_length() assert isinstance(result, Path)
def test_preserve_id_append_when_shortening(test_filename: str, test_ending: str, expected_end: str): result = FileNameFormatter.limit_file_name_length(test_filename, test_ending, Path('.')) assert len(result.name) <= 255 assert len(result.name.encode('utf-8')) <= 255 assert result.name.endswith(expected_end) assert len(str(result)) <= FileNameFormatter.find_max_path_length()
def test_format_name_mock(test_format_string: str, expected: str, submission: MagicMock): test_formatter = FileNameFormatter(test_format_string, '', 'ISO') result = test_formatter._format_name(submission, test_format_string) assert do_test_string_equality(result, expected)
def test_format_file_name_for_windows(test_string: str, expected: str): result = FileNameFormatter._format_for_windows(test_string) assert result == expected
def test_format_name_real(test_format_string: str, expected: str, reddit_submission: praw.models.Submission): test_formatter = FileNameFormatter(test_format_string, '', '') result = test_formatter._format_name(reddit_submission, test_format_string) assert do_test_string_equality(result, expected)
def test_strip_emojies(test_string: str, expected: str): result = FileNameFormatter._strip_emojis(test_string) assert result == expected