diff --git a/applications/cls-cad-backend/cls_cad_backend/database/commands.py b/applications/cls-cad-backend/cls_cad_backend/database/commands.py index 1900790..43f0aad 100644 --- a/applications/cls-cad-backend/cls_cad_backend/database/commands.py +++ b/applications/cls-cad-backend/cls_cad_backend/database/commands.py @@ -11,13 +11,13 @@ from pymongo.collection import Collection database: MontyClient | MongoClient -parts: Collection -taxonomies: Collection -results: Collection +parts: Collection = None +taxonomies: Collection = None +results: Collection = None storage_engine = "flatfile" if any(platform.win32_ver()) else "lightning" -def init_database(): +def init_database(): # pragma: no cover """ Initialize the database for the backend. This can either be a remote MongoDB instance, or a local MontyDB instance. When possible, the local instance uses LMDB @@ -33,15 +33,13 @@ def init_database(): config_path = os.path.join(application_path, "config.ini") container_path = os.path.join(application_path, "container") config = configparser.ConfigParser() - if not os.path.exists(config_path) and not os.path.exists( - container_path - ): # pragma: no cover + if not os.path.exists(config_path) and not os.path.exists(container_path): is_remote = askyesno( "Connect to remote DB?", "Do you want to connect to a hosted MongoDB instance?", ) connection_url: str | None = "" - if is_remote: # pragma: no cover + if is_remote: connection_url = askstring( "Remote URL", "Please enter the connection url (with user and password, stored locally in plain text): ", @@ -52,7 +50,7 @@ def init_database(): except errors.ServerSelectionTimeoutError as err: showerror("Connection Error", "Could not connect to database. Exiting.") exit(0) - else: # pragma: no cover + else: database = MontyClient(os.path.join(application_path, "db")) if askyesno("Import", "Import an existing database?"): import_data = askopenfilename() @@ -82,7 +80,7 @@ def init_database(): map_size="1073741824", ) database = MontyClient(os.path.join(application_path, "db")) - else: # pragma: no cover + else: config.read(config_path) if config["db"]["is_remote"] and config["db"]["connection_url"] != "": try: diff --git a/applications/cls-cad-backend/cls_cad_backend/responses.py b/applications/cls-cad-backend/cls_cad_backend/responses.py index bbf88cf..7933917 100644 --- a/applications/cls-cad-backend/cls_cad_backend/responses.py +++ b/applications/cls-cad-backend/cls_cad_backend/responses.py @@ -24,7 +24,7 @@ def render(self, content: typing.Any) -> bytes: :param content: The content to encode. :return: The encoded content. """ - if base_json: + if base_json: # pragma: no cover return json.dumps(content, indent=2, ensure_ascii=False).encode("utf-8") try: # pragma: no cover return orjson.dumps(content, option=orjson.OPT_INDENT_2) diff --git a/applications/cls-cad-backend/cls_cad_backend/server.py b/applications/cls-cad-backend/cls_cad_backend/server.py index 1c4668d..47b5628 100644 --- a/applications/cls-cad-backend/cls_cad_backend/server.py +++ b/applications/cls-cad-backend/cls_cad_backend/server.py @@ -10,7 +10,6 @@ get_all_result_ids_for_project, get_result_for_id_in_project, get_taxonomy_for_project, - init_database, upsert_part, upsert_result, upsert_taxonomy, @@ -56,14 +55,11 @@ mimetypes.init() mimetypes.add_type("application/javascript", ".js") - app.mount( "/static", StaticFiles(directory=os.path.join(os.path.dirname(__file__), "static"), html=True), name="static", ) -init_database() - cache = {} diff --git a/applications/cls-cad-backend/main.py b/applications/cls-cad-backend/main.py index bd71f0c..f8c687f 100644 --- a/applications/cls-cad-backend/main.py +++ b/applications/cls-cad-backend/main.py @@ -1,8 +1,10 @@ import uvicorn +from cls_cad_backend.database.commands import init_database def start(): """Launched with `poetry run start` at root level.""" + init_database() uvicorn.run("cls_cad_backend.server:app", reload=True) diff --git a/applications/cls-cad-backend/tests/conftest.py b/applications/cls-cad-backend/tests/conftest.py index b97f23d..d5f7adf 100644 --- a/applications/cls-cad-backend/tests/conftest.py +++ b/applications/cls-cad-backend/tests/conftest.py @@ -1,7 +1,6 @@ import os import shutil -import cls_cad_backend.database.commands import cls_cad_backend.server import pytest from cls_cad_backend.database.commands import switch_to_test_database