Skip to content

Commit

Permalink
Merge pull request #205 from Materials-Consortia/ml-evs/validator_upd…
Browse files Browse the repository at this point in the history
…ates

Test mandatory queries in validator
  • Loading branch information
ml-evs committed Mar 6, 2020
2 parents 3dff907 + dec4e2e commit 9a0c1fb
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions optimade/validator/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@
LINKS_ENDPOINT = "links"
REQUIRED_ENTRY_ENDPOINTS = ["references", "structures"]

ENDPOINT_MANDATORY_QUERIES = {
"structures": [
'elements HAS "Na"',
'elements HAS ANY "Na","Cl"',
'elements HAS ALL "Na","Cl"',
],
"references": [],
}

RESPONSE_CLASSES = {
"references": ValidatorReferenceResponseMany,
"references/": ValidatorReferenceResponseOne,
Expand Down Expand Up @@ -248,6 +257,10 @@ def __init__( # pylint: disable=too-many-arguments
REQUIRED_ENTRY_ENDPOINTS_INDEX if self.index else REQUIRED_ENTRY_ENDPOINTS
)
self.test_entry_endpoints = set(self.expected_entry_endpoints)
self.endpoint_mandatory_queries = (
{} if self.index else ENDPOINT_MANDATORY_QUERIES
)

self.response_classes = (
RESPONSE_CLASSES_INDEX if self.index else RESPONSE_CLASSES
)
Expand Down Expand Up @@ -317,6 +330,14 @@ def main(self):
self._log.debug("Testing single entry request of type %s", endp)
self.test_single_entry_endpoint(endp)

for endp in self.endpoint_mandatory_queries:
# skip empty endpoint query lists
if self.endpoint_mandatory_queries[endp]:
self._log.debug("Testing mandatory query syntax on endpoint %s", endp)
self.test_mandatory_query_syntax(
endp, self.endpoint_mandatory_queries[endp]
)

self._log.debug("Testing %s endpoint", LINKS_ENDPOINT)
self.test_info_or_links_endpoints(LINKS_ENDPOINT)

Expand Down Expand Up @@ -491,3 +512,18 @@ def get_endpoint(self, request_str):
f"Request to '{request_str}' returned HTTP code: {response.status_code}"
)
return response, "request successful."

def test_mandatory_query_syntax(self, endpoint, endpoint_queries):
""" Perform a list of valid queries and assert that no errors are raised.
Parameters:
endpoint (str): the endpoint to query (e.g. "structures").
endpoint_queries (list): the list of valid mandatory queries
for that endpoint, where the queries do not include the
"?filter=" prefix, e.g. ['elements HAS "Na"'].
"""

valid_queries = [f"{endpoint}?filter={query}" for query in endpoint_queries]
for query in valid_queries:
self.get_endpoint(query)

0 comments on commit 9a0c1fb

Please sign in to comment.