mirror of
https://github.com/fhswf/aki_prj23_transparenzregister.git
synced 2025-06-21 21:43:55 +02:00
test: utils.mongo
This commit is contained in:
@ -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)
|
||||
|
26
tests/utils/mongo_test.py
Normal file
26
tests/utils/mongo_test.py
Normal 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
|
Reference in New Issue
Block a user