Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions entityshape/api_v2/comparejsonld.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand All @@ -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:
"""
Expand Down
6 changes: 5 additions & 1 deletion entityshape/api_v2/getjsonld.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -37,14 +38,17 @@ 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:
"""
Gets the name of the schema

: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 ""
22 changes: 22 additions & 0 deletions tests/test_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,28 @@ 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)

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()