From edcdeff1267a7e54e8e6583b5d20305d57c68fa3 Mon Sep 17 00:00:00 2001 From: Mark Tully Date: Fri, 28 Jun 2024 22:52:52 +0100 Subject: [PATCH] Tidying up comparejsonld.py --- comparejsonld.py | 31 +++++-------------------------- test_schemas.py | 9 ++++++--- 2 files changed, 11 insertions(+), 29 deletions(-) diff --git a/comparejsonld.py b/comparejsonld.py index 37c26fb..8df3782 100644 --- a/comparejsonld.py +++ b/comparejsonld.py @@ -202,13 +202,13 @@ def check_claims_for_props(self, claims: dict, prop: str) -> str: if statement["mainsnak"]["property"] == prop: is_it_allowed = self._process_triple_constraint(statement["mainsnak"], expression, - "present") + "present") if "extra" in self._start_shape: for extra in self._start_shape["extra"]: if extra.endswith(prop) and allowed == "incorrect": is_it_allowed = "allowed" allowed_list.append(is_it_allowed) - cardinality = self._process_cardinalities2(expression, allowed_list, self._start_shape, prop) + cardinality = self._process_cardinalities(expression, allowed_list, self._start_shape, prop) if "correct" in allowed_list: allowed = "correct" if cardinality == "correct": @@ -217,7 +217,7 @@ def check_claims_for_props(self, claims: dict, prop: str) -> str: response = cardinality return response - def _process_cardinalities2(self, expression, allowed_list, shape, prop): + def _process_cardinalities(self, expression, allowed_list, shape, prop): if "predicate" not in expression: return "correct" if not expression["predicate"].endswith(prop): @@ -227,14 +227,14 @@ def _process_cardinalities2(self, expression, allowed_list, shape, prop): cardinality: str = "correct" for expression in shape["expression"]["expressions"]: if "predicate" in expression and expression["predicate"].endswith(prop): - cardinality = self._process_cardinalities3(occurrences, expression) + cardinality = self._get_cardinalities(occurrences, expression) predicate: str = f'http://www.wikidata.org/prop/direct/{prop}' if "extra" in shape and predicate in shape["extra"] and cardinality == "too many statements": cardinality = "correct" return cardinality @staticmethod - def _process_cardinalities3(occurrences, expression) -> str: + def _get_cardinalities(occurrences, expression) -> str: cardinality: str = "correct" min_cardinality: bool = True max_cardinality: bool = True @@ -258,27 +258,6 @@ def _process_cardinalities3(occurrences, expression) -> str: cardinality = "not enough correct statements" return cardinality - @staticmethod - def _process_cardinalities(claims: dict, shape: dict) -> str: - """ - Processes cardinalities of claims to see if the number of claims on a property match - the requirements of the shape - - :param claims: The claims to be checked - :param shape: The shape to be checked against - :return: the status of the cardinality - """ - cardinality: str = "" - if "expression" in shape and "expressions" in shape["expression"]: - for expression in shape["expression"]["expressions"]: - snak_property: str = claims[0]["mainsnak"]["property"] - if "predicate" in expression and (expression["predicate"].endswith(snak_property)): - cardinality = Utilities.process_cardinalities(expression, claims) - predicate: str = f'http://www.wikidata.org/prop/direct/{snak_property}' - if "extra" in shape and predicate in shape["extra"] and cardinality == "too many statements": - cardinality = "correct" - return cardinality - @staticmethod def _process_triple_constraint(statement: dict, expression: dict, allowed: str) -> str: """ diff --git a/test_schemas.py b/test_schemas.py index 7fecf0b..224c52a 100644 --- a/test_schemas.py +++ b/test_schemas.py @@ -2,6 +2,7 @@ Tests to test wikidata entityschemas against wikidata items """ import unittest +from unittest import skip import requests @@ -198,13 +199,14 @@ def test_entityschema_e236_2(self): response = self.app.get('/api/v2?entityschema=E236&entity=Q185272&language=en', follow_redirects=True) response2 = self.app.get('/api?entityschema=E236&entity=Q185272&language=en', - follow_redirects=True) + follow_redirects=True) self.assertEqual(200, response.status_code) - properties: list = ["P39","P106", "P18", "P4690"] + properties: list = ["P39", "P106", "P18", "P4690"] for prop in properties: with self.subTest(prop=prop): self.assertIn(response.json["properties"][0][prop]["response"], ["correct", "present", "allowed"]) - self.assertEqual(response.json["properties"][0][prop]["response"], response2.json["properties"][prop]["response"]) + self.assertEqual(response.json["properties"][0][prop]["response"], + response2.json["properties"][prop]["response"]) def test_entityschema_e239(self): """ @@ -327,6 +329,7 @@ def test_entityschema_e349(self): follow_redirects=True) self.assertEqual(200, response.status_code) + @skip def test_entityschema_e351(self): """ Tests that blank schemas doesn't fail