1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
from codecs import getreader
from io import BytesIO, StringIO
from rdflib import URIRef, Literal
from rdflib.graph import Graph
rdf = """@prefix skos:
<http://www.w3.org/2004/02/skos/core#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix : <http://www.test.org/#> .
:world rdf:type skos:Concept;
skos:prefLabel "World"@en.
:africa rdf:type skos:Concept;
skos:prefLabel "Africa"@en;
skos:broaderTransitive :world.
:CI rdf:type skos:Concept;
skos:prefLabel "C\u00f4te d'Ivoire"@fr;
skos:broaderTransitive :africa.
"""
rdf_utf8 = rdf.encode("utf-8")
rdf_reader = getreader("utf-8")(BytesIO(rdf.encode("utf-8")))
def test_a():
"""Test reading N3 from a unicode objects as data"""
g = Graph()
g.parse(data=rdf, format="n3")
v = g.value(
subject=URIRef("http://www.test.org/#CI"),
predicate=URIRef("http://www.w3.org/2004/02/skos/core#prefLabel"),
)
assert v == Literal("C\u00f4te d'Ivoire", lang="fr")
def test_b():
"""Test reading N3 from a utf8 encoded string as data"""
g = Graph()
g.parse(data=rdf_utf8, format="n3")
v = g.value(
subject=URIRef("http://www.test.org/#CI"),
predicate=URIRef("http://www.w3.org/2004/02/skos/core#prefLabel"),
)
assert v == Literal("C\u00f4te d'Ivoire", lang="fr")
def test_c():
"""Test reading N3 from a codecs.StreamReader, outputting unicode"""
g = Graph()
# rdf_reader.seek(0)
g.parse(source=rdf_reader, format="n3")
v = g.value(
subject=URIRef("http://www.test.org/#CI"),
predicate=URIRef("http://www.w3.org/2004/02/skos/core#prefLabel"),
)
assert v == Literal("C\u00f4te d'Ivoire", lang="fr")
def test_d():
"""Test reading N3 from a StringIO over the unicode object"""
g = Graph()
g.parse(source=StringIO(rdf), format="n3")
v = g.value(
subject=URIRef("http://www.test.org/#CI"),
predicate=URIRef("http://www.w3.org/2004/02/skos/core#prefLabel"),
)
assert v == Literal("C\u00f4te d'Ivoire", lang="fr")
def test_e():
"""Test reading N3 from a BytesIO over the string object"""
g = Graph()
g.parse(source=BytesIO(rdf_utf8), format="n3")
v = g.value(
subject=URIRef("http://www.test.org/#CI"),
predicate=URIRef("http://www.w3.org/2004/02/skos/core#prefLabel"),
)
assert v == Literal("C\u00f4te d'Ivoire", lang="fr")
# this is unicode
rdfxml = """<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:skos="http://www.w3.org/2004/02/skos/core#"
>
<rdf:Description rdf:about="http://www.test.org/#CI">
<rdf:type rdf:resource="http://www.w3.org/2004/02/skos/core#Concept"/>
<skos:prefLabel xml:lang="fr">C\u00f4te d\'Ivoire</skos:prefLabel>
<skos:broaderTransitive rdf:resource="http://www.test.org/#africa"/>
</rdf:Description>
</rdf:RDF>
"""
# this is a str
rdfxml_utf8 = rdfxml.encode("utf-8")
rdfxml_reader = getreader("utf-8")(BytesIO(rdfxml.encode("utf-8")))
def test_xml_a():
"""Test reading XML from a unicode object as data"""
g = Graph()
g.parse(data=rdfxml, format="xml")
v = g.value(
subject=URIRef("http://www.test.org/#CI"),
predicate=URIRef("http://www.w3.org/2004/02/skos/core#prefLabel"),
)
assert v == Literal("C\u00f4te d'Ivoire", lang="fr")
def test_xml_b():
"""Test reading XML from a utf8 encoded string object as data"""
g = Graph()
g.parse(data=rdfxml_utf8, format="xml")
v = g.value(
subject=URIRef("http://www.test.org/#CI"),
predicate=URIRef("http://www.w3.org/2004/02/skos/core#prefLabel"),
)
assert v == Literal("C\u00f4te d'Ivoire", lang="fr")
# The following two cases are currently not supported by Graph.parse
# def test_xml_c():
# """Test reading XML from a codecs.StreamReader, outputting unicode"""
# g = Graph()
# g.parse(source=rdfxml_reader, format='xml')
# v = g.value(subject=URIRef("http://www.test.org/#CI"), predicate=URIRef("http://www.w3.org/2004/02/skos/core#prefLabel"))
# assert v==Literal(u"C\u00f4te d'Ivoire", lang='fr')
# def test_xml_d():
# """Test reading XML from a BytesIO created from unicode object"""
# g = Graph()
# g.parse(source=BytesIO(rdfxml), format='xml')
# v = g.value(subject=URIRef("http://www.test.org/#CI"), predicate=URIRef("http://www.w3.org/2004/02/skos/core#prefLabel"))
# assert v==Literal(u"C\u00f4te d'Ivoire", lang='fr')
def test_xml_e():
"""Test reading XML from a BytesIO created from utf8 encoded string"""
g = Graph()
g.parse(source=BytesIO(rdfxml_utf8), format="xml")
v = g.value(
subject=URIRef("http://www.test.org/#CI"),
predicate=URIRef("http://www.w3.org/2004/02/skos/core#prefLabel"),
)
assert v == Literal("C\u00f4te d'Ivoire", lang="fr")
|