def test_eq(self): mock_post_equal = RedditPost( "[PREBUILT] Acer Aspire Desktop: i5-10400, 12GB DDR4, 512GB SSD, Win 10 - $499.17", "/r/buildapcsales/comments/iocyre/prebuilt_acer_aspire_desktop_i510400_12gb_ddr4/", "Prebuilt", "iocyre", "https://www.amazon.com/gp/product/B088X2YR3X", ) mock_post_not_equal = RedditPost( "The Title", "The Link", "The Flair", "The different ID", "The External Link", ) self.assertTrue(self.mock_post.__eq__(mock_post_equal)) self.assertFalse(self.mock_post.__eq__(mock_post_not_equal)) self.assertEqual(self.mock_post.__eq__("Hello"), NotImplemented)
def test_from_json_self(self): mock_post_json_value = ( "{" + '\n "title": "[PREBUILT] Acer Aspire Desktop: i5-10400, 12GB DDR4, 512GB SSD, Win 10 - $499.17",' + '\n "link": "/r/buildapcsales/comments/iocyre/prebuilt_acer_aspire_desktop_i510400_12gb_ddr4/",' + '\n "flair": "Prebuilt",' + '\n "id": "iocyre",' + '\n "external_link": "https://www.amazon.com/gp/product/B088X2YR3X"' + "\n}") self.assertEqual(RedditPost.__from_file__(mock_post_json_value), self.mock_post)
class TestFileManipulation(unittest.TestCase): mock_post = RedditPost("The Title", "The Link", "The Flair", "The ID", "The External Link") output_file_name = "testing/output.json" def test_read_and_write(self): file_manipulation.update_last_known_post(self.output_file_name, self.mock_post) self.assertEqual( self.mock_post, file_manipulation.get_last_known_post(self.output_file_name))
class TestNotifier(unittest.TestCase): mock_post = RedditPost("The Title", "The Link", "The Flair", "The ID", "The External Link") def test_send_notification(self): self.assertTrue(notifier.send_notification(self.mock_post)) def test_matches_filter(self): mock_post_does_match = RedditPost("x570", "", "MOTHERBOARD", "wahoo", "5") self.assertFalse(notifier.matches_filter(self.mock_post)) self.assertTrue(notifier.matches_filter(mock_post_does_match))
def test_get_new_posts(self): posts_zero_through_three = reddit_api_handler.get_new_posts( MOCK_POSTS, MOCK_POSTS[4]) self.assertEqual(posts_zero_through_three, MOCK_POSTS[:4]) # we could use assertRaises here but I want to check that it throws my custom exception try: new_posts = reddit_api_handler.get_new_posts( MOCK_POSTS, RedditPost("", "", "", "", "")) except ValueError as e: self.assertEqual( e.__str__(), "Couldn't find last know post in passed posts list")
def get_mock_posts_from_file(file_name: str) -> List[RedditPost]: file_manipulation.change_dir() with open("testing/input.json", "r") as file: content = file.read() post_dicts = jsonpickle.decode(content) posts = [] for post in post_dicts: posts.append( RedditPost( post["title"], post["link"], post["flair"], post["id"], post["external_link"], )) return posts
def update_last_known_post(file_name: str, post: RedditPost): write_file(file_name, post.__json__())
def get_last_known_post(file_name: str) -> RedditPost: json_data = read_file(file_name) return RedditPost.__from_file__(json_data)
def test_match(self): # TODO consolidate sub filters into mock filters so we don't have so many mock filters mock_post = RedditPost( "[PREBUILT] Acer Aspire Desktop: i5-10400, 12GB DDR4, 512GB SSD, Win 10 - $499.17", "/r/buildapcsales/comments/iocyre/prebuilt_acer_aspire_desktop_i510400_12gb_ddr4/", "Prebuilt", "iocyre", "https://www.amazon.com/gp/product/B088X2YR3X", ) sub_filter_empty = [] sub_filter_no_match_in = [PostFilter(Operator.check_in, "No Match")] sub_filter_title_in = [PostFilter(Operator.check_in, "12GB")] sub_filter_flair_in = [PostFilter(Operator.check_in, "Prebuilt")] sub_filter_domain_in = [PostFilter(Operator.check_in, "amazon.com")] sub_filter_title_not_in = [ PostFilter(Operator.check_not_in, "No Match") ] sub_filter_title_not_in_fail = [ PostFilter(Operator.check_not_in, "12GB") ] sub_filter_title_equal = [ PostFilter( Operator.check_equals, "[PREBUILT] Acer Aspire Desktop: i5-10400, 12GB DDR4, 512GB SSD, Win 10 - $499.17", ) ] sub_filter_title_equal_fail = [ PostFilter(Operator.check_equals, "No Match") ] sub_filter_title_not_equal = [ PostFilter(Operator.check_not_equals, "No Match") ] sub_filter_title_not_equal_fail = [ PostFilter( Operator.check_not_equals, "[PREBUILT] Acer Aspire Desktop: i5-10400, 12GB DDR4, 512GB SSD, Win 10 - $499.17", ) ] sub_filter_no_operator = [PostFilter(None, "Doesn't matter")] mock_filter_all_match = RedditPostAlert("All match", sub_filter_title_in, sub_filter_flair_in, sub_filter_domain_in) mock_filter_title_no_match = RedditPostAlert( "Title No Match", sub_filter_no_match_in, sub_filter_flair_in, sub_filter_domain_in, ) mock_filter_title_empty = RedditPostAlert("Title Empty", sub_filter_empty, sub_filter_flair_in, sub_filter_domain_in) mock_filter_flair_no_match = RedditPostAlert( "Flair No Match", sub_filter_empty, sub_filter_no_match_in, sub_filter_domain_in, ) mock_filter_title_and_flair_empty = RedditPostAlert( "Title, Flair Empty", sub_filter_empty, sub_filter_empty, sub_filter_domain_in, ) mock_filter_domain_no_match = RedditPostAlert( "Domain No Match", sub_filter_empty, sub_filter_empty, sub_filter_no_match_in, ) mock_filter_title_not_in = RedditPostAlert("Title Not In", sub_filter_title_not_in, sub_filter_empty, sub_filter_empty) mock_filter_title_not_in_fail = RedditPostAlert( "Title Not In Fail", sub_filter_title_not_in_fail, sub_filter_empty, sub_filter_empty, ) mock_filter_title_equal = RedditPostAlert("Title Equal", sub_filter_title_equal, sub_filter_empty, sub_filter_empty) mock_filter_title_equal_fail = RedditPostAlert( "Title Equal Fail", sub_filter_title_equal_fail, sub_filter_empty, sub_filter_empty, ) mock_filter_title_not_equal = RedditPostAlert( "Title Not Equal", sub_filter_title_not_equal, sub_filter_empty, sub_filter_empty, ) mock_filter_title_not_equal_fail = RedditPostAlert( "Title Not Equal Fail", sub_filter_title_not_equal_fail, sub_filter_empty, sub_filter_empty, ) mock_filter_no_operator = RedditPostAlert("No Operator", sub_filter_no_operator, sub_filter_empty, sub_filter_empty) self.assertTrue(mock_post.matches(mock_filter_all_match)) self.assertFalse(mock_post.matches(mock_filter_title_no_match)) self.assertTrue(mock_post.matches(mock_filter_title_empty)) self.assertFalse(mock_post.matches(mock_filter_flair_no_match)) self.assertTrue(mock_post.matches(mock_filter_title_and_flair_empty)) self.assertFalse(mock_post.matches(mock_filter_domain_no_match)) self.assertTrue(mock_post.matches(mock_filter_title_not_in)) self.assertFalse(mock_post.matches(mock_filter_title_not_in_fail)) self.assertTrue(mock_post.matches(mock_filter_title_equal)) self.assertFalse(mock_post.matches(mock_filter_title_equal_fail)) self.assertTrue(mock_post.matches(mock_filter_title_not_equal)) self.assertFalse(mock_post.matches(mock_filter_title_not_equal_fail)) self.assertFalse(mock_post.matches(mock_filter_no_operator))
def test_matches_filter(self): mock_post_does_match = RedditPost("x570", "", "MOTHERBOARD", "wahoo", "5") self.assertFalse(notifier.matches_filter(self.mock_post)) self.assertTrue(notifier.matches_filter(mock_post_does_match))
def parse_posts_from_json(json_data): posts = [] post_dicts = json_data["data"]["children"] for post_dict in post_dicts: posts.append(RedditPost.__from_web__(post_dict["data"])) return posts
def matches_filter(post: RedditPost) -> bool: if post.matches(mock_filter): return True return False