From 1dc2888eabb089e1312c5e583ec064cd19e3c14b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Palancher?= Date: Tue, 3 Dec 2024 12:54:01 +0100 Subject: [PATCH] tests: cover disabled labels in infrastructures Tests differences when rack, row and equipments are either enabled or disabled in infrastructures graphical representations. --- racksdb/tests/drawers/test_infrastructure.py | 74 ++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/racksdb/tests/drawers/test_infrastructure.py b/racksdb/tests/drawers/test_infrastructure.py index 9d900b70..d04ed960 100644 --- a/racksdb/tests/drawers/test_infrastructure.py +++ b/racksdb/tests/drawers/test_infrastructure.py @@ -5,6 +5,7 @@ # SPDX-License-Identifier: GPL-3.0-or-later import unittest +from unittest import mock import tempfile from pathlib import Path @@ -67,3 +68,76 @@ def test_draw_not_existing_infrastructure(self): self.db, "fail", filename, "png", self.parameters, None, "yaml" ) self.assertFalse(filename.exists()) + + def test_rack_row_spacing_height(self): + drawer = InfrastructureDrawer( + self.db, "mercury", "dontmind.png", "png", self.parameters, None, "yaml" + ) + # Check that by default, row and racks labels are enabled. + self.assertTrue(self.parameters.rack.labels) + self.assertTrue(self.parameters.row.labels) + + # Change values of rack offset and rack label offset to validate sums. + self.assertEqual(self.parameters.row.label_offset, 20) + self.parameters.rack.offset = 10 + self.parameters.rack.label_offset = 30 + + # Check spacing height between rows with all labels enabled. + self.assertEqual(drawer._rack_row_spacing_height(), 60) # 30 + 20 + 10 + # Disable racks labels. + self.parameters.rack.labels = False + self.assertEqual(drawer._rack_row_spacing_height(), 30) # 20 + 10 + # Disable row labels (ie. no labels). + self.parameters.row.labels = False + self.assertEqual(drawer._rack_row_spacing_height(), 10) + # Re-enable racks labels (w/o row labels). + self.parameters.rack.labels = True + self.assertEqual(drawer._rack_row_spacing_height(), 40) # 30 + 10 + + def test_labels_call_print_text(self): + with tempfile.TemporaryDirectory() as tmpdir: + filename = Path(tmpdir) / "output.png" + drawer = InfrastructureDrawer( + self.db, "mercury", filename, "png", self.parameters, None, "yaml" + ) + # Mock _print_text() method to check if it is called or not. + drawer._print_text = mock.Mock() + + # Check _print_text() is not called when all labels are disabled. + ( + self.parameters.rack.labels, + self.parameters.row.labels, + self.parameters.infrastructure.equipment_labels, + ) = (False, False, False) + drawer.draw() + drawer._print_text.assert_not_called() + drawer._print_text.reset_mock() + + # Check _print_text() is called when either rack, row or equipment + # labels are enabled. + ( + self.parameters.rack.labels, + self.parameters.row.labels, + self.parameters.infrastructure.equipment_labels, + ) = (True, False, False) + drawer.draw() + drawer._print_text.assert_called() + drawer._print_text.reset_mock() + + ( + self.parameters.rack.labels, + self.parameters.row.labels, + self.parameters.infrastructure.equipment_labels, + ) = (False, True, False) + drawer.draw() + drawer._print_text.assert_called() + drawer._print_text.reset_mock() + + ( + self.parameters.rack.labels, + self.parameters.row.labels, + self.parameters.infrastructure.equipment_labels, + ) = (False, False, True) + drawer.draw() + drawer._print_text.assert_called() + drawer._print_text.reset_mock()