aboutsummaryrefslogtreecommitdiff
path: root/rrd/tests/test_mqtt.py
blob: 68a168eefead5aa81824ed1d95d3f623eb720bcd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import os
import time

import django.test
from django.conf import settings

from .. import models, mqtt


class TestMQTT(django.test.TransactionTestCase):
    def setUp(self):
        self.mqtt = mqtt.MQTTClient()
        self.mqtt.loop_start()
        time.sleep(0.1)
        if not self.mqtt.connected:
            self.skipTest("Could not find an mqtt server")
        self.files = []

    def tearDown(self):
        self.mqtt.loop_stop()
        for path in self.files:
            os.remove(path)

    def test_disconnect(self):
        # after disconnecting from the mqtt server, we should
        # automatically reconnect
        self.mqtt.disconnect()
        time.sleep(2)
        self.assertTrue(self.mqtt.connected)

    def test_disconnect_and_stay(self):
        # unless we really want to force a disconnection
        self.mqtt.disconnect(reconnect=False)
        time.sleep(2)
        self.assertFalse(self.mqtt.connected)
        self.assertFalse(self.mqtt.reconnect)

    def test_receive_data(self):
        os.makedirs(settings.RRD_DB_PATH, exist_ok=True)
        self.files.append(os.path.join(settings.RRD_DB_PATH, "test.rrd"))
        self.mqtt.client.publish(
            topic=settings.MQTT_TOPIC + "test",
            payload="10",
        )
        time.sleep(1)
        ds = models.DataSource.objects.get(topic="test")
        self.assertEqual(ds.lastupdate[1], 10)

    def test_receive_data_non_existing_ds(self):
        pass

    def test_receive_invalid_data(self):
        pass