From 4b747947f94d854c981f23041e6f36b812151f10 Mon Sep 17 00:00:00 2001 From: Mark Date: Thu, 11 Jul 2024 13:19:45 +0100 Subject: [PATCH] Take into account that a shape with 1 expression won't have an "expressions" key --- comparejsonld.py | 16 +++++++++++++--- test_schemas.py | 11 +++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/comparejsonld.py b/comparejsonld.py index f4eed0a..ac49e2c 100644 --- a/comparejsonld.py +++ b/comparejsonld.py @@ -388,10 +388,20 @@ def calculate_necessity(self, prop: str, shape: dict) -> str: :return: necessity """ necessity: str = "absent" - if "expression" in shape and "expressions" in shape["expression"]: + list_of_expressions: list = [] + + if "expression" not in shape: + return necessity + + if "expressions" in shape["expression"]: for expression in shape["expression"]["expressions"]: - if "predicate" in expression and expression["predicate"].endswith(prop): - necessity = self.required_or_absent(expression) + list_of_expressions.append(expression) + else: + list_of_expressions.append(shape["expression"]) + + for expression in list_of_expressions: + if "predicate" in expression and expression["predicate"].endswith(prop): + necessity = self.required_or_absent(expression) return necessity @staticmethod diff --git a/test_schemas.py b/test_schemas.py index 3483497..00cebd3 100644 --- a/test_schemas.py +++ b/test_schemas.py @@ -339,6 +339,17 @@ def test_entityschema_e351(self): follow_redirects=True) self.assertIn(response.json["properties"][0]["P31"]["response"], ["not enough correct statements"]) + def test_entityschema_e438(self): + """ + Tests that a schemas with only 1 expression evaluates correctly + + This test tests entityschema E438 (wikimedia disambiguation page) against entity Q11645745. + P31 should return as present + """ + response = self.app.get('/api/v2?entityschema=E438&entity=Q11645745&language=en', + follow_redirects=True) + self.assertIn(response.json["properties"][0]["P31"]["response"], ["correct", "present"]) + if __name__ == '__main__': unittest.main()