Skip to content

Commit

Permalink
Update tests for changes to JSON libraries (fall-through now working …
Browse files Browse the repository at this point in the history
…slightly differently)
  • Loading branch information
Jekannadar committed Jun 11, 2024
1 parent 6ae94d6 commit 7e549e9
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 16 deletions.
18 changes: 8 additions & 10 deletions applications/cls-cad-backend/cls_cad_backend/database/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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): ",
Expand All @@ -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()
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion applications/cls-cad-backend/cls_cad_backend/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 0 additions & 4 deletions applications/cls-cad-backend/cls_cad_backend/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 = {}


Expand Down
2 changes: 2 additions & 0 deletions applications/cls-cad-backend/main.py
Original file line number Diff line number Diff line change
@@ -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)


Expand Down
1 change: 0 additions & 1 deletion applications/cls-cad-backend/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 7e549e9

Please sign in to comment.