From 5e485ed969493ef071f79e8ce0e89441c07c3459 Mon Sep 17 00:00:00 2001 From: Mohammad Amin Date: Fri, 29 Dec 2023 08:45:07 +0330 Subject: [PATCH 1/2] fix: Updated the automations saga schema! --- automation/automation_workflow.py | 4 +- discord_utils.py | 2 +- .../test_get_guild_community_ids.py | 2 +- .../integration/test_get_guild_platform_id.py | 55 +++++++++++++++++++ ...ld_community_ids.py => get_guild_utils.py} | 27 +++++++++ 5 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 tests/integration/test_get_guild_platform_id.py rename utils/{get_guild_community_ids.py => get_guild_utils.py} (59%) diff --git a/automation/automation_workflow.py b/automation/automation_workflow.py index 98ae719..688f2be 100644 --- a/automation/automation_workflow.py +++ b/automation/automation_workflow.py @@ -6,6 +6,7 @@ from pybars import Compiler from tc_messageBroker.rabbit_mq.event import Event from tc_messageBroker.rabbit_mq.queue import Queue +from utils.get_guild_utils import get_guild_platform_id class AutomationWorkflow(AutomationBase): @@ -194,8 +195,9 @@ def _prepare_saga_data( message : str the message to send the user """ + platform_id = get_guild_platform_id(guild_id) data = { - "guildId": guild_id, + "platformId": platform_id, "created": False, "discordId": user_id, "message": message, diff --git a/discord_utils.py b/discord_utils.py index 8ae4943..5d7af82 100644 --- a/discord_utils.py +++ b/discord_utils.py @@ -5,7 +5,7 @@ from automation.automation_workflow import AutomationWorkflow from tc_messageBroker.rabbit_mq.saga.saga_base import get_saga from utils.daolytics_uitls import get_mongo_credentials, get_saga_db_location -from utils.get_guild_community_ids import get_guild_community_ids +from utils.get_guild_utils import get_guild_community_ids from utils.get_rabbitmq import prepare_rabbit_mq from utils.transactions_ordering import sort_transactions diff --git a/tests/integration/test_get_guild_community_ids.py b/tests/integration/test_get_guild_community_ids.py index 82fdd18..69e8a7c 100644 --- a/tests/integration/test_get_guild_community_ids.py +++ b/tests/integration/test_get_guild_community_ids.py @@ -2,7 +2,7 @@ from unittest import TestCase from bson.objectid import ObjectId -from utils.get_guild_community_ids import get_guild_community_ids +from utils.get_guild_utils import get_guild_community_ids from utils.get_mongo_client import MongoSingleton diff --git a/tests/integration/test_get_guild_platform_id.py b/tests/integration/test_get_guild_platform_id.py new file mode 100644 index 0000000..c55c9b0 --- /dev/null +++ b/tests/integration/test_get_guild_platform_id.py @@ -0,0 +1,55 @@ +from datetime import datetime +from unittest import TestCase + +from bson.objectid import ObjectId +from utils.get_guild_utils import get_guild_platform_id +from utils.get_mongo_client import MongoSingleton + + +class TestGetGuildId(TestCase): + def test_get_avalable_guild(self): + client = MongoSingleton.get_instance().client + platform_id = ObjectId("515151515151515151515151") + guild_id = "999888877766655" + + client.drop_database("Core") + client["Core"]["platforms"].insert_one( + { + "_id": ObjectId(platform_id), + "name": "discord", + "metadata": { + "id": guild_id, + "icon": "111111111111111111111111", + "name": "A guild", + "selectedChannels": [ + "11111111", + "22222222", + "33333333", + "44444444", + "55555555", + "66666666", + "77777777", + ], + "period": datetime(2023, 6, 1), + }, + "community": ObjectId("aabbccddeeff001122334455"), + "disconnectedAt": None, + "connectedAt": datetime(2023, 11, 1), + "isInProgress": True, + "createdAt": datetime(2023, 11, 1), + "updatedAt": datetime(2023, 11, 1), + "__v": 0, + } + ) + + platform_id = get_guild_platform_id(guild_id) + self.assertEqual(platform_id, "515151515151515151515151") + + def test_no_document_raise_error(self): + client = MongoSingleton.get_instance().client + guild_id = "999888877766655" + + client.drop_database("Core") + + with self.assertRaises(ValueError): + get_guild_platform_id(guild_id) diff --git a/utils/get_guild_community_ids.py b/utils/get_guild_utils.py similarity index 59% rename from utils/get_guild_community_ids.py rename to utils/get_guild_utils.py index b0252c4..90e8817 100644 --- a/utils/get_guild_community_ids.py +++ b/utils/get_guild_utils.py @@ -30,3 +30,30 @@ def get_guild_community_ids(platform_id: str) -> tuple[str, str]: guild_id = platform["metadata"]["id"] community_id = str(platform["community"]) return guild_id, community_id + + +def get_guild_platform_id(guild_id: str) -> str: + """ + get the guild platform id using the given guild id + + Parameters + ------------ + guild_id : str + the id for the specified guild + + Returns + -------- + platform_id : str + the platform id related to the given guild + """ + mongo_client = MongoSingleton.get_instance().client + + guild_info = mongo_client["Core"]["platforms"].find_one( + {"metadata.id": guild_id}, {"_id": 1} + ) + if guild_info is not None: + platform_id = str(guild_info["_id"]) + else: + raise ValueError(f"No available guild with id {guild_id}") + + return platform_id From dae0290c348e997106dee17fdc3a7effe5ff8018 Mon Sep 17 00:00:00 2001 From: Mohammad Amin Date: Fri, 29 Dec 2023 09:03:17 +0330 Subject: [PATCH 2/2] fix: test cases with latest guild platform document As the documents were having the isInProgress under the metadata, the test cases were forgotten to be updated. --- .../integration/test_member_activity_from_start_no_past_data.py | 2 +- ..._member_activity_from_start_with_guild_heatmaps_available.py | 2 +- ...activity_from_start_with_guild_memberactivities_available.py | 2 +- .../test_member_activity_from_start_with_one_interval.py | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/integration/test_member_activity_from_start_no_past_data.py b/tests/integration/test_member_activity_from_start_no_past_data.py index 8f7844e..4943f0c 100644 --- a/tests/integration/test_member_activity_from_start_no_past_data.py +++ b/tests/integration/test_member_activity_from_start_no_past_data.py @@ -108,4 +108,4 @@ def test_analyzer_member_activities_from_start_empty_memberactivities(): # testing whether any data is available assert memberactivities_data is not None assert heatmaps_data is not None - assert guild_document["isInProgress"] is False + assert guild_document["metadata"]["isInProgress"] is False diff --git a/tests/integration/test_member_activity_from_start_with_guild_heatmaps_available.py b/tests/integration/test_member_activity_from_start_with_guild_heatmaps_available.py index d4ca76b..06a9a0f 100644 --- a/tests/integration/test_member_activity_from_start_with_guild_heatmaps_available.py +++ b/tests/integration/test_member_activity_from_start_with_guild_heatmaps_available.py @@ -70,4 +70,4 @@ def test_analyzer_member_activities_from_start_available_heatmaps(): # testing whether any data is available assert memberactivities_data is not None assert heatmaps_data is not None - assert guild_document["isInProgress"] is False + assert guild_document["metadata"]["isInProgress"] is False diff --git a/tests/integration/test_member_activity_from_start_with_guild_memberactivities_available.py b/tests/integration/test_member_activity_from_start_with_guild_memberactivities_available.py index ce2536a..bd160ac 100644 --- a/tests/integration/test_member_activity_from_start_with_guild_memberactivities_available.py +++ b/tests/integration/test_member_activity_from_start_with_guild_memberactivities_available.py @@ -64,4 +64,4 @@ def test_analyzer_member_activities_from_start_available_member_activity(): # testing whether any data is available assert memberactivities_data is not None assert heatmaps_data is not None - assert guild_document["isInProgress"] is False + assert guild_document["metadata"]["isInProgress"] is False diff --git a/tests/integration/test_member_activity_from_start_with_one_interval.py b/tests/integration/test_member_activity_from_start_with_one_interval.py index abf2f42..3e42915 100644 --- a/tests/integration/test_member_activity_from_start_with_one_interval.py +++ b/tests/integration/test_member_activity_from_start_with_one_interval.py @@ -55,4 +55,4 @@ def test_analyzer_from_start_one_interval(): # testing whether any data is available assert memberactivities_data is not None assert heatmaps_data is not None - assert guild_document["isInProgress"] is False + assert guild_document["metadata"]["isInProgress"] is False