Skip to content

Commit

Permalink
Fix some unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bbrondel committed Jan 8, 2025
1 parent 370c587 commit b3b1e75
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
9 changes: 4 additions & 5 deletions python/lsst/consdb/handlers/external.py
Original file line number Diff line number Diff line change
Expand Up @@ -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] = [
Expand Down Expand Up @@ -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:
Expand Down
22 changes: 21 additions & 1 deletion python/lsst/consdb/pqserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)

0 comments on commit b3b1e75

Please sign in to comment.