def test_updatelist_cut_bnodes(self): bn = B() self.g.set((self.g.value(PA, VOCAB.prefLang), RDF.first, bn)) self.g.set((bn, VOCAB.foo, Literal("foo"))) if not isomorphic(self.g, G(INITIAL.replace('''( "fr" "en" "tlh" )''', '''( [v:foo "foo"] "en" "tlh" )'''))): raise ValueError("Error while setting up test...") self._my_updatelist(PA, VOCAB.prefLang, Slice(0, 1), []) exp = G(INITIAL.replace('''( "fr" "en" "tlh" )''', '''( "en" "tlh" )''')) # NB: no trace for the arc ''' _:nb v:foo "foo" ''' in the graph got = self.g assert isomorphic(got, exp), got.serialize(format="turtle")
def test_to_graph_should_return_bounding_box_as_graph() -> None: """It returns a bounding box graph isomorphic to spec.""" location = Location() location.identifier = "http://example.com/locations/1" location.bounding_box = """POLYGON (( 3.053 47.975 , 7.24 47.975 , 7.24 53.504 , 3.053 53.504 , 3.053 47.975 ))""" src = """ @prefix dct: <http://purl.org/dc/terms/> . @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . @prefix dcat: <http://www.w3.org/ns/dcat#> . @prefix locn: <http://www.w3.org/ns/locn#> . @prefix geosparql: <http://www.opengis.net/ont/geosparql#> . <http://example.com/locations/1> a dct:Location ; dcat:bbox \"\"\"POLYGON (( 3.053 47.975 , 7.24 47.975 , 7.24 53.504 , 3.053 53.504 , 3.053 47.975 ))\"\"\"^^geosparql:asWKT ; . """ g1 = Graph().parse(data=location.to_rdf(), format="turtle") g2 = Graph().parse(data=src, format="turtle") _isomorphic = isomorphic(g1, g2) if not _isomorphic: _dump_diff(g1, g2) pass assert _isomorphic
def updateSummary(self): context = aq_inner(self.context) # If the Summarizer Source is inactive, we're done if not context.active: raise SourceNotActive(context) # Check if the Summarizer Source has an Summarizer Generator if not context.generator: raise NoGeneratorError(context) generator = context.generator.to_object generatorPath = '/'.join(generator.getPhysicalPath()) # Adapt the generator to a graph generator, and get the graph in XML form. serialized = None mimetype = None if generator.datatype == 'json': generator = IJsonGenerator(generator) serialized = generator.generateJson() json = jsonlib.read(serialized) mimetype = SUMMARIZER_JSON_MIMETYPE # Is there an active file? if context.approvedFile: # Is it identical to what we just generated? print context.approvedFile.to_object.get_data() current = jsonlib.read(context.approvedFile.to_object.get_data()) if sorted(json.items()) == sorted(current.items()): raise NoUpdateRequired(context) elif generator.datatype == 'rdf': generator = IGraphGenerator(generator) rdf = generator.generateGraph() serialized = rdf.serialize() mimetype = SUMMARIZER_XML_MIMETYPE # Is there an active file? if context.approvedFile: # Is it identical to what we just generated? current = Graph().parse(data=context.approvedFile.to_object.get_data()) if isomorphic(rdf, current): raise NoUpdateRequired(context) else: raise UnknownGeneratorError(context) # Create a new file and set it active # TODO: Add validation steps here timestamp = datetime.datetime.utcnow().isoformat() newFile = context[context.invokeFactory( 'File', context.generateUniqueId('File'), title=u'Summary %s' % timestamp, description=u'Generated at %s by %s' % (timestamp, generatorPath), file=serialized, )] newFile.getFile().setContentType(mimetype) newFile.reindexObject() intIDs = getUtility(IIntIds) newFileID = intIDs.getId(newFile) context.approvedFile = RelationValue(newFileID) notify(ObjectModifiedEvent(context))
def test(self): metadata = None if 'metadata' in option: metadata = option['metadata'] try: csvw = CSVW(csv_file, metadata_url=metadata) except Exception as e: # this should be a negative test if TYPES[type]: traceback.print_exc() self.assertFalse(TYPES[type]) return # if we get here this should be a positive test self.assertTrue(TYPES[type]) # if we can parse it we should at least produce some embedded metadata self.assertNotEqual(csvw.metadata, None) # and the result should exists self.assertNotEqual(result_url, None) gr = Graph() result = gr.parse(result_url) converted_result = csvw.to_rdf() result.serialize('output_rdf/' + name + '.ttl', format='turtle') converted_result.serialize('output_rdf/generated' + name + '.ttl', format='turtle') self.assertTrue(compare.isomorphic(result, converted_result))
def test_add_complex(self): g2add = G([(PA, VOCAB.favNumbers, B("l1"))]) Collection(g2add, B("l1"), [ Literal(i) for i in (42, 7, 2, 10) ]) self.e.add(g2add) exp = G(INITIAL + """<http://champin.net/#pa> v:favNumbers (42 7 2 10) .""") got = self.g assert isomorphic(got, exp), got.serialize(format="turtle")
def test_to_graph_should_return_centroid_as_graph() -> None: """It returns a centroid graph isomorphic to spec.""" location = Location() location.identifier = "http://example.com/locations/1" location.centroid = "POINT(4.88412 52.37509)" src = """ @prefix dct: <http://purl.org/dc/terms/> . @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . @prefix dcat: <http://www.w3.org/ns/dcat#> . @prefix locn: <http://www.w3.org/ns/locn#> . @prefix geosparql: <http://www.opengis.net/ont/geosparql#> . <http://example.com/locations/1> a dct:Location ; dcat:centroid \"POINT(4.88412 52.37509)\"^^geosparql:asWKT ; . """ g1 = Graph().parse(data=location.to_rdf(), format="turtle") g2 = Graph().parse(data=src, format="turtle") _isomorphic = isomorphic(g1, g2) if not _isomorphic: _dump_diff(g1, g2) pass assert _isomorphic
def turtle(test): g = Graph() try: base = 'http://www.w3.org/2013/TurtleTests/'+split_uri(test.action)[1] g.parse(test.action, publicID=base, format='turtle') if not test.syntax: raise AssertionError("Input shouldn't have parsed!") if test.result: # eval test res = Graph() res.parse(test.result, format='nt') if verbose: both, first, second = graph_diff(g,res) if not first and not second: return print "Diff:" #print "%d triples in both"%len(both) print "Turtle Only:" for t in first: print t print "--------------------" print "NT Only" for t in second: print t raise Exception('Graphs do not match!') assert isomorphic(g, res), 'graphs must be the same' except: if test.syntax: raise
def _test_parser(jsonpath, turtlepath): test_tree, test_graph = _load_test_data(jsonpath, turtlepath) graph = to_rdf(test_tree, Graph()) assert isomorphic(graph, test_graph), \ "Expected graph:\n%s\nGot:\n %s" % ( test_graph.serialize(format='n3'), graph.serialize(format='n3'))
def test_graph_prefix(): """ This is issue https://github.com/RDFLib/rdflib/issues/313 """ g1 = Graph() g1.parse(data=""" @prefix : <urn:ns1:> . :foo <p> 42. """, format="n3") g2 = Graph() g2.parse(data=""" @prefix : <urn:somethingelse:> . <urn:ns1:foo> <p> 42. """, format="n3") assert isomorphic(g1, g2) q_str = (""" PREFIX : <urn:ns1:> SELECT ?val WHERE { :foo ?p ?val } """) q_prepared = prepareQuery(q_str) expected = [(Literal(42),)] eq_(list(g1.query(q_prepared)), expected) eq_(list(g2.query(q_prepared)), expected) eq_(list(g1.query(q_str)), expected) eq_(list(g2.query(q_str)), expected)
def _test_serializer(inputpath, expectedpath, context, serpar): test_tree, test_graph = _load_test_data(inputpath, expectedpath, context) if isinstance(test_tree, ConjunctiveGraph): expected = test_tree.serialize(format="json-ld") else: expected = _to_json(_to_ordered(test_tree)) if test_graph is not None: # toRdf, expected are nquads result_tree = to_tree(test_graph, context_data=context) result = _to_json(_to_ordered(result_tree)) elif inputpath.startswith('fromRdf'): # fromRdf, expected in json-ld g = ConjunctiveGraph() data = open(p.join(test_dir, inputpath), 'rb').read() g.parse(data=data, format="nquads", context=context) result = g.serialize(format="json-ld", base=context) else: # json f = open(p.join(test_dir, inputpath), 'rb') result = json.load(f)[0] f.close() if isinstance(result, ConjunctiveGraph): assert isomorphic(result, expected), \ "Expected graph of %s:\n%s\nGot graph of %s:\n %s" % ( expected.serialize(format='n3'), result.serialize(format='n3')) else: assert jsonld_compare(expected, result) == True, \ "Expected JSON:\n%s\nGot:\n%s" % (expected, result)
def test_add_two_bnodes(self): # check that distinct bnodes in the source graph # generate distinct fresh bnodes in the target graph ; # as above, try to mess with existing bnode labels to check robustness jdoe = BNode("ucbl") jdup = BNode("am") self.e.add(G([ (PA, FOAF.knows, jdoe), (jdoe, FOAF.givenName, Literal("John")), (jdoe, FOAF.familyName, Literal("Doe")), (PA, FOAF.knows, jdup), (jdup, FOAF.givenName, Literal("Jeanne")), (jdup, FOAF.familyName, Literal("Dupont")), ])) exp = G(INITIAL + """ <http://champin.net/#pa> f:knows [ f:givenName "John" ; f:familyName "Doe" ; ], [ f:givenName "Jeanne" ; f:familyName "Dupont" ; ]; . """) got = self.g assert isomorphic(got, exp), got.serialize(format="turtle")
def test_delete_simple(self): self.e.delete(G([ (PA, FOAF.name, Literal("Pierre-Antoine Champin")), ])) exp = G(INITIAL.replace("""f:name "Pierre-Antoine Champin" ;""", "")) got = self.g assert isomorphic(got, exp), got.serialize(format="turtle")
def testMatchWildcardPatternOnObject(self): g = fromN3(''' { :a :b :c } => { :d :e :f } . ''') escapeOutputStatements(g, [(NS['d'], NS['e'], None)]) expected = fromN3(''' { :a :b :c } => { :output :statement [ :subj :d; :pred :e; :obj :f ] } . ''') self.assert_(isomorphic(impliedGraph(expected), impliedGraph(g)))
def test_query2(self): with open(join(EXAMPLES, "query2.json")) as query_file: q = load(query_file) result = apply(q, self.g) expected = Graph() expected.load(join(EXAMPLES, "query2.result.ttl"), format="turtle") assert isomorphic(result.graph, expected), result.graph.serialize(format="turtle")
def testDontReplaceSourceStatements(self): g = fromN3(''' { :a :b :c } => { :a :b :c } . ''') escapeOutputStatements(g, [(NS['a'], NS['b'], NS['c'])]) expected = fromN3(''' { :a :b :c } => { :output :statement [ :subj :a; :pred :b; :obj :c ] } . ''') self.assert_(isomorphic(impliedGraph(expected), impliedGraph(g)))
def testWildcardAndNonMatchingStatements(self): g = fromN3(''' { :a :b :c } => { :d :e :f . :g :e :f . } . ''') escapeOutputStatements(g, [(NS['d'], NS['e'], NS['f'])]) expected = fromN3(''' { :a :b :c } => { :output :statement [ :subj :d; :pred :e; :obj :f ] . :g :e :f } . ''') self.assert_(isomorphic(impliedGraph(expected), impliedGraph(g)))
def test_namespace_registry_load_cuba(self): """Test loading the CUBA namespace.""" g = rdflib.Graph() g.parse(CUBA_FILE, format="ttl") self.assertTrue(isomorphic(g, self.graph)) self.assertIn("cuba", self.namespace_registry._namespaces) self.assertEqual(self.namespace_registry._namespaces["cuba"], rdflib.URIRef("http://www.osp-core.com/cuba#"))
def testTwoMatchingStatements(self): g = fromN3(''' { :a :b :c } => { :d :e :f . :g :e :f } . ''') escapeOutputStatements(g, [(None, NS['e'], None)]) expected = fromN3(''' { :a :b :c } => { :output :statement [ :subj :d; :pred :e; :obj :f ], [ :subj :g; :pred :e; :obj :f ] } . ''') self.assert_(isomorphic(impliedGraph(expected), impliedGraph(g)))
def test03(self): ingraph = to_rdf(json.loads(test03_in), ConjunctiveGraph()) outgraph = ConjunctiveGraph().parse( data=ingraph.serialize(format="xml"), format="xml") assert isomorphic(outgraph, ingraph), \ "Expected graph of %s:\n%s\nGot graph of %s:\n %s" % ( len(outgraph), outgraph.serialize(), len(ingraph), ingraph.serialize())
def test_delete_variable_subject(self): self.e.bind(V("s"), PA, [InvIRI(FOAF.member)]) self.e.delete(G([ (V("s"), FOAF.name, Literal("Université Claude Bernard Lyon 1")), ])) exp = G(INITIAL.replace("""f:name "Université Claude Bernard Lyon 1" ;""", "")) got = self.g assert isomorphic(got, exp), got.serialize(format="turtle")
def test_add_variable_subject(self): self.e.bind(V("s"), PA, [InvIRI(FOAF.member)]) self.e.add(G([ (V("s"), FOAF.homepage, IRI("http://www.univ-lyon1.fr/")), ])) exp = G(INITIAL + """_:ucbl f:homepage <http://www.univ-lyon1.fr/>.""") got = self.g assert isomorphic(got, exp), got.serialize(format="turtle")
def test_parse_rdf(self): """Test parsing an rdf file.""" with tempfile.TemporaryDirectory() as tempdir: new_config = self.config_file new_config["namespaces"] = {} new_yml_path = os.path.join(tempdir, os.path.basename(YML_FILE)) new_ttl_path = os.path.join(tempdir, os.path.basename(RDF_FILE)) with open(new_yml_path, "w") as file: yaml.dump(new_config, file) shutil.copy(RDF_FILE, new_ttl_path) self.assertRaises(TypeError, new_yml_path, self.parser.parse) with tempfile.TemporaryDirectory() as tempdir: new_config = self.config_file new_config["identifier"] = "x" new_yml_path = os.path.join(tempdir, os.path.basename(YML_FILE)) new_ttl_path = os.path.join(tempdir, os.path.basename(RDF_FILE)) with open(new_yml_path, "w") as file: yaml.dump(new_config, file) shutil.copy(RDF_FILE, new_ttl_path) self.assertRaises(TypeError, new_yml_path, self.parser.parse) with tempfile.TemporaryDirectory() as tempdir: new_config = self.config_file new_config["namespaces"] = {} new_config["identifier"] = "x" new_config["invalid"] = True new_yml_path = os.path.join(tempdir, os.path.basename(YML_FILE)) new_ttl_path = os.path.join(tempdir, os.path.basename(RDF_FILE)) with open(new_yml_path, "w") as file: yaml.dump(new_config, file) shutil.copy(RDF_FILE, new_ttl_path) self.assertRaises(TypeError, new_yml_path, self.parser.parse) with tempfile.TemporaryDirectory() as tempdir: config = dict( identifier="parser_test", ontology_file=RDF_FILE, namespaces={ "parser_test": "http://www.osp-core.com/parser_test" }, format="ttl", file="file.ttl", ) new_yml_path = os.path.join(tempdir, "file.yml") new_ttl_path = os.path.join(tempdir, "file.ttl") with open(new_yml_path, "w") as file: yaml.dump(config, file) shutil.copy(RDF_FILE, new_ttl_path) rdf = rdflib.Graph() rdf.parse(RDF_FILE, format="ttl") parser = OntologyParser.get_parser(new_yml_path) graph = parser.graph self.assertEqual(len(graph), len(rdf)) self.assertTrue(isomorphic(graph, rdf)) self.assertIn( rdflib.URIRef("http://www.osp-core.com/parser_test#"), list(parser.namespaces.values()), )
def test01(self): # tree, graph, base=None, context_data=None g = ConjunctiveGraph() ingraph = to_rdf(json.loads(test01_in), g) outgraph = ConjunctiveGraph() outgraph.parse(data=test01_out, format="nquads") assert isomorphic(outgraph, ingraph), \ "Expected graph of %s:\n%s\nGot graph of %s:\n %s" % ( len(outgraph), outgraph.serialize(), len(ingraph), ingraph.serialize())
async def test_validator_with_default_config(http_service: Any) -> None: """Should return OK and successful validation.""" url = f"{http_service}/validator" data_graph_file = "tests/files/valid_catalog.ttl" shapes_graph_file = "tests/files/mock_dcat-ap-no-shacl_shapes_2.00.ttl" ontology_graph_file = "tests/files/ontologies.ttl" config = {"expand": True, "includeExpandedTriples": False} with MultipartWriter("mixed") as mpwriter: p = mpwriter.append(open(data_graph_file, "rb")) p.set_content_disposition("attachment", name="data-graph-file", filename=data_graph_file) p.headers[hdrs.CONTENT_ENCODING] = "gzip" p = mpwriter.append(json.dumps(config)) p.set_content_disposition("inline", name="config") p = mpwriter.append(open(shapes_graph_file, "rb")) p.set_content_disposition("attachment", name="shapes-graph-file", filename=shapes_graph_file) p = mpwriter.append(open(ontology_graph_file, "rb")) p.set_content_disposition("attachment", name="ontology-graph-file", filename=ontology_graph_file) session = ClientSession() async with session.post(url, data=mpwriter) as resp: body = await resp.text() await session.close() assert resp.status == 200 assert "text/turtle" in resp.headers[hdrs.CONTENT_TYPE] # results_graph (validation report) should be isomorphic to the following: src = """ @prefix sh: <http://www.w3.org/ns/shacl#> . @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . [] a sh:ValidationReport ; sh:conforms true . """ with open(data_graph_file, "r") as file: text = file.read() g0 = Graph().parse(data=text, format="text/turtle") g1 = g0 + Graph().parse(data=src, format="text/turtle") g2 = Graph().parse(data=body, format="text/turtle") _isomorphic = isomorphic(g1, g2) if not _isomorphic: _dump_diff(g1, g2) pass assert _isomorphic, "results_graph is incorrect"
def test_delete_variable_object(self): # no easy way to test it in the current graph, # so we add an arc and remove it again... self.g.add((PA, VOCAB.memberOf, self.ucbl)) self.e.bind(V("o"), PA, [InvIRI(FOAF.member)]) self.e.delete(G([ (PA, VOCAB.memberOf, V("o")), ])) exp = G(INITIAL) got = self.g assert isomorphic(got, exp), got.serialize(format="turtle")
def test_compiler_v1_4(): compiler = RDFCompiler() with open("tests/data/metadata_v14.ttl", "r") as _input_file: expected_graph = Graph() expected_graph.parse(data=_input_file.read(), format="ttl") _ = compiler.visit(metadata_v_1_4) expected = to_isomorphic(expected_graph) got = to_isomorphic(compiler.graph) for (t1, t2) in _squashed_graphs_triples(expected, got): assert t1 == t2 assert isomorphic(expected, got)
def test_parse_ontology(self): """Test the _parse_ontology method of the YAML Parser.""" self.graph.parse(CUBA_FILE, format="ttl") pre = set(self.graph) self.parser._parse_ontology() test_graph1 = rdflib.Graph() test_graph1.parse(RDF_FILE, format="ttl") test_graph2 = rdflib.Graph() for triple in set(self.parser.graph) - pre: test_graph2.add(triple) self.assertTrue(isomorphic(test_graph1, test_graph2))
def _test_parser(cat, num, inputpath, expectedpath, context, options): input_obj = _load_json(inputpath) expected_graph = _load_nquads(expectedpath) base = TC_BASE + inputpath result_graph = ConjunctiveGraph() to_rdf(input_obj, result_graph, base=base, context_data=context, produce_generalized_rdf = options.get('produceGeneralizedRdf', False)) assert isomorphic( result_graph, expected_graph), "Expected:\n%s\nGot:\n%s" % ( expected_graph.serialize(format='turtle'), result_graph.serialize(format='turtle'))
def _test_parser(inputpath, expectedpath, context): input_tree = _load_json(inputpath) expected_graph = _load_nquads(expectedpath) base = TC_BASE + inputpath result_graph = to_rdf( input_tree, ConjunctiveGraph(), base=base, context_data=context) assert isomorphic( result_graph, expected_graph), "Expected:\n%s\nGot:\n%s" % ( expected_graph.serialize(format='n3'), result_graph.serialize(format='n3'))
def jsonLdEqual(a, b): """Check if to JSON documents containing JSON LD are equal.""" if isinstance(a, dict) and isinstance(b, dict) and a.keys() == b.keys(): return all(jsonLdEqual(a[k], b[k]) for k in a.keys()) elif a and isinstance(a, list) and isinstance(a[0], dict) \ and "@id" in a[0]: return isomorphic(json_to_rdf(a, rdflib.Graph()), json_to_rdf(b, rdflib.Graph())) elif a and isinstance(a, list) and isinstance(a[0], list) \ and isinstance(a[0][0], dict) and "@id" in a[0][0]: graph_a, graph_b = rdflib.Graph(), rdflib.Graph() for x in a: json_to_rdf(x, graph_a) for x in b: json_to_rdf(x, graph_b) return isomorphic(graph_a, graph_b) elif isinstance(a, list) and isinstance(b, list) and len(a) == len(b): return all(jsonLdEqual(aa, bb) for aa, bb in zip(a, b)) else: return a == b
def complete_new_graph(cls, service, uri, parameters, new_graph, resource=None): """I implement :meth:`ILocalResource.complete_new_graph`. If new_graph contains only a wikitext property, then all corresponding triples are generated. If new_graph contains other triples and either no wikitext *or* the same wikitext as previously, then the wikitext is updated to reflect the triples. If new_graph contains other triples and a wikitext different from the previous one, then the wikitext and the triples *have* to be consistent, or a InvalidDataError will be raised. """ assert resource is not None # topics can only be created by PUT wikitexts = list(new_graph.objects(uri, SW.wikitext)) if len(wikitexts) > 1: # leave it to WithCardinalityMixin to raise an error return if len(wikitexts) == 0: new_wikitext = None else: new_wikitext = unicode(wikitexts[0]) if new_wikitext is not None and len(new_graph) == 1: # wikitext only: parse other triples from it wikitext_to_triples(resource, new_wikitext, into=new_graph) return if new_wikitext is not None and new_wikitext != resource.wikitext: # wikitext *and* triples were changed: they must be consistent from_text = wikitext_to_triples(resource, new_wikitext) from_text.add((uri, SW.wikitext, wikitexts[0])) if not isomorphic(from_text, new_graph): raise InvalidDataError("wikitext and triples are inconsistent") else: return # new_wikitext is either None or equal to old wikitext, # so we focus on the triples of new_graph if new_wikitext is None: old_wikitext = resource.get_state().value(uri, SW.wikitext) new_graph.add((uri, SW.wikitext, old_wikitext)) new_wikitext = unicode(old_wikitext) _, added, removed = graph_diff(new_graph, resource.get_state()) if added: new_wikitext = add_triples(resource, new_wikitext, added) if removed: new_wikitext = ban_triples(resource, new_wikitext, removed) if added or removed: new_graph.set((uri, SW.wikitext, Literal(new_wikitext)))
def test_add_bnode(self): # deliberately reusing a BNode label from the original graph ('ucbl'), # to check that a fresh bnode is nonetheless created mytwitter = BNode("ucbl") self.e.add(G([ (PA, FOAF.holdsAccount, mytwitter), (mytwitter, FOAF.accountName, Literal("pchampin")), ])) exp = G(INITIAL + """<http://champin.net/#pa> f:holdsAccount [ """ """ f:accountName "pchampin"\n].""") got = self.g assert isomorphic(got, exp), got.serialize(format="turtle")
def test_skolem_de_skolem_roundtrip(): """Test round-trip of skolemization/de-skolemization of data. Issue: https://github.com/RDFLib/rdflib/issues/1404 """ ttl = ''' @prefix wd: <http://www.wikidata.org/entity/> . @prefix foaf: <http://xmlns.com/foaf/0.1/> . wd:Q1203 foaf:knows [ a foaf:Person; foaf:name "Ringo" ]. ''' graph = Graph() graph.parse(data=ttl, format='turtle') query = { "subject": URIRef("http://www.wikidata.org/entity/Q1203"), "predicate": FOAF.knows } # Save the original bnode id. bnode_id = graph.value(**query) skolemized_graph = graph.skolemize() # Check the BNode is now an RDFLibGenid after skolemization. skolem_bnode = skolemized_graph.value(**query) assert type(skolem_bnode) == RDFLibGenid # Check that the original bnode id exists somewhere in the uri. assert bnode_id in skolem_bnode # Check that the original data is not isomorphic with the skolemized data. assert not isomorphic(graph, skolemized_graph) # Check that the original graph data is the same as the de-skolemized data. de_skolemized_graph = skolemized_graph.de_skolemize() assert isomorphic(graph, de_skolemized_graph)
def test_namespace_registry_load(self): """Test loading an installed namespaces.""" # no graph.xml found self.namespace_registry.clear() self.namespace_registry.load(self.tempdir.name) g = rdflib.Graph() g.parse(CUBA_FILE, format="ttl") self.assertTrue(isomorphic(g, self.namespace_registry._graph)) self.namespace_registry.clear() self.graph = self.namespace_registry._graph # graph.ttl found self.graph.parse(RDF_FILE, format="ttl") self.graph.bind("parser_test", rdflib.URIRef("http://www.osp-core.com/parser_test#")) self.namespace_registry.update_namespaces() self.namespace_registry.store(self.tempdir.name) nr = NamespaceRegistry() nr.load(self.tempdir.name) self.assertTrue(isomorphic(nr._graph, self.graph)) self.assertIn("parser_test", nr)
def test_to_graph_should_return_dataservice_skolemization( mocker: MockFixture) -> None: """It returns a dataservice graph isomorphic to spec.""" catalog = Catalog() catalog.identifier = "http://example.com/catalogs/1" dataservice1 = DataService() dataservice1.title = {"nb": "Dataservice 1", "en": "Dataservice 1"} catalog.services.append(dataservice1) dataservice2 = DataService() dataservice2.title = {"nb": "Dataservice 2", "en": "Dataservice 2"} catalog.services.append(dataservice2) src = """ @prefix dct: <http://purl.org/dc/terms/> . @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . @prefix dcat: <http://www.w3.org/ns/dcat#> . <http://example.com/catalogs/1> a dcat:Catalog ; dcat:service <http://wwww.digdir.no/.well-known/skolem/21043186-80ce-11eb-9829-cf7c8fc855ce> , <http://wwww.digdir.no/.well-known/skolem/284db4d2-80c2-11eb-82c3-83e80baa2f94> ; . <http://wwww.digdir.no/.well-known/skolem/284db4d2-80c2-11eb-82c3-83e80baa2f94> a dcat:DataService ; dct:title "Dataservice 1"@en, "Dataservice 1"@nb ; . <http://wwww.digdir.no/.well-known/skolem/21043186-80ce-11eb-9829-cf7c8fc855ce> a dcat:DataService ; dct:title "Dataservice 2"@en, "Dataservice 2"@nb ; . """ skolemutils = SkolemUtils() mocker.patch( "skolemizer.Skolemizer.add_skolemization", side_effect=skolemutils.get_skolemization, ) g1 = Graph().parse(data=catalog.to_rdf(), format="turtle") g2 = Graph().parse(data=src, format="turtle") _isomorphic = isomorphic(g1, g2) if not _isomorphic: _dump_diff(g1, g2) pass assert _isomorphic
async def test_validator_url(http_service: Any) -> None: """Should return OK and successful validation.""" url = f"{http_service}/validator" data_graph_url = "https://raw.githubusercontent.com/Informasjonsforvaltning/dcat-ap-no-validator-service/main/tests/files/valid_catalog.ttl" # noqa: B950 shapes_graph_file = "tests/files/mock_dcat-ap-no-shacl_shapes_2.00.ttl" ontology_graph_file = "tests/files/ontologies.ttl" with MultipartWriter("mixed") as mpwriter: p = mpwriter.append(data_graph_url) p.set_content_disposition("inline", name="data-graph-url") p = mpwriter.append(open(shapes_graph_file, "rb")) p.set_content_disposition("attachment", name="shapes-graph-file", filename=shapes_graph_file) p = mpwriter.append(open(ontology_graph_file, "rb")) p.set_content_disposition("attachment", name="ontology-graph-file", filename=ontology_graph_file) session = ClientSession() async with session.post(url, data=mpwriter) as resp: body = await resp.text() await session.close() assert resp.status == 200 assert "text/turtle" in resp.headers[hdrs.CONTENT_TYPE] # results_graph (validation report) should be isomorphic to the following: src = """ @prefix sh: <http://www.w3.org/ns/shacl#> . @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . [] a sh:ValidationReport ; sh:conforms true . """ session = ClientSession() async with session.get(data_graph_url) as resp: text = await resp.text() await session.close() g0 = Graph().parse(data=text, format="text/turtle") g1 = g0 + Graph().parse(data=src, format="text/turtle") g2 = Graph().parse(data=body, format="text/turtle") _isomorphic = isomorphic(g1, g2) if not _isomorphic: _dump_diff(g1, g2) pass assert _isomorphic, "results_graph is incorrect"
async def test_validator_with_skos_ap_no(http_service: Any) -> None: """Should return OK and successful validation.""" url = f"{http_service}/validator" data_graph_file = "tests/files/valid_collection.ttl" shapes_graph_file = "tests/files/mock_skos-ap-no-shacl_shapes.ttl" ontology_graph_file = "tests/files/skos_ontologies.ttl" with MultipartWriter("mixed") as mpwriter: p = mpwriter.append(open(data_graph_file, "rb")) p.set_content_disposition("attachment", name="data-graph-file", filename=data_graph_file) p = mpwriter.append(open(shapes_graph_file, "rb")) p.set_content_disposition("attachment", name="shapes-graph-file", filename=shapes_graph_file) p = mpwriter.append(open(ontology_graph_file, "rb")) p.set_content_disposition("attachment", name="ontology-graph-file", filename=ontology_graph_file) session = ClientSession() async with session.post(url, data=mpwriter) as resp: body = await resp.text() await session.close() assert resp.status == 200 assert "text/turtle" in resp.headers[hdrs.CONTENT_TYPE] # results_graph (validation report) should be isomorphic to the following: src = """ @prefix sh: <http://www.w3.org/ns/shacl#> . @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . [] a sh:ValidationReport ; sh:conforms true . """ with open("tests/files/valid_collection.ttl", "r") as file: text = file.read() # body is graph of both the input data and the validation report g0 = Graph().parse(data=text, format="text/turtle") g1 = g0 + Graph().parse(data=src, format="turtle") g2 = Graph().parse(data=body, format="text/turtle") _isomorphic = isomorphic(g1, g2) if not _isomorphic: _dump_diff(g1, g2) pass assert _isomorphic, "results_graph is incorrect"
def test_to_graph_should_return_location_skolemized( mocker: MockFixture) -> None: """It returns a title graph isomorphic to spec.""" location = Location() location.geometry = """POLYGON (( 4.8842353 52.375108 , 4.884276 52.375153 , 4.8842567 52.375159 , 4.883981 52.375254 , 4.8838502 52.375109 , 4.883819 52.375075 , 4.8841037 52.374979 , 4.884143 52.374965 , 4.8842069 52.375035 , 4.884263 52.375016 , 4.8843200 52.374996 , 4.884255 52.374926 , 4.8843289 52.374901 , 4.884451 52.375034 , 4.8842353 52.375108 ))""" src = """ @prefix dct: <http://purl.org/dc/terms/> . @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . @prefix dcat: <http://www.w3.org/ns/dcat#> . @prefix locn: <http://www.w3.org/ns/locn#> . @prefix geosparql: <http://www.opengis.net/ont/geosparql#> . <http://wwww.digdir.no/.well-known/skolem/284db4d2-80c2-11eb-82c3-83e80baa2f94> a dct:Location ; locn:geometry \"\"\"POLYGON (( 4.8842353 52.375108 , 4.884276 52.375153 , 4.8842567 52.375159 , 4.883981 52.375254 , 4.8838502 52.375109 , 4.883819 52.375075 , 4.8841037 52.374979 , 4.884143 52.374965 , 4.8842069 52.375035 , 4.884263 52.375016 , 4.8843200 52.374996 , 4.884255 52.374926 , 4.8843289 52.374901 , 4.884451 52.375034 , 4.8842353 52.375108 ))\"\"\"^^geosparql:asWKT ; . """ mocker.patch( "skolemizer.Skolemizer.add_skolemization", return_value=skolemization, ) g1 = Graph().parse(data=location.to_rdf(), format="turtle") g2 = Graph().parse(data=src, format="turtle") _isomorphic = isomorphic(g1, g2) if not _isomorphic: _dump_diff(g1, g2) pass assert _isomorphic
def test_to_graph_should_return_model_as_graph() -> None: """It returns a model graph isomorphic to spec.""" catalog = Catalog() catalog.identifier = "http://example.com/catalogs/1" # Creating a class as a placeholder for real InformationModel class InformationModel = type( "InformationModel", (object, ), { "title": "", "identifier": "", "_to_graph": lambda self: model_to_graph(self), }, ) model_1 = InformationModel() model_1.identifier = "http://example.com/models/1" # type: ignore model_1.title = {"en": "My first model"} # type: ignore catalog.models.append(model_1) model_2 = InformationModel() model_2.identifier = "http://example.com/models/2" # type: ignore model_2.title = {"en": "My second model"} # type: ignore catalog.models.append(model_2) src = """ @prefix dct: <http://purl.org/dc/terms/> . @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . @prefix dcat: <http://www.w3.org/ns/dcat#> . @prefix modelldcatno: <https://data.norge.no/vocabulary/modelldcatno#> . <http://example.com/catalogs/1> a dcat:Catalog ; modelldcatno:model <http://example.com/models/1> , <http://example.com/models/2> ; . <http://example.com/models/1> a modelldcatno:InformationModel ; dct:title "My first model"@en ; . <http://example.com/models/2> a modelldcatno:InformationModel ; dct:title "My second model"@en ; . """ g1 = Graph().parse(data=catalog.to_rdf(), format="turtle") g2 = Graph().parse(data=src, format="turtle") _isomorphic = isomorphic(g1, g2) if not _isomorphic: _dump_diff(g1, g2) pass assert _isomorphic
def trig(test): g = ConjunctiveGraph() try: base = "http://www.w3.org/2013/TriGTests/" + split_uri(test.action)[1] g.parse(test.action, publicID=base, format="trig") if not test.syntax: raise AssertionError("Input shouldn't have parsed!") if test.result: # eval test res = ConjunctiveGraph() res.parse(test.result, format="nquads") if verbose: both, first, second = graph_diff(g, res) if not first and not second: return print("===============================") print("TriG") print(g.serialize(format="nquads")) print("===============================") print("NQuads") print(res.serialize(format="nquads")) print("===============================") print("Diff:") # print "%d triples in both"%len(both) print("TriG Only:") for t in first: print(t) print("--------------------") print("NQuads Only") for t in second: print(t) raise Exception("Graphs do not match!") assert isomorphic( g, res ), "graphs must be the same, expected\n%s\n, got\n%s" % ( g.serialize(), res.serialize(), ) except: if test.syntax: raise
def test_add_one_variable_multiple_times(self): self.e.bind(V("x"), PA, [InvIRI(FOAF.member)]) self.e.add(G([ (V("x"), FOAF.homepage, IRI("http://www.univ-lyon1.fr/")), (PA , FOAF.knows, IRI("http://doe.org/#john")), (IRI("http://doe.org/#john"), VOCAB.memberOf, V("x")), ])) exp = G(INITIAL + """ _:ucbl f:homepage <http://www.univ-lyon1.fr/> . <http://champin.net/#pa> f:knows <http://doe.org/#john> . <http://doe.org/#john> v:memberOf _:ucbl . """) got = self.g assert isomorphic(got, exp), got.serialize(format="turtle")
def test_parse(self): """Test the parse method of the YAML parser.""" self.graph.parse(CUBA_FILE, format="ttl") self.parser = YmlParser(self.graph) pre = set(self.graph) self.parser.parse(YML_FILE) test_graph1 = rdflib.Graph() test_graph1.parse(RDF_FILE, format="ttl") test_graph2 = rdflib.Graph() for triple in set(self.parser.graph) - pre: test_graph2.add(triple) self.assertTrue(isomorphic(test_graph1, test_graph2)) self.assertEqual(self.parser._file_path, YML_FILE) self.assertEqual(self.parser._namespace, "parser_test")
def assert_isomorphic(g1: Graph, g2: Graph) -> None: """Compares two graphs an asserts that they are isomorphic. If not isomorpic a graph diff will be dumped. Args: g1 (Graph): a graph to compare g2 (Graph): the graph to compare with """ _isomorphic = isomorphic(g1, g2) if not _isomorphic: _dump_diff(g1, g2) assert _isomorphic
def test_two_spurious_semicolons_no_period(): sparql = """ PREFIX : <http://example.org/> CONSTRUCT { :a :b :c ; :d :e ; ; } WHERE {} """ expected = Graph() expected.addN(t + (expected, ) for t in [ (NS.a, NS.b, NS.c), (NS.a, NS.d, NS.e), ]) got = Graph().query(sparql).graph assert isomorphic(got, expected), got.serialize(format="turtle")
def test_two_spurious_semicolons_no_period(): sparql = """ PREFIX : <http://example.org/> CONSTRUCT { :a :b :c ; :d :e ; ; } WHERE {} """ expected = Graph() expected.addN(t + (expected,) for t in [ (NS.a, NS.b, NS.c), (NS.a, NS.d, NS.e), ]) got = Graph().query(sparql).graph assert isomorphic(got, expected), got.serialize(format="turtle")
def test_one_spurious_semicolons_bnode(): sparql = """ PREFIX : <http://example.org/> CONSTRUCT { [ :b :c ; :d :e ; ] } WHERE {} """ expected = Graph() expected.addN(t + (expected,) for t in [ (BNode("a"), NS.b, NS.c), (BNode("a"), NS.d, NS.e), ]) got = Graph().query(sparql).graph assert isomorphic(got, expected), got.serialize(format="turtle")
def test_loadGraph(): defaultGraph = 'http://www.xyz.abc/test_loadGraph' ep = BrickEndpoint('http://localhost:8890/sparql', '1.0.3', defaultGraph) ep.dropGraph(defaultGraph, force=True) g = Graph() g.parse('tests/data/sample_graph.ttl', format='turtle') ep.loadGraph(g, defaultGraph) resultG = ep.queryGraph(defaultGraph, verbose=True) assert compare.isomorphic(g, resultG), 'loaded graph and query result not match' ep.dropGraph(defaultGraph, force=True)
def test_to_graph_should_return_included_distribution_as_graph() -> None: """It returns a dataset graph isomorphic to spec.""" dataset = Dataset() dataset.identifier = "http://example.com/datasets/1" distribution1 = Distribution() distribution1.identifier = "http://example.com/distributions/1" distribution1.title = { "nb": "API-distribusjon 1", "en": "API-distribution 1" } dataset.distributions.append(distribution1) distribution2 = Distribution() distribution2.identifier = "http://example.com/distributions/2" distribution2.title = { "nb": "API-distribusjon 2", "en": "API-distribution 2" } dataset.distributions.append(distribution2) src = """ @prefix dct: <http://purl.org/dc/terms/> . @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . @prefix dcat: <http://www.w3.org/ns/dcat#> . @prefix prov: <http://www.w3.org/ns/prov#> . <http://example.com/datasets/1> a dcat:Dataset ; dcat:distribution <http://example.com/distributions/1>, <http://example.com/distributions/2> . <http://example.com/distributions/1> a dcat:Distribution ; dct:title "API-distribution 1"@en, "API-distribusjon 1"@nb ; . <http://example.com/distributions/2> a dcat:Distribution ; dct:title "API-distribution 2"@en, "API-distribusjon 2"@nb ; . """ g1 = Graph().parse(data=dataset.to_rdf(), format="turtle") g2 = Graph().parse(data=src, format="turtle") _isomorphic = isomorphic(g1, g2) if not _isomorphic: _dump_diff(g1, g2) pass assert _isomorphic
def test_citation_data_ttl(self): g1 = ConjunctiveGraph() g1.load(self.citation_data_ttl_path, format="nt11") g2 = ConjunctiveGraph() for c in [ self.citation_1, self.citation_2, self.citation_3, self.citation_4, self.citation_5, self.citation_6 ]: for s, p, o in c.get_citation_rdf(self.base_url, False, False, False): g2.add((s, p, o)) self.assertTrue(isomorphic(g1, g2))
def test_citation_prov_ttl(self): g1 = ConjunctiveGraph() g1.load(self.citation_prov_ttl_path, format="nquads") g2 = ConjunctiveGraph() for c in [ self.citation_1, self.citation_2, self.citation_3, self.citation_4, self.citation_5, self.citation_6 ]: for s, p, o, g in c.get_citation_prov_rdf(self.base_url).quads( (None, None, None, None)): g2.add((s, p, o, g)) self.assertTrue(isomorphic(g1, g2))
def test_one_spurious_semicolons_bnode(): sparql = """ PREFIX : <http://example.org/> CONSTRUCT { [ :b :c ; :d :e ; ] } WHERE {} """ expected = Graph() expected.addN(t + (expected, ) for t in [ (BNode("a"), NS.b, NS.c), (BNode("a"), NS.d, NS.e), ]) got = Graph().query(sparql).graph assert isomorphic(got, expected), got.serialize(format="turtle")
def citations_rdf(self, origin_citation_list, stored_citation_list): g1 = ConjunctiveGraph() g2 = ConjunctiveGraph() for idx, cit in enumerate(origin_citation_list): for s, p, o, g in cit.get_citation_rdf(self.baseurl, False, False, True).quads((None, None, None, None)): g1.add((s, p, o, g)) for s, p, o, g in stored_citation_list[idx].get_citation_rdf( self.baseurl, False, False, True).quads( (None, None, None, None)): g2.add((s, p, o, g)) self.assertTrue(isomorphic(g1, g2))
def test_add_two_variables_multiple_times(self): self.e.bind(V("x"), PA, [InvIRI(FOAF.member)]) self.e.bind(V("y"), Literal("Alain Mille"), [InvIRI(FOAF.name)]) self.e.add(G([ (V("x"), FOAF.homepage, IRI("http://www.univ-lyon1.fr/")), (PA , FOAF.knows, V("y")), (V("y"), VOCAB.memberOf, V("x")), ])) exp = G(INITIAL + """ _:ucbl f:homepage <http://www.univ-lyon1.fr/> . <http://champin.net/#pa> f:knows _:am . _:am v:memberOf _:ucbl . """) got = self.g assert isomorphic(got, exp), got.serialize(format="turtle")
def verify_ask(sparql, graph, expected): try: result = graph.query(sparql.decode('utf-8')) ok = result.serialize('python') == expected except: # TODO: parse failures are probably sparql processor bugs ok = False if ok: return ok # TODO: sparql bugs cause a bunch to fail (at least bnodes and xmlliterals) # .. extract N3 from ASK and compare graphs instead: from rdflib.compare import isomorphic for ask_graph in _sparql_to_graphs(sparql): if isomorphic(graph, ask_graph) == expected: return True #else: print ask_graph.serialize(format='nt') return False
def test_bounded_description(): g1 = Graph() g1.parse( StringIO( """@prefix : <http://example.org/> . :node :p1 :succ1 ; :p2 42 ; :p3 (101 :succ2 [ :p3 "blank list item" ] ) ; . :succ1 :p4 :out1 . :succ2 :p5 :out2 . :pred1 :p6 :node . :pred2 :p7 [ :p8 :node ] ; :p8 [ :p9 "out3" ] ; :p9 :out4 . [ :pA :pred3 ] :pB :node . :out5 :pC :pred3 . :pred1 :pD :succ1 . """ ), format="n3", ) gref = Graph() gref.parse( StringIO( """@prefix : <http://example.org/> . :node :p1 :succ1 ; :p2 42 ; :p3 (101 :succ2 [ :p3 "blank list item" ] ) ; . :pred1 :p6 :node . :pred2 :p7 [ :p8 :node ] . [ :pA :pred3 ] :pB :node . """ ), format="n3", ) gbd = bounded_description(URIRef("http://example.org/node"), g1) assert isomorphic(gref, gbd)