def test_invalid_reply_to_seed(self): reddit = Login.create_reddit_object() url = "https://www.reddit.com/r/AskReddit/comments/md0ny3/what_nonsensical_invasive_thoughts_do_you_have/" forest = Comments.Forest( reddit.submission(url = url), url ) metadata = { "body": "A test node.", "id": "test", "parent_id": "t1_inv@l1d", } reply = Comments.CommentNode(metadata) EncodeNode().encode(reply) try: forest.seed(reply) assert False except IndexError: assert True assert len(forest.root.replies) == 0
def test_write_structured_comments(self): test_nodes = [] first_node = MockNode("test one") EncodeNode().encode(first_node) second_node = MockNode("test two") EncodeNode().encode(second_node) third_node = MockNode("test three") EncodeNode().encode(third_node) first_node.replies.append(second_node) first_node.replies[0].replies.append(third_node) test_nodes.append(first_node) Export.write_structured_comments(test_nodes, "structured_comments_test") with open(f"../scrapes/{date}/comments/structured_comments_test.json", "r", encoding = "utf-8") as test_json: test_dict = json.load(test_json) assert test_dict == [{'string': 'test one', 'replies': [{'string': 'test two', 'replies': [{'string': 'test three', 'replies': []}]}]}]
def sort_structured(submission, url): """ Sort all comments in structured format. Calls previously defined public methods: CommentNode() Forest() Forest().seed() CreateComment.create() Calls a public method from an external module: EncodeNode().encode() Parameters ---------- submission: PRAW submission object url: str String denoting the submission's url Returns ------- replies: list List containing `CommentNode`s """ forest = Forest(submission, url) seed_status = Status( "Forest has fully matured.", Fore.CYAN + Style.BRIGHT + "Seeding Forest.", "cyan" ) seed_status.start() for comment in submission.comments.list(): comment_node = CommentNode(CreateComment.create(comment)) EncodeNode().encode(comment_node) forest.seed(comment_node) seed_status.succeed() return forest.root.replies