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
|
import os
import unittest
from rdflib import ConjunctiveGraph, URIRef, Namespace
from test import TEST_DIR
TEST_BASE = "test/nquads.rdflib"
class NQuadsParserTest(unittest.TestCase):
def _load_example(self):
g = ConjunctiveGraph()
nq_path = os.path.relpath(
os.path.join(TEST_DIR, "nquads.rdflib/example.nquads"), os.curdir
)
with open(nq_path, "rb") as data:
g.parse(data, format="nquads")
return g
def test_01_simple_open(self):
g = self._load_example()
assert len(g.store) == 449
def test_02_contexts(self):
# There should be 16 separate contexts
g = self._load_example()
assert len([x for x in g.store.contexts()]) == 16
def test_03_get_value(self):
# is the name of entity E10009 "Arco Publications"?
# (in graph http://bibliographica.org/entity/E10009)
# Looking for:
# <http://bibliographica.org/entity/E10009>
# <http://xmlns.com/foaf/0.1/name>
# "Arco Publications"
# <http://bibliographica.org/entity/E10009>
g = self._load_example()
s = URIRef("http://bibliographica.org/entity/E10009")
FOAF = Namespace("http://xmlns.com/foaf/0.1/")
self.assertTrue(g.value(s, FOAF.name).eq("Arco Publications"))
def test_context_is_optional(self):
g = ConjunctiveGraph()
nq_path = os.path.relpath(
os.path.join(TEST_DIR, "nquads.rdflib/test6.nq"), os.curdir
)
with open(nq_path, "rb") as data:
g.parse(data, format="nquads")
assert len(g) > 0
def test_serialize(self):
g = ConjunctiveGraph()
uri1 = URIRef("http://example.org/mygraph1")
uri2 = URIRef("http://example.org/mygraph2")
bob = URIRef("urn:bob")
likes = URIRef("urn:likes")
pizza = URIRef("urn:pizza")
g.get_context(uri1).add((bob, likes, pizza))
g.get_context(uri2).add((bob, likes, pizza))
s = g.serialize(format="nquads", encoding="utf-8")
self.assertEqual(len([x for x in s.split(b"\n") if x.strip()]), 2)
g2 = ConjunctiveGraph()
g2.parse(data=s, format="nquads")
self.assertEqual(len(g), len(g2))
self.assertEqual(
sorted(x.identifier for x in g.contexts()),
sorted(x.identifier for x in g2.contexts()),
)
class BnodeContextTest(unittest.TestCase):
def setUp(self):
self.data = open("test/nquads.rdflib/bnode_context.nquads", "rb")
self.data_obnodes = open(
"test/nquads.rdflib/bnode_context_obj_bnodes.nquads", "rb"
)
def tearDown(self):
self.data.close()
def test_parse_shared_bnode_context(self):
bnode_ctx = dict()
g = ConjunctiveGraph()
h = ConjunctiveGraph()
g.parse(self.data, format="nquads", bnode_context=bnode_ctx)
self.data.seek(0)
h.parse(self.data, format="nquads", bnode_context=bnode_ctx)
self.assertEqual(set(h.subjects()), set(g.subjects()))
def test_parse_shared_bnode_context_same_graph(self):
bnode_ctx = dict()
g = ConjunctiveGraph()
g.parse(self.data_obnodes, format="nquads", bnode_context=bnode_ctx)
o1 = set(g.objects())
self.data_obnodes.seek(0)
g.parse(self.data_obnodes, format="nquads", bnode_context=bnode_ctx)
o2 = set(g.objects())
self.assertEqual(o1, o2)
def test_parse_distinct_bnode_context(self):
g = ConjunctiveGraph()
g.parse(self.data, format="nquads", bnode_context=dict())
s1 = set(g.subjects())
self.data.seek(0)
g.parse(self.data, format="nquads", bnode_context=dict())
s2 = set(g.subjects())
self.assertNotEqual(set(), s2 - s1)
def test_parse_distinct_bnode_contexts_between_graphs(self):
g = ConjunctiveGraph()
h = ConjunctiveGraph()
g.parse(self.data, format="nquads")
s1 = set(g.subjects())
self.data.seek(0)
h.parse(self.data, format="nquads")
s2 = set(h.subjects())
self.assertNotEqual(s1, s2)
def test_parse_distinct_bnode_contexts_named_graphs(self):
g = ConjunctiveGraph()
h = ConjunctiveGraph()
g.parse(self.data, format="nquads")
self.data.seek(0)
h.parse(self.data, format="nquads")
self.assertNotEqual(set(h.contexts()), set(g.contexts()))
def test_parse_shared_bnode_contexts_named_graphs(self):
bnode_ctx = dict()
g = ConjunctiveGraph()
h = ConjunctiveGraph()
g.parse(self.data, format="nquads", bnode_context=bnode_ctx)
self.data.seek(0)
h.parse(self.data, format="nquads", bnode_context=bnode_ctx)
self.assertEqual(set(h.contexts()), set(g.contexts()))
if __name__ == "__main__":
unittest.main()
|