Skip to content

Commit

Permalink
Tidying up comparejsonld.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Teester committed Jun 28, 2024
1 parent 68bfb64 commit edcdeff
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 29 deletions.
31 changes: 5 additions & 26 deletions comparejsonld.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand All @@ -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):
Expand All @@ -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
Expand All @@ -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:
"""
Expand Down
9 changes: 6 additions & 3 deletions test_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Tests to test wikidata entityschemas against wikidata items
"""
import unittest
from unittest import skip

import requests

Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit edcdeff

Please sign in to comment.