test: utils.mongo

This commit is contained in:
TrisNol
2023-07-16 15:53:01 +02:00
parent 4b5f63ee62
commit b05088c966
2 changed files with 44 additions and 33 deletions

View File

@ -14,6 +14,23 @@ class MongoConnection:
username: str | None username: str | None
password: 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: class MongoConnector:
"""Wrapper for establishing a connection to a MongoDB instance.""" """Wrapper for establishing a connection to a MongoDB instance."""
@ -24,37 +41,5 @@ class MongoConnector:
Args: Args:
connection (MongoConnection): Wrapper for connection string connection (MongoConnection): Wrapper for connection string
""" """
self.client = self.connect( self.client = pymongo.MongoClient(connection.get_conn_string())
connection.hostname,
connection.port,
connection.username,
connection.password,
)
self.database = self.client[connection.database] 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)

26
tests/utils/mongo_test.py Normal file
View File

@ -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