From ba8e043be64ce504470d9db0683387ff547fbfe3 Mon Sep 17 00:00:00 2001 From: Pauline Ribeyre <4224001+paulineribeyre@users.noreply.github.com> Date: Mon, 4 Nov 2024 16:26:38 -0600 Subject: [PATCH] fix a bunch of tests --- poetry.lock | 13 ++++++++++++- pyproject.toml | 1 + src/mds/agg_mds/query.py | 8 ++++---- src/mds/aliases.py | 15 ++++----------- src/mds/maintain.py | 3 +-- tests/test_aliases.py | 20 +++++++++++++------- 6 files changed, 35 insertions(+), 25 deletions(-) diff --git a/poetry.lock b/poetry.lock index 844b6fb6..0c175164 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1464,6 +1464,17 @@ files = [ [package.extras] cli = ["click (>=5.0)"] +[[package]] +name = "python-multipart" +version = "0.0.17" +description = "A streaming multipart parser for Python" +optional = false +python-versions = ">=3.8" +files = [ + {file = "python_multipart-0.0.17-py3-none-any.whl", hash = "sha256:15dc4f487e0a9476cc1201261188ee0940165cffc94429b6fc565c4d3045cb5d"}, + {file = "python_multipart-0.0.17.tar.gz", hash = "sha256:41330d831cae6e2f22902704ead2826ea038d0419530eadff3ea80175aec5538"}, +] + [[package]] name = "pyyaml" version = "6.0.2" @@ -2063,4 +2074,4 @@ files = [ [metadata] lock-version = "2.0" python-versions = "^3.9" -content-hash = "69b00d1efd06d9bff3a1898cc86ff5926bc065302f9a25871b76048e9c4a040b" +content-hash = "dfd80eb37e76da9910a1682438356c18c18811b0f9c94a94c77b0fecc4170cd8" diff --git a/pyproject.toml b/pyproject.toml index 0103126d..f3420cf4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -31,6 +31,7 @@ bleach = ">=3.3.1" tenacity = ">=8.0.0" pydash = "^5.1.0" pathvalidate = "^3.2.0" +python-multipart = "<1" [tool.poetry.dev-dependencies] pytest = "^5.3" diff --git a/src/mds/agg_mds/query.py b/src/mds/agg_mds/query.py index 4bac360b..6ca0ba5c 100644 --- a/src/mds/agg_mds/query.py +++ b/src/mds/agg_mds/query.py @@ -1,4 +1,4 @@ -from fastapi import HTTPException, Query, APIRouter, Request +from fastapi import HTTPException, Path, Query, APIRouter, Request from starlette.status import HTTP_404_NOT_FOUND from mds import config from mds.agg_mds import datastore @@ -143,9 +143,9 @@ async def get_aggregate_metadata( @mod.get("/aggregate/metadata/{name}") async def get_aggregate_metadata_for_commons( - name: str = Query( - False, description="Return the results without grouping items by commons." - ) + name: str = Path( + description="Return the results without grouping items by commons." + ), ): """et all metadata records from a commons by name diff --git a/src/mds/aliases.py b/src/mds/aliases.py index eec71f3b..15380593 100644 --- a/src/mds/aliases.py +++ b/src/mds/aliases.py @@ -7,30 +7,23 @@ to point to the same blob, aliases allow that without duplicating the actual blob. """ -import json -import re +from typing import Union from asyncpg import UniqueViolationError, ForeignKeyViolationError from fastapi import HTTPException, APIRouter, Depends from pydantic import BaseModel -from sqlalchemy import bindparam -from sqlalchemy.dialects.postgresql import insert from starlette.requests import Request from starlette.responses import JSONResponse from starlette.status import ( - HTTP_200_OK, HTTP_201_CREATED, HTTP_204_NO_CONTENT, - HTTP_400_BAD_REQUEST, HTTP_404_NOT_FOUND, HTTP_409_CONFLICT, ) -import urllib.parse from .admin_login import admin_required -from .models import db, MetadataAlias, Metadata -from . import config, logger -from .objects import FORBIDDEN_IDS +from .models import MetadataAlias +from . import logger mod = APIRouter() @@ -43,7 +36,7 @@ class AliasObjInput(BaseModel): specified """ - aliases: list = None + aliases: list[str] = None @mod.post("/metadata/{guid:path}/aliases") diff --git a/src/mds/maintain.py b/src/mds/maintain.py index 1c7b6f21..9a76fda3 100644 --- a/src/mds/maintain.py +++ b/src/mds/maintain.py @@ -1,5 +1,4 @@ import json -import re from asyncpg import UniqueViolationError from fastapi import HTTPException, APIRouter, Depends @@ -24,7 +23,7 @@ @mod.post("/metadata") async def batch_create_metadata( - request: Request, data_list: list, overwrite: bool = True + request: Request, data_list: list[dict], overwrite: bool = True ): """Create metadata in batch.""" request.scope.get("add_close_watcher", lambda: None)() diff --git a/tests/test_aliases.py b/tests/test_aliases.py index 243b763a..cb3983c5 100644 --- a/tests/test_aliases.py +++ b/tests/test_aliases.py @@ -1,4 +1,4 @@ -import gino +import httpx import pytest import urllib.parse @@ -28,14 +28,20 @@ def test_create_read_delete_new_aliases(guid, aliases, client, is_post): client.post(f"/metadata/{guid}", json=data).raise_for_status() if is_post: - client.post( - f"/metadata/{guid}/aliases", json={"aliases": aliases} - ).raise_for_status() + res = client.post(f"/metadata/{guid}/aliases", json={"aliases": aliases}) + if aliases is None: + with pytest.raises(httpx.HTTPStatusError, match="422 Unprocessable Entity"): + res.raise_for_status() + else: + res.raise_for_status() else: # use PUT instead of POST, should result in same behavior for new aliases - client.put( - f"/metadata/{guid}/aliases", json={"aliases": aliases} - ).raise_for_status() + res = client.put(f"/metadata/{guid}/aliases", json={"aliases": aliases}) + if aliases is None: + with pytest.raises(httpx.HTTPStatusError, match="422 Unprocessable Entity"): + res.raise_for_status() + else: + res.raise_for_status() # convert None to empty list for comparison of output # because we always expect the output to be a list, even if the input is not