# HG changeset patch
# User drewp@bigasterisk.com
# Date 1631902012 25200
# Node ID af136cf6dd26bb0afdee3cdc334f7d3599a2151b
# Parent d3b295c28a2690d8344bf087e4b5ae144a9532a7
make namespaces in tests less confusing
diff -r d3b295c28a26 -r af136cf6dd26 service/mqtt_to_rdf/inference_test.py
--- a/service/mqtt_to_rdf/inference_test.py Fri Sep 17 11:05:21 2021 -0700
+++ b/service/mqtt_to_rdf/inference_test.py Fri Sep 17 11:06:52 2021 -0700
@@ -23,7 +23,8 @@
def N3(txt: str):
g = ConjunctiveGraph()
prefix = """
-@prefix : .
+@prefix : .
+@prefix ex: .
@prefix room: .
@prefix math: .
"""
@@ -67,6 +68,13 @@
self.assertGraphEqual(implied, N3(":a :b :new1, :new2 ."))
+class TestNonRuleStatements(WithGraphEqual):
+
+ def test(self):
+ inf = makeInferenceWithRules(":d :e :f . { :a :b :c . } => { :a :b :new . } .")
+ self.assertCountEqual(inf.nonRuleStatements(), [(ROOM.d, ROOM.e, ROOM.f)])
+
+
class TestInferenceWithVars(WithGraphEqual):
def testVarInSubject(self):
@@ -137,19 +145,20 @@
implied = inf.infer(N3("[] :a :b ."))
self.assertGraphEqual(implied, N3(":new :stmt :here ."))
+
class TestBnodeGenerating(WithGraphEqual):
def testRuleBnodeMakesNewBnode(self):
inf = makeInferenceWithRules("{ [ :a :b ] . } => { [ :c :d ] } .")
implied = inf.infer(N3("[ :a :b ] ."))
- ruleNode = list(inf.rules[0].rhsGraph)[0]
+ ruleNode = list(inf.rules[0].rhsGraph)[0]
stmt0Node = list(implied)[0][0]
self.assertNotEqual(ruleNode, stmt0Node)
def testRuleBnodeMakesNewBnodesEachTime(self):
inf = makeInferenceWithRules("{ [ :a ?x ] . } => { [ :c :d ] } .")
implied = inf.infer(N3("[ :a :b, :e ] ."))
- ruleNode = list(inf.rules[0].rhsGraph)[0]
+ ruleNode = list(inf.rules[0].rhsGraph)[0]
stmt0Node = list(implied)[0][0]
stmt1Node = list(implied)[1][0]
@@ -169,10 +178,11 @@
inf = makeInferenceWithRules("{ (2) math:sum ?x } => { :new :stmt ?x } .")
self.assertGraphEqual(inf.infer(N3("")), N3(":new :stmt 2 ."))
+
# @unittest.skip("too hard for now")
- # def test3(self):
- # inf = makeInferenceWithRules("{ :a :b :c . :a :b ?x . } => { :new :stmt ?x } .")
- # self.assertGraphEqual(inf.infer(N3("")), N3(":new :stmt :c ."))
+# def test3(self):
+# inf = makeInferenceWithRules("{ :a :b :c . :a :b ?x . } => { :new :stmt ?x } .")
+# self.assertGraphEqual(inf.infer(N3("")), N3(":new :stmt :c ."))
class TestInferenceWithMathFunctions(WithGraphEqual):
@@ -227,7 +237,7 @@
''')
out = inf.infer(N3('[] a :MqttMessage ; :body "online" ; :topic :foo .'))
- self.assertIn((EX['frontDoorLockStatus'], EX['connectedStatus'], EX['Online']), out)
+ self.assertIn((ROOM['frontDoorLockStatus'], ROOM['connectedStatus'], ROOM['Online']), out)
def testTopicIsList(self):
inf = makeInferenceWithRules('''
@@ -243,7 +253,7 @@
''')
out = inf.infer(N3('[] a :MqttMessage ; :body "online" ; :topic ( "frontdoorlock" "status" ) .'))
- self.assertIn((EX['frontDoorLockStatus'], EX['connectedStatus'], EX['Online']), out)
+ self.assertIn((ROOM['frontDoorLockStatus'], ROOM['connectedStatus'], ROOM['Online']), out)
def testPerformance0(self):
inf = makeInferenceWithRules('''
@@ -265,7 +275,7 @@
:topic :topic1 .
'''))
- vlit = cast(Literal, out.value(EX['airQualityIndoorTemperature'], EX['temperatureF']))
+ vlit = cast(Literal, out.value(ROOM['airQualityIndoorTemperature'], ROOM['temperatureF']))
valueF = cast(Decimal, vlit.toPython())
self.assertAlmostEqual(float(valueF), 75.02)
@@ -288,7 +298,7 @@
:bodyFloat 2.39e+01 ;
:topic ( "air_quality_indoor" "sensor" "bme280_temperature" "state" ) .
'''))
- vlit = cast(Literal, out.value(EX['airQualityIndoorTemperature'], EX['temperatureF']))
+ vlit = cast(Literal, out.value(ROOM['airQualityIndoorTemperature'], ROOM['temperatureF']))
valueF = cast(Decimal, vlit.toPython())
self.assertAlmostEqual(float(valueF), 75.02)
@@ -304,15 +314,17 @@
'''))
out.bind('', ROOM)
out.bind('ex', EX)
- self.assertEqual(out.serialize(format='n3'), b'''@prefix : .
+ self.assertEqual(
+ out.serialize(format='n3'), b'''\
+@prefix : .
@prefix ex: .
@prefix rdf: .
@prefix rdfs: .
@prefix xml: .
@prefix xsd: .
-[] a ex:MqttStatementSource ;
- ex:mqttTopic ( "air_quality_outdoor" "sensor" "bme280_temperature" "state" ) .
+[] a :MqttStatementSource ;
+ :mqttTopic ( "air_quality_outdoor" "sensor" "bme280_temperature" "state" ) .
''')
@@ -343,7 +355,6 @@
# def fakeStats():
# return defaultdict(lambda: 0)
-
# class TestLhsFindCandidateBindings(WithGraphEqual):
# def testBnodeMatchesStmt(self):