From b3b1e758ce6b69b1ead4ad71e0c71c6c73ee21bd Mon Sep 17 00:00:00 2001 From: Brian Brondel Date: Fri, 3 Jan 2025 20:56:16 -0300 Subject: [PATCH] Fix some unit tests --- python/lsst/consdb/handlers/external.py | 9 ++++----- python/lsst/consdb/pqserver.py | 22 +++++++++++++++++++++- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/python/lsst/consdb/handlers/external.py b/python/lsst/consdb/handlers/external.py index 6b37df0..bf86853 100644 --- a/python/lsst/consdb/handlers/external.py +++ b/python/lsst/consdb/handlers/external.py @@ -92,15 +92,14 @@ def add_flexible_metadata_key( """Add a key to a flexible metadata table.""" schema_table = instrument_table.get_flexible_metadata_schema(obs_type) - new_entry = schema_table( + insert_stmt = schema_table.insert().values( key=data.key, dtype=data.dtype.value, doc=data.doc, unit=data.unit, ucd=data.ucd, ) - logger.debug(f"Inserting: {new_entry}") - db.add(new_entry) + db.execute(insert_stmt) db.commit() # Update cached copy without re-querying database. instrument_table.flexible_metadata_schemas[obs_type.lower()][data.key] = [ @@ -157,9 +156,9 @@ def get_flexible_metadata( schema = instrument_table.flexible_metadata_schemas[obs_type] result = dict() - query = db.query(table.key, table.value).filter(table.c.obs_id == obs_id) + query = db.query(table.c.key, table.c.value).filter(table.c.obs_id == obs_id) if len(k) > 0: - query = query.filter(table.key.in_(k)) + query = query.filter(table.c.key.in_(k)) rows = query.all() for key, value in rows: if key not in schema: diff --git a/python/lsst/consdb/pqserver.py b/python/lsst/consdb/pqserver.py index b8bd5c4..27c48a3 100644 --- a/python/lsst/consdb/pqserver.py +++ b/python/lsst/consdb/pqserver.py @@ -21,10 +21,13 @@ # The main application factory for consdb.pqserver. -from fastapi import FastAPI +from fastapi import FastAPI, Request, status +from fastapi.responses import JSONResponse from safir.middleware.x_forwarded import XForwardedMiddleware +from sqlalchemy.exc import SQLAlchemyError from .config import config +from .exceptions import BadValueException, UnknownInstrumentException from .handlers.external import external_router from .handlers.internal import internal_router @@ -45,3 +48,20 @@ # Add the middleware app.add_middleware(XForwardedMiddleware) + + +@app.exception_handler(UnknownInstrumentException) +def unknown_instrument_exception_handler(request: Request, exc: UnknownInstrumentException): + return JSONResponse(content=exc.to_dict(), status_code=status.HTTP_404_NOT_FOUND) + + +@app.exception_handler(BadValueException) +def bad_value_exception_handler(request: Request, exc: BadValueException): + exc_str = f"{exc}".replace("\n", " ").replace(" ", " ") + return JSONResponse(content=exc.to_dict(), status_code=status.HTTP_404_NOT_FOUND) + + +@app.exception_handler(SQLAlchemyError) +def sqlalchemy_exception_handler(request: Request, exc: SQLAlchemyError): + content = {"message": str(exc)} + return JSONResponse(content=content, status_code=status.HTTP_500_INTERNAL_SERVER_ERROR)