diff --git a/skosprovider_rdf/utils.py b/skosprovider_rdf/utils.py index de62b52..b874f88 100644 --- a/skosprovider_rdf/utils.py +++ b/skosprovider_rdf/utils.py @@ -35,7 +35,7 @@ def rdf_dumper(provider): for l in c.labels: predicate = URIRef(SKOS + l.type) lang = l.language - if lang: + if isinstance(lang, bytes): lang = lang.decode("UTF-8") graph.add((subject, predicate, Literal(l.label, lang=lang))) if isinstance(c, Concept): @@ -55,7 +55,7 @@ def rdf_dumper(provider): for n in c.notes: predicate = URIRef(SKOS + "#" + n.type) lang = n.language - if lang: + if isinstance(lang, bytes): lang = lang.decode("UTF-8") graph.add((subject, predicate, Literal(n.note, lang=lang))) elif isinstance(c, Collection): diff --git a/tests/test_utils.py b/tests/test_utils.py index 7dad39f..9211271 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -2,6 +2,7 @@ import os from rdflib import Graph from rdflib.namespace import RDF, SKOS +from skosprovider.providers import DictionaryProvider from skosprovider_rdf.providers import RDFProvider from skosprovider_rdf import utils from rdflib.term import URIRef @@ -17,33 +18,112 @@ def setUp(self): # Set up testdata self._create_test_data() - # Set up provider - self.provider = RDFProvider( - {'id': 'PRODUCTS', 'conceptscheme_id': 1}, self.graph) - def tearDown(self): - return + del self.larch_dump + del self.chestnut_dump + del self.world_dump def _create_test_data(self): self.graph = Graph() - filepath=os.path.dirname(os.path.realpath(__file__)) - abspath=os.path.abspath(filepath + "/data/simple_turtle_products") + filepath = os.path.dirname(os.path.realpath(__file__)) + abspath = os.path.abspath(filepath + "/data/simple_turtle_products") self.graph.parse(abspath, format="turtle") - self.u_products=URIRef("http://www.products.com/") - self.u_jewellery=URIRef("http://www.products.com/Jewellery") - self.u_perfume=URIRef("http://www.products.com/Perfume") - self.u_product=URIRef("http://www.products.com/Product") - self.u_stuff=URIRef("http://www.products.com/Stuff") + self.u_products = URIRef("http://www.products.com/") + self.u_jewellery = URIRef("http://www.products.com/Jewellery") + self.u_perfume = URIRef("http://www.products.com/Perfume") + self.u_product = URIRef("http://www.products.com/Product") + self.u_stuff = URIRef("http://www.products.com/Stuff") + + self.larch_dump = { + 'id': '1', + 'uri': 'http://id.trees.org/1', + 'type': 'concept', + 'labels': [ + {'type': 'prefLabel', 'language': 'en', 'label': 'The Larch'}, + {'type': 'prefLabel', 'language': 'nl', 'label': 'De Lariks'} + ], + 'notes': [ + {'type': 'definition', + 'language': 'en', + 'note': 'A type of tree.'} + ], + 'narrower': [], + 'broader': [], + 'related': [], + 'member_of': ['3'] + } + self.chestnut_dump = { + 'id': '2', + 'uri': 'http://id.trees.org/2', + 'type': 'concept', + 'labels': [ + {'type': 'prefLabel', + 'language': 'en', + 'label': 'The Chestnut'}, + {'type': 'altLabel', + 'language': 'nl', + 'label': 'De Paardekastanje'}, + {'type': 'altLabel', + 'language': 'fr', + 'label': 'la châtaigne'} + ], + 'notes': [ + { + 'type': 'definition', 'language': 'en', + 'note': 'A different type of tree.' + } + ], + 'narrower': [], + 'broader': [], + 'related': [], + 'member_of': ['3'] + } + self.species_dump = { + 'id': 3, + 'uri': 'http://id.trees.org/3', + 'labels': [ + {'type': 'prefLabel', 'language': 'en', 'label': 'Trees by species'}, + {'type': 'prefLabel', 'language': 'nl', 'label': 'Bomen per soort'} + ], + 'type': 'collection', + 'members': ['1', '2'], + 'member_of': [] + } + self.world_dump = { + 'id': '1', + 'uri': 'urn:x-skosprovider:geography:1', + 'type': 'concept', + 'labels': [ + {'type': 'prefLabel', 'language': 'en', 'label': 'World'} + ], + 'notes': [ + ], + 'narrower': [2, 3], + 'broader': [], + 'related': [], + 'member_of': [] + } + + self.tree_provider = DictionaryProvider({'id': 'TREE'}, [self.larch_dump,self.chestnut_dump,self.species_dump]) + self.world_provider = DictionaryProvider({'id': 'WORLD'}, [self.world_dump]) + # Set up rdf_provider + self.rdf_products_provider = RDFProvider( + {'id': 'PRODUCTS', 'conceptscheme_id': 1}, self.graph) + + def test_dump_rdf_to_rdf(self): + graph_dump = utils.rdf_dumper(self.rdf_products_provider) + xml = graph_dump.serialize(format='xml', encoding="UTF-8").decode() + self.assertEquals("