def test_to_json_and_back(mock_client): 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, mock_client) 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, mock_client) 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_str == "query": assert list_subs[0].destination == 1234 assert list_subs[1].query_str == "example" assert list_subs[1].destination == 5678 else: assert list_subs[0].query_str == "example" assert list_subs[0].destination == 5678 assert list_subs[1].query_str == "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 start(self): # request = Request(con_pool_size=8) self.client = TelegramClient("fasearchbot", self.config.telegram.api_id, self.config.telegram.api_hash) self.client.start(bot_token=self.config.telegram.bot_token) self.subscription_watcher = SubscriptionWatcher.load_from_json( self.api, self.client) self.functionalities = self.initialise_functionalities() for func in self.functionalities: logger.info("Registering functionality: %s", func.__class__.__name__) func.register(self.client) self.alive = True event_loop = asyncio.get_event_loop() # Log every couple seconds so we know the bot is still running self.log_task = event_loop.create_task(self.periodic_log()) # Start the sub watcher self.watcher_task = event_loop.create_task( self.subscription_watcher.run())
def test_from_json(mock_client): 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 try: data = { "latest_ids": ["12423", "12422", "12420"], "subscriptions": [{ "query": "test", "destination": 87238, "latest_update": "2019-10-26T18:57:09" }, { "query": "example", "destination": -87023, "latest_update": "2019-10-25T17:34:08" }], "blacklists": { "8732": ["example", "ych"], "-123": ["fred"] } } with open(test_watcher_file, "w+") as f: json.dump(data, f) api = MockExportAPI() watcher = SubscriptionWatcher.load_from_json(api, mock_client) assert len(watcher.latest_ids) == 3 assert "12423" in watcher.latest_ids assert "12422" in watcher.latest_ids assert "12420" in watcher.latest_ids assert len(watcher.subscriptions) == 2 list_subs = list(watcher.subscriptions) if list_subs[0].query_str == "test": assert list_subs[0].destination == 87238 assert list_subs[0].latest_update == datetime.datetime( 2019, 10, 26, 18, 57, 9) assert list_subs[1].query_str == "example" assert list_subs[1].destination == -87023 assert list_subs[1].latest_update == datetime.datetime( 2019, 10, 25, 17, 34, 8) else: assert list_subs[0].query_str == "example" assert list_subs[0].destination == -87023 assert list_subs[0].latest_update == datetime.datetime( 2019, 10, 25, 17, 34, 8) assert list_subs[1].query_str == "test" assert list_subs[1].destination == 87238 assert list_subs[1].latest_update == datetime.datetime( 2019, 10, 26, 18, 57, 9) assert len(watcher.blocklists) == 2 assert 8732 in watcher.blocklists assert len(watcher.blocklists[8732]) == 2 assert isinstance(watcher.blocklists[8732], set) assert "example" in watcher.blocklists[8732] assert "ych" in watcher.blocklists[8732] assert -123 in watcher.blocklists assert len(watcher.blocklists[-123]) == 1 assert isinstance(watcher.blocklists[-123], set) assert "fred" in watcher.blocklists[-123] finally: SubscriptionWatcher.FILENAME = old_filename os.remove(test_watcher_file)
def test_load_old_save_new(mock_client): old_data = { "latest_ids": ["38447607", "38447608", "38447609", "38447610"], "subscriptions": [ { "query": "deer", "destination": -10053, "latest_update": "2020-09-20T11:11:19.329747" }, { "query": "rating:safe rabbit", "destination": -76543, "latest_update": "2020-10-31T21:50:54.020924" }, { "query": "@keywords deer", "destination": 34342, "latest_update": "2020-10-31T21:09:58.755093" }, { "query": "@keywords dragon", "destination": 34342, "latest_update": "2020-10-31T21:09:58.755093" }, ], "blacklists": { "87654": ["artist:fender", "fox"], "-76543": ["artist:fred"] } } api = MockExportAPI() def mock_load(*args, **kwargs): return old_data class MockDump: def __init__(self): self.dumped_data = None def call(self, data, *args, **kwargs): self.dumped_data = data mock_dump = MockDump() with mock.patch("json.loads", mock_load): watcher = SubscriptionWatcher.load_from_json(api, mock_client) assert len(watcher.subscriptions) == 4 assert len(watcher.blocklists) == 2 assert len(watcher.latest_ids) == 4 with mock.patch("json.dump", mock_dump.call): watcher.save_to_json() new_data = mock_dump.dumped_data assert "latest_ids" in new_data assert new_data["latest_ids"] == old_data["latest_ids"] assert "destinations" in new_data assert len(new_data["destinations"]) == 4 for destination in new_data["destinations"].values(): assert "subscriptions" in destination assert "blocks" in destination # Check dest -10053 dest1 = new_data["destinations"]["-10053"] assert len(dest1["subscriptions"]) == 1 assert len(dest1["blocks"]) == 0 assert dest1["subscriptions"][0] == { "query": "deer", "latest_update": "2020-09-20T11:11:19.329747", "paused": False } # check dest -76543 dest2 = new_data["destinations"]["-76543"] assert len(dest2["subscriptions"]) == 1 assert len(dest2["blocks"]) == 1 assert dest2["subscriptions"][0] == { "query": "rating:safe rabbit", "latest_update": "2020-10-31T21:50:54.020924", "paused": False } assert dest2["blocks"][0] == {"query": "artist:fred"} # Check dest 34342 dest3 = new_data["destinations"]["34342"] assert len(dest3["subscriptions"]) == 2 assert len(dest3["blocks"]) == 0 assert \ { "query": "@keywords deer", "latest_update": "2020-10-31T21:09:58.755093", "paused": False } in dest3["subscriptions"] assert \ { "query": "@keywords dragon", "latest_update": "2020-10-31T21:09:58.755093", "paused": False } in dest3["subscriptions"] # Check dest 87654 dest4 = new_data["destinations"]["87654"] assert len(dest4["subscriptions"]) == 0 assert len(dest4["blocks"]) == 2 assert \ { "query": "artist:fender" } in dest4["blocks"] assert \ { "query": "fox" } in dest4["blocks"]