diff --git a/tc_messageBroker/message_broker.py b/tc_messageBroker/message_broker.py index 1d58445..ac5aab9 100644 --- a/tc_messageBroker/message_broker.py +++ b/tc_messageBroker/message_broker.py @@ -30,11 +30,8 @@ def __new__(cls, broker_url: str, port: int, username: str, password: str): return cls.instance def connect( - self, - queue_name: str, - consume_options: dict = None, - heartbeat: int = 60 - ) -> bool: + self, queue_name: str, consume_options: dict = None, heartbeat: int = 60 + ) -> bool: """ connect the rabbitMQ broker and start consuming @@ -63,7 +60,7 @@ def connect( host=amqpServer, port=self.port, credentials=credentials, - heartbeat=heartbeat, ## disabling the heartbeat + heartbeat=heartbeat, ## disabling the heartbeat ), ) self.channel = self.connection.channel() diff --git a/tc_messageBroker/rabbit_mq/event/events_microservice.py b/tc_messageBroker/rabbit_mq/event/events_microservice.py index 0fb6922..e84a589 100644 --- a/tc_messageBroker/rabbit_mq/event/events_microservice.py +++ b/tc_messageBroker/rabbit_mq/event/events_microservice.py @@ -15,6 +15,7 @@ class ServerEvent: class DiscordBotEvent: FETCH = BotBaseEvent.FETCH SEND_MESSAGE = "SEND_MESSAGE" + FETCH_MEMBERS = "FETCH_MEMBERS" class DiscordAnalyzerEvent: diff --git a/tc_messageBroker/rabbit_mq/saga/choreography.py b/tc_messageBroker/rabbit_mq/saga/choreography.py index d2efa25..2d7cbfa 100644 --- a/tc_messageBroker/rabbit_mq/saga/choreography.py +++ b/tc_messageBroker/rabbit_mq/saga/choreography.py @@ -2,6 +2,7 @@ from .transactions import ( DISCORD_UPDATE_CHANNELS_TRANSACTIONS, DISCORD_SCHEDULED_JOB_TRANSACTIONS, + DISCORD_FETCH_MEMBERS_TRANSACTIONS, ) @@ -15,7 +16,13 @@ transactions=DISCORD_SCHEDULED_JOB_TRANSACTIONS, ) +DISCORD_FETCH_MEMBERS = IChoreography( + name="DISCORD_FETCH_MEMBERS", + transactions=DISCORD_FETCH_MEMBERS_TRANSACTIONS, +) + class ChoreographyDict: DISCORD_UPDATE_CHANNELS = DISCORD_UPDATE_CHANNELS DISCORD_SCHEDULED_JOB = DISCORD_SCHEDULED_JOB + DISCORD_FETCH_MEMBERS = DISCORD_FETCH_MEMBERS diff --git a/tc_messageBroker/rabbit_mq/saga/transactions.py b/tc_messageBroker/rabbit_mq/saga/transactions.py index fc6e01f..259e111 100644 --- a/tc_messageBroker/rabbit_mq/saga/transactions.py +++ b/tc_messageBroker/rabbit_mq/saga/transactions.py @@ -51,3 +51,12 @@ status=Status.NOT_STARTED, ), ] + +DISCORD_FETCH_MEMBERS_TRANSACTIONS = [ + ITransaction( + Queue.DISCORD_BOT, + Event.DISCORD_BOT.FETCH_MEMBERS, + order=1, + status=Status.NOT_STARTED, + ) +] diff --git a/tests/unit/test_event.py b/tests/unit/test_event.py index 6d9ce95..4314c0d 100644 --- a/tests/unit/test_event.py +++ b/tests/unit/test_event.py @@ -8,4 +8,5 @@ def test_enum_event_default(): assert Event.DISCORD_BOT.SEND_MESSAGE == "SEND_MESSAGE" assert Event.DISCORD_ANALYZER.RUN == "RUN" assert Event.DISCORD_ANALYZER.RUN_ONCE == "RUN_ONCE" + assert Event.DISCORD_BOT.FETCH_MEMBERS == "FETCH_MEMBERS" # assert Event.DISCORD_ANALYZER.SAVE == "SAVE" diff --git a/tests/unit/test_mongodb.py b/tests/unit/test_mongodb.py new file mode 100644 index 0000000..d5e1259 --- /dev/null +++ b/tests/unit/test_mongodb.py @@ -0,0 +1,15 @@ +from tc_messageBroker.rabbit_mq.db_operations.mongodb import MongoDB + + +def test_mongo_class(): + connection_url = "connection_str" + db_name = "Sagas" + collection_name = "saga" + mongodb = MongoDB( + connection_str=connection_url, db_name=db_name, collection_name=collection_name + ) + + assert mongodb.connection_str == connection_url + assert mongodb.db_name == db_name + assert mongodb.collection_name == collection_name + assert mongodb.client is None diff --git a/tests/unit/test_predefined_transactions.py b/tests/unit/test_predefined_transactions.py index 898a016..9775f4c 100644 --- a/tests/unit/test_predefined_transactions.py +++ b/tests/unit/test_predefined_transactions.py @@ -1,6 +1,7 @@ from tc_messageBroker.rabbit_mq.saga.transactions import ( DISCORD_SCHEDULED_JOB_TRANSACTIONS, DISCORD_UPDATE_CHANNELS_TRANSACTIONS, + DISCORD_FETCH_MEMBERS_TRANSACTIONS, ) from tc_messageBroker.rabbit_mq.queue import Queue from tc_messageBroker.rabbit_mq.event import Event @@ -40,3 +41,14 @@ def test_discord_scheduled_job_tx(): assert tx[0].queue == Queue.DISCORD_ANALYZER assert tx[0].event == Event.DISCORD_ANALYZER.RUN_ONCE assert tx[0].status == Status.NOT_STARTED + + +def test_discord_bot_fetch_tx(): + tx = DISCORD_FETCH_MEMBERS_TRANSACTIONS + + assert len(tx) == 1 + + assert tx[0].order == 1 + assert tx[0].queue == Queue.DISCORD_BOT + assert tx[0].event == Event.DISCORD_BOT.FETCH_MEMBERS + assert tx[0].status == Status.NOT_STARTED diff --git a/tests/unit/test_saga_base.py b/tests/unit/test_saga_base.py index 2f23204..a15a8cb 100644 --- a/tests/unit/test_saga_base.py +++ b/tests/unit/test_saga_base.py @@ -115,3 +115,48 @@ def test_choreagraphy_sorting_status_random_choreogprahy(): assert condition is False else: condition = True + + +def test_saga_create_data(): + saga_data = {"guild": "some_guildId"} + creation_date = datetime.now() + + saga = Saga( + ChoreographyDict.DISCORD_UPDATE_CHANNELS, + Status.NOT_STARTED, + data=saga_data, + created_at=creation_date, + ) + + saga_dict = saga._create_data() + + assert ( + saga_dict["choreography"]["name"] + == ChoreographyDict.DISCORD_UPDATE_CHANNELS.name + ) + + ## we had a list of transactions + for idx, tx in enumerate(saga_dict["choreography"]["transactions"]): + predefined_tx = ChoreographyDict.DISCORD_UPDATE_CHANNELS.transactions[idx] + assert tx["queue"] == predefined_tx.queue + assert tx["event"] == predefined_tx.event + assert tx["order"] == predefined_tx.order + assert tx["status"] == predefined_tx.status + ## these should be not defined in predefine transaction + assert "start" not in tx.keys() + assert "end" not in tx.keys() + assert "runtime" not in tx.keys() + assert "message" not in tx.keys() + assert "error" not in tx.keys() + + assert predefined_tx.start is None + assert predefined_tx.end is None + assert predefined_tx.message is None + assert predefined_tx.runtime is None + assert predefined_tx.error is None + + assert saga_dict["status"] == Status.NOT_STARTED + assert saga_dict["data"] == saga_data + assert saga_dict["sagaId"] == saga.uuid + assert saga_dict["createdAt"] == creation_date + assert saga_dict["updatedAt"] is not None