Mercurial > code > home > repos > homeauto
diff service/mqtt_to_rdf/inference_test.py @ 1609:34f2817320cc
new tests for a smaller part of the inner loop
author | drewp@bigasterisk.com |
---|---|
date | Mon, 06 Sep 2021 18:06:11 -0700 |
parents | 6cf39d43fd40 |
children | 272f78d4671a |
line wrap: on
line diff
--- a/service/mqtt_to_rdf/inference_test.py Mon Sep 06 17:03:19 2021 -0700 +++ b/service/mqtt_to_rdf/inference_test.py Mon Sep 06 18:06:11 2021 -0700 @@ -1,14 +1,16 @@ """ also see https://github.com/w3c/N3/tree/master/tests/N3Tests """ +from collections import defaultdict from decimal import Decimal from typing import cast import unittest from rdflib import RDF, BNode, ConjunctiveGraph, Graph, Literal, Namespace +from rdflib.graph import ReadOnlyGraphAggregate from rdflib.parser import StringInputSource -from inference import Inference +from inference import Inference, Lhs from rdflib_debug_patches import patchBnodeCounter, patchSlimReprs patchSlimReprs() @@ -283,3 +285,29 @@ # inf = makeInferenceWithRules("{ :a :b (:e0 :e1 :e2 :e3) . } => { :new :stmt :here } .") # implied = inf.infer(N3(":a :b (:e0 :e1 :e2 :e3) .")) # self.assertGraphEqual(implied, N3(":new :stmt :here .")) + + +def fakeStats(): + return defaultdict(lambda: 0) + +class TestLhsFindCandidateBindings(WithGraphEqual): + + def testBnodeMatchesStmt(self): + l = Lhs(N3("[] :a :b .")) + ws = ReadOnlyGraphAggregate([N3("[] :a :b .")]) + cands = list(l.findCandidateBindings(ws, fakeStats())) + self.assertEqual(len(cands), 1) + + def testVarMatchesStmt(self): + l = Lhs(N3("?x :a :b .")) + ws = ReadOnlyGraphAggregate([N3("[] :a :b .")]) + cands = list(l.findCandidateBindings(ws, fakeStats())) + self.assertEqual(len(cands), 1) + + def testListsOnlyMatchEachOther(self): + l = Lhs(N3(":a :b (:e0 :e1) .")) + ws = ReadOnlyGraphAggregate([N3(":a :b (:e0 :e1) .")]) + stats = fakeStats() + cands = list(l.findCandidateBindings(ws, stats)) + self.assertLess(stats['permCountFailingVerify'], 20) + self.fail(str(cands)) \ No newline at end of file