Commit 9cee43e0 authored by Karagkounis Dimitris's avatar Karagkounis Dimitris
Browse files

add context manager capabilities to MongoManager. migrate pydantic to v2. fix...

add context manager capabilities to MongoManager. migrate pydantic to v2. fix breaking controller. add tests. add dev dependencies
parent 37c88565
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
import os
from pydantic import BaseSettings
from pydantic.v1 import BaseSettings
from dotenv import load_dotenv

load_dotenv()
+3 −3
Original line number Diff line number Diff line
from flask import jsonify

# from database import mongo
from pydantic import ValidationError
from edge_cloud_management_api.models.application_models import AppManifest

# from edge_cloud_management_api.models.application_models import AppManifest


def submit_app(body: dict):
@@ -11,7 +11,7 @@ def submit_app(body: dict):
    """
    try:
        # Validate the input data using Pydantic
        validated_data = AppManifest(**body)
        # validated_data = AppManifest(**body)

        # Insert into MongoDB
        # app_id = mongo.db.applications.insert_one(validated_data.dict()).inserted_id
+25 −1
Original line number Diff line number Diff line
@@ -3,14 +3,38 @@ from edge_cloud_management_api.configs.env_config import Config


class MongoManager:
    """
    A utility class for managing MongoDB operations.
    The class implements the context manager protocol to ensure that the connection is closed after use.

    Methods:
        insert_document: Inserts a document into a collection.
        find_document: Finds a single document in a collection.
        find_documents: Finds multiple documents in a collection.
        update_document: Updates a single document in a collection.
        delete_document: Deletes a single document in a collection.
        close_connection: Closes the MongoDB connection.

    Example:
        with MongoManager() as db:
            db.insert_document("users", {"name": "Test User", "email": "test-user@sunrise6g.eu"})

    """

    def __init__(self):
        """
        Initializes the MongoDB connection using the URI from Config.
        """
        self.client = MongoClient(Config.MONGO_URI)
        self.client = MongoClient(Config.MONGO_URI, maxPoolSize=50)
        mongo_db_name: str = Config.MONGO_URI.split("/")[-1]
        self.db = self.client[mongo_db_name]

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.close_connection()

    def insert_document(self, collection_name, document):
        """
        Inserts a document into the specified collection.
+12 −1
Original line number Diff line number Diff line
@@ -22,7 +22,18 @@ requires = ["hatchling"]
build-backend = "hatchling.build"

[dependency-groups]
dev = ["hatchling>=1.26.3", "ruff>=0.8.1"]
dev = [
    "hatchling>=1.26.3",
    "mongomock>=4.3.0",
    "mypy>=1.14.1",
    "pytest>=8.3.4",
    "ruff>=0.8.1",
    "tox>=4.23.2",
]

[tool.pytest.ini_options]
pythonpath = ["."]
testpaths = ["tests"]

[tool.ruff]
exclude = [".eggs", ".git", ".ruff_cache", ".tox", ".venv"]
+16 −0
Original line number Diff line number Diff line
import pytest

from edge_cloud_management_api.controllers.app_controllers import get_app


@pytest.mark.parametrize("app_id, x_correlator, expected", [
    (1, None, "do some magic!"),
    (2, "123", "do some magic!"),
])
def test_get_app(app_id, x_correlator, expected):
    """
    Test the get_app controller.
    """
    result = get_app(app_id, x_correlator)
    assert result == expected
Loading