Beispiel #1
0
 def test_serialize(self):
     # Issue #1 - serialize goes after the entire graph
     WD = Namespace("http://www.wikidata.org/entity/")
     g = SlurpyGraph(endpoint, agent=UserAgent)
     _ = list(g.predicate_objects(WD.Q29017194))
     g.debug_slurps = True
     # Warning - this will go away forever if the bug isn't fixed
     serialized_graph = g.serialize(format="turtle").decode()
     g2 = Graph()
     g2.parse(data=serialized_graph, format="turtle")
     self.assertEqual(len(list(g)), len(list(g2)))
Beispiel #2
0
 def test_debug_slurps(self):
     WD = Namespace("http://www.wikidata.org/entity/")
     g = SlurpyGraph(endpoint, agent=UserAgent)
     g.debug_slurps = True
     output = io.StringIO()
     with redirect_stdout(output):
         _ = list(g.predicate_objects(WD.Q29017194))
     if not output.getvalue().startswith("SPARQL: "
                                         "(SELECT ?s ?p ?o {<http://www.wikidata.org/entity/Q29017194> ?p ?o})"):
         print("Unexpected:")
         print(output.getvalue())
         self.assertTrue(False)
Beispiel #3
0
def SlurpyGraphWithAgent(endpoint: str,
                         *args,
                         persistent_bnodes: bool = False,
                         agent: Optional[str] = None,
                         gdb_slurper: Optional[bool] = False,
                         **kwargs) -> SlurpyGraph:
    rval = GraphDBSlurpyGraph(endpoint, *args, persistent_bnodes=persistent_bnodes, **kwargs) if gdb_slurper else \
        SlurpyGraph(endpoint, *args, persistent_bnodes=persistent_bnodes, **kwargs)
    rval.sparql.agent = agent if agent else UserAgent
    return rval
 def test_persistent_bnode(self):
     """ Test the persistent_bnode argument """
     g = SlurpyGraph("http://sparql-playground.nextprot.org/sparql")
     with self.assertRaises(ValueError):
         _ = list(g.objects(NEXTPROT['NATUR-NX_PEPT00119967-NX_Q9Y5X1-1'], NEXTPROT.evidence))
     g.persistent_bnodes = True
     evidence = g.value(NEXTPROT['NATUR-NX_PEPT00119967-NX_Q9Y5X1-1'], NEXTPROT.evidence)
     self.assertTrue(isinstance(evidence, BNode))
     peptide_name = g.value(evidence, NEXTPROT.peptideName)
     if peptide_name != "NX_PEPT00119967":
         print(f"Warning: {g.sparql.endpoint} does not support persistent BNodes!")
Beispiel #5
0
 def test_reactome(self):
     WD = Namespace("http://www.wikidata.org/entity/")
     P = Namespace("http://www.wikidata.org/prop/")
     g = SlurpyGraph(endpoint, agent=UserAgent)
     g.debug_slurps = False
     gpo = list(g.predicate_objects(WD.Q29017194))
     orig_len = len(gpo)
     # Test that we get something
     self.assertTrue(30 < orig_len < 60)                 # Current value is 33
     # Test that it is reproducable
     self.assertEqual(orig_len, len(list(g.predicate_objects((WD.Q29017194)))))
     # Test that it is cached
     self.assertEqual(orig_len, len(g))
     # Add a path
     added_length = 0
     for s in g.objects(WD.Q29017194, P.P31):
         gp31s = list(g.predicate_objects(s))
         added_length += len(gp31s)
     self.assertEqual(orig_len + added_length, len(g))
     print(f"{g.total_calls}, {g.total_queries}, {g.total_slurptime}, {g.total_triples}")
     self.assertEqual((5, 3, 41), (g.total_calls, g.total_queries, g.total_triples))
     self.assertTrue(g.total_slurptime > 0.1)
Beispiel #6
0
    def test_already_resolved(self):
        g = SlurpyGraph("endpoint")
        EX = Namespace("http://example.org/")
        g.resolved_nodes.append((EX.s1, EX.p1, EX.o1))
        g.resolved_nodes.append((EX.s1, EX.p1, Literal("Steam")))
        g.resolved_nodes.append((EX.s1, EX.p1, Literal(117)))
        g.resolved_nodes.append((EX.s2, EX.p2, None))
        g.resolved_nodes.append((EX.s3, None, EX.o3))
        g.resolved_nodes.append((EX.s3, None, Literal("Gas")))
        g.resolved_nodes.append((EX.s3, None, Literal(-42)))
        g.resolved_nodes.append((EX.s4, None, None))
        g.resolved_nodes.append((None, EX.p5, EX.o5))
        g.resolved_nodes.append((None, EX.p5, Literal("Coal")))
        g.resolved_nodes.append((None, EX.p5, Literal(11.2)))
        g.resolved_nodes.append((None, EX.p6, None))
        g.resolved_nodes.append((None, None, EX.o7))
        g.resolved_nodes.append((None, None, Literal("Wood")))
        g.resolved_nodes.append((None, None, Literal(3.14159)))
        self.assertTrue(g.already_resolved((None, None, None)))

        self.assertTrue(g.already_resolved((EX.s1, EX.p1, EX.o1)))
        self.assertFalse(g.already_resolved((EX.s1, EX.p1, EX.o2)))
        self.assertTrue(g.already_resolved((EX.s1, EX.p1, Literal("Steam"))))
        self.assertFalse(g.already_resolved((EX.s1, EX.p1, Literal("Gas"))))
        self.assertTrue(g.already_resolved((EX.s1, EX.p1, Literal(117))))
        self.assertFalse(g.already_resolved((EX.s1, EX.p1, Literal(118))))
        self.assertFalse(g.already_resolved((EX.s1, None, Literal(117))))
        self.assertFalse(g.already_resolved((None, EX.p1, Literal(117))))

        self.assertTrue(g.already_resolved((EX.s2, EX.p2, Literal(143))))
        self.assertFalse(g.already_resolved((EX.s2, None, Literal(143))))
        self.assertTrue(g.already_resolved((EX.s4, EX.p4, None)))
        self.assertTrue(g.already_resolved((EX.s4, None, Literal(12))))
        self.assertTrue(g.already_resolved((EX.s4, None, None)))

        self.assertTrue(g.already_resolved((EX.s5, EX.p5, EX.o5)))
        self.assertFalse(g.already_resolved((EX.s5, EX.p5, EX.o5n)))

        self.assertTrue(g.already_resolved((EX.s7, None, EX.o7)))
        self.assertTrue(g.already_resolved((None, EX.p7, EX.o7)))
        self.assertFalse(g.already_resolved((EX.s7, None, None)))
 def do_test(g: SlurpyGraph) -> str:
     output = io.StringIO()
     with redirect_stdout(output):
         _ = list(g.predicate_objects(WD.Q29017194))
     return output.getvalue()
 def setup_test() -> SlurpyGraph:
     g = SlurpyGraph(endpoint, agent=UserAgent)
     g.bind("wd", WD)
     g.bind("wdp", "http://www.wikidata.org/prop/")
     g.bind("wdd", "http://www.wikidata.org/prop/direct/")
     g.bind("sdo", "http://schema.org/")
     g.bind("wds", "http://www.wikidata.org/entity/statement/")
     g.bind("wikiba", "http://wikiba.se/ontology#")
     g.add_result_hook(QueryResultPrinter)
     return g