From d53c343f45c5b919cc73fb34665a72a83c8feb79 Mon Sep 17 00:00:00 2001 From: Matthew Evans Date: Wed, 4 Mar 2020 17:44:04 +0000 Subject: [PATCH 1/2] Added mandatory query tests, with some examples for the structure endpoint --- optimade/validator/validator.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/optimade/validator/validator.py b/optimade/validator/validator.py index 055458a14..2ae9e8de5 100644 --- a/optimade/validator/validator.py +++ b/optimade/validator/validator.py @@ -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, @@ -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) @@ -491,3 +506,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) From 4c30c053dcd820fa4a570a2a790ff63e7b44526e Mon Sep 17 00:00:00 2001 From: Matthew Evans Date: Thu, 5 Mar 2020 17:01:44 +0000 Subject: [PATCH 2/2] Handle index-db case of mandatory queries --- optimade/validator/validator.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/optimade/validator/validator.py b/optimade/validator/validator.py index 2ae9e8de5..01b2b5ffd 100644 --- a/optimade/validator/validator.py +++ b/optimade/validator/validator.py @@ -257,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 ) @@ -326,11 +330,13 @@ 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: + for endp in self.endpoint_mandatory_queries: # skip empty endpoint query lists - if ENDPOINT_MANDATORY_QUERIES[endp]: + if self.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.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)