Skip to content

Commit

Permalink
Merge pull request #24 from RnDAO/dev_amin_discord_fetch_members
Browse files Browse the repository at this point in the history
Discord fetch members choreography added!
  • Loading branch information
cyri113 authored Jul 1, 2023
2 parents 6ad5270 + dec0af8 commit 42b12e7
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 6 deletions.
9 changes: 3 additions & 6 deletions tc_messageBroker/message_broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions tc_messageBroker/rabbit_mq/event/events_microservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class ServerEvent:
class DiscordBotEvent:
FETCH = BotBaseEvent.FETCH
SEND_MESSAGE = "SEND_MESSAGE"
FETCH_MEMBERS = "FETCH_MEMBERS"


class DiscordAnalyzerEvent:
Expand Down
7 changes: 7 additions & 0 deletions tc_messageBroker/rabbit_mq/saga/choreography.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from .transactions import (
DISCORD_UPDATE_CHANNELS_TRANSACTIONS,
DISCORD_SCHEDULED_JOB_TRANSACTIONS,
DISCORD_FETCH_MEMBERS_TRANSACTIONS,
)


Expand All @@ -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
9 changes: 9 additions & 0 deletions tc_messageBroker/rabbit_mq/saga/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
]
1 change: 1 addition & 0 deletions tests/unit/test_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
15 changes: 15 additions & 0 deletions tests/unit/test_mongodb.py
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions tests/unit/test_predefined_transactions.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
45 changes: 45 additions & 0 deletions tests/unit/test_saga_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 42b12e7

Please sign in to comment.