From 88898e8fe0492775f0ca1fbb8d695cf97cbfbf5c Mon Sep 17 00:00:00 2001 From: Mohammad Amin Date: Tue, 21 May 2024 12:07:17 +0330 Subject: [PATCH 1/5] fix: update mongodb credentials! + now we're reading from `.env` if not passed in saga.next + updated .env variable names --- docker-compose.test.yml | 9 ++-- setup.py | 2 +- tc_messageBroker/rabbit_mq/saga/saga_base.py | 34 +++++++++--- tc_messageBroker/utils/__init__.py | 0 tc_messageBroker/utils/credentials.py | 36 +++++++++++++ tests/integration/test_mongodb.py | 56 ++++++++++---------- tests/integration/test_saga.py | 16 +++--- tests/unit/test_credentials.py | 16 ++++++ tests/unit/test_mongodb.py | 16 +++--- 9 files changed, 128 insertions(+), 57 deletions(-) create mode 100644 tc_messageBroker/utils/__init__.py create mode 100644 tc_messageBroker/utils/credentials.py create mode 100644 tests/unit/test_credentials.py diff --git a/docker-compose.test.yml b/docker-compose.test.yml index fb4a3b6..7a034da 100644 --- a/docker-compose.test.yml +++ b/docker-compose.test.yml @@ -8,12 +8,13 @@ services: dockerfile: Dockerfile environment: - PORT=3000 - - DB_HOST=mongo - - DB_PORT=27017 - - DB_USER=root + - MONGODB_HOST=mongo + - MONGODB_PORT=27017 + - MONGODB_USER=root + - MONGODB_PASS=pass - DB_SAGA_NAME=Saga - DB_SAGA_COLLECTION_NAME=saga - - DB_PASSWORD=pass + - MONGODB_PASS=pass - RABBIT_HOST=rabbitmq - RABBIT_PORT=5672 - RABBIT_USER=root diff --git a/setup.py b/setup.py index edca7cd..00ba047 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ setup( name="tc-messageBroker", - version="1.6.6", + version="1.6.7", author="Mohammad Amin Dadgar, RnDAO", maintainer="Mohammad Amin Dadgar", maintainer_email="dadgaramin96@gmail.com", diff --git a/tc_messageBroker/rabbit_mq/saga/saga_base.py b/tc_messageBroker/rabbit_mq/saga/saga_base.py index 4148a4c..3cef385 100644 --- a/tc_messageBroker/rabbit_mq/saga/saga_base.py +++ b/tc_messageBroker/rabbit_mq/saga/saga_base.py @@ -10,6 +10,7 @@ from .choreography_base import IChoreography from .transaction_base import ITransaction from .utils.saga_base_utils import convert_tx_dict, get_transactions +from tc_messageBroker.utils.credentials import load_mongo_credentials class Saga: @@ -56,8 +57,8 @@ def next( self, publish_method: Callable, call_function: Callable, - mongo_creds: dict[str, Any], - test_mode=False, + mongo_creds: dict[str, Any] = None, + **kwargs, ): """ calling the next transaction within the saga @@ -69,13 +70,30 @@ def next( the publish methods that are from the RabbitMQ call_function : Callable a function to be called when the message recieved - mongo_creds : dict[str, Any] - the mongodb credentials to update the db - the keys must be `connection_str`, `db_name`, and `collection_name` - test_mode : bool - testing the function indicates that we wouldn't read or write on DB - default is False + **kwargs : + test_mode : bool + testing the function indicates that we wouldn't read or write on DB + default is False + mongo_creds : dict[str, Any] | None + the mongodb credentials to update the db + if not `None`, the keys must be `connection_str`, `db_name`, and `collection_name` + saga_db_name : str + the saga database that data is going to be saved within it + default would be `Saga` + saga_collection_name : str + the saga collection that data is going to be saved within it + default would be `sagas` """ + test_mode = kwargs.get("test_mode", False) + mongo_creds = kwargs.get("mongo_creds", None) + saga_db_name = kwargs.get("saga_db_name", "Saga") + saga_collection_name = kwargs.get("saga_db_name", "sagas") + + if mongo_creds is None: + mongo_creds = load_mongo_credentials() + mongo_creds["db_name"] = saga_db_name + mongo_creds["collection_name"] = saga_collection_name + tx_sorted, tx_not_started_count = self._sort_transactions( self.choreography.transactions ) diff --git a/tc_messageBroker/utils/__init__.py b/tc_messageBroker/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tc_messageBroker/utils/credentials.py b/tc_messageBroker/utils/credentials.py new file mode 100644 index 0000000..2583b44 --- /dev/null +++ b/tc_messageBroker/utils/credentials.py @@ -0,0 +1,36 @@ +from dotenv import load_dotenv +import os + +def load_mongo_credentials(): + """ + load mongo db credentials from .env + + Returns: + --------- + mongo_creds : dict[str, Any] + mongodb credentials + a dictionary representive of + `user`: str + `password` : str + `host` : str + `port` : int + `connection_str`: str + """ + load_dotenv() + + mongo_creds = {} + + user = os.getenv("MONGODB_USER") + password = os.getenv("MONGODB_PASS") + host = os.getenv("MONGODB_HOST") + port = os.getenv("MONGODB_PORT") + + mongo_creds["user"] = user + mongo_creds["password"] = password + mongo_creds["host"] = host + mongo_creds["port"] = port + + connection = f"mongodb://{user}:{password}@{host}:{port}" + mongo_creds["connection_str"] = connection + + return mongo_creds \ No newline at end of file diff --git a/tests/integration/test_mongodb.py b/tests/integration/test_mongodb.py index 2ee27e6..57db281 100644 --- a/tests/integration/test_mongodb.py +++ b/tests/integration/test_mongodb.py @@ -9,10 +9,10 @@ def test_insert_one(): load_dotenv() - host = os.getenv("DB_HOST") - port = os.getenv("DB_PORT") - user = os.getenv("DB_USER") - password = os.getenv("DB_PASSWORD") + host = os.getenv("MONGODB_HOST") + port = os.getenv("MONGODB_PORT") + user = os.getenv("MONGODB_USER") + password = os.getenv("MONGODB_PASS") connection_url = f"mongodb://{user}:{password}@{host}:{port}" @@ -30,10 +30,10 @@ def test_insert_one(): def test_insert_multiple(): load_dotenv() - host = os.getenv("DB_HOST") - port = os.getenv("DB_PORT") - user = os.getenv("DB_USER") - password = os.getenv("DB_PASSWORD") + host = os.getenv("MONGODB_HOST") + port = os.getenv("MONGODB_PORT") + user = os.getenv("MONGODB_USER") + password = os.getenv("MONGODB_PASS") connection_url = f"mongodb://{user}:{password}@{host}:{port}" db_name = os.getenv("DB_SAGA_NAME") @@ -56,10 +56,10 @@ def test_read_count_one(): read from db after writing using the previous function (above) """ load_dotenv() - host = os.getenv("DB_HOST") - port = os.getenv("DB_PORT") - user = os.getenv("DB_USER") - password = os.getenv("DB_PASSWORD") + host = os.getenv("MONGODB_HOST") + port = os.getenv("MONGODB_PORT") + user = os.getenv("MONGODB_USER") + password = os.getenv("MONGODB_PASS") connection_url = f"mongodb://{user}:{password}@{host}:{port}" db_name = os.getenv("DB_SAGA_NAME") @@ -93,10 +93,10 @@ def test_read_count_multiple(): read from db after writing using the previous function (above) """ load_dotenv() - host = os.getenv("DB_HOST") - port = os.getenv("DB_PORT") - user = os.getenv("DB_USER") - password = os.getenv("DB_PASSWORD") + host = os.getenv("MONGODB_HOST") + port = os.getenv("MONGODB_PORT") + user = os.getenv("MONGODB_USER") + password = os.getenv("MONGODB_PASS") connection_url = f"mongodb://{user}:{password}@{host}:{port}" db_name = os.getenv("DB_SAGA_NAME") @@ -135,10 +135,10 @@ def test_replace(): test replacing a document we just inserted """ load_dotenv() - host = os.getenv("DB_HOST") - port = os.getenv("DB_PORT") - user = os.getenv("DB_USER") - password = os.getenv("DB_PASSWORD") + host = os.getenv("MONGODB_HOST") + port = os.getenv("MONGODB_PORT") + user = os.getenv("MONGODB_USER") + password = os.getenv("MONGODB_PASS") connection_url = f"mongodb://{user}:{password}@{host}:{port}" db_name = os.getenv("DB_SAGA_NAME") @@ -178,10 +178,10 @@ def test_delete(): delete all except one of the ligitimate data which we choose """ load_dotenv() - host = os.getenv("DB_HOST") - port = os.getenv("DB_PORT") - user = os.getenv("DB_USER") - password = os.getenv("DB_PASSWORD") + host = os.getenv("MONGODB_HOST") + port = os.getenv("MONGODB_PORT") + user = os.getenv("MONGODB_USER") + password = os.getenv("MONGODB_PASS") connection_url = f"mongodb://{user}:{password}@{host}:{port}" db_name = os.getenv("DB_SAGA_NAME") @@ -201,10 +201,10 @@ def test_wrong_input(): give wrong input to insert """ load_dotenv() - host = os.getenv("DB_HOST") - port = os.getenv("DB_PORT") - user = os.getenv("DB_USER") - password = os.getenv("DB_PASSWORD") + host = os.getenv("MONGODB_HOST") + port = os.getenv("MONGODB_PORT") + user = os.getenv("MONGODB_USER") + password = os.getenv("MONGODB_PASS") connection_url = f"mongodb://{user}:{password}@{host}:{port}" db_name = os.getenv("DB_SAGA_NAME") diff --git a/tests/integration/test_saga.py b/tests/integration/test_saga.py index 2c8e930..a3e20e4 100644 --- a/tests/integration/test_saga.py +++ b/tests/integration/test_saga.py @@ -18,10 +18,10 @@ def test_inputs(): load_dotenv() - host = os.getenv("DB_HOST") - port = os.getenv("DB_PORT") - user = os.getenv("DB_USER") - password = os.getenv("DB_PASSWORD") + host = os.getenv("MONGODB_HOST") + port = os.getenv("MONGODB_PORT") + user = os.getenv("MONGODB_USER") + password = os.getenv("MONGODB_PASS") saga_db = os.getenv("DB_SAGA_NAME") saga_collection = os.getenv("DB_SAGA_COLLECTION_NAME") @@ -48,10 +48,10 @@ def test_saga_update(): test updating a saga instance in db """ load_dotenv() - host = os.getenv("DB_HOST") - port = os.getenv("DB_PORT") - user = os.getenv("DB_USER") - password = os.getenv("DB_PASSWORD") + host = os.getenv("MONGODB_HOST") + port = os.getenv("MONGODB_PORT") + user = os.getenv("MONGODB_USER") + password = os.getenv("MONGODB_PASS") connection_creds = {} connection_creds["connection_str"] = f"mongodb://{user}:{password}@{host}:{port}" diff --git a/tests/unit/test_credentials.py b/tests/unit/test_credentials.py new file mode 100644 index 0000000..4f27c3e --- /dev/null +++ b/tests/unit/test_credentials.py @@ -0,0 +1,16 @@ +from unittest import TestCase + +from tc_messageBroker.utils.credentials import load_mongo_credentials + + +class TestCredentialsLoading(TestCase): + def test_non_none_values(self): + creds = load_mongo_credentials() + self.assertIsNotNone(creds["user"]) + self.assertIsNotNone(creds["password"]) + self.assertIsNotNone(creds["host"]) + self.assertIsNotNone(creds["port"]) + self.assertNotEqual( + creds["connection_str"], + "mongodb://None:None@None:None" + ) \ No newline at end of file diff --git a/tests/unit/test_mongodb.py b/tests/unit/test_mongodb.py index ca8671f..f761713 100644 --- a/tests/unit/test_mongodb.py +++ b/tests/unit/test_mongodb.py @@ -6,10 +6,10 @@ def test_mongo_class(): load_dotenv() - host = os.getenv("DB_HOST") - port = os.getenv("DB_PORT") - user = os.getenv("DB_USER") - password = os.getenv("DB_PASSWORD") + host = os.getenv("MONGODB_HOST") + port = os.getenv("MONGODB_PORT") + user = os.getenv("MONGODB_USER") + password = os.getenv("MONGODB_PASS") connection_url = f"{user}:{password}@{host}:{port}" db_name = os.getenv("DB_SAGA_NAME") @@ -26,10 +26,10 @@ def test_mongo_class(): def test_credentials(): load_dotenv() - host = os.getenv("DB_HOST") - port = os.getenv("DB_PORT") - user = os.getenv("DB_USER") - password = os.getenv("DB_PASSWORD") + host = os.getenv("MONGODB_HOST") + port = os.getenv("MONGODB_PORT") + user = os.getenv("MONGODB_USER") + password = os.getenv("MONGODB_PASS") db_name = os.getenv("DB_SAGA_NAME") collection_name = os.getenv("DB_SAGA_COLLECTION_NAME") From ca5ea675e66c5f2567a67034955c7a7f67812f56 Mon Sep 17 00:00:00 2001 From: Mohammad Amin Date: Tue, 21 May 2024 12:14:26 +0330 Subject: [PATCH 2/5] fix: remove duplicate .env value! --- docker-compose.test.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/docker-compose.test.yml b/docker-compose.test.yml index 7a034da..d974c35 100644 --- a/docker-compose.test.yml +++ b/docker-compose.test.yml @@ -11,7 +11,6 @@ services: - MONGODB_HOST=mongo - MONGODB_PORT=27017 - MONGODB_USER=root - - MONGODB_PASS=pass - DB_SAGA_NAME=Saga - DB_SAGA_COLLECTION_NAME=saga - MONGODB_PASS=pass From aa6c1beec779faebda7d4ebdb0038ccf07f0b296 Mon Sep 17 00:00:00 2001 From: Mohammad Amin Date: Tue, 21 May 2024 12:34:54 +0330 Subject: [PATCH 3/5] fix: lint issues! --- tc_messageBroker/rabbit_mq/saga/saga_base.py | 2 +- tc_messageBroker/utils/credentials.py | 3 ++- tests/unit/test_credentials.py | 5 +---- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/tc_messageBroker/rabbit_mq/saga/saga_base.py b/tc_messageBroker/rabbit_mq/saga/saga_base.py index 3cef385..cce0847 100644 --- a/tc_messageBroker/rabbit_mq/saga/saga_base.py +++ b/tc_messageBroker/rabbit_mq/saga/saga_base.py @@ -70,7 +70,7 @@ def next( the publish methods that are from the RabbitMQ call_function : Callable a function to be called when the message recieved - **kwargs : + **kwargs : test_mode : bool testing the function indicates that we wouldn't read or write on DB default is False diff --git a/tc_messageBroker/utils/credentials.py b/tc_messageBroker/utils/credentials.py index 2583b44..4615607 100644 --- a/tc_messageBroker/utils/credentials.py +++ b/tc_messageBroker/utils/credentials.py @@ -1,6 +1,7 @@ from dotenv import load_dotenv import os + def load_mongo_credentials(): """ load mongo db credentials from .env @@ -33,4 +34,4 @@ def load_mongo_credentials(): connection = f"mongodb://{user}:{password}@{host}:{port}" mongo_creds["connection_str"] = connection - return mongo_creds \ No newline at end of file + return mongo_creds diff --git a/tests/unit/test_credentials.py b/tests/unit/test_credentials.py index 4f27c3e..eb3915d 100644 --- a/tests/unit/test_credentials.py +++ b/tests/unit/test_credentials.py @@ -10,7 +10,4 @@ def test_non_none_values(self): self.assertIsNotNone(creds["password"]) self.assertIsNotNone(creds["host"]) self.assertIsNotNone(creds["port"]) - self.assertNotEqual( - creds["connection_str"], - "mongodb://None:None@None:None" - ) \ No newline at end of file + self.assertNotEqual(creds["connection_str"], "mongodb://None:None@None:None") From f7db9f817c7e809795825085f00f82039fd6ca1a Mon Sep 17 00:00:00 2001 From: Mohammad Amin Date: Tue, 21 May 2024 12:38:37 +0330 Subject: [PATCH 4/5] fix: lint mypy issues! --- tc_messageBroker/rabbit_mq/saga/saga_base.py | 2 +- tc_messageBroker/utils/credentials.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/tc_messageBroker/rabbit_mq/saga/saga_base.py b/tc_messageBroker/rabbit_mq/saga/saga_base.py index cce0847..af73c3e 100644 --- a/tc_messageBroker/rabbit_mq/saga/saga_base.py +++ b/tc_messageBroker/rabbit_mq/saga/saga_base.py @@ -57,7 +57,7 @@ def next( self, publish_method: Callable, call_function: Callable, - mongo_creds: dict[str, Any] = None, + mongo_creds: dict[str, Any] | None = None, **kwargs, ): """ diff --git a/tc_messageBroker/utils/credentials.py b/tc_messageBroker/utils/credentials.py index 4615607..bda1105 100644 --- a/tc_messageBroker/utils/credentials.py +++ b/tc_messageBroker/utils/credentials.py @@ -1,6 +1,7 @@ -from dotenv import load_dotenv import os +from dotenv import load_dotenv + def load_mongo_credentials(): """ From 0d4aec6944b24142025d90d5f5edc4978a932fbc Mon Sep 17 00:00:00 2001 From: Mohammad Amin Date: Tue, 21 May 2024 12:47:56 +0330 Subject: [PATCH 5/5] fix: isort linter issue! --- tc_messageBroker/rabbit_mq/saga/saga_base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tc_messageBroker/rabbit_mq/saga/saga_base.py b/tc_messageBroker/rabbit_mq/saga/saga_base.py index af73c3e..fe8f37d 100644 --- a/tc_messageBroker/rabbit_mq/saga/saga_base.py +++ b/tc_messageBroker/rabbit_mq/saga/saga_base.py @@ -6,11 +6,11 @@ import numpy as np from tc_messageBroker.rabbit_mq.db_operations import MongoDB from tc_messageBroker.rabbit_mq.status import Status +from tc_messageBroker.utils.credentials import load_mongo_credentials from .choreography_base import IChoreography from .transaction_base import ITransaction from .utils.saga_base_utils import convert_tx_dict, get_transactions -from tc_messageBroker.utils.credentials import load_mongo_credentials class Saga: