Skip to content

Commit

Permalink
Fix test and lint issues with Refreshable merge.
Browse files Browse the repository at this point in the history
  • Loading branch information
bboe committed Sep 23, 2012
2 parents f52f113 + dff2955 commit 77e3fff
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
29 changes: 25 additions & 4 deletions praw/objects.py
Expand Up @@ -220,6 +220,27 @@ def send_message(self, subject, message):
return self.reddit_session.send_message(self, subject, message)


class Refreshable(RedditContentObject):
"""Interface for objects that can be refreshed."""
def refresh(self):
"""
Requery to update object with latest values.
Note that if this call is made within cache_timeout as specified in
praw.ini then this will return the cached content. Any listing, such
as the submissions on a subreddits top page, will automatically be
refreshed serverside. Refreshing a submission will also refresh all its
comments.
"""
if isinstance(self, Redditor):
other = Redditor(self.reddit_session, self.name)
elif isinstance(self, Subreddit):
other = Subreddit(self.reddit_session, self.display_name)
elif isinstance(self, Submission):
other = Submission.get_info(self.reddit_session, self.permalink)
self.__dict__ = other.__dict__ # pylint: disable-msg=W0201


class Reportable(RedditContentObject):
"""Interface for RedditContentObjects that can be reported."""
@require_login
Expand Down Expand Up @@ -391,7 +412,7 @@ def comments(self, update=True):
return self._comments


class Redditor(Messageable):
class Redditor(Messageable, Refreshable):
"""A class representing the users of reddit."""
get_overview = _get_section('')
get_comments = _get_section('comments')
Expand Down Expand Up @@ -482,8 +503,8 @@ def my_reddits(self, limit=0):
return self.reddit_session.get_content(url, limit=limit)


class Submission(Approvable, Deletable, Distinguishable, Editable, Reportable,
Saveable, Voteable):
class Submission(Approvable, Deletable, Distinguishable, Editable, Refreshable,
Reportable, Saveable, Voteable):
"""A class for submissions to reddit."""
@staticmethod
def get_info(reddit_session, url, comments_only=False):
Expand Down Expand Up @@ -692,7 +713,7 @@ def short_link(self):
return urljoin(self.reddit_session.config.short_domain, self.id)


class Subreddit(Messageable):
class Subreddit(Messageable, Refreshable):
"""A class for Subreddits."""
ban = _modify_relationship('banned', is_sub=True)
unban = _modify_relationship('banned', unlink=True, is_sub=True)
Expand Down
30 changes: 30 additions & 0 deletions praw/tests.py
Expand Up @@ -707,6 +707,36 @@ def test_user_set_on_login(self):
self.assertTrue(isinstance(self.r.user, LoggedInRedditor))


class RefreshableTest(unittest.TestCase, AuthenticatedHelper):
def setUp(self):
self.configure()
self.real_timeout = self.r.config.cache_timeout
self.r.config.cache_timeout = 0

def tearDown(self):
self.r.config.cache_timeout = self.real_timeout

def test_refresh_subreddit(self):
subreddit = self.r.get_subreddit(self.sr)
new_description = 'Description %s' % uuid.uuid4()
subreddit.update_settings(public_description=new_description)
self.assertNotEqual(subreddit.public_description, new_description)
subreddit.refresh()
self.assertEqual(subreddit.public_description, new_description)

def test_refresh_submission(self):
subreddit = self.r.get_subreddit(self.sr)
submission = six_next(subreddit.get_top())
same_submission = self.r.get_submission(submission_id=submission.id)
if submission.likes:
submission.downvote()
else:
submission.upvote()
self.assertEqual(submission.likes, same_submission.likes)
submission.refresh()
self.assertNotEqual(submission.likes, same_submission.likes)


class SubmissionTest(unittest.TestCase, AuthenticatedHelper):
def setUp(self):
self.configure()
Expand Down

0 comments on commit 77e3fff

Please sign in to comment.