def test_remove_sub__removes_subscription(context): api = MockExportAPI() watcher = SubscriptionWatcher(api, context.bot) watcher.subscriptions.add(Subscription("example", 18749)) watcher.subscriptions.add(Subscription("test", 18747)) new_sub = Subscription("test", 18749) new_sub.latest_update = datetime.datetime.now() watcher.subscriptions.add(new_sub) func = SubscriptionFunctionality(watcher) list_subs = MockMethod("Listing subscriptions") func._list_subs = list_subs.call resp = func._remove_sub(18749, "test") assert "Removed subscription: \"test\"." in resp assert list_subs.called assert list_subs.args[0] == 18749 assert "Listing subscriptions" in resp assert len(watcher.subscriptions) == 2 subscriptions = list(watcher.subscriptions) if subscriptions[0].query == "test": assert subscriptions[0].destination == 18747 assert subscriptions[1].query == "example" assert subscriptions[1].destination == 18749 else: assert subscriptions[0].query == "example" assert subscriptions[0].destination == 18749 assert subscriptions[1].query == "test" assert subscriptions[1].destination == 18747
def test_to_json_and_back_no_update(): sub = Subscription("an example", -63939) data = sub.to_json() new_sub = Subscription.from_json(data) assert new_sub.query == sub.query assert new_sub.destination == sub.destination assert new_sub.latest_update == sub.latest_update
def test_matches_not_negative_rating(): query = "deer -rating:general" subscription = Subscription(query, 12432) submission = SubmissionBuilder( title="Deer plays in woods", rating=Rating.GENERAL).build_full_submission() match = subscription.matches_result(submission, set()) assert not match
def test_to_json_no_updates(): sub = Subscription("test query", -12322) data = sub.to_json() assert "query" in data assert data["query"] == "test query" assert "destination" in data assert data["destination"] == -12322 assert "latest_update" in data assert data["latest_update"] is None
def test_matches_not_rating(): query = "deer rating:general" subscription = Subscription(query, 12432) submission = SubmissionBuilder( title="Deer 'plays' in woods", rating=Rating.ADULT).build_full_submission() match = subscription.matches_result(submission, set()) assert not match
def test_matches_result__two_words_in_description_matches(): query = "example submission" subscription = Subscription(query, 12432) submission = SubmissionBuilder( title="test", description="this submission is just an example", keywords=["example", "keywords"]).build_full_submission() match = subscription.matches_result(submission, set()) assert match
def test_remove_sub__non_existent_subscription(context): api = MockExportAPI() watcher = SubscriptionWatcher(api, context.bot) watcher.subscriptions.add(Subscription("example", 18749)) watcher.subscriptions.add(Subscription("test", 18747)) func = SubscriptionFunctionality(watcher) resp = func._remove_sub(18749, "test") assert resp == "There is not a subscription for \"test\" in this chat." assert len(watcher.subscriptions) == 2
def test_to_json_and_back(self, bot): test_watcher_file = "./test_subscription_watcher.json" if os.path.exists(test_watcher_file): os.remove(test_watcher_file) old_filename = SubscriptionWatcher.FILENAME SubscriptionWatcher.FILENAME = test_watcher_file api = MockExportAPI() latest_submissions = [ SubmissionBuilder(submission_id="123243").build_short_submission(), SubmissionBuilder(submission_id="123242").build_short_submission(), SubmissionBuilder(submission_id="123240").build_short_submission() ] subscription1 = Subscription("query", 1234) subscription2 = Subscription("example", 5678) watcher = SubscriptionWatcher(api, bot) watcher._update_latest_ids(latest_submissions) watcher.subscriptions.add(subscription1) watcher.subscriptions.add(subscription2) watcher.blocklists[3452] = {"test", "example"} watcher.blocklists[1453] = {"ych"} try: watcher.save_to_json() new_watcher = SubscriptionWatcher.load_from_json(api, bot) assert len(new_watcher.latest_ids) == 3 assert "123243" in new_watcher.latest_ids assert "123242" in new_watcher.latest_ids assert "123240" in new_watcher.latest_ids assert list(watcher.latest_ids) == list(new_watcher.latest_ids) assert len(new_watcher.subscriptions) == 2 list_subs = list(new_watcher.subscriptions) if list_subs[0].query == "query": assert list_subs[0].destination == 1234 assert list_subs[1].query == "example" assert list_subs[1].destination == 5678 else: assert list_subs[0].query == "example" assert list_subs[0].destination == 5678 assert list_subs[1].query == "query" assert list_subs[1].destination == 1234 assert len(new_watcher.blocklists) == 2 assert 3452 in new_watcher.blocklists assert len(new_watcher.blocklists[3452]) == 2 assert isinstance(new_watcher.blocklists[3452], set) assert "test" in new_watcher.blocklists[3452] assert "example" in new_watcher.blocklists[3452] assert 1453 in new_watcher.blocklists assert len(new_watcher.blocklists[1453]) == 1 assert isinstance(new_watcher.blocklists[1453], set) assert "ych" in new_watcher.blocklists[1453] finally: SubscriptionWatcher.FILENAME = old_filename os.remove(test_watcher_file)
def test_to_json(): sub = Subscription("test query", -12322) sub.latest_update = datetime.datetime(2019, 9, 17, 21, 8, 35) data = sub.to_json() assert "query" in data assert data["query"] == "test query" assert "destination" in data assert data["destination"] == -12322 assert "latest_update" in data assert data["latest_update"] == "2019-09-17T21:08:35"
def test_save_to_json(self, bot): test_watcher_file = "./test_subscription_watcher.json" if os.path.exists(test_watcher_file): os.remove(test_watcher_file) api = MockExportAPI() latest_submissions = [ SubmissionBuilder(submission_id="123243").build_short_submission(), SubmissionBuilder(submission_id="123242").build_short_submission(), SubmissionBuilder(submission_id="123240").build_short_submission() ] subscription1 = Subscription("query", 1234) subscription2 = Subscription("example", 5678) watcher = SubscriptionWatcher(api, bot) watcher._update_latest_ids(latest_submissions) watcher.subscriptions.add(subscription1) watcher.subscriptions.add(subscription2) watcher.blocklists[3452] = {"test", "example"} watcher.blocklists[1453] = {"ych"} watcher.FILENAME = test_watcher_file try: watcher.save_to_json() assert os.path.exists(test_watcher_file) with open(test_watcher_file, "r") as f: data = json.load(f) assert data is not None assert len(data['latest_ids']) == 3 assert "123240" in data['latest_ids'] assert "123242" in data['latest_ids'] assert "123243" in data['latest_ids'] assert len(data['subscriptions']) == 2 if data['subscriptions'][0]['query'] == "query": assert data['subscriptions'][0]['destination'] == 1234 assert data['subscriptions'][1]['query'] == "example" assert data['subscriptions'][1]['destination'] == 5678 else: assert data['subscriptions'][0]['query'] == "example" assert data['subscriptions'][0]['destination'] == 5678 assert data['subscriptions'][1]['query'] == "query" assert data['subscriptions'][1]['destination'] == 1234 assert len(data["blacklists"]) == 2 assert "3452" in data["blacklists"] assert len(data["blacklists"]["3452"]) == 2 assert isinstance(data["blacklists"]["3452"], list) assert "test" in data["blacklists"]["3452"] assert "example" in data["blacklists"]["3452"] assert "1453" in data["blacklists"] assert len(data["blacklists"]["1453"]) == 1 assert isinstance(data["blacklists"]["1453"], list) assert data["blacklists"]["1453"] == ["ych"] finally: os.remove(test_watcher_file)
def test_matches_result__does_not_match_negated_query_case_insensitive(): query = "-test" subscription = Subscription(query, 12432) submission = SubmissionBuilder( title="Test submission", description="this submission is just an example", keywords=["example", "submission", "keywords"]).build_full_submission() match = subscription.matches_result(submission, set()) assert not match
def test_matches_result__substring_in_keywords_no_match(): query = "keyword" subscription = Subscription(query, 12432) submission = SubmissionBuilder( title="test submission", description="this submission is just an example", keywords=["example", "submission", "keywords"]).build_full_submission() match = subscription.matches_result(submission, set()) assert not match
def test_list_subs__alphabetical(context): api = MockExportAPI() watcher = SubscriptionWatcher(api, context.bot) watcher.subscriptions.add(Subscription("example", 18749)) watcher.subscriptions.add(Subscription("test", 18749)) watcher.subscriptions.add(Subscription("deer", 18749)) func = SubscriptionFunctionality(watcher) resp = func._list_subs(18749) assert "Current active subscriptions in this chat:" in resp assert "- deer\n- example\n- test" in resp
def test_matches_word_in_tag(): query = "deer" subscription = Subscription(query, 12432) submission = SubmissionBuilder( title="Test submission", description="this submission is <b>deer</b>", keywords=["example", "submission", "keywords"]).build_full_submission() match = subscription.matches_result(submission, set()) assert match
def test_matches_result__doesnt_match_blocklisted_tag(): query = "an-example" subscription = Subscription(query, 12432) submission = SubmissionBuilder( title="Test submission", description="this submission is just an-example", keywords=["example", "submission", "keywords"]).build_full_submission() match = subscription.matches_result(submission, {"test"}) assert not match
def test_matches_result__matches_query_with_hyphen(): query = "an-example" subscription = Subscription(query, 12432) submission = SubmissionBuilder( title="Test submission", description="this submission is just an-example", keywords=["example", "submission", "keywords"]).build_full_submission() match = subscription.matches_result(submission, set()) assert match
def test_matches_result__does_not_match_query_with_applicable_negated_query(): query = "test -example" subscription = Subscription(query, 12432) submission = SubmissionBuilder( title="Test submission", description="this submission is just an example", keywords=["example", "submission", "keywords"]).build_full_submission() match = subscription.matches_result(submission, set()) assert not match
def test_matches_result__case_insensitive_query(): query = "SUBMISSION" subscription = Subscription(query, 12432) submission = SubmissionBuilder( title="test submission", description="this submission is just an example", keywords=["example", "submission", "keywords"]).build_full_submission() match = subscription.matches_result(submission, set()) assert match
def _remove_sub(self, destination: int, query: str): old_sub = Subscription(query, destination) try: self.watcher.subscriptions.remove(old_sub) return f"Removed subscription: \"{query}\".\n{self._list_subs(destination)}" except KeyError: return f"There is not a subscription for \"{query}\" in this chat."
def test_to_json_and_back(): sub = Subscription("something", 3223) sub.latest_update = datetime.datetime(2019, 9, 17, 21, 16, 14, tzinfo=datetime.timezone.utc) data = sub.to_json() new_sub = Subscription.from_json(data) assert new_sub.query == sub.query assert new_sub.destination == sub.destination assert new_sub.latest_update == sub.latest_update
def test_send_update__updates_latest(self, bot): api = MockExportAPI() watcher = SubscriptionWatcher(api, bot) subscription = Subscription("test", 12345) submission = SubmissionBuilder().build_mock_submission() watcher._send_update(subscription, submission) assert subscription.latest_update is not None
def test_init(): query = "yo" destination = 12432 sub = Subscription(query, destination) assert sub.query == query assert sub.destination == destination assert sub.latest_update is None
def test_from_json_no_update(): data = { "query": "example query", "destination": 17839, "latest_update": None } sub = Subscription.from_json(data) assert sub.query == "example query" assert sub.destination == 17839 assert sub.latest_update is None
def test_send_update__sends_message(self, bot): api = MockExportAPI() watcher = SubscriptionWatcher(api, bot) subscription = Subscription("test", 12345) submission = SubmissionBuilder().build_mock_submission() watcher._send_update(subscription, submission) bot.send_message.assert_not_called() bot.send_photo.assert_called_once() kwargs_photo = bot.send_photo.call_args[1] assert kwargs_photo['chat_id'] == 12345 assert kwargs_photo['photo'] == submission.download_url assert "update" in kwargs_photo['caption'].lower() assert "\"test\"" in kwargs_photo['caption'] assert "subscription" in kwargs_photo['caption'].lower() assert submission.link in kwargs_photo['caption']
def test_from_json(): data = { "query": "example query", "destination": 17839, "latest_update": "2019-09-17T21:14:07Z" } sub = Subscription.from_json(data) assert sub.query == "example query" assert sub.destination == 17839 assert sub.latest_update == datetime.datetime(2019, 9, 17, 21, 14, 7, tzinfo=datetime.timezone.utc)
def test_matches_general_rating(): query1 = "deer rating:general" query2 = "deer rating:safe" subscription1 = Subscription(query1, 12432) subscription2 = Subscription(query2, 12432) submission = SubmissionBuilder( title="Deer plays in woods", rating=Rating.GENERAL).build_full_submission() match1 = subscription1.matches_result(submission, set()) match2 = subscription2.matches_result(submission, set()) assert match1 assert match2
def test_matches_mature_rating(): query1 = "deer rating:mature" query2 = "deer rating:questionable" subscription1 = Subscription(query1, 12432) subscription2 = Subscription(query2, 12432) submission = SubmissionBuilder( title="Deer plays in woods", rating=Rating.MATURE).build_full_submission() match1 = subscription1.matches_result(submission, set()) match2 = subscription2.matches_result(submission, set()) assert match1 assert match2
def test_matches_explicit_rating(): query1 = "deer rating:adult" query2 = "deer rating:explicit" subscription1 = Subscription(query1, 12432) subscription2 = Subscription(query2, 12432) submission = SubmissionBuilder( title="Deer plays in woods", rating=Rating.ADULT).build_full_submission() match1 = subscription1.matches_result(submission, set()) match2 = subscription2.matches_result(submission, set()) assert match1 assert match2
def _add_sub(self, destination: int, query: str): if query == "": return f"Please specify the subscription query you wish to add." new_sub = Subscription(query, destination) self.watcher.subscriptions.add(new_sub) return f"Added subscription: \"{query}\".\n{self._list_subs(destination)}"