Exemplo n.º 1
0
 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)
Exemplo n.º 2
0
 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"
Exemplo n.º 3
0
 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
Exemplo n.º 4
0
 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)
Exemplo n.º 5
0
    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)
Exemplo n.º 6
0
 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]"
Exemplo n.º 7
0
 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)
Exemplo n.º 8
0
 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
Exemplo n.º 9
0
 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
Exemplo n.º 10
0
    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="")
Exemplo n.º 11
0
 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
Exemplo n.º 12
0
 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
Exemplo n.º 13
0
 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()
Exemplo n.º 14
0
 def test_comment(self):
     assert Comment(self.reddit, id="cklfmye").id == "cklfmye"
Exemplo n.º 15
0
 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()
Exemplo n.º 16
0
 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"
Exemplo n.º 17
0
 def test_construct_from_url(self):
     url = "https://reddit.com/comments/2gmzqe/_/cklhv0f/"
     assert Comment(self.reddit, url=url) == "cklhv0f"
Exemplo n.º 18
0
 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_
Exemplo n.º 19
0
 def test_str(self):
     comment = Comment(self.reddit, _data={"id": "dummy"})
     assert str(comment) == "dummy"
Exemplo n.º 20
0
 def test_repr(self):
     comment = Comment(self.reddit, id="dummy")
     assert repr(comment) == "Comment(id='dummy')"
Exemplo n.º 21
0
 def test_attribute_error(self):
     with pytest.raises(AttributeError):
         Comment(self.reddit, _data={"id": "1"}).mark_as_read()