Add informative error when testing user existence

This commit is contained in:
Serene-Arc
2021-04-27 15:10:29 +10:00
parent e6551bb797
commit 17499baf61
2 changed files with 49 additions and 16 deletions

View File

@@ -314,8 +314,10 @@ class RedditDownloader:
def _get_user_data(self) -> list[Iterator]: def _get_user_data(self) -> list[Iterator]:
if any([self.args.submitted, self.args.upvoted, self.args.saved]): if any([self.args.submitted, self.args.upvoted, self.args.saved]):
if self.args.user: if self.args.user:
if not self._check_user_existence(self.args.user): try:
logger.error(f'User {self.args.user} does not exist') self._check_user_existence(self.args.user)
except errors.BulkDownloaderException as e:
logger.error(e)
return [] return []
generators = [] generators = []
if self.args.submitted: if self.args.submitted:
@@ -339,14 +341,16 @@ class RedditDownloader:
else: else:
return [] return []
def _check_user_existence(self, name: str) -> bool: def _check_user_existence(self, name: str):
user = self.reddit_instance.redditor(name=name) user = self.reddit_instance.redditor(name=name)
try: try:
if not user.id: if user.id:
return False return
except (prawcore.exceptions.NotFound, AttributeError): except prawcore.exceptions.NotFound:
return False raise errors.BulkDownloaderException(f'Could not find user {name}')
return True except AttributeError:
if hasattr(user, 'is_suspended'):
raise errors.BulkDownloaderException(f'User {name} is banned')
def _create_file_name_formatter(self) -> FileNameFormatter: def _create_file_name_formatter(self) -> FileNameFormatter:
return FileNameFormatter(self.args.file_scheme, self.args.folder_scheme) return FileNameFormatter(self.args.file_scheme, self.args.folder_scheme)

View File

@@ -462,17 +462,46 @@ def test_read_excluded_submission_ids_from_file(downloader_mock: MagicMock, tmp_
@pytest.mark.online @pytest.mark.online
@pytest.mark.reddit @pytest.mark.reddit
@pytest.mark.parametrize(('test_redditor_name', 'expected'), ( @pytest.mark.parametrize('test_redditor_name', (
('anthonyhui', True), # Real 'Paracortex',
('lhnhfkuhwreolo', False), # Fake 'crowdstrike',
('Bree-Boo', False), # Banned 'HannibalGoddamnit',
)) ))
def test_check_user_existence( def test_check_user_existence_good(
test_redditor_name: str,
reddit_instance: praw.Reddit,
downloader_mock: MagicMock,
):
downloader_mock.reddit_instance = reddit_instance
RedditDownloader._check_user_existence(downloader_mock, test_redditor_name)
@pytest.mark.online
@pytest.mark.reddit
@pytest.mark.parametrize('test_redditor_name', (
'lhnhfkuhwreolo',
'adlkfmnhglojh',
))
def test_check_user_existence_nonexistent(
test_redditor_name: str, test_redditor_name: str,
expected: bool,
reddit_instance: praw.Reddit, reddit_instance: praw.Reddit,
downloader_mock: MagicMock, downloader_mock: MagicMock,
): ):
downloader_mock.reddit_instance = reddit_instance downloader_mock.reddit_instance = reddit_instance
result = RedditDownloader._check_user_existence(downloader_mock, test_redditor_name) with pytest.raises(BulkDownloaderException, match='Could not find'):
assert result == expected RedditDownloader._check_user_existence(downloader_mock, test_redditor_name)
@pytest.mark.online
@pytest.mark.reddit
@pytest.mark.parametrize('test_redditor_name', (
'Bree-Boo',
))
def test_check_user_existence_banned(
test_redditor_name: str,
reddit_instance: praw.Reddit,
downloader_mock: MagicMock,
):
downloader_mock.reddit_instance = reddit_instance
with pytest.raises(BulkDownloaderException, match='is banned'):
RedditDownloader._check_user_existence(downloader_mock, test_redditor_name)