def test_hash(self): comment1 = Comment(self.reddit, _data={"id": "dummy1", "n": 1}) comment2 = Comment(self.reddit, _data={"id": "Dummy1", "n": 2}) comment3 = Comment(self.reddit, _data={"id": "dummy3", "n": 2}) assert hash(comment1) == hash(comment1) assert hash(comment2) == hash(comment2) assert hash(comment3) == hash(comment3) assert hash(comment1) == hash(comment2) assert hash(comment2) != hash(comment3) assert hash(comment1) != hash(comment3)
def test_equality(self): comment1 = Comment(self.reddit, _data={"id": "dummy1", "n": 1}) comment2 = Comment(self.reddit, _data={"id": "Dummy1", "n": 2}) comment3 = Comment(self.reddit, _data={"id": "dummy3", "n": 2}) assert comment1 == comment1 assert comment2 == comment2 assert comment3 == comment3 assert comment1 == comment2 assert comment2 != comment3 assert comment1 != comment3 assert "dummy1" == comment1 assert comment2 == "dummy1"
async def test_refresh__with_reply_sort_and_limit(self): with self.use_cassette(): comment = Comment(self.reddit, "e4j4830") comment.reply_limit = 4 comment.reply_sort = "new" await comment.refresh() replies = comment.replies last_created = float("inf") for reply in replies: if isinstance(reply, Comment): if reply.created_utc > last_created: assert False, "sort order incorrect" last_created = reply.created_utc assert len(comment.replies) == 3
async def test_comment_forest_async_iterator(self): submission = await self.reddit.submission("1234", fetch=False) submission._fetched = True submission.comments._comments = [Comment(None, id="1234")] with pytest.deprecated_call(): async for comment in submission.comments: assert isinstance(comment, Comment)
async def monitor(self, subreddit: str, **kwargs): """ Monitors comments for the subreddit Yields comments as they come in until instructed to stop. """ self.logger.debug(f'Monitoring comments in {subreddit}') subreddit = self.connection.subreddit(subreddit) args = self.stream_args(**kwargs) stream = subreddit.stream.comments(**args) async for comment in stream: if not self.run: self.logger.info('Breaking monitoring...') break if not isinstance(comment, Comment): self.__log_type_warning(Comment(), comment) continue # Check filters for f in self.filters: if f.test(comment): continue self.logger.info(f'Handling comment: {comment.id}') # Invoke handlers for h in self.handlers: h.handle(comment)
async def test_delete(self, _): self.reddit.read_only = False with self.use_cassette(): comment = Comment(self.reddit, "fx1tgzm") await comment.delete() comment = await self.reddit.comment("fx1tgzm") assert comment.author is None assert comment.body == "[deleted]"
def test_id_from_url__invalid_urls(self): urls = [ "", "1", "/", "my.it/2gmzqe", "http://my.it/_", "https://redd.it/_/", "http://reddit.com/comments/_/2gmzqe", "http://my.it/2gmzqe", "https://redd.it/2gmzqe", "http://reddit.com/comments/2gmzqe", "https://www.reddit.com/r/redditdev/comments/2gmzqe/", ] for url in urls: with pytest.raises(ClientException): Comment.id_from_url(url)
async def test_parent__submission(self): comment = Comment(self.reddit, "cklfmye") with self.use_cassette(): parent = await comment.parent() parent_comments = await parent.comments() assert comment in parent_comments assert isinstance(parent, Submission) assert parent.fullname == comment.parent_id
async def test_parent__comment(self): comment = Comment(self.reddit, "cklhv0f") with self.use_cassette(): parent = await comment.parent() await parent.refresh() assert comment in parent.replies assert isinstance(parent, Comment) assert parent.fullname == comment.parent_id
def test_construct_failure(self): message = "Exactly one of `id`, `url`, or `_data` must be provided." with pytest.raises(TypeError) as excinfo: Comment(self.reddit) assert str(excinfo.value) == message with pytest.raises(TypeError) as excinfo: Comment(self.reddit, id="dummy", url="dummy") assert str(excinfo.value) == message with pytest.raises(TypeError) as excinfo: Comment(self.reddit, "dummy", _data={"id": "dummy"}) assert str(excinfo.value) == message with pytest.raises(TypeError) as excinfo: Comment(self.reddit, url="dummy", _data={"id": "dummy"}) assert str(excinfo.value) == message with pytest.raises(TypeError) as excinfo: Comment(self.reddit, "dummy", "dummy", {"id": "dummy"}) assert str(excinfo.value) == message with pytest.raises(ValueError): Comment(self.reddit, "") with pytest.raises(ValueError): Comment(self.reddit, url="")
async def test_reply(self): self.reddit.read_only = False with self.use_cassette(): parent_comment = Comment(self.reddit, "fx1ec2p") comment = await parent_comment.reply("Comment reply") assert comment.author == pytest.placeholders.username assert comment.body == "Comment reply" assert not comment.is_root assert comment.parent_id == parent_comment.fullname
def test_id_from_url(self): urls = [ "http://reddit.com/comments/2gmzqe/_/cklhv0f/", "https://reddit.com/comments/2gmzqe/_/cklhv0f", "http://www.reddit.com/r/redditdev/comments/2gmzqe/_/cklhv0f/", "https://www.reddit.com/r/redditdev/comments/2gmzqe/_/cklhv0f", ] for url in urls: assert Comment.id_from_url(url) == "cklhv0f", url
async def test_parent__chain(self): comment = Comment(self.reddit, "dkk4qjd") counter = 0 with self.use_cassette(): await comment.refresh() parent = await comment.parent() while parent != comment.submission: if counter % 9 == 0: await parent.refresh() counter += 1 parent = await parent.parent()
def test_comment(self): assert Comment(self.reddit, id="cklfmye").id == "cklfmye"
async def test_enable_inbox_replies(self): self.reddit.read_only = False comment = Comment(self.reddit, "fwxxs5d") with self.use_cassette(): await comment.enable_inbox_replies()
async def test_edit(self, _): self.reddit.read_only = False with self.use_cassette(): comment = Comment(self.reddit, "fwxxs5d") await comment.edit("New text") assert comment.body == "New text"
def test_construct_from_url(self): url = "https://reddit.com/comments/2gmzqe/_/cklhv0f/" assert Comment(self.reddit, url=url) == "cklhv0f"
def test_unset_hidden_attribute_does_not_fetch(self): comment = Comment(self.reddit, _data={"id": "dummy"}) assert comment._fetched with pytest.raises(AttributeError): comment._ipython_canary_method_should_not_exist_
def test_str(self): comment = Comment(self.reddit, _data={"id": "dummy"}) assert str(comment) == "dummy"
def test_repr(self): comment = Comment(self.reddit, id="dummy") assert repr(comment) == "Comment(id='dummy')"
def test_attribute_error(self): with pytest.raises(AttributeError): Comment(self.reddit, _data={"id": "1"}).mark_as_read()