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)