From 6dcd2447f1b889cf41afd07725664a859ef9527e Mon Sep 17 00:00:00 2001 From: yang-ruoxi Date: Mon, 12 Aug 2024 13:58:45 -0700 Subject: [PATCH] update test --- src/maggma/api/resource/read_resource.py | 5 ++-- tests/api/test_read_resource.py | 29 +++++++++++++++++++++++- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/maggma/api/resource/read_resource.py b/src/maggma/api/resource/read_resource.py index ec440a474..60d97f4a5 100644 --- a/src/maggma/api/resource/read_resource.py +++ b/src/maggma/api/resource/read_resource.py @@ -201,8 +201,8 @@ def search(**queries: dict[str, STORE_PARAMS]) -> Union[dict, Response]: if self.query_to_configure_on_request is not None: # give the key name "request", arbitrary choice, as only the value gets merged into the query - queries["request"] = self.header_processor.configure_query_from_request( - request, self.query_to_configure_on_request + queries["groups"] = self.header_processor.configure_query_on_request( + request=request, query_operator=self.query_to_configure_on_request ) # allowed query parameters query_params = [ @@ -217,7 +217,6 @@ def search(**queries: dict[str, STORE_PARAMS]) -> Union[dict, Response]: detail="'limit' and 'skip' parameters have been renamed. " "Please update your API client to the newest version.", ) - else: raise HTTPException( status_code=400, diff --git a/tests/api/test_read_resource.py b/tests/api/test_read_resource.py index 79ca5cf64..0adc2dec8 100644 --- a/tests/api/test_read_resource.py +++ b/tests/api/test_read_resource.py @@ -11,7 +11,7 @@ from maggma.api.query_operator import NumericQuery, SparseFieldsQuery, StringQueryOperator from maggma.api.resource import ReadOnlyResource -from maggma.api.resource.core import HintScheme +from maggma.api.resource.core import HeaderProcessor, HintScheme from maggma.stores import AliasingStore, MemoryStore @@ -32,6 +32,18 @@ class Owner(BaseModel): total_owners = len(owners) +# Create a subclass of the header processor to prevent TypeErrors: +# Can't instantiate abstract class HeaderProcessor with abstract methods +class TestHeaderProcessor(HeaderProcessor): + def configure_query_on_request(self, request, query_operator): + # Implement the method + return {"name": "PersonAge9"} + + def process_header(self, response, request): + # Implement the method + pass + + @pytest.fixture() def owner_store(): store = MemoryStore("owners", key="name") @@ -134,6 +146,8 @@ def search_helper(payload, base: str = "/?", debug=True) -> Response: NumericQuery(model=Owner), SparseFieldsQuery(model=Owner), ], + header_processor=TestHeaderProcessor(), + query_to_configure_on_request=StringQueryOperator(model=Owner), disable_validation=True, ) app = FastAPI() @@ -214,3 +228,16 @@ def test_resource_compound(): assert len(data) == 1 assert data[0]["name"] == "PersonAge20Weight200" assert "weight" not in data[0] + + +def test_configure_query_on_request(): + payload = { + "name": "PersonAge20Weight200", + "_all_fields": False, + "_fields": "name,age", + "weight_min": 199.3, + "weight_max": 201.9, + "age": 20, + } + res, data = search_helper(payload=payload, base="/?", debug=True) + assert res.status_code == 200