From 31ef137b2cd5ca84b0dce3636bced8450d42d3de Mon Sep 17 00:00:00 2001 From: Mark Tully Date: Wed, 22 Oct 2025 00:42:18 +0100 Subject: [PATCH 1/2] Fix error in JSONLDShape when entityschema doesn't exist --- entityshape/api_v2/getjsonld.py | 6 +++++- tests/test_schemas.py | 11 +++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/entityshape/api_v2/getjsonld.py b/entityshape/api_v2/getjsonld.py index 6a7da2f..b285dcd 100644 --- a/entityshape/api_v2/getjsonld.py +++ b/entityshape/api_v2/getjsonld.py @@ -17,6 +17,7 @@ class JSONLDShape: """ def __init__(self, schema: str, language: str) -> None: self._language: str = language + self._json_text: dict = {} self._get_schema_json(schema) def get_json_ld(self) -> dict: @@ -37,7 +38,8 @@ def _get_schema_json(self, schema) -> None: url: str = f"https://www.wikidata.org/wiki/EntitySchema:{schema}?action=raw" response = requests.get(url=url, headers={'User-Agent': 'Userscript Entityshape by User:Teester'}) - self._json_text: dict = response.json() + if response.status_code == 200: + self._json_text = response.json() def get_name(self) -> str: """ @@ -45,6 +47,8 @@ def get_name(self) -> str: :return: the name of the schema """ + if "labels" not in self._json_text: + return "" if self._language in self._json_text["labels"]: return self._json_text["labels"][self._language] return "" \ No newline at end of file diff --git a/tests/test_schemas.py b/tests/test_schemas.py index 83475d6..2458136 100644 --- a/tests/test_schemas.py +++ b/tests/test_schemas.py @@ -354,6 +354,17 @@ def test_entityschema_e438(self): follow_redirects=True) self.assertIn(response.json["properties"][0]["P31"]["response"], ["correct", "present"]) + def test_non_existent_entityschema(self): + """ + Tests that we get a correct response if the entityschema doesn't exist + + This test tests entityschema E99900, (which does not exist) against entity Q11645745. + This should give a 200 response + """ + response = self.app.get('/api/v2?entityschema=E99900&entity=Q11645745&language=en', + follow_redirects=True) + self.assertEqual(200, response.status_code) + if __name__ == '__main__': unittest.main() From c4bc9144eec7ca75fc2ed45704f9948baa707e6b Mon Sep 17 00:00:00 2001 From: Mark Tully Date: Wed, 22 Oct 2025 00:54:36 +0100 Subject: [PATCH 2/2] Fix error in JSONLDShape when entity doesn't exist --- entityshape/api_v2/comparejsonld.py | 28 +++++++++++++++++----------- tests/test_schemas.py | 11 +++++++++++ 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/entityshape/api_v2/comparejsonld.py b/entityshape/api_v2/comparejsonld.py index 98650ef..23b0d0a 100644 --- a/entityshape/api_v2/comparejsonld.py +++ b/entityshape/api_v2/comparejsonld.py @@ -26,11 +26,12 @@ def __init__(self, shape: dict, entity: str, language: str) -> None: """ self._entity: str = entity self._shape: dict = shape - + self._entities: dict = {} + self._props: list = [] self._property_responses: dict = {} self._get_entity_json() - if self._entities["entities"][self._entity]: + if "entities" in self._entities and self._entities["entities"][self._entity]: self._get_props(self._entities["entities"][self._entity]['claims']) self._get_property_names(language) self.start_shape: dict = self._get_start_shape() @@ -58,17 +59,21 @@ def get_general(self) -> dict: :return: json for general properties of the comparison """ + if "shapes" not in self._shape: + return {} + if "entities" not in self._entities: + return {} + general: dict = {} properties: list = ["lexicalCategory", "language"] for item in properties: - if "shapes" in self._shape: - data_string: str = json.dumps(self._shape["shapes"]) - if item in data_string and item in self._entities["entities"][self._entity]: - general[item] = "incorrect" - expected: list = self._shape["shapes"] - actual: str = self._entities["entities"][self._entity][item] - if actual in expected: - general[item] = "correct" + data_string: str = json.dumps(self._shape["shapes"]) + if item in data_string and item in self._entities["entities"][self._entity]: + general[item] = "incorrect" + expected: list = self._shape["shapes"] + actual: str = self._entities["entities"][self._entity][item] + if actual in expected: + general[item] = "correct" return general def _get_entity_json(self) -> None: @@ -78,7 +83,8 @@ def _get_entity_json(self) -> None: url: str = f"https://www.wikidata.org/wiki/Special:EntityData/{self._entity}.json" response: Response = requests.get(url=url, headers={'User-Agent': 'Userscript Entityshape by User:Teester'}) - self._entities = response.json() + if response.status_code == 200: + self._entities = response.json() def _get_props(self, claims: dict) -> None: """ diff --git a/tests/test_schemas.py b/tests/test_schemas.py index 2458136..89ab92b 100644 --- a/tests/test_schemas.py +++ b/tests/test_schemas.py @@ -365,6 +365,17 @@ def test_non_existent_entityschema(self): follow_redirects=True) self.assertEqual(200, response.status_code) + def test_non_existent_entity(self): + """ + Tests that we get a correct response if the entityschema doesn't exist + + This test tests entityschema E10 (human) against entity Q6, which has been deleted. + This should give a 200 response + """ + response = self.app.get('/api/v2?entityschema=E10&entity=Q6&language=en', + follow_redirects=True) + self.assertEqual(200, response.status_code) + if __name__ == '__main__': unittest.main()