Mercurial > code > home > repos > homeauto
annotate service/mqtt_to_rdf/stmt_chunk_test.py @ 1672:23beadd3e83a
split a test
author | drewp@bigasterisk.com |
---|---|
date | Tue, 21 Sep 2021 23:21:59 -0700 |
parents | 6fa7ddee9ba8 |
children | e6d28e6d47b2 |
rev | line source |
---|---|
1655 | 1 import unittest |
2 | |
1670 | 3 from rdflib import Namespace, Variable |
1655 | 4 |
1670 | 5 from candidate_binding import CandidateBinding |
1655 | 6 from inference_test import N3 |
1670 | 7 from lhs_evaluation import functionsFor |
8 from stmt_chunk import AlignedRuleChunk, Chunk, ChunkedGraph, applyChunky | |
1655 | 9 |
10 ROOM = Namespace('http://projects.bigasterisk.com/room/') | |
11 | |
12 | |
13 class TestChunkedGraph(unittest.TestCase): | |
14 | |
15 def testMakesSimpleChunks(self): | |
16 cg = ChunkedGraph(N3(':a :b :c .'), functionsFor) | |
17 | |
18 self.assertSetEqual(cg.chunksUsedByFuncs, set()) | |
19 self.assertSetEqual(cg.patternChunks, set()) | |
20 self.assertSetEqual(cg.staticChunks, set([Chunk((ROOM.a, ROOM.b, ROOM.c), subjList=None, objList=None)])) | |
21 | |
22 def testSeparatesPatternChunks(self): | |
23 cg = ChunkedGraph(N3('?x :b :c . :a ?y :c . :a :b ?z .'), functionsFor) | |
24 self.assertEqual(len(cg.patternChunks), 3) | |
25 | |
26 def testBoolMeansEmpty(self): | |
27 self.assertTrue(ChunkedGraph(N3(":a :b :c ."), functionsFor)) | |
28 self.assertFalse(ChunkedGraph(N3(""), functionsFor)) | |
29 | |
30 def testContains(self): | |
31 # If I write with assertIn, there's a seemingly bogus pytype error. | |
32 self.assert_(Chunk((ROOM.a, ROOM.b, ROOM.c)) in ChunkedGraph(N3(":a :b :c ."), functionsFor)) | |
33 self.assert_(Chunk((ROOM.a, ROOM.b, ROOM.zzz)) not in ChunkedGraph(N3(":a :b :c ."), functionsFor)) | |
34 | |
35 def testNoPredicatesAppear(self): | |
36 cg = ChunkedGraph(N3(":a :b :c ."), functionsFor) | |
37 self.assertTrue(cg.noPredicatesAppear([ROOM.d, ROOM.e])) | |
38 self.assertFalse(cg.noPredicatesAppear([ROOM.b, ROOM.d])) | |
39 | |
40 | |
1659
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
41 class TestListCollection(unittest.TestCase): |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
42 |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
43 def testSubjList(self): |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
44 cg = ChunkedGraph(N3('(:u :v) :b :c .'), functionsFor) |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
45 expected = Chunk((None, ROOM.b, ROOM.c), subjList=[ROOM.u, ROOM.v]) |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
46 self.assertEqual(cg.staticChunks, set([expected])) |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
47 |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
48 def testObjList(self): |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
49 cg = ChunkedGraph(N3(':a :b (:u :v) .'), functionsFor) |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
50 expected = Chunk((ROOM.a, ROOM.b, None), objList=[ROOM.u, ROOM.v]) |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
51 self.assertSetEqual(cg.staticChunks, set([expected])) |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
52 |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
53 def testVariableInListMakesAPatternChunk(self): |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
54 cg = ChunkedGraph(N3(':a :b (?x :v) .'), functionsFor) |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
55 expected = Chunk((ROOM.a, ROOM.b, None), objList=[Variable('x'), ROOM.v]) |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
56 self.assertSetEqual(cg.patternChunks, set([expected])) |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
57 |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
58 def testListUsedTwice(self): |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
59 cg = ChunkedGraph(N3('(:u :v) :b :c, :d .'), functionsFor) |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
60 |
1661 | 61 self.assertSetEqual( |
62 cg.staticChunks, | |
63 set([ | |
64 Chunk((None, ROOM.b, ROOM.c), subjList=[ROOM.u, ROOM.v]), | |
65 Chunk((None, ROOM.b, ROOM.d), subjList=[ROOM.u, ROOM.v]) | |
66 ])) | |
1659
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
67 |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
68 def testUnusedListFragment(self): |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
69 cg = ChunkedGraph(N3(':a rdf:first :b .'), functionsFor) |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
70 self.assertFalse(cg) |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
71 |
1661 | 72 |
1655 | 73 class TestApplyChunky(unittest.TestCase): |
74 binding = CandidateBinding({Variable('x'): ROOM.xval}) | |
75 | |
1670 | 76 def testAllStatements(self): |
77 rule0 = Chunk((ROOM.a, Variable('pred'), Variable('x'))) | |
78 rule1 = Chunk((ROOM.a, Variable('pred'), Variable('x'))) | |
1655 | 79 ret = list( |
80 applyChunky(self.binding, | |
1670 | 81 g=[ |
82 AlignedRuleChunk(ruleChunk=rule0, workingSetChunk=Chunk((ROOM.a, ROOM.b, ROOM.xval))), | |
83 AlignedRuleChunk(ruleChunk=rule1, workingSetChunk=Chunk((ROOM.a, ROOM.b, ROOM.yval))), | |
84 ])) | |
1655 | 85 self.assertCountEqual( |
86 ret, | |
87 [ | |
1670 | 88 AlignedRuleChunk(ruleChunk=Chunk((ROOM.a, Variable('pred'), ROOM.xval)), workingSetChunk=Chunk((ROOM.a, ROOM.b, ROOM.xval))) |
1655 | 89 ]) |