Skip to content

Commit

Permalink
test(dashboards): dashboards files are properly created
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaumebeuzeboc committed Feb 7, 2024
1 parent 589089c commit 61136eb
Showing 1 changed file with 74 additions and 25 deletions.
99 changes: 74 additions & 25 deletions cos_registration_server/api/tests.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
import json
from os import mkdir
from os import mkdir, path
from pathlib import Path
from shutil import rmtree

from devices.models import Device, default_dashboards_json_field
from django.urls import reverse
from django.utils import timezone
from rest_framework.test import APITestCase

SIMPLE_GRAFANA_DASHBOARD = {
"id": None,
"uid": None,
"title": "Production Overview",
"tags": ["templated"],
"timezone": "browser",
"schemaVersion": 16,
"refresh": "25s",
}


class DevicesViewTests(APITestCase):
def setUp(self):
self.url = reverse("api:devices")
self.grafana_dashboards_path = "grafana_dashboards"
self.grafana_dashboards_path = Path("grafana_dashboards")
rmtree(self.grafana_dashboards_path, ignore_errors=True)
mkdir(self.grafana_dashboards_path)

self.simple_grafana_dashboard = {
"id": None,
"uid": None,
"title": "Production Overview",
"tags": ["templated"],
"timezone": "browser",
"schemaVersion": 16,
"refresh": "25s",
}

def create_device(self, **fields):
data = {}
for field, value in fields.items():
Expand All @@ -40,7 +41,7 @@ def test_create_device(self):
uid = "robot-1"
address = "192.168.0.1"
custom_grafana_dashboards = default_dashboards_json_field()
custom_grafana_dashboards.append(SIMPLE_GRAFANA_DASHBOARD)
custom_grafana_dashboards.append(self.simple_grafana_dashboard)
response = self.create_device(
uid=uid,
address=address,
Expand All @@ -58,6 +59,15 @@ def test_create_device(self):
self.assertEqual(
Device.objects.get().grafana_dashboards, custom_grafana_dashboards
)
with open(
self.grafana_dashboards_path / "robot-1-Production_Overview.json",
"r",
) as file:
dashboard_data = json.load(file)
self.simple_grafana_dashboard[
"title"
] = f'{uid}-{self.simple_grafana_dashboard["title"]}'
self.assertEqual(dashboard_data, self.simple_grafana_dashboard)

def test_create_multiple_devices(self):
devices = [
Expand Down Expand Up @@ -99,7 +109,7 @@ def test_grafana_dashboard_not_in_a_list(self):
response = self.create_device(
uid=uid,
address=address,
grafana_dashboards=SIMPLE_GRAFANA_DASHBOARD,
grafana_dashboards=self.simple_grafana_dashboard,
)
self.assertEqual(Device.objects.count(), 0)
self.assertContains(
Expand Down Expand Up @@ -128,11 +138,27 @@ def test_grafana_dashboard_illformed_json(self):


class DeviceViewTests(APITestCase):
def setUp(self):
self.grafana_dashboards_path = Path("grafana_dashboards")
rmtree(self.grafana_dashboards_path, ignore_errors=True)
mkdir(self.grafana_dashboards_path)
self.simple_grafana_dashboard = {
"id": None,
"uid": None,
"title": "Production Overview",
"tags": ["templated"],
"timezone": "browser",
"schemaVersion": 16,
"refresh": "25s",
}

def url(self, uid):
return reverse("api:device", args=(uid,))

def create_device(self, uid, address):
data = {"uid": uid, "address": address}
def create_device(self, **fields):
data = {}
for field, value in fields.items():
data[field] = value
url = reverse("api:devices")
return self.client.post(url, data, format="json")

Expand All @@ -143,7 +169,7 @@ def test_get_nonexistent_device(self):
def test_get_device(self):
uid = "robot-1"
address = "192.168.1.2"
self.create_device(uid, address)
self.create_device(uid=uid, address=address)
response = self.client.get(self.url(uid))
self.assertEqual(response.status_code, 200)
content_json = json.loads(response.content)
Expand All @@ -160,7 +186,7 @@ def test_get_device(self):
def test_patch_device(self):
uid = "robot-1"
address = "192.168.1.2"
self.create_device(uid, address)
self.create_device(uid=uid, address=address)
address = "192.168.1.200"
data = {"address": address}
response = self.client.patch(self.url(uid), data, format="json")
Expand All @@ -172,25 +198,31 @@ def test_patch_device(self):
def test_patch_dashboards(self):
uid = "robot-1"
address = "192.168.1.2"
self.create_device(uid, address)
data = {"grafana_dashboards": [SIMPLE_GRAFANA_DASHBOARD]}
self.create_device(uid=uid, address=address)
data = {"grafana_dashboards": [self.simple_grafana_dashboard]}
response = self.client.patch(self.url(uid), data, format="json")
self.assertEqual(response.status_code, 200)
content_json = json.loads(response.content)
# necessary since patching returns the modified title
SIMPLE_GRAFANA_DASHBOARD[
self.simple_grafana_dashboard[
"title"
] = f'{uid}-{SIMPLE_GRAFANA_DASHBOARD["title"]}'
] = f'{uid}-{self.simple_grafana_dashboard["title"]}'
self.assertEqual(
content_json["grafana_dashboards"][0],
SIMPLE_GRAFANA_DASHBOARD,
self.simple_grafana_dashboard,
)
self.assertEqual(content_json["address"], address)
with open(
self.grafana_dashboards_path / "robot-1-Production_Overview.json",
"r",
) as file:
dashboard_data = json.load(file)
self.assertEqual(dashboard_data, self.simple_grafana_dashboard)

def test_invalid_patch_device(self):
uid = "robot-1"
address = "192.168.1.2"
self.create_device(uid, address)
self.create_device(uid=uid, address=address)
address = "192.168.1" # invalid IP
data = {"address": address}
response = self.client.patch(self.url(uid), data, format="json")
Expand All @@ -199,10 +231,27 @@ def test_invalid_patch_device(self):
def test_delete_device(self):
uid = "robot-1"
address = "192.168.1.2"
self.create_device(uid, address)
self.create_device(
uid=uid,
address=address,
grafana_dashboards=[self.simple_grafana_dashboard],
)
response = self.client.get(self.url(uid))
self.assertEqual(response.status_code, 200)
self.assertTrue(
path.isfile(
self.grafana_dashboards_path
/ "robot-1-Production_Overview.json"
)
)
response = self.client.delete(self.url(uid))
self.assertEqual(response.status_code, 204)
response = self.client.get(self.url(uid))
self.assertEqual(response.status_code, 404)
self.assertFalse(
path.isfile(
self.grafana_dashboards_path
/ "robot-1-Production_Overview.json"
)
)

0 comments on commit 61136eb

Please sign in to comment.