From 6e3f6b898144ce89307e61858364d005f8f6a35d Mon Sep 17 00:00:00 2001 From: Karel Blavka Date: Wed, 22 Sep 2021 12:02:59 +0200 Subject: [PATCH] Add test for more complex payload --- tests/test.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/tests/test.py b/tests/test.py index 1465368..2176f80 100644 --- a/tests/test.py +++ b/tests/test.py @@ -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 @@ -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] ''' @@ -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') @@ -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()