diff options
Diffstat (limited to 'rrd/tests')
-rw-r--r-- | rrd/tests/test_datasources.py | 52 | ||||
-rw-r--r-- | rrd/tests/test_django.py | 5 | ||||
-rw-r--r-- | rrd/tests/test_mqtt.py | 24 |
3 files changed, 75 insertions, 6 deletions
diff --git a/rrd/tests/test_datasources.py b/rrd/tests/test_datasources.py new file mode 100644 index 0000000..4057b14 --- /dev/null +++ b/rrd/tests/test_datasources.py @@ -0,0 +1,52 @@ +import datetime +import os + +from django.conf import settings +from django.test import TestCase + +from .. import models + + +class TestDataSource(TestCase): + def setUp(self): + self.rrd_file = "test.rrd" + try: + os.remove(os.path.join(settings.RRD_DB_PATH, self.rrd_file)) + except FileNotFoundError: + pass + + def tearDown(self): + try: + os.remove(os.path.join(settings.RRD_DB_PATH, self.rrd_file)) + except FileNotFoundError: + pass + + def test_create_with_defaults(self): + os.makedirs(settings.RRD_DB_PATH, exist_ok=True) + ds = models.DataSource.objects.create( + topic="test", + path=self.rrd_file, + ) + self.assertTrue(ds.active) + self.assertIn("RRA:AVERAGE:0.5:1:2016", ds.rrd_config) + + def test_read_empty_ds(self): + ds = models.DataSource.objects.create( + topic="test", + path=self.rrd_file, + ) + with self.assertLogs() as cm: + self.assertEqual(ds.lastupdate, (None, None)) + self.assertIn("WARNING:rrd.models:Failure", cm.output[0]) + + def test_update(self): + ds = models.DataSource.objects.create( + topic="test", + path=self.rrd_file, + ) + now = datetime.datetime.now() + ts = int(now.timestamp()) + ds.update(ts, 10) + last = ds.lastupdate + self.assertEqual(last[1], 10) + self.assertEqual(last[0].year, now.year) diff --git a/rrd/tests/test_django.py b/rrd/tests/test_django.py deleted file mode 100644 index e0fbb45..0000000 --- a/rrd/tests/test_django.py +++ /dev/null @@ -1,5 +0,0 @@ -from django.test import TestCase - -# Create your tests here. - - diff --git a/rrd/tests/test_mqtt.py b/rrd/tests/test_mqtt.py index 0a7b90d..66ced97 100644 --- a/rrd/tests/test_mqtt.py +++ b/rrd/tests/test_mqtt.py @@ -1,8 +1,10 @@ +import os import time import django.test +from django.conf import settings -from .. import mqtt +from .. import models, mqtt class TestMQTT(django.test.TestCase): @@ -12,9 +14,12 @@ class TestMQTT(django.test.TestCase): 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 @@ -29,3 +34,20 @@ class TestMQTT(django.test.TestCase): 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 |