diff --git a/tests/integration/test_data_source_selector.py b/tests/integration/test_data_source_selector.py index 142efdf..81cd8cf 100644 --- a/tests/integration/test_data_source_selector.py +++ b/tests/integration/test_data_source_selector.py @@ -11,45 +11,29 @@ def setUp(self) -> None: self.client = MongoSingleton.get_instance().get_client() self.community_id = "6579c364f1120850414e0dc4" self.client["Core"].drop_collection("modules") - self.client["Core"].drop_collection("platforms") def test_no_community(self): """ test if no community selected hivemind modeules """ selector = DataSourceSelector() - data_sources = selector.select_data_source(community_id=self.community_id) - self.assertEqual(data_sources, {}) + with self.assertRaises(ValueError): + selector.select_data_source(community_id=self.community_id) def test_single_platform(self): platform_id = "6579c364f1120850414e0da1" - self.client["Core"]["platforms"].insert_one( - { - "_id": ObjectId(platform_id), - "name": "discord", - "metadata": { - "name": "TEST", - "channels": ["1234", "4321"], - "roles": ["111", "222"], - }, - "community": ObjectId(self.community_id), - "disconnectedAt": None, - "connectedAt": datetime(2023, 12, 1), - "createdAt": datetime(2023, 12, 1), - "updatedAt": datetime(2023, 12, 1), - } - ) - self.client["Core"]["modules"].insert_one( { "name": "hivemind", - "communityId": ObjectId(self.community_id), + "community": ObjectId(self.community_id), "options": { "platforms": [ { - "platformId": ObjectId(platform_id), - "fromDate": datetime(2024, 1, 1), - "options": {}, + "platform": ObjectId(platform_id), + "name": "discord", + "metadata": { + "fromDate": datetime(2024, 1, 1), + }, } ] }, @@ -68,69 +52,33 @@ def test_multiple_platform(self): platform_id1 = "6579c364f1120850414e0da1" platform_id2 = "6579c364f1120850414e0da2" platform_id3 = "6579c364f1120850414e0da3" - self.client["Core"]["platforms"].insert_many( - [ - { - "_id": ObjectId(platform_id1), - "name": "discord", - "metadata": { - "name": "TEST", - "channels": ["1234", "4321"], - "roles": ["111", "222"], - }, - "community": ObjectId(self.community_id), - "disconnectedAt": None, - "connectedAt": datetime(2023, 12, 1), - "createdAt": datetime(2023, 12, 1), - "updatedAt": datetime(2023, 12, 1), - }, - { - "_id": ObjectId(platform_id2), - "name": "github", - "metadata": { - "organizationId": 12345, - }, - "community": ObjectId(self.community_id), - "disconnectedAt": None, - "connectedAt": datetime(2023, 12, 1), - "createdAt": datetime(2023, 12, 1), - "updatedAt": datetime(2023, 12, 1), - }, - { - "_id": ObjectId(platform_id3), - "name": "discourse", - "metadata": { - "some_id": 133445, - }, - "community": ObjectId(self.community_id), - "disconnectedAt": None, - "connectedAt": datetime(2023, 12, 1), - "createdAt": datetime(2023, 12, 1), - "updatedAt": datetime(2023, 12, 1), - }, - ] - ) self.client["Core"]["modules"].insert_one( { "name": "hivemind", - "communityId": ObjectId(self.community_id), + "community": ObjectId(self.community_id), "options": { "platforms": [ { - "platformId": ObjectId(platform_id1), - "fromDate": datetime(2024, 1, 1), - "options": {}, + "platform": ObjectId(platform_id1), + "name": "discord", + "metadata": { + "fromDate": datetime(2024, 1, 1), + }, }, { - "platformId": ObjectId(platform_id2), - "fromDate": datetime(2024, 1, 1), - "options": {}, + "platform": ObjectId(platform_id2), + "name": "github", + "metadata": { + "fromDate": datetime(2024, 1, 1), + }, }, { - "platformId": ObjectId(platform_id3), - "fromDate": datetime(2024, 1, 1), - "options": {}, + "platform": ObjectId(platform_id3), + "name": "discourse", + "metadata": { + "fromDate": datetime(2024, 1, 1), + }, }, ] }, diff --git a/utils/data_source_selector.py b/utils/data_source_selector.py index a066aaf..7734be8 100644 --- a/utils/data_source_selector.py +++ b/utils/data_source_selector.py @@ -19,33 +19,25 @@ def select_data_source(self, community_id: str) -> dict[str, bool]: for the given community """ db_results = self._query_modules_db(community_id) - platforms = list(map(lambda data: data["platform"]["name"], db_results)) + platforms = list(map(lambda data: data["name"], db_results)) data_sources = dict.fromkeys(platforms, True) return data_sources def _query_modules_db(self, community_id: str) -> list[dict]: client = MongoSingleton.get_instance().get_client() - - pipeline = [ - {"$match": {"name": "hivemind", "communityId": ObjectId(community_id)}}, - {"$unwind": "$options.platforms"}, + hivemind_module = client["Core"]["modules"].find_one( { - "$lookup": { - "from": "platforms", - "localField": "options.platforms.platformId", - "foreignField": "_id", - "as": "platform", - } + "community": ObjectId(community_id), + "name": "hivemind", }, - {"$unwind": "$platform"}, { - "$project": { - "_id": 0, - "platform.name": 1, - } + "options.platforms.name": 1, }, - ] - cursor = client["Core"]["modules"].aggregate(pipeline) + ) + if hivemind_module is None: + raise ValueError( + f"No hivemind modules set for the given community id: {community_id}" + ) + platforms = hivemind_module["options"]["platforms"] - data_sources = list(cursor) - return data_sources + return platforms