ListRedditsContent.from_user method replaced with from_func; updated tests accordingly
This commit is contained in:
@@ -388,7 +388,7 @@ class SubredditContent(Content):
|
|||||||
# Strip leading, trailing, and redundant backslashes
|
# Strip leading, trailing, and redundant backslashes
|
||||||
name_list = [seg for seg in name.strip(' /').split('/') if seg]
|
name_list = [seg for seg in name.strip(' /').split('/') if seg]
|
||||||
name_order = None
|
name_order = None
|
||||||
if name_list[0] in ['r', 'u', 'user', 'domain'] and len(name_list) > 1:
|
if len(name_list) > 1 and name_list[0] in ['r', 'u', 'user', 'domain']:
|
||||||
listing, name_list = name_list[0], name_list[1:]
|
listing, name_list = name_list[0], name_list[1:]
|
||||||
if len(name_list) == 2:
|
if len(name_list) == 2:
|
||||||
name, name_order = name_list
|
name, name_order = name_list
|
||||||
@@ -540,8 +540,8 @@ class ListRedditsContent(Content):
|
|||||||
raise exceptions.ListRedditsError('No {}'.format(self.name))
|
raise exceptions.ListRedditsError('No {}'.format(self.name))
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_user(cls, name, reddits, loader):
|
def from_func(cls, name, func, loader):
|
||||||
reddits = (r for r in reddits)
|
reddits = (r for r in func())
|
||||||
return cls(name, reddits, loader)
|
return cls(name, reddits, loader)
|
||||||
|
|
||||||
def get(self, index, n_cols=70):
|
def get(self, index, n_cols=70):
|
||||||
|
|||||||
@@ -14,12 +14,13 @@ class ListRedditsController(PageController):
|
|||||||
|
|
||||||
class ListRedditsPage(Page):
|
class ListRedditsPage(Page):
|
||||||
|
|
||||||
def __init__(self, reddit, name, reddits, term, config, oauth):
|
def __init__(self, reddit, name, func, term, config, oauth):
|
||||||
super(ListRedditsPage, self).__init__(reddit, term, config, oauth)
|
super(ListRedditsPage, self).__init__(reddit, term, config, oauth)
|
||||||
|
|
||||||
self.controller = ListRedditsController(self, keymap=config.keymap)
|
self.controller = ListRedditsController(self, keymap=config.keymap)
|
||||||
self.name = name
|
self.name = name
|
||||||
self.content = ListRedditsContent.from_user(name, reddits, term.loader)
|
self.func = func
|
||||||
|
self.content = ListRedditsContent.from_func(name, func, term.loader)
|
||||||
self.nav = Navigator(self.content.get)
|
self.nav = Navigator(self.content.get)
|
||||||
self.reddit_data = None
|
self.reddit_data = None
|
||||||
|
|
||||||
@@ -33,8 +34,8 @@ class ListRedditsPage(Page):
|
|||||||
return
|
return
|
||||||
|
|
||||||
with self.term.loader():
|
with self.term.loader():
|
||||||
self.content = ListRedditsContent.from_user(self.name, self.reddit,
|
self.content = ListRedditsContent.from_func(self.name,
|
||||||
self.term.loader)
|
self.func, self.term.loader)
|
||||||
if not self.term.loader.exception:
|
if not self.term.loader.exception:
|
||||||
self.nav = Navigator(self.content.get)
|
self.nav = Navigator(self.content.get)
|
||||||
|
|
||||||
|
|||||||
@@ -156,10 +156,10 @@ class SubredditPage(Page):
|
|||||||
def open_subscriptions(self):
|
def open_subscriptions(self):
|
||||||
"Open user subscriptions page"
|
"Open user subscriptions page"
|
||||||
|
|
||||||
|
func = lambda : self.reddit.get_my_subreddits(limit=None)
|
||||||
with self.term.loader('Loading subscriptions'):
|
with self.term.loader('Loading subscriptions'):
|
||||||
page = ListRedditsPage(self.reddit, 'My Subscriptions',
|
page = ListRedditsPage(self.reddit, 'My Subscriptions', func,
|
||||||
self.reddit.get_my_subreddits(limit=None), self.term,
|
self.term, self.config, self.oauth)
|
||||||
self.config, self.oauth)
|
|
||||||
if self.term.loader.exception:
|
if self.term.loader.exception:
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -176,10 +176,10 @@ class SubredditPage(Page):
|
|||||||
def open_multireddit_subscriptions(self):
|
def open_multireddit_subscriptions(self):
|
||||||
"Open user multireddit subscriptions page"
|
"Open user multireddit subscriptions page"
|
||||||
|
|
||||||
|
func = lambda : self.reddit.get_my_multireddits()
|
||||||
with self.term.loader('Loading multireddits'):
|
with self.term.loader('Loading multireddits'):
|
||||||
page = ListRedditsPage(self.reddit,
|
page = ListRedditsPage(self.reddit,
|
||||||
'My Multireddits', self.reddit.get_my_multireddits(),
|
'My Multireddits', func, self.term, self.config, self.oauth)
|
||||||
self.term, self.config, self.oauth)
|
|
||||||
if self.term.loader.exception:
|
if self.term.loader.exception:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|||||||
@@ -210,14 +210,11 @@ def subreddit_page(reddit, terminal, config, oauth):
|
|||||||
|
|
||||||
|
|
||||||
@pytest.fixture()
|
@pytest.fixture()
|
||||||
def reddits(reddit, terminal, config, oauth):
|
def list_reddits_page(reddit, terminal, config, oauth):
|
||||||
return reddit.get_popular_subreddits()
|
title = 'Popular Subreddits'
|
||||||
|
func = reddit.get_popular_subreddits
|
||||||
|
|
||||||
@pytest.fixture()
|
|
||||||
def list_reddits_page(reddit, name, reddits, terminal, config, oauth):
|
|
||||||
with terminal.loader():
|
with terminal.loader():
|
||||||
page = ListRedditsPage(reddit, name, reddits, terminal, config, oauth)
|
page = ListRedditsPage(reddit, title, func, terminal, config, oauth)
|
||||||
assert terminal.loader.exception is None
|
assert terminal.loader.exception is None
|
||||||
page.draw()
|
page.draw()
|
||||||
return page
|
return page
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import praw
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from rtv.content import (
|
from rtv.content import (
|
||||||
Content, SubmissionContent, SubredditContent, SubscriptionContent)
|
Content, SubmissionContent, SubredditContent, ListRedditsContent)
|
||||||
from rtv import exceptions
|
from rtv import exceptions
|
||||||
|
|
||||||
|
|
||||||
@@ -291,7 +291,6 @@ def test_content_subreddit_from_name(reddit, terminal):
|
|||||||
# Queries
|
# Queries
|
||||||
SubredditContent.from_name(reddit, 'front', terminal.loader, query='pea')
|
SubredditContent.from_name(reddit, 'front', terminal.loader, query='pea')
|
||||||
SubredditContent.from_name(reddit, 'python', terminal.loader, query='pea')
|
SubredditContent.from_name(reddit, 'python', terminal.loader, query='pea')
|
||||||
SubredditContent.from_name(reddit, 'me', terminal.loader, query='pea')
|
|
||||||
|
|
||||||
|
|
||||||
def test_content_subreddit_multireddit(reddit, terminal):
|
def test_content_subreddit_multireddit(reddit, terminal):
|
||||||
@@ -333,23 +332,16 @@ def test_content_subreddit_me(reddit, oauth, refresh_token, terminal):
|
|||||||
assert isinstance(terminal.loader.exception, exceptions.SubredditError)
|
assert isinstance(terminal.loader.exception, exceptions.SubredditError)
|
||||||
|
|
||||||
|
|
||||||
def test_content_subscription(reddit, oauth, refresh_token, terminal):
|
def test_content_list_reddits(reddit, oauth, refresh_token, terminal):
|
||||||
|
|
||||||
# Not logged in
|
title = 'Popular Subreddits'
|
||||||
|
func = reddit.get_popular_subreddits
|
||||||
with terminal.loader():
|
with terminal.loader():
|
||||||
SubscriptionContent.from_user(reddit, terminal.loader)
|
content = ListRedditsContent.from_func(title, func, terminal.loader)
|
||||||
assert isinstance(
|
|
||||||
terminal.loader.exception, praw.errors.LoginOrScopeRequired)
|
|
||||||
|
|
||||||
# Logged in
|
|
||||||
oauth.config.refresh_token = refresh_token
|
|
||||||
oauth.authorize()
|
|
||||||
with terminal.loader():
|
|
||||||
content = SubscriptionContent.from_user(reddit, terminal.loader)
|
|
||||||
assert terminal.loader.exception is None
|
assert terminal.loader.exception is None
|
||||||
|
|
||||||
# These are static
|
# These are static
|
||||||
assert content.name == 'Subscriptions'
|
assert content.name == title
|
||||||
assert content.order is None
|
assert content.order is None
|
||||||
|
|
||||||
# Validate content
|
# Validate content
|
||||||
@@ -361,11 +353,11 @@ def test_content_subscription(reddit, oauth, refresh_token, terminal):
|
|||||||
assert not isinstance(val, six.binary_type)
|
assert not isinstance(val, six.binary_type)
|
||||||
|
|
||||||
|
|
||||||
def test_content_subscription_empty(terminal):
|
def test_content_list_reddits_empty(terminal):
|
||||||
|
|
||||||
# Simulate an empty subscription generator
|
# Simulate an empty list of reddits
|
||||||
subscriptions = iter([])
|
func = lambda : iter([])
|
||||||
|
|
||||||
with terminal.loader():
|
with terminal.loader():
|
||||||
SubscriptionContent(subscriptions, terminal.loader)
|
ListRedditsContent('test', func(), terminal.loader())
|
||||||
assert isinstance(terminal.loader.exception, exceptions.SubscriptionError)
|
assert isinstance(terminal.loader.exception, exceptions.ListRedditsError)
|
||||||
|
|||||||
@@ -14,13 +14,14 @@ except ImportError:
|
|||||||
import mock
|
import mock
|
||||||
|
|
||||||
|
|
||||||
def test_list_reddits_page_construct(reddit, reddits, terminal, config,
|
def test_list_reddits_page_construct(reddit, terminal, config,
|
||||||
oauth, refresh_token):
|
oauth, refresh_token):
|
||||||
window = terminal.stdscr.subwin
|
window = terminal.stdscr.subwin
|
||||||
title = 'Subscriptions'
|
title = 'Popular Subreddits'
|
||||||
|
func = reddit.get_popular_subreddits
|
||||||
|
|
||||||
with terminal.loader():
|
with terminal.loader():
|
||||||
page = ListRedditsPage(reddit, title, reddits, terminal, config, oauth)
|
page = ListRedditsPage(reddit, title, func, terminal, config, oauth)
|
||||||
assert terminal.loader.exception is None
|
assert terminal.loader.exception is None
|
||||||
|
|
||||||
page.draw()
|
page.draw()
|
||||||
@@ -49,7 +50,7 @@ def test_list_reddits_page_construct(reddit, reddits, terminal, config,
|
|||||||
terminal.stdscr.ncols = 20
|
terminal.stdscr.ncols = 20
|
||||||
terminal.stdscr.nlines = 10
|
terminal.stdscr.nlines = 10
|
||||||
with terminal.loader():
|
with terminal.loader():
|
||||||
page = ListRedditsPage(reddit, title, reddits, terminal, config, oauth)
|
page = ListRedditsPage(reddit, title, func, terminal, config, oauth)
|
||||||
assert terminal.loader.exception is None
|
assert terminal.loader.exception is None
|
||||||
|
|
||||||
page.draw()
|
page.draw()
|
||||||
|
|||||||
Reference in New Issue
Block a user