Skip to content

Commit

Permalink
Tweak for single structure testing with check_response
Browse files Browse the repository at this point in the history
- If a single ID is passed as a string, validate the request as a single
entry request
- If a single ID is passed within a list, validate it is a multi-entry
request that only returns one result
  • Loading branch information
ml-evs committed May 10, 2022
1 parent 52296b8 commit d53cc06
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
25 changes: 19 additions & 6 deletions tests/server/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,25 +102,38 @@ def inner(

@pytest.fixture
def check_response(get_good_response):
"""Fixture to check "good" response"""
"""Check response matches expectations for a given request.
Parameters:
request: The request to check.
expected_ids: A list of IDs, or a single ID to check
the response for.
page_limit: The number of results expected per page.
expected_return: The number of results expected to be returned.
expected_as_is: Whether to enforce the order of the IDs.
expected_warnings: A list of expected warning messages.
server: The type of server to test, or the actual test client class.
"""
from typing import List
from optimade.server.config import CONFIG
from .utils import OptimadeTestClient

def inner(
request: str,
expected_ids: List[str],
expected_ids: Union[str, List[str]],
page_limit: int = CONFIG.page_limit,
expected_return: int = None,
expected_as_is: bool = False,
expected_warnings: List[Dict[str, str]] = None,
server: Union[str, OptimadeTestClient] = "regular",
):
response = get_good_response(request, server)
if isinstance(response["data"], dict):
response_ids = [response["data"]["id"]]
else:
response_ids = [struct["id"] for struct in response["data"]]
if isinstance(expected_ids, str):
expected_ids = [expected_ids]
response["data"] = [response["data"]]

response_ids = [struct["id"] for struct in response["data"]]

if expected_return is not None:
assert expected_return == response["meta"]["data_returned"]
Expand Down
2 changes: 1 addition & 1 deletion tests/server/routers/test_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def test_check_response_single_structure(check_response):
"""Tests whether check_response also handles single endpoint queries correctly."""

test_id = "mpf_1"
expected_ids = ["mpf_1"]
expected_ids = "mpf_1"
request = f"/structures/{test_id}?response_fields=chemical_formula_reduced"
check_response(request, expected_ids=expected_ids)

Expand Down

0 comments on commit d53cc06

Please sign in to comment.