Skip to content

Commit

Permalink
Added mandatory query tests, with some examples for the structure end…
Browse files Browse the repository at this point in the history
…point
  • Loading branch information
ml-evs committed Mar 4, 2020
1 parent f95f146 commit 04c4be8
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 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 @@ -317,6 +326,12 @@ def main(self):
self._log.debug("Testing single entry request of type %s", endp)
self.test_single_entry_endpoint(endp)

for endp in ENDPOINT_MANDATORY_QUERIES:
# skip empty endpoint query lists
if ENDPOINT_MANDATORY_QUERIES[endp]:
self._log.debug("Testing mandatory query syntax on endpoint %s", endp)
self.test_mandatory_query_syntax(endp, 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 +506,21 @@ 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]
query_test_cases = [
test_case(self.get_endpoint(query)) for query in valid_queries
]
for case in query_test_cases:
case()

0 comments on commit 04c4be8

Please sign in to comment.