diff --git a/docker-compose.test.yml b/docker-compose.test.yml index fb4a3b6..d974c35 100644 --- a/docker-compose.test.yml +++ b/docker-compose.test.yml @@ -8,12 +8,12 @@ services: dockerfile: Dockerfile environment: - PORT=3000 - - DB_HOST=mongo - - DB_PORT=27017 - - DB_USER=root + - MONGODB_HOST=mongo + - MONGODB_PORT=27017 + - MONGODB_USER=root - 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..fe8f37d 100644 --- a/tc_messageBroker/rabbit_mq/saga/saga_base.py +++ b/tc_messageBroker/rabbit_mq/saga/saga_base.py @@ -6,6 +6,7 @@ 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 @@ -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 = 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..bda1105 --- /dev/null +++ b/tc_messageBroker/utils/credentials.py @@ -0,0 +1,38 @@ +import os + +from dotenv import load_dotenv + + +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 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..eb3915d --- /dev/null +++ b/tests/unit/test_credentials.py @@ -0,0 +1,13 @@ +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") 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")