Skip to content

Commit

Permalink
improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lambdamusic committed Sep 27, 2021
1 parent 0ba75a1 commit 4f4b0cd
Show file tree
Hide file tree
Showing 13 changed files with 271 additions and 62 deletions.
8 changes: 3 additions & 5 deletions ontospy/core/rdf_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@


class RDFLoader(object):
"""
Utility to Load any RDF source into an RDFLIB graph instance.
"""Utility to Load any RDF source into an RDFLIB graph instance.
Accepts: [single item or list]
:: uri_or_path = a uri or local path
Expand Down Expand Up @@ -112,12 +111,11 @@ def load(self, uri_or_path=None, data=None, file_obj=None, rdf_format=""):
return self.rdflib_graph

def load_uri(self, uri):
"""
Load a single resource into the graph for this object.
"""Load a single resource into the graph for this object.
Approach: try loading into a temporary graph first, if that succeeds merge it into the main graph. This allows to deal with the JSONLD loading issues which can solved only by using a ConjunctiveGraph (https://github.com/RDFLib/rdflib/issues/436). Also it deals with the RDFA error message which seems to stick into a graph even if the parse operation fails.
NOTE the final merge operation can be improved as graph-set operations involving blank nodes could case collisions (https://rdflib.readthedocs.io/en/stable/merging.html)
TODO the final merge operation can be improved as graph-set operations involving blank nodes could case collisions (https://rdflib.readthedocs.io/en/stable/merging.html)
:param uri: single RDF source location
:return: None (sets self.rdflib_graph and self.sources_valid)
Expand Down
4 changes: 2 additions & 2 deletions ontospy/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -877,13 +877,13 @@ def slugify(value):


def get_files_with_extensions(folder, extensions):
"""walk dir and return .* files as a list
"""walk dir and return .* files as a list. Hidden files are ignored.
Note: directories are walked recursively"""
out = []
for root, dirs, files in os.walk(folder):
for file in files:
filename, file_extension = os.path.splitext(file)
if file_extension.replace(".", "") in extensions:
if not filename.startswith('.') and file_extension.replace(".", "") in extensions:
out += [os.path.join(root, file)]
# break

Expand Down
11 changes: 11 additions & 0 deletions ontospy/tests/context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# !/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
Unit tests shared constants
"""

import os

dir_path = os.path.dirname(os.path.realpath(__file__))
TEST_RDF_FOLDER = dir_path + "/rdf/"
TEST_SHAPES_FOLDER = dir_path + "/shapes/"
33 changes: 8 additions & 25 deletions ontospy/tests/test_quick.py → ontospy/tests/quick.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
Running it:
./run-quick-test.sh
.tools/run-quick-test.sh
TIP
Expand All @@ -30,23 +30,14 @@
from ..core import *
from ..core.utils import *

# sanity check
print("-------------------\nOntospy ", VERSION, "\n-------------------")

from .context import TEST_RDF_FOLDER, TEST_SHAPES_FOLDER

class MyRDFEntity(ontospy.RdfEntity):
def __init__(self, uri, rdftype=None, namespaces=None, ext_model=False):
super().__init__(uri, rdftype, namespaces, ext_model)

def __repr__(self):
return "<MyRDFEntity *%s*>" % (self.uri)
# sanity check
print("-------------------\nOntospy ", VERSION, "\n-------------------")


def disjointWith(self):
"""
Example: pull out disjoint with statements
"""
pred = "http://www.w3.org/2002/07/owl#disjointWith"
return self.getValuesForProperty(pred)


class TestQuick(unittest.TestCase):
Expand All @@ -58,17 +49,9 @@ def test_quick1(self):
"""
print("=================\n*** QUICK TEST 1 ***\n=================\n")

dir_path = os.path.dirname(os.path.realpath(__file__))
DATA_FOLDER = dir_path + "/rdf/"
f = DATA_FOLDER + "multilingual.ttl"
o = Ontospy(f, verbose=True, pref_title="label", pref_lang="it")

for c in o.all_classes:
print("URI: ", c.uri)
print("RDFTYPE: ", c.rdftype)
print("BEST LABEL: ", c.bestLabel())
print("TITLE: ", c.title)
print("===")
f = TEST_RDF_FOLDER + "periodical.jsonld"

o = Ontospy(f, verbose=True, rdf_format="json-ld")



Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
[{
"@context": "http://schema.org/",
"@graph": [
{
Expand Down Expand Up @@ -36,4 +36,4 @@
"author": "Smiraglia, Richard P."
}
]
}
}]
46 changes: 46 additions & 0 deletions ontospy/tests/rdf/.recipe.jsonld
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"@context": {
"name": "http://rdf.data-vocabulary.org/#name",
"ingredient": "http://rdf.data-vocabulary.org/#ingredients",
"yield": "http://rdf.data-vocabulary.org/#yield",
"instructions": "http://rdf.data-vocabulary.org/#instructions",
"step": {
"@id": "http://rdf.data-vocabulary.org/#step",
"@type": "xsd:integer"
},
"description": "http://rdf.data-vocabulary.org/#description",
"xsd": "http://www.w3.org/2001/XMLSchema#"
},
"name": "Mojito",
"ingredient": [
"12 fresh mint leaves",
"1/2 lime, juiced with pulp",
"1 tablespoons white sugar",
"1 cup ice cubes",
"2 fluid ounces white rum",
"1/2 cup club soda"
],
"yield": "1 cocktail",
"instructions": [
{
"step": 1,
"description": "Crush lime juice, mint and sugar together in glass."
},
{
"step": 2,
"description": "Fill glass to top with ice cubes."
},
{
"step": 3,
"description": "Pour white rum over ice."
},
{
"step": 4,
"description": "Fill the rest of glass with club soda, stir."
},
{
"step": 5,
"description": "Garnish with a lime wedge."
}
]
}
171 changes: 171 additions & 0 deletions ontospy/tests/rdf/paper.jsonld
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
[
{
"@context": "https://springernature.github.io/scigraph/jsonld/sgcontext.json",
"about": [
{
"id": "http://purl.org/au-research/vocabulary/anzsrc-for/2008/1199",
"inDefinedTermSet": "http://purl.org/au-research/vocabulary/anzsrc-for/2008/",
"name": "Other Medical and Health Sciences",
"type": "DefinedTerm"
},
{
"id": "http://purl.org/au-research/vocabulary/anzsrc-for/2008/11",
"inDefinedTermSet": "http://purl.org/au-research/vocabulary/anzsrc-for/2008/",
"name": "Medical and Health Sciences",
"type": "DefinedTerm"
}
],
"author": [
{
"affiliation": {
"name": [
"MBA Datamonitor plc, Charles House, 108\u2013110 Finchley Road, NW3 5JJ, London, UK"
],
"type": "Organization"
},
"familyName": "Belsey",
"givenName": "Mark",
"id": "sg:person.07674367221.06",
"sameAs": [
"https://app.dimensions.ai/discover/publication?and_facet_researcher=ur.07674367221.06"
],
"type": "Person"
},
{
"affiliation": {
"name": [
"MBA Datamonitor plc, Charles House, 108\u2013110 Finchley Road, NW3 5JJ, London, UK"
],
"type": "Organization"
},
"familyName": "Evans",
"givenName": "David",
"id": "sg:person.012622670623.47",
"sameAs": [
"https://app.dimensions.ai/discover/publication?and_facet_researcher=ur.012622670623.47"
],
"type": "Person"
},
{
"affiliation": {
"name": [
"MBA Datamonitor plc, Charles House, 108\u2013110 Finchley Road, NW3 5JJ, London, UK"
],
"type": "Organization"
},
"familyName": "Pavlou",
"givenName": "Alex",
"id": "sg:person.010471747621.06",
"sameAs": [
"https://app.dimensions.ai/discover/publication?and_facet_researcher=ur.010471747621.06"
],
"type": "Person"
},
{
"affiliation": {
"name": [
"MBA Datamonitor plc, Charles House, 108\u2013110 Finchley Road, NW3 5JJ, London, UK"
],
"type": "Organization"
},
"familyName": "Savopoulos",
"givenName": "John",
"id": "sg:person.01175104551.77",
"sameAs": [
"https://app.dimensions.ai/discover/publication?and_facet_researcher=ur.01175104551.77"
],
"type": "Person"
}
],
"citation": [
{
"id": "https://doi.org/10.1016/s0140-6736(04)16934-1",
"sameAs": [
"https://app.dimensions.ai/details/publication/pub.1011166483"
],
"type": "CreativeWork"
},
{
"id": "https://doi.org/10.1016/j.virusres.2004.02.006",
"sameAs": [
"https://app.dimensions.ai/details/publication/pub.1015699065"
],
"type": "CreativeWork"
},
{
"id": "https://doi.org/10.1016/j.jviromet.2004.03.011",
"sameAs": [
"https://app.dimensions.ai/details/publication/pub.1017215790"
],
"type": "CreativeWork"
}
],
"datePublished": "2006-01",
"datePublishedReg": "2006-01-01",
"description": "Following flu vaccine manufacturing and supply issues in the USA and the global threat of an avian flu pandemic, the flu market is attracting considerable interest. This market was worth approximately US$1.1bn in 2004, and it is expected to grow to US$3.1bn by 2010: a compound annual growth rate of 17.1 per cent. To capitalise on this explosive growth rate, flu vaccine manufacturers should position their products towards combating the avian flu pandemic threat. However, there are a number of challenges associated with producing avian flu vaccines using egg-based manufacture systems: currently the dominant vaccine production technique. In the current paper, Datamonitor's infectious disease analysis team argues the case for the potential transformation of the flu market by governmental initiatives, and specifically on the role of these initiatives in driving cell culture flu manufacture as part of pandemic preparedness plans.",
"genre": "research_article",
"id": "sg:pub.10.1057/palgrave.jcb.3040160",
"inLanguage": [
"en"
],
"isAccessibleForFree": false,
"isPartOf": [
{
"id": "sg:journal.1020999",
"issn": [
"1462-8732",
"1478-565X"
],
"name": "Journal of Commercial Biotechnology",
"type": "Periodical"
},
{
"issueNumber": "2",
"type": "PublicationIssue"
},
{
"type": "PublicationVolume",
"volumeNumber": "12"
}
],
"name": "Growth drivers and resistors of the influenza market: The importance of cell culture flu",
"pagination": "150-155",
"productId": [
{
"name": "readcube_id",
"type": "PropertyValue",
"value": [
"a005dd24aba8e6af14ce8998a6c8c57b1c155ebbdae3208e274ee78870fa4ccc"
]
},
{
"name": "doi",
"type": "PropertyValue",
"value": [
"10.1057/palgrave.jcb.3040160"
]
},
{
"name": "dimensions_id",
"type": "PropertyValue",
"value": [
"pub.1033592356"
]
}
],
"sameAs": [
"https://doi.org/10.1057/palgrave.jcb.3040160",
"https://app.dimensions.ai/details/publication/pub.1033592356"
],
"sdDataset": "articles",
"sdDatePublished": "2019-04-11T00:13",
"sdLicense": "https://scigraph.springernature.com/explorer/license/",
"sdPublisher": {
"name": "Springer Nature - SN SciGraph project",
"type": "Organization"
},
"sdSource": "s3://com-uberresearch-data-dimensions-target-20181106-alternative/cleanup/v134/2549eaecd7973599484d7c17b260dba0a4ecb94b/merge/v9/a6c9fde33151104705d4d7ff012ea9563521a3ce/jats-lookup/v90/0000000001_0000000264/records_8695_00000500.jsonl",
"type": "ScholarlyArticle",
"url": "http://link.springer.com/10.1057/palgrave.jcb.3040160"
}
]
11 changes: 5 additions & 6 deletions ontospy/tests/test_load_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Run like this:
:path/to/ontospyProject>python -m ontospy.tests.test_load_local
$ python -m ontospy.tests.test_load_local
"""

Expand All @@ -16,6 +16,7 @@
from .. core import *
from .. core.utils import *

from .context import TEST_RDF_FOLDER, TEST_SHAPES_FOLDER


# sanity check
Expand All @@ -24,20 +25,18 @@

class TestLoadOntologies(unittest.TestCase):

dir_path = os.path.dirname(os.path.realpath(__file__))
DATA_FOLDER = dir_path + "/rdf/"

def test1_load_locally(self):
"""
Check if the ontologies in /RDF folder load ok
"""
print("=================\nTEST 1: Loading ontologies from <%s> folder and printing detailed entities descriptions.\n=================" % self.DATA_FOLDER)
print("=================\nTEST 1: Loading ontologies from <%s> folder and printing detailed entities descriptions.\n=================" % TEST_RDF_FOLDER)

for f in os.listdir(self.DATA_FOLDER):
for f in os.listdir(TEST_RDF_FOLDER):
if not f.startswith('.'):
printDebug("\n*****\nTest: loading local file... > %s\n*****" % str(f), "important")

o = Ontospy(self.DATA_FOLDER + f, verbose=True)
o = Ontospy(TEST_RDF_FOLDER + f, verbose=True)

print("CLASS TREE")
o.printClassTree()
Expand Down
Loading

0 comments on commit 4f4b0cd

Please sign in to comment.