def test_content_subreddit_from_name_query(prompt, query, reddit, terminal, config): SubredditContent.from_name(reddit, config, prompt, terminal.loader, query=query)
def test_content_subreddit_from_name_invalid(prompt, reddit, terminal, config): with terminal.loader(): SubredditContent.from_name(reddit, config, prompt, terminal.loader) assert isinstance(terminal.loader.exception, praw.errors.InvalidSubreddit) # Must always have an argument because it gets displayed assert terminal.loader.exception.args[0]
def test_content_subreddit_multireddit(reddit, terminal, config): name = '/r/python+linux' content = SubredditContent.from_name(reddit, config, name, terminal.loader) assert content.name == '/r/python+linux' # Invalid multireddit name = '/r/a+b' with terminal.loader(): SubredditContent.from_name(reddit, config, name, terminal.loader) assert isinstance(terminal.loader.exception, praw.errors.NotFound)
def test_content_subreddit_from_name(prompt, name, order, reddit, terminal, config): content = SubredditContent.from_name(reddit, config, prompt, terminal.loader) assert content.name == name assert content.order == order
def test_content_subreddit_saved(reddit, oauth, refresh_token, terminal, config): # Not logged in with terminal.loader(): SubredditContent.from_name(reddit, config, '/u/me/saved', terminal.loader) assert isinstance(terminal.loader.exception, exceptions.AccountError) # Logged in oauth.config.refresh_token = refresh_token oauth.authorize() with terminal.loader(): SubredditContent.from_name(reddit, config, '/u/me/saved', terminal.loader)
def test_content_subreddit_initialize_invalid(reddit, terminal, config): submissions = reddit.get_subreddit('invalidsubreddit7').get_top(limit=None) with terminal.loader(): SubredditContent(config, 'python', submissions, terminal.loader, 'top') assert isinstance(terminal.loader.exception, praw.errors.InvalidSubreddit)
def test_content_subreddit_initialize(reddit, terminal, config): submissions = reddit.get_subreddit('python').get_top(limit=None) content = SubredditContent(config, 'python', submissions, terminal.loader, 'top') assert content.name == 'python' assert content.order == 'top' assert content.range == (0, 0)
def test_content_subreddit_nsfw_filter(reddit, oauth, refresh_token, terminal, config): # NSFW subreddits should load if not logged in name = '/r/ImGoingToHellForThis' SubredditContent.from_name(reddit, config, name, terminal.loader) # Log in oauth.config.refresh_token = refresh_token oauth.authorize() # Make sure the API parameter hasn't changed assert reddit.user.over_18 is not None # Turn on safe search reddit.user.over_18 = False # Should refuse to load this subreddit with pytest.raises(exceptions.SubredditError): name = '/r/ImGoingToHellForThis' SubredditContent.from_name(reddit, config, name, terminal.loader) # Should filter out all of the nsfw posts name = '/r/ImGoingToHellForThis+python' content = SubredditContent.from_name(reddit, config, name, terminal.loader) for data in islice(content.iterate(0, 1), 50): assert data['object'].over_18 is False # Turn off safe search reddit.user.over_18 = True # The NSFW subreddit should load now name = '/r/ImGoingToHellForThis' SubredditContent.from_name(reddit, config, name, terminal.loader)
def test_content_subreddit_from_name_authenticated(prompt, name, order, reddit, terminal, config, oauth, refresh_token): with pytest.raises(exceptions.AccountError): SubredditContent.from_name(reddit, config, prompt, terminal.loader) # Login and try again oauth.config.refresh_token = refresh_token oauth.authorize() if '{username}' in name: name = name.format(username=reddit.user.name) content = SubredditContent.from_name(reddit, config, prompt, terminal.loader) assert content.name == name assert content.order == order
def test_content_subreddit_me(reddit, oauth, refresh_token, terminal, config): # Not logged in with terminal.loader(): SubredditContent.from_name(reddit, config, '/u/me', terminal.loader) assert isinstance(terminal.loader.exception, exceptions.AccountError) # Logged in oauth.config.refresh_token = refresh_token oauth.authorize() with terminal.loader(): SubredditContent.from_name(reddit, config, '/u/me', terminal.loader) # If there is no submitted content, an error should be raised if terminal.loader.exception: assert isinstance(terminal.loader.exception, exceptions.NoSubmissionsError) assert terminal.loader.exception.name == '/u/me'
def test_content_subreddit_load_more(reddit, terminal, config): config['look_and_feel'] = 'default' submissions = reddit.get_front_page(limit=None) content = SubredditContent(config, 'front', submissions, terminal.loader) assert content.get(50)['type'] == 'Submission' assert content.range == (0, 50) for i, data in enumerate(islice(content.iterate(0, 1), 0, 50)): assert all(k in data for k in ('object', 'n_rows', 'h_offset', 'type', 'index', 'title', 'split_title')) # All text should be converted to unicode by this point for val in data.values(): assert not isinstance(val, six.binary_type) # Index be appended to each title, starting at "1." and incrementing assert data['index'] == i + 1 assert data['title'].startswith(six.text_type(i + 1))
def test_content_subreddit_from_name_order(reddit, terminal, config): # Explicit order trumps implicit name = '/r/python/top' content = SubredditContent.from_name(reddit, config, name, terminal.loader, order='new') assert content.name == '/r/python' assert content.order == 'new'
def test_content_subreddit(reddit, terminal, config): submissions = reddit.get_front_page(limit=5) content = SubredditContent(config, 'front', submissions, terminal.loader) # Submissions are loaded on demand, excluding for the first one assert content.range == (0, 0) assert content.get(0)['type'] == 'Submission' for data in content.iterate(0, 1): assert all(k in data for k in ('object', 'n_rows', 'h_offset', 'type', 'index', 'title', 'split_title', 'hidden')) # All text should be converted to unicode by this point for val in data.values(): assert not isinstance(val, six.binary_type) # Out of bounds with pytest.raises(IndexError): content.get(-1) with pytest.raises(IndexError): content.get(5)
def test_content_subreddit_gilded(reddit, terminal, config): name = '/r/python/gilded' content = SubredditContent.from_name(reddit, config, name, terminal.loader) assert content.order == 'gilded' assert content.get(0)['object'].gilded
def test_content_subreddit_random(reddit, terminal, config): name = '/r/random' content = SubredditContent.from_name(reddit, config, name, terminal.loader) assert content.name.startswith('/r/') assert content.name != name