def test_enable_inbox_replies(self): self.reddit.read_only = False submission = Submission(self.reddit, "6ckfdz") with self.recorder.use_cassette( "TestSubmission.test_enable_inbox_replies" ): submission.enable_inbox_replies()
def test_delete(self, _): self.reddit.read_only = False with self.recorder.use_cassette("TestSubmission.test_delete"): submission = Submission(self.reddit, "4b1tfm") submission.delete() assert submission.author is None assert submission.selftext == "[deleted]"
def test_edit(self, _): self.reddit.read_only = False with self.recorder.use_cassette( 'TestSubmission.test_edit'): submission = Submission(self.reddit, '4b1tfm') submission.edit('New text') assert submission.selftext == 'New text'
def test_id_from_url__invalid_urls(self): urls = ['', '1', '/', 'my.it/2gmzqe', 'http://my.it/_', 'https://redd.it/_/', 'http://reddit.com/comments/_/2gmzqe'] for url in urls: with pytest.raises(ClientException): Submission.id_from_url(url)
def test_reply(self): self.reddit.read_only = False with self.recorder.use_cassette("TestSubmission.test_reply"): submission = Submission(self.reddit, "4b1tfm") comment = submission.reply("Test reply") assert comment.author == self.reddit.config.username assert comment.body == "Test reply" assert comment.parent_id == submission.fullname
def test_replace__all_with_comment_limit(self): with self.recorder.use_cassette( 'TestCommentForest.test_replace__all_with_comment_limit', match_requests_on=['uri', 'method', 'body']): submission = Submission(self.reddit, '3hahrw') submission.comment_limit = 10 skipped = submission.comments.replace_more(None, threshold=0) assert len(skipped) == 0 assert len(submission.comments.list()) >= 500
def test_replace__all_with_comment_sort(self): with self.recorder.use_cassette( "TestCommentForest.test_replace__all_with_comment_sort", match_requests_on=["uri", "method", "body"], ): submission = Submission(self.reddit, "3hahrw") submission.comment_sort = "old" skipped = submission.comments.replace_more(None, threshold=0) assert len(skipped) == 0 assert len(submission.comments.list()) >= 500
def test_id_from_url__invalid_urls(self): urls = [ "", "1", "/", "my.it/2gmzqe", "http://my.it/_", "https://redd.it/_/", "http://reddit.com/comments/_/2gmzqe", "https://reddit.com/r/wallpapers/", "https://reddit.com/r/wallpapers", ] for url in urls: with pytest.raises(ClientException): Submission.id_from_url(url)
def test_iter_saved(self): """Tests that __iter__ behaves as expected""" posts_direct = set() for url in []: submission = Submission(url) submission.save() posts_direct.add(SubmissionWrapper(submission)) reddit = sign_in(os.environ["dummy_user"], os.environ["dummy_pass"]) posts_source = SubmissionSource.Builder().from_user_saved( reddit).build() for direct, sourced in zip(posts_direct, posts_source): self.assertEqual(direct, sourced)
def is_crosspost(cj_post: Submission, relevant_post: Submission) -> bool: duplicates: Iterator[Submission] = cj_post.duplicates() for duplicate in duplicates: if duplicate.id == relevant_post.id: logger.info("Post is a cross-post, not commenting") return True return False
async def process_post(submission: Submission): print("New post: " + submission.title) # Do not respond to memes! # Or non-image posts. # god I miss nullish coalescing if (hasattr(submission, 'link_flair_text') and submission.link_flair_text and ('meme' in submission.link_flair_text.lower() or 'news' in submission.link_flair_text.lower())) or submission.is_self: print("Either this is flaired Meme or this is a self-post.") return # Never ask for sauce twice on the same post. c.execute('SELECT * FROM allposts WHERE id=?', (submission.id, )) if c.fetchone(): print("Duplicate post. Not sure how this happened, but it did.") return # Make sure they have an author in the post. author_matcher = re.compile(r'.*\[.*\].*') if not author_matcher.match(submission.title): comment = submission.reply( "**Post titles must have the author in square brackets.**\n\n To avoid getting your post removed, make sure the author is in the " "title (i.e. [Author] Title).\n\n" + config['suffix']) if comment is not None: comment.mod.distinguish(how='yes', sticky=True) submission.mod.remove(mod_note="No author provided") return print("This is an actual post, asking for sauce...") await ask_for_sauce(submission)
def test_comments(self): with self.recorder.use_cassette( 'TestSubmission.test_comments'): submission = Submission(self.reddit, '2gmzqe') assert len(submission.comments) == 1 assert isinstance(submission.comments[0], Comment) assert isinstance(submission.comments[0].replies[0], Comment)
def test_gild__no_creddits(self): self.reddit.read_only = False with self.recorder.use_cassette( 'TestSubmission.test_gild__no_creddits'): with pytest.raises(BadRequest) as excinfo: Submission(self.reddit, '4b1tfm').gild() reason = excinfo.value.response.json()['reason'] assert 'INSUFFICIENT_CREDDITS' == reason
def test_self_submission_no_has_text_no_has_url(self): submission = Submission( reddit, url= "https://www.reddit.com/r/AskReddit/comments/989eg1/what_real_event_in_your_life_had_it_happened_in_a/", ) assert find_urls_in(submission) is None
def test_id_from_url__invalid_urls(self): urls = [ "", "1", "/", "my.it/2gmzqe", "http://my.it/_", "https://redd.it/_/", "http://reddit.com/comments/_/2gmzqe", "https://reddit.com/r/wallpapers/", "https://reddit.com/r/wallpapers", "https://www.reddit.com/r/test/comments/", "https://reddit.com/comments/", ] for url in urls: with pytest.raises(ClientException): Submission.id_from_url(url)
def test_replace__skip_at_limit(self): with self.recorder.use_cassette( "TestCommentForest.test_replace__skip_at_limit", match_requests_on=["uri", "method", "body"], ): submission = Submission(self.reddit, "3hahrw") skipped = submission.comments.replace_more(1) assert len(skipped) == 17
def test_invalid_attribute(self): with self.recorder.use_cassette( 'TestSubmission.test_invalid_attribute'): submission = Submission(self.reddit, '2gmzqe') with pytest.raises(AttributeError) as excinfo: submission.invalid_attribute assert excinfo.value.args[0] == ("'Submission' object has no attribute" " 'invalid_attribute'")
def test_submission_that_no_has_url(self): submission = Submission( reddit, url= "https://www.reddit.com/r/macbookpro/comments/98a0r1/macbook_pro_15_2018_doesnt_wake_up_properly/", ) assert find_urls_in(submission) is None
def test_gild__no_creddits(self): self.reddit.read_only = False with self.recorder.use_cassette( "TestSubmission.test_gild__no_creddits"): with pytest.raises(BadRequest) as excinfo: Submission(self.reddit, "4b1tfm").gild() reason = excinfo.value.response.json()["reason"] assert "INSUFFICIENT_CREDDITS" == reason
def test_add_removal_reason_without_id_or_note(self, _): self.reddit.read_only = False with self.use_cassette(): with pytest.raises(ValueError) as excinfo: submission = Submission(self.reddit, "e4ds11") submission.mod.remove() submission.mod._add_removal_reason() assert excinfo.value.args[0].startswith("mod_note cannot be blank")
def test_id_from_url(self): urls = ['http://my.it/2gmzqe', 'https://redd.it/2gmzqe', 'http://reddit.com/comments/2gmzqe', 'https://www.reddit.com/r/redditdev/comments/2gmzqe/' 'praw_https_enabled_praw_testing_needed/'] for url in urls: assert Submission.id_from_url(url) == '2gmzqe', url
def test_award__not_enough_coins(self): self.reddit.read_only = False with self.use_cassette(): with pytest.raises(RedditAPIException) as excinfo: Submission(self.reddit, "j3fkiw").award( gild_type="award_2385c499-a1fb-44ec-b9b7-d260f3dc55de") exception = excinfo.value assert "INSUFFICIENT_COINS_WITH_AMOUNT" == exception.error_type
def test_add_removal_reason_without_id(self, _): self.reddit.read_only = False with self.recorder.use_cassette( "TestSubmissionModeration.test_add_removal_reason_without_id" ): submission = Submission(self.reddit, "e3om6k") submission.mod.remove() submission.mod._add_removal_reason(mod_note="Foobar")
def initialize_posts_wrappers( self) -> List[Tuple(Submission, SubmissionWrapper)]: posts_wrappers = [] for url in self.urls: post = Submission(url) wrapper = SubmissionWrapper(post) posts_wrappers.append((post, wrapper)) return posts_wrappers
def test_gild__no_creddits(self): self.reddit.read_only = False with self.recorder.use_cassette( "TestSubmission.test_gild__no_creddits"): with pytest.raises(RedditAPIException) as excinfo: Submission(self.reddit, "4b1tfm").gild() exception = excinfo.value assert "INSUFFICIENT_CREDDITS" == exception.error_type
def test_add_removal_reason(self, _): self.reddit.read_only = False with self.recorder.use_cassette( "TestSubmissionModeration.test_add_removal_reason"): submission = Submission(self.reddit, "e3oo6a") submission.mod.remove() submission.mod._add_removal_reason(mod_note="Foobar", reason_id="110nhral8vygf")
def test_iter_subreddit(self): """Tests that __iter__ behaves as expected""" subreddit_posts = {SubmissionWrapper(Submission(url)) for url in []} reddit = sign_in() source = SubmissionSource.Builder().from_user_saved(reddit).build()
def test_construct_failure(self): message = "Exactly one of `id`, `url`, or `_data` must be provided." with pytest.raises(TypeError) as excinfo: Submission(self.reddit) assert str(excinfo.value) == message with pytest.raises(TypeError) as excinfo: Submission(self.reddit, id="dummy", url="dummy") assert str(excinfo.value) == message with pytest.raises(TypeError) as excinfo: Submission(self.reddit, "dummy", _data={"id": "dummy"}) assert str(excinfo.value) == message with pytest.raises(TypeError) as excinfo: Submission(self.reddit, url="dummy", _data={"id": "dummy"}) assert str(excinfo.value) == message with pytest.raises(TypeError) as excinfo: Submission(self.reddit, "dummy", "dummy", {"id": "dummy"}) assert str(excinfo.value) == message with pytest.raises(ValueError): Submission(self.reddit, "") with pytest.raises(ValueError): Submission(self.reddit, url="")
def update_existing(self, active_games): # For all active/recent games # Format thread for game in active_games: stored_game = self.games[game.id] for thread_type in ('gamethread', 'post_gamethread'): if thread_type in stored_game.threads: # Update body thread_id = stored_game.threads[thread_type] thread = Submission(self.r, id=thread_id) title, body = self.renderer.render_game(game, thread_type, thread=thread) if body is not None and body != thread.selftext: print("Update %s (/r/%s, %s)" % (title, self.sub.display_name, thread_id)) print(diff_strings(thread.selftext, body)) thread.edit(body)
def test_replace__skip_all(self): with self.use_cassette(match_requests_on=["uri", "method", "body"]): submission = Submission(self.reddit, "3hahrw") before_count = len(submission.comments.list()) skipped = submission.comments.replace_more(limit=0) assert len(skipped) == 18 assert all(x.submission == submission for x in skipped) after_count = len(submission.comments.list()) assert before_count == after_count + len(skipped)
def submission(url): data = {} submission = Submission(reddit=reddit, url=url) data['author'] = submission.author data['content'] = submission.selftext data['flair'] = submission.link_flair_text data['subreddit'] = submission.subreddit data['title'] = submission.title return data
def test_award__self_gild(self): self.reddit.read_only = False with self.recorder.use_cassette( "TestSubmission.test_award__self_gild"): with pytest.raises(RedditAPIException) as excinfo: Submission(self.reddit, "j3fkiw").award( gild_type="award_2385c499-a1fb-44ec-b9b7-d260f3dc55de") exception = excinfo.value assert "SELF_GILDING_NOT_ALLOWED" == exception.error_type
def test_replace__skip_below_threshold(self): with self.use_cassette(match_requests_on=["uri", "method", "body"]): submission = Submission(self.reddit, "3hahrw") before_count = len(submission.comments.list()) skipped = submission.comments.replace_more(16, 5) assert len(skipped) == 13 assert all(x.count < 5 for x in skipped) assert all(x.submission == submission for x in skipped) assert before_count < len(submission.comments.list())
def test_id_from_url(self): urls = [ 'http://my.it/2gmzqe', 'https://redd.it/2gmzqe', 'http://reddit.com/comments/2gmzqe', 'https://www.reddit.com/r/redditdev/comments/2gmzqe/' 'praw_https_enabled_praw_testing_needed/' ] for url in urls: assert Submission.id_from_url(url) == '2gmzqe', url
def test_replace__on_comment_from_submission(self): with self.use_cassette(match_requests_on=["uri", "method", "body"]): submission = Submission(self.reddit, "3hahrw") types = [type(x) for x in submission.comments.list()] assert types.count(Comment) == 472 assert types.count(MoreComments) == 18 assert submission.comments[0].replies.replace_more() == [] types = [type(x) for x in submission.comments.list()] assert types.count(Comment) == 489 assert types.count(MoreComments) == 11
def test_replace__all_large(self): with self.recorder.use_cassette( 'TestCommentForest.test_replace__all_large', match_requests_on=['uri', 'method', 'body']): submission = Submission(self.reddit, 'n49rw') skipped = submission.comments.replace_more(None, threshold=0) assert len(skipped) == 0 assert len(submission.comments.list()) > 1000 assert len(submission.comments.list()) == \ len(submission.comments._comments_by_id)
def test_replace__all_large(self): with self.use_cassette(match_requests_on=["uri", "method", "body"]): submission = Submission(self.reddit, "n49rw") skipped = submission.comments.replace_more(None, threshold=0) assert len(skipped) == 0 assert all( isinstance(x, Comment) for x in submission.comments.list()) assert len(submission.comments.list()) > 1000 assert len(submission.comments.list()) == len( submission._comments_by_id)
def test_submission_that_has_url(self): submission = Submission( reddit, url= "https://www.reddit.com/r/worldnews/comments/98aj0i/former_un_chief_kofi_annan_dies_at_80/", ) assert find_urls_in(submission) == [ "http://www.bbc.co.uk/news/world-africa-45232892" ]
def test_self_submission_that_has_url(self): submission = Submission( reddit, url= "https://www.reddit.com/r/hiphopheads/comments/9897up/kodak_black_has_just_been_released_from_prison/", ) assert find_urls_in(submission) == [ "https://www.instagram.com/p/Bmm4J9OgL16/?tagged=kodakblack" ]
def test_disable_inbox_replies(self): self.reddit.read_only = False submission = Submission(self.reddit, '6ckfdz') with self.recorder.use_cassette( 'TestSubmission.test_disable_inbox_replies'): submission.disable_inbox_replies()
def test_edit(self, _): self.reddit.read_only = False with self.recorder.use_cassette("TestSubmission.test_edit"): submission = Submission(self.reddit, "4b1tfm") submission.edit("New text") assert submission.selftext == "New text"
def test_duplicates(self): with self.recorder.use_cassette("TestSubmission.test_duplicates"): submission = Submission(self.reddit, "avj2v") assert len(list(submission.duplicates())) > 0