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
|
from __future__ import unicode_literals, print_function
from rdflib import *
from rdflib.plugin import register, Parser
register("application/ld+json", Parser, "rdflib_jsonld.parser", "JsonLDParser")
data = """
{
"@context": {"@vocab": "http://schema.org/"},
"@graph": [
{ "@id": "http://example.org/data#jdoe",
"name": "John"
},
{ "@id": "http://example.org/data#janedoe",
"name": "Jane"
},
{ "@id": "http://example.org/data#metadata",
"@graph": [
{ "@id": "http://example.org/data",
"creator": "http://example.org/data#janedoe"
}
]
}
]
}
"""
meta_ctx = URIRef("http://example.org/data#metadata")
def test_graph():
g = Graph()
g.parse(data=data, format="application/ld+json")
assert len(g) == 2
def test_conjunctive_graph():
cg = ConjunctiveGraph()
cg.default_context.parse(data=data, format="application/ld+json")
assert len(cg) == 3
print(
"default graph (%s) contains %s triples (expected 2)"
% (cg.identifier, len(cg.default_context))
)
for ctx in cg.contexts():
print("named graph (%s) contains %s triples" % (ctx.identifier, len(ctx)))
assert len(cg.default_context) == 2
assert len(list(cg.contexts())) == 2
def test_dataset():
ds = Dataset()
ds.default_context.parse(data=data, format="application/ld+json")
assert len(ds) == 3
assert len(ds.default_context) == 2
print(
"default graph (%s) contains %s triples (expected 2)"
% (ds.identifier, len(ds.default_context))
)
contexts = dict((ctx.identifier, ctx) for ctx in ds.contexts())
assert len(contexts) == 2
assert len(contexts.pop(meta_ctx)) == 1
assert len(list(contexts.values())[0]) == 2
|