1
0
Fork 0
mirror of synced 2024-05-29 16:40:06 +12:00

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]:
if any([self.args.submitted, self.args.upvoted, self.args.saved]):
if self.args.user:
if not self._check_user_existence(self.args.user):
logger.error(f'User {self.args.user} does not exist')
try:
self._check_user_existence(self.args.user)
except errors.BulkDownloaderException as e:
logger.error(e)
return []
generators = []
if self.args.submitted:
@ -339,14 +341,16 @@ class RedditDownloader:
else:
return []
def _check_user_existence(self, name: str) -> bool:
def _check_user_existence(self, name: str):
user = self.reddit_instance.redditor(name=name)
try:
if not user.id:
return False
except (prawcore.exceptions.NotFound, AttributeError):
return False
return True
if user.id:
return
except prawcore.exceptions.NotFound:
raise errors.BulkDownloaderException(f'Could not find user {name}')
except AttributeError:
if hasattr(user, 'is_suspended'):
raise errors.BulkDownloaderException(f'User {name} is banned')
def _create_file_name_formatter(self) -> FileNameFormatter:
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.reddit
@pytest.mark.parametrize(('test_redditor_name', 'expected'), (
('anthonyhui', True), # Real
('lhnhfkuhwreolo', False), # Fake
('Bree-Boo', False), # Banned
@pytest.mark.parametrize('test_redditor_name', (
'Paracortex',
'crowdstrike',
'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,
expected: bool,
reddit_instance: praw.Reddit,
downloader_mock: MagicMock,
):
downloader_mock.reddit_instance = reddit_instance
result = RedditDownloader._check_user_existence(downloader_mock, test_redditor_name)
assert result == expected
with pytest.raises(BulkDownloaderException, match='Could not find'):
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)