Add unittest for Bar

This commit is contained in:
nicolargo 2024-01-28 16:06:56 +01:00
parent 7f2e8525b4
commit ab5bf02e6b
5 changed files with 52 additions and 19 deletions

View File

@ -34,6 +34,19 @@ class Bar(object):
unit_char='%',
display_value=True,
min_value=0, max_value=100):
"""Init a bar (used in Quicllook plugin)
Args:
size (_type_): Bar size
bar_char (str, optional): Bar character. Defaults to '|'.
empty_char (str, optional): Empty character. Defaults to ' '.
pre_char (str, optional): Display this char before the bar. Defaults to '['.
post_char (str, optional): Display this char after the bar. Defaults to ']'.
unit_char (str, optional): Unit char to be displayed. Defaults to '%'.
display_value (bool, optional): Do i need to display the value. Defaults to True.
min_value (int, optional): Minimum value. Defaults to 0.
max_value (int, optional): Maximum value (percent can be higher). Defaults to 100.
"""
# Build curses_bars
self.__curses_bars = [empty_char] * 5 + [bar_char] * 5
# Bar size
@ -77,26 +90,34 @@ class Bar(object):
def post_char(self):
return self.__post_char
def get(self, overwrite=''):
def get(self, overlay: str = None):
"""Return the bars."""
value = self.max_value if self.percent > self.max_value else self.percent
# Build the bar
frac, whole = modf(self.size * value / 100.0)
ret = self.__curses_bars[8] * int(whole)
if frac > 0:
ret += self.__curses_bars[int(frac * 8)]
whole += 1
ret += self.__empty_char * int(self.size - whole)
# Add the value
if self.__display_value:
if self.percent > self.max_value:
ret = '{}>{:4.0f}{}'.format(ret,
if self.percent >= self.max_value:
ret = '{} {}{:3.0f}{}'.format(ret,
'>' if self.percent > self.max_value else ' ',
self.max_value,
self.__unit_char)
else:
ret = '{}{:5.1f}{}'.format(ret,
self.percent,
self.__unit_char)
if overwrite and len(overwrite) < len(ret) - 6:
ret = overwrite + ret[len(overwrite):]
# Add overlay
if overlay and len(overlay) < len(ret) - 6:
ret = overlay + ret[len(overlay):]
return ret
def __str__(self):

View File

@ -11,6 +11,7 @@
from glances.globals import iterkeys
from glances.plugins.plugin.model import GlancesPluginModel
from glances.logger import logger
import psutil

View File

@ -777,8 +777,12 @@ class GlancesPluginModel(object):
else:
return stat_name + '_' + criticality in self._limits
def get_limit(self, criticality, stat_name=""):
"""Return the limit value for the alert."""
def get_limit(self, criticality=None, stat_name=""):
"""Return the limit value for the given criticality.
If criticality is None, return the dict of all the limits."""
if criticality is None:
return self._limits
# Get the limit for stat + header
# Example: network_wlan0_rx_careful
try:

View File

@ -238,14 +238,10 @@ class PluginModel(GlancesPluginModel):
def _msg_create_line(self, msg, data, key):
"""Create a new line to the Quick view."""
# if key == 'mem' and self.get_alert(self.stats['swap'], header='swap') != 'DEFAULT':
# overwrite = 'SWAP'
# else:
overwrite = ''
return [
self.curse_add_line(msg),
self.curse_add_line(data.pre_char, decoration='BOLD'),
self.curse_add_line(data.get(overwrite), self.get_views(key=key, option='decoration')),
self.curse_add_line(data.get(), self.get_views(key=key, option='decoration')),
self.curse_add_line(data.post_char, decoration='BOLD'),
self.curse_add_line(' '),
]

View File

@ -371,7 +371,7 @@ class TestGlances(unittest.TestCase):
self.assertEqual(len(h.get()), 2)
self.assertEqual(len(h.get()['a']), 0)
def test_099_output_bars_must_be_between_0_and_100_percent(self):
def test_099_output_bars(self):
"""Test quick look plugin.
> bar.min_value
@ -381,16 +381,27 @@ class TestGlances(unittest.TestCase):
> bar.percent = -1
> bar.percent
0
> bar.percent = 101
> bar.percent
100
"""
print('INFO: [TEST_099] Test progress bar')
bar = Bar(size=1)
# Percent value can not be lower than min_value
bar.percent = -1
self.assertLessEqual(bar.percent, bar.min_value)
# but... percent value can be higher than max_value
bar.percent = 101
self.assertGreaterEqual(bar.percent, bar.max_value)
self.assertLessEqual(bar.percent, 101)
# Test display
bar = Bar(size=50)
bar.percent = 0
self.assertEqual(bar.get(), ' 0.0%')
bar.percent = 70
self.assertEqual(bar.get(), '||||||||||||||||||||||||||||||| 70.0%')
bar.percent = 100
self.assertEqual(bar.get(), '|||||||||||||||||||||||||||||||||||||||||||| 100%')
bar.percent = 110
self.assertEqual(bar.get(), '|||||||||||||||||||||||||||||||||||||||||||| >100%')
def test_100_secure(self):
"""Test secure functions"""