"""Tests for dashboard.serializers.""" import json from django.test import TestCase from dashboard.serializers import ( serialize_audit_for_response, serialize_current_cluster_for_template, ) class SerializeAuditForResponseTest(TestCase): def test_serializes_list_fields_to_json_strings(self): audit = { "id": "audit-1", "name": "Test", "migrations": [{"instanceName": "i1", "source": "h1", "destination": "h2"}], "host_labels": ["h1", "h2"], "cpu_current": [10.0, 20.0], "cpu_projected": [15.0, 25.0], } result = serialize_audit_for_response(audit) self.assertEqual(result["id"], "audit-1") self.assertEqual(result["name"], "Test") self.assertEqual(json.loads(result["migrations"]), audit["migrations"]) self.assertEqual(json.loads(result["host_labels"]), audit["host_labels"]) self.assertEqual(json.loads(result["cpu_current"]), audit["cpu_current"]) self.assertEqual(json.loads(result["cpu_projected"]), audit["cpu_projected"]) def test_leaves_already_serialized_strings_unchanged(self): audit = { "id": "a", "migrations": "[1,2]", "host_labels": "[]", "cpu_current": "[0]", "cpu_projected": "[0]", } result = serialize_audit_for_response(audit) self.assertEqual(result["migrations"], "[1,2]") self.assertEqual(result["host_labels"], "[]") self.assertEqual(result["cpu_current"], "[0]") self.assertEqual(result["cpu_projected"], "[0]") class SerializeCurrentClusterForTemplateTest(TestCase): def test_serializes_lists_to_json_strings(self): cluster = {"host_labels": ["c0", "c1"], "cpu_current": [30.0, 40.0]} result = serialize_current_cluster_for_template(cluster) self.assertEqual(json.loads(result["host_labels"]), cluster["host_labels"]) self.assertEqual(json.loads(result["cpu_current"]), cluster["cpu_current"]) def test_leaves_already_serialized_strings_unchanged(self): cluster = {"host_labels": "[]", "cpu_current": "[]"} result = serialize_current_cluster_for_template(cluster) self.assertEqual(result["host_labels"], "[]") self.assertEqual(result["cpu_current"], "[]")