From 61136eb865317dfe1744c7577b7cf4fb229bcc48 Mon Sep 17 00:00:00 2001 From: Guillaumebeuzeboc Date: Wed, 7 Feb 2024 18:27:40 +0100 Subject: [PATCH] test(dashboards): dashboards files are properly created --- cos_registration_server/api/tests.py | 99 +++++++++++++++++++++------- 1 file changed, 74 insertions(+), 25 deletions(-) diff --git a/cos_registration_server/api/tests.py b/cos_registration_server/api/tests.py index 22660e5..e62b328 100644 --- a/cos_registration_server/api/tests.py +++ b/cos_registration_server/api/tests.py @@ -1,5 +1,6 @@ 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 @@ -7,24 +8,24 @@ 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(): @@ -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, @@ -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 = [ @@ -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( @@ -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") @@ -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) @@ -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") @@ -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") @@ -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" + ) + ) +