comparison 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
comparison
equal deleted inserted replaced
1608:f928eb06a4f6 1609:34f2817320cc
1 """ 1 """
2 also see https://github.com/w3c/N3/tree/master/tests/N3Tests 2 also see https://github.com/w3c/N3/tree/master/tests/N3Tests
3 """ 3 """
4 from collections import defaultdict
4 from decimal import Decimal 5 from decimal import Decimal
5 from typing import cast 6 from typing import cast
6 import unittest 7 import unittest
7 8
8 from rdflib import RDF, BNode, ConjunctiveGraph, Graph, Literal, Namespace 9 from rdflib import RDF, BNode, ConjunctiveGraph, Graph, Literal, Namespace
10 from rdflib.graph import ReadOnlyGraphAggregate
9 from rdflib.parser import StringInputSource 11 from rdflib.parser import StringInputSource
10 12
11 from inference import Inference 13 from inference import Inference, Lhs
12 from rdflib_debug_patches import patchBnodeCounter, patchSlimReprs 14 from rdflib_debug_patches import patchBnodeCounter, patchSlimReprs
13 15
14 patchSlimReprs() 16 patchSlimReprs()
15 patchBnodeCounter() 17 patchBnodeCounter()
16 18
281 283
282 # def testList4(self): 284 # def testList4(self):
283 # inf = makeInferenceWithRules("{ :a :b (:e0 :e1 :e2 :e3) . } => { :new :stmt :here } .") 285 # inf = makeInferenceWithRules("{ :a :b (:e0 :e1 :e2 :e3) . } => { :new :stmt :here } .")
284 # implied = inf.infer(N3(":a :b (:e0 :e1 :e2 :e3) .")) 286 # implied = inf.infer(N3(":a :b (:e0 :e1 :e2 :e3) ."))
285 # self.assertGraphEqual(implied, N3(":new :stmt :here .")) 287 # self.assertGraphEqual(implied, N3(":new :stmt :here ."))
288
289
290 def fakeStats():
291 return defaultdict(lambda: 0)
292
293 class TestLhsFindCandidateBindings(WithGraphEqual):
294
295 def testBnodeMatchesStmt(self):
296 l = Lhs(N3("[] :a :b ."))
297 ws = ReadOnlyGraphAggregate([N3("[] :a :b .")])
298 cands = list(l.findCandidateBindings(ws, fakeStats()))
299 self.assertEqual(len(cands), 1)
300
301 def testVarMatchesStmt(self):
302 l = Lhs(N3("?x :a :b ."))
303 ws = ReadOnlyGraphAggregate([N3("[] :a :b .")])
304 cands = list(l.findCandidateBindings(ws, fakeStats()))
305 self.assertEqual(len(cands), 1)
306
307 def testListsOnlyMatchEachOther(self):
308 l = Lhs(N3(":a :b (:e0 :e1) ."))
309 ws = ReadOnlyGraphAggregate([N3(":a :b (:e0 :e1) .")])
310 stats = fakeStats()
311 cands = list(l.findCandidateBindings(ws, stats))
312 self.assertLess(stats['permCountFailingVerify'], 20)
313 self.fail(str(cands))