Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test mandatory queries in validator #205

Merged
merged 3 commits into from
Mar 6, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)