From 76d585900a4540ab443846be0874a1033a9e8672 Mon Sep 17 00:00:00 2001 From: Mark Tully Date: Sat, 29 Jun 2024 00:23:55 +0100 Subject: [PATCH] Fix broken test and clean up comparejsonld.py --- comparejsonld.py | 60 ++++++++++++++++++++++++++++-------------------- test_schemas.py | 2 -- 2 files changed, 35 insertions(+), 27 deletions(-) diff --git a/comparejsonld.py b/comparejsonld.py index 8df3782..f4eed0a 100644 --- a/comparejsonld.py +++ b/comparejsonld.py @@ -192,37 +192,47 @@ def check_claims_for_props(self, claims: dict, prop: str) -> str: :return: """ - cardinality = "correct" + cardinality: str = "correct" allowed: str = "present" - if "expression" in self._start_shape and "expressions" in self._start_shape["expression"]: - allowed_list = [] - for expression in self._start_shape["expression"]["expressions"]: - for statement in claims[prop]: - is_it_allowed = [] - if statement["mainsnak"]["property"] == prop: - is_it_allowed = self._process_triple_constraint(statement["mainsnak"], - expression, - "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_cardinalities(expression, allowed_list, self._start_shape, prop) - if "correct" in allowed_list: - allowed = "correct" + if "expression" not in self._start_shape: + return "present" + if "expressions" not in self._start_shape["expression"]: + return "present" + for expression in self._start_shape["expression"]["expressions"]: + if "predicate" in expression and expression["predicate"].endswith(prop): + allowed_list = self._get_allowed_list(claims, prop, expression) + cardinality2 = self._process_cardinalities(expression, allowed_list, self._start_shape, prop) + if cardinality2 not in ["", "correct"]: + cardinality = cardinality2 + if "correct" in allowed_list: + allowed = "correct" if cardinality == "correct": - response = allowed + response: str = allowed else: - response = cardinality + response: str = cardinality return response - def _process_cardinalities(self, expression, allowed_list, shape, prop): + def _get_allowed_list(self, claims: dict, prop: str, expression: dict) -> list: + allowed_list: list = [] + for statement in claims[prop]: + is_it_allowed: str = "" + if statement["mainsnak"]["property"] == prop: + is_it_allowed = self._process_triple_constraint(statement["mainsnak"], + expression, + "") + if "extra" in self._start_shape: + for extra in self._start_shape["extra"]: + if extra.endswith(prop) and is_it_allowed == "incorrect": + is_it_allowed = "allowed" + allowed_list.append(is_it_allowed) + return allowed_list + + def _process_cardinalities(self, expression: dict, allowed_list: list, shape: dict, prop: str) -> str: if "predicate" not in expression: - return "correct" + return "" if not expression["predicate"].endswith(prop): - return "correct" - occurrences: dict = allowed_list.count("correct") + return "" + occurrences: int = allowed_list.count("correct") occurrences += allowed_list.count("present") cardinality: str = "correct" for expression in shape["expression"]["expressions"]: @@ -234,7 +244,7 @@ def _process_cardinalities(self, expression, allowed_list, shape, prop): return cardinality @staticmethod - def _get_cardinalities(occurrences, expression) -> str: + def _get_cardinalities(occurrences: int, expression: dict) -> str: cardinality: str = "correct" min_cardinality: bool = True max_cardinality: bool = True diff --git a/test_schemas.py b/test_schemas.py index 224c52a..3483497 100644 --- a/test_schemas.py +++ b/test_schemas.py @@ -2,7 +2,6 @@ Tests to test wikidata entityschemas against wikidata items """ import unittest -from unittest import skip import requests @@ -329,7 +328,6 @@ 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