| 1 |
import unittest |
| 2 |
|
| 3 |
from rdflib import Literal |
| 4 |
from rdflib import RDF |
| 5 |
from rdflib import RDFS |
| 6 |
from rdflib import URIRef |
| 7 |
from rdflib import plugin |
| 8 |
from rdflib import query |
| 9 |
from rdflib.graph import ConjunctiveGraph |
| 10 |
from rdflib.graph import Graph |
| 11 |
from rdflib.graph import ReadOnlyGraphAggregate |
| 12 |
from rdflib.store import Store |
| 13 |
from six.moves import cStringIO as StringIO |
| 14 |
|
| 15 |
|
| 16 |
plugin.register( |
| 17 |
'xml', query.ResultParser, |
| 18 |
'rdflib.plugins.sparql.results.xmlresults', 'XMLResultParser') |
| 19 |
plugin.register( |
| 20 |
'xml', query.ResultSerializer, |
| 21 |
'rdflib.plugins.sparql.results.xmlresults', 'XMLResultSerializer') |
| 22 |
|
| 23 |
plugin.register( |
| 24 |
'json', query.ResultParser, |
| 25 |
'rdflib.plugins.sparql.results.jsonresults', 'JSONResultParser') |
| 26 |
plugin.register( |
| 27 |
'json', query.ResultSerializer, |
| 28 |
'rdflib.plugins.sparql.results.jsonresults', 'JSONResultSerializer') |
| 29 |
|
| 30 |
|
| 31 |
testGraph1N3 = """ |
| 32 |
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . |
| 33 |
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . |
| 34 |
@prefix : <http://test/> . |
| 35 |
:foo a rdfs:Class. |
| 36 |
:bar :d :c. |
| 37 |
:a :d :c. |
| 38 |
""" |
| 39 |
|
| 40 |
testGraph2N3 = """ |
| 41 |
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . |
| 42 |
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . |
| 43 |
@prefix : <http://test/> . |
| 44 |
@prefix log: <http://www.w3.org/2000/10/swap/log#>. |
| 45 |
:foo a rdfs:Resource. |
| 46 |
:bar rdfs:isDefinedBy [ a log:Formula ]. |
| 47 |
:a :d :e. |
| 48 |
""" |
| 49 |
|
| 50 |
testGraph3N3 = """ |
| 51 |
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . |
| 52 |
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . |
| 53 |
@prefix log: <http://www.w3.org/2000/10/swap/log#>. |
| 54 |
@prefix : <http://test/> . |
| 55 |
<> a log:N3Document. |
| 56 |
""" |
| 57 |
|
| 58 |
sparqlQ3 =\ |
| 59 |
""" |
| 60 |
PREFIX log: <http://www.w3.org/2000/10/swap/log#> |
| 61 |
SELECT ?n3Doc |
| 62 |
WHERE {?n3Doc a log:N3Document }""" |
| 63 |
|
| 64 |
|
| 65 |
class GraphAggregates1(unittest.TestCase): |
| 66 |
dburi = Literal('sqlite://') |
| 67 |
|
| 68 |
def setUp(self): |
| 69 |
memStore = plugin.get('SQLAlchemy', Store)( |
| 70 |
identifier="rdflib_test", configuration=self.dburi) |
| 71 |
self.graph1 = Graph(memStore) |
| 72 |
self.graph2 = Graph(memStore) |
| 73 |
self.graph3 = Graph(memStore) |
| 74 |
|
| 75 |
for n3Str, graph in [(testGraph1N3, self.graph1), |
| 76 |
(testGraph2N3, self.graph2), |
| 77 |
(testGraph3N3, self.graph3)]: |
| 78 |
graph.parse(StringIO(n3Str), format='n3') |
| 79 |
|
| 80 |
self.G = ReadOnlyGraphAggregate( |
| 81 |
[self.graph1, self.graph2, self.graph3]) |
| 82 |
|
| 83 |
def testAggregateRaw(self): |
| 84 |
|
| 85 |
assert len(list( |
| 86 |
self.G.triples((None, RDF.type, None)))) == 4 |
| 87 |
assert len(list( |
| 88 |
self.G.triples((URIRef("http://test/bar"), None, None)))) == 2 |
| 89 |
assert len(list( |
| 90 |
self.G.triples((None, URIRef("http://test/d"), None)))) == 3 |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
assert len(list(self.G.triples((None, None, None)))) == 8 |
| 95 |
|
| 96 |
|
| 97 |
for g in self.G.contexts(): |
| 98 |
assert isinstance(g, Graph) |
| 99 |
|
| 100 |
|
| 101 |
assert (URIRef("http://test/foo"), RDF.type, RDFS.Resource) in self.G |
| 102 |
|
| 103 |
barPredicates = [URIRef("http://test/d"), RDFS.isDefinedBy] |
| 104 |
assert len(list(self.G.triples_choices( |
| 105 |
(URIRef("http://test/bar"), barPredicates, None)))) == 2 |
| 106 |
|
| 107 |
|
| 108 |
class GraphAggregates3(unittest.TestCase): |
| 109 |
def setUp(self): |
| 110 |
memStore = plugin.get('SQLAlchemy', Store)( |
| 111 |
identifier="rdflib_test", configuration=Literal("sqlite://")) |
| 112 |
self.graph1 = Graph(memStore, URIRef("graph1")) |
| 113 |
self.graph2 = Graph(memStore, URIRef("graph2")) |
| 114 |
self.graph3 = Graph(memStore, URIRef("graph3")) |
| 115 |
|
| 116 |
for n3Str, graph in [(testGraph1N3, self.graph1), |
| 117 |
(testGraph2N3, self.graph2), |
| 118 |
(testGraph3N3, self.graph3)]: |
| 119 |
graph.parse(StringIO(n3Str), format='n3') |
| 120 |
self.G = ConjunctiveGraph(memStore) |
| 121 |
|
| 122 |
def testDefaultGraph(self): |
| 123 |
|
| 124 |
assert self.G.query(sparqlQ3), "CG as default graph should *all* triples" |
| 125 |
assert not self.graph2.query(sparqlQ3), "Graph as " + \ |
| 126 |
"default graph should *not* include triples from other graphs" |
| 127 |
|
| 128 |
|
| 129 |
if __name__ == '__main__': |
| 130 |
unittest.main() |