Skip to content

Commit

Permalink
update test
Browse files Browse the repository at this point in the history
  • Loading branch information
yang-ruoxi committed Aug 12, 2024
1 parent c2c0796 commit 6dcd244
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
5 changes: 2 additions & 3 deletions src/maggma/api/resource/read_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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,
Expand Down
29 changes: 28 additions & 1 deletion tests/api/test_read_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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")
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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

0 comments on commit 6dcd244

Please sign in to comment.