Skip to content

Commit

Permalink
Add test for more complex payload
Browse files Browse the repository at this point in the history
  • Loading branch information
blavka committed Sep 22, 2021
1 parent eecab92 commit 6e3f6b8
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion tests/test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import unittest
import io
import sys
import json
from paho.mqtt.client import MQTTMessage
from mqtt2influxdb.mqtt2influxdb import Mqtt2InfluxDB
from mqtt2influxdb.config import load_config
Expand All @@ -24,6 +25,13 @@
tags:
id: $.topic[1]
channel: $.topic[3]
- measurement: tempnexus
topic: home/+/PilighttoMQTT/nexus/42/
fields:
value: $.payload.message.temperature
tags:
id: $.topic[1]
'''


Expand Down Expand Up @@ -76,7 +84,7 @@ def setUp(self):

def test_measurement_temperature(self):
self.mqtt_pub('node/push-button:1/thermometer/0:1/temperature', 22.94)
print(self.influxdb.data)
# print(self.influxdb.data)
self.assertEqual(len(self.influxdb.data), 1, msg='Expect 1 call write_points')
self.assertEqual(len(self.influxdb.data[0]['points']), 1, msg='Expect 1 points')
self.assertEqual(self.influxdb.data[0]['database'], 'node')
Expand All @@ -86,6 +94,20 @@ def test_measurement_temperature(self):
self.assertDictEqual(point['tags'], {'id': 'push-button:1', 'channel': '0:1'})
self.assertTrue('time' in point)

def test_measurement_tempnexus(self):
message = {"message": {"id": 42, "channel": 1, "battery": 1, "temperature": 12.9, "humidity": 0},
"protocol": "nexus", "length": "42", "value": "42", "repeats": 2, "status": 2}
self.mqtt_pub('home/test/PilighttoMQTT/nexus/42/', json.dumps(message))
# print(self.influxdb.data)
self.assertEqual(len(self.influxdb.data), 1, msg='Expect 1 call write_points')
self.assertEqual(len(self.influxdb.data[0]['points']), 1, msg='Expect 1 points')
self.assertEqual(self.influxdb.data[0]['database'], 'node')
point = self.influxdb.data[0]['points'][0]
self.assertEqual(point['measurement'], 'tempnexus')
self.assertEqual(point['fields']['value'], 12.9)
self.assertDictEqual(point['tags'], {'id': 'test'})
self.assertTrue('time' in point)


if __name__ == '__main__':
unittest.main()

0 comments on commit 6e3f6b8

Please sign in to comment.