aboutsummaryrefslogtreecommitdiff
path: root/rrd/tests/test_datasources.py
blob: 4057b1404234508646ea62df34c1805a4740f951 (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
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)