def test_content_wrap_text(): text = 'four score\nand seven\n\n' assert Content.wrap_text(text, 6) == ['four', 'score', 'and', 'seven', ''] assert Content.wrap_text(text, 15) == ['four score', 'and seven', ''] assert Content.wrap_text('', 70) == [] assert Content.wrap_text('\n\n\n\n', 70) == ['', '', '', '']
def test_content_extract_links(): # Should handle relative & absolute links, should ignore empty links. html = """ <a href='/'>Home Page</a> <a href='https://www.github.com'>Github</a> <a>Blank</a> """ assert Content.extract_links(html) == [{ 'href': 'https://www.reddit.com/', 'text': 'Home Page' }, { 'href': 'https://www.github.com', 'text': 'Github' }]
def test_content_humanize_timestamp(): timestamp = time.time() - 30 assert Content.humanize_timestamp(timestamp) == '0min' assert Content.humanize_timestamp(timestamp, True) == 'moments ago' timestamp = time.time() - 60 * 60 * 24 * 30.4 * 12 assert Content.humanize_timestamp(timestamp) == '11month' assert Content.humanize_timestamp(timestamp, True) == '11 months ago' timestamp = time.time() - 60 * 60 * 24 * 30.4 * 12 * 5 assert Content.humanize_timestamp(timestamp) == '5yr' assert Content.humanize_timestamp(timestamp, True) == '5 years ago'
def test_content_flatten_comments_2(reddit): # Grab a large MoreComments instance to test url = 'https://www.reddit.com/r/CollegeBasketball/comments/31owr1' submission = reddit.get_submission(url, comment_sort='top') more_comment = submission.comments[-1] assert isinstance(more_comment, praw.objects.MoreComments) # Make sure that all comments are displayed one level below their parents comments = more_comment.comments() flattened = Content.flatten_comments(comments) for i, item in enumerate(flattened): for j in range(i - 1, -1, -1): prev = flattened[j] if item.parent_id and item.parent_id.endswith(prev.id): x, y = item.nested_level, prev.nested_level assert item.nested_level == prev.nested_level + 1 break else: assert item.nested_level == 0
def test_content_flatten_comments(reddit): # Grab a large MoreComments instance to test url = 'https://www.reddit.com/r/AskReddit/comments/cmwov' submission = reddit.get_submission(url, comment_sort='top') more_comment = submission.comments[-1] assert isinstance(more_comment, praw.objects.MoreComments) # Double check that reddit's api hasn't changed the response structure comments = more_comment.comments() top_level_comments = [] for comment in comments[:-1]: if comment.parent_id == more_comment.parent_id: top_level_comments.append(comment.id) else: # Sometimes replies are returned below their parents instead of # being automatically nested. In this case, make sure the parent_id # of the comment matches the most recent top level comment. if not comment.parent_id.endswith(top_level_comments[-1]): pass # The last item should be a MoreComments linked to the original parent top_level_comments.append(comments[-1].id) assert isinstance(comments[-1], praw.objects.MoreComments) assert comments[-1].parent_id == more_comment.parent_id flattened = Content.flatten_comments(comments, root_level=2) # Because the comments returned by praw's comment.comments() don't have # nested replies, the flattened size should not change. assert len(flattened) == len(comments) for i, comment in enumerate(flattened): # Order should be preserved assert comment.id == comments[i].id # And the nested level should be added if comment.id in top_level_comments: assert comment.nested_level == 2 else: assert comment.nested_level > 2
def test_content_flatten_comments(reddit): # Grab a large MoreComments instance to test url = 'https://www.reddit.com/r/AskReddit/comments/cmwov' submission = reddit.get_submission(url, comment_sort='top') more_comment = submission.comments[-1] assert isinstance(more_comment, praw.objects.MoreComments) # Double check that reddit's api hasn't changed the response structure comments = more_comment.comments() top_level_comments = [] for comment in comments[:-1]: if comment.parent_id == more_comment.parent_id: top_level_comments.append(comment.id) else: # Sometimes replies are returned below their parents instead of # being automatically nested. In this case, make sure the parent_id # of the comment matches the most recent top level comment. assert comment.parent_id.endswith(top_level_comments[-1]) # The last item should be a MoreComments linked to the original parent top_level_comments.append(comments[-1].id) assert isinstance(comments[-1], praw.objects.MoreComments) assert comments[-1].parent_id == more_comment.parent_id flattened = Content.flatten_comments(comments, root_level=2) # Because the comments returned by praw's comment.comments() don't have # nested replies, the flattened size should not change. assert len(flattened) == len(comments) for i, comment in enumerate(flattened): # Order should be preserved assert comment.id == comments[i].id # And the nested level should be added if comment.id in top_level_comments: assert comment.nested_level == 2 else: assert comment.nested_level > 2
def test_content_flatten_comments_3(reddit): # Build the comment structure as described in issue # https://github.com/michael-lazar/rtv/issues/327 class MockComment(object): def __init__(self, comment_id, parent_id='t3_xxxxx'): self.id = comment_id self.parent_id = parent_id self.replies = [] def __repr__(self): return '%s (%s)' % (self.id, self.parent_id) # This is an example of something that might be returned by PRAW after # clicking to expand a "More comments [6]" link. comments = [ MockComment('axxxx'), MockComment('a1xxx', parent_id='t1_axxxx'), MockComment('a11xx', parent_id='t1_a1xxx'), MockComment('a12xx', parent_id='t1_a1xxx'), MockComment('a2xxx', parent_id='t1_axxxx'), MockComment('a3xxx', parent_id='t1_axxxx'), MockComment('bxxxx'), ] # Make sure that all comments are displayed one level below their parents flattened = Content.flatten_comments(comments) for i, item in enumerate(flattened): for j in range(i - 1, -1, -1): prev = flattened[j] if item.parent_id and item.parent_id.endswith(prev.id): x, y = item.nested_level, prev.nested_level assert item.nested_level == prev.nested_level + 1 break else: assert item.nested_level == 0