Refactor dashboard data serialization and mock context for improved clarity
Some checks failed
CI / ci (push) Has been cancelled
Some checks failed
CI / ci (push) Has been cancelled
- Introduced `serialize_audit_for_response` and `serialize_current_cluster_for_template` functions to handle JSON serialization of audit and cluster data, enhancing data consistency for API responses and template rendering. - Updated `get_mock_context` in `mock_data.py` to utilize the new serialization functions, simplifying the mock data structure and improving readability. - Refactored `collect_context` and `collect_audits` in `views.py` to leverage the new serialization methods, ensuring a cleaner and more maintainable codebase. - Added unit tests for the new serialization functions to ensure correctness and reliability of data formatting.
This commit is contained in:
57
dashboard/tests/test_serializers.py
Normal file
57
dashboard/tests/test_serializers.py
Normal file
@@ -0,0 +1,57 @@
|
||||
"""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"], "[]")
|
||||
Reference in New Issue
Block a user