archey4/archey/test/entries/test_archey_load_average.py
2024-04-13 21:22:03 +01:00

88 lines
2.9 KiB
Python

"""Test module for Archey LoadAverage detection module"""
import unittest
from archey.colors import Colors
from archey.entries.load_average import LoadAverage
from archey.test.entries import HelperMethods
class TestLoadAverageEntry(unittest.TestCase):
"""LoadAverage `pretty_value` configuration-based coloration test class"""
def setUp(self):
"""Define mocked entry before each test"""
self.load_average_mock = HelperMethods.entry_mock(LoadAverage)
@HelperMethods.patch_clean_configuration
def test_pretty_value_coloration(self):
"""Test output coloration based on user preferences"""
self.load_average_mock.value = (0.5, 1.25, 2.5)
self.load_average_mock.options = {
"warning_threshold": 0.75,
"danger_threshold": 2.25,
}
self.assertEqual(
str(self.load_average_mock),
(
f"{Colors.GREEN_NORMAL}0.5{Colors.CLEAR} "
f"{Colors.YELLOW_NORMAL}1.25{Colors.CLEAR} "
f"{Colors.RED_NORMAL}2.5{Colors.CLEAR}"
),
)
@HelperMethods.patch_clean_configuration
def test_pretty_value_rounding(self):
"""Test output rounding based on user preferences"""
self.load_average_mock.value = (0.33333, 1.25, 2.0)
with self.subTest("No decimal places"):
self.load_average_mock.options = {
"decimal_places": 0,
"warning_threshold": 5,
"danger_threshold": 5,
}
self.assertEqual(
str(self.load_average_mock),
(
f"{Colors.GREEN_NORMAL}0.0{Colors.CLEAR} "
f"{Colors.GREEN_NORMAL}1.0{Colors.CLEAR} "
f"{Colors.GREEN_NORMAL}2.0{Colors.CLEAR}"
),
)
with self.subTest("1 decimal place"):
self.load_average_mock.options = {
"decimal_places": 1,
"warning_threshold": 5,
"danger_threshold": 5,
}
self.assertEqual(
str(self.load_average_mock),
(
f"{Colors.GREEN_NORMAL}0.3{Colors.CLEAR} "
f"{Colors.GREEN_NORMAL}1.2{Colors.CLEAR} "
f"{Colors.GREEN_NORMAL}2.0{Colors.CLEAR}"
),
)
with self.subTest("2 decimal places"):
self.load_average_mock.options = {
"decimal_places": 2,
"warning_threshold": 5,
"danger_threshold": 5,
}
self.assertEqual(
str(self.load_average_mock),
(
f"{Colors.GREEN_NORMAL}0.33{Colors.CLEAR} "
f"{Colors.GREEN_NORMAL}1.25{Colors.CLEAR} "
f"{Colors.GREEN_NORMAL}2.0{Colors.CLEAR}"
),
)
if __name__ == "__main__":
unittest.main()