From b05088c966425ca203aab50a5ef7d34a517449f3 Mon Sep 17 00:00:00 2001 From: TrisNol Date: Sun, 16 Jul 2023 15:53:01 +0200 Subject: [PATCH] test: utils.mongo --- .../utils/mongo.py | 51 +++++++------------ tests/utils/mongo_test.py | 26 ++++++++++ 2 files changed, 44 insertions(+), 33 deletions(-) create mode 100644 tests/utils/mongo_test.py diff --git a/src/aki_prj23_transparenzregister/utils/mongo.py b/src/aki_prj23_transparenzregister/utils/mongo.py index 503b96b..cac9e26 100644 --- a/src/aki_prj23_transparenzregister/utils/mongo.py +++ b/src/aki_prj23_transparenzregister/utils/mongo.py @@ -14,6 +14,23 @@ class MongoConnection: username: str | None password: str | None + def get_conn_string(self) -> str: + """Transforms the information of the object to a MongoDB connection string. + + Returns: + str: Connection string + """ + if self.username is not None and self.password is not None: + connection_string = ( + f"mongodb+srv://{self.username}:{self.password}@{self.hostname}" + ) + else: + connection_string = f"mongodb+srv://{self.hostname}" + if self.port is not None: + connection_string += f":{self.port}" + connection_string = connection_string.replace("mongodb+srv", "mongodb") + return connection_string + class MongoConnector: """Wrapper for establishing a connection to a MongoDB instance.""" @@ -24,37 +41,5 @@ class MongoConnector: Args: connection (MongoConnection): Wrapper for connection string """ - self.client = self.connect( - connection.hostname, - connection.port, - connection.username, - connection.password, - ) + self.client = pymongo.MongoClient(connection.get_conn_string()) self.database = self.client[connection.database] - - def connect( - self, - hostname: str, - port: int | None, - username: str | None, - password: str | None, - ) -> pymongo.MongoClient: - """_summary_. - - Args: - hostname (str): hostname - port (int): port - username (str): Username - password (str): Password - - Returns: - pymongo.MongoClient: MongoClient connect to the DB - """ - if username is not None and password is not None: - connection_string = f"mongodb+srv://{username}:{password}@{hostname}" - else: - connection_string = f"mongodb+srv://{hostname}" - if port is not None: - connection_string += f":{port}" - connection_string = connection_string.replace("mongodb+srv", "mongodb") - return pymongo.MongoClient(connection_string) diff --git a/tests/utils/mongo_test.py b/tests/utils/mongo_test.py new file mode 100644 index 0000000..f52a192 --- /dev/null +++ b/tests/utils/mongo_test.py @@ -0,0 +1,26 @@ +from unittest.mock import patch + +from aki_prj23_transparenzregister.utils.mongo import MongoConnection, MongoConnector + + +def test_get_conn_string_no_credentials(): + conn = MongoConnection("localhost", "", 27017, None, None) + assert conn.get_conn_string() == "mongodb://localhost:27017" + + +def test_get_conn_string_no_port_but_credentials(): + conn = MongoConnection("localhost", "", None, "admin", "password") + assert conn.get_conn_string() == "mongodb+srv://admin:password@localhost" + + +def test_get_conn_simple(): + conn = MongoConnection("localhost", "", None, None, None) + assert conn.get_conn_string() == "mongodb+srv://localhost" + + +def test_mongo_connector(): + with patch("pymongo.MongoClient") as mock_mongo_client: + expected_result = 42 + mock_mongo_client.return_value = {"db": expected_result} + temp = MongoConnector(MongoConnection("localhost", "db", None, None, None)) + assert temp.database == expected_result