Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1import unittest 

2import posix 

3from unittest import mock 

4from pi3bar.plugins.disk import Disk 

5 

6 

7# (irrelevant numbers are just 1) bs size free avail 

8RESULT_100 = posix.statvfs_result((1, 4096, 26214400, 26214400, 26214400, 1, 1, 1, 1, 1)) # critical 

9 

10RESULT_92 = posix.statvfs_result((1, 4096, 26214400, 24117248, 24117248, 1, 1, 1, 1, 1)) # warning 

11 

12RESULT_50 = posix.statvfs_result((1, 4096, 26214400, 13107200, 13107200, 1, 1, 1, 1, 1)) 

13 

14RESULT_0 = posix.statvfs_result((1, 4096, 26214400, 0, 0, 1, 1, 1, 1, 1)) 

15 

16 

17class DiskTestCase(unittest.TestCase): 

18 def test_default_values(self): 

19 plugin = Disk('/') 

20 self.assertEqual('/', plugin.mount_path) 

21 self.assertEqual('/', plugin.instance) 

22 self.assertEqual('%(usage_p).2f%% (%(size)s)', plugin.full_format) 

23 self.assertEqual('%(usage_p).2f%%', plugin.short_format) 

24 self.assertEqual(90, plugin.warning_usage) 

25 self.assertEqual(95, plugin.critical_usage) 

26 self.assertEqual('#ffff00', plugin.warning_color) 

27 self.assertIsNone(plugin.warning_background) 

28 self.assertIsNone(plugin.critical_color) 

29 self.assertEqual('#ff0000', plugin.critical_background) 

30 

31 

32class DiskGetStatusTestCase(unittest.TestCase): 

33 def setUp(self): 

34 self.plugin = Disk('') 

35 

36 @mock.patch('pi3bar.plugins.disk.os.statvfs', return_value=RESULT_100) 

37 def test_full(self, mock_os): 

38 self.stats = self.plugin.get_stats() 

39 self.assertEqual('100.0GB', self.stats['size']) 

40 self.assertEqual('100.0GB', self.stats['free']) 

41 self.assertEqual('100.0GB', self.stats['available']) 

42 self.assertEqual('0.0bytes', self.stats['usage']) 

43 self.assertEqual(100.0, self.stats['free_p']) 

44 self.assertEqual(100.0, self.stats['available_p']) 

45 self.assertEqual(0.0, self.stats['usage_p']) 

46 

47 @mock.patch('pi3bar.plugins.disk.os.statvfs', return_value=RESULT_0) 

48 def test_empty(self, mock_os): 

49 self.stats = self.plugin.get_stats() 

50 self.assertEqual('100.0GB', self.stats['size']) 

51 self.assertEqual('0.0bytes', self.stats['free']) 

52 self.assertEqual('0.0bytes', self.stats['available']) 

53 self.assertEqual('100.0GB', self.stats['usage']) 

54 self.assertEqual(0.0, self.stats['free_p']) 

55 self.assertEqual(0.0, self.stats['available_p']) 

56 self.assertEqual(100.0, self.stats['usage_p']) 

57 

58 

59class DiskCycleTestCase(unittest.TestCase): 

60 def setUp(self): 

61 self.plugin = Disk('/') 

62 

63 def test(self): 

64 stats = { 

65 'size': '100.00GB', 

66 'usage_p': 10.0 

67 } 

68 with mock.patch.object(Disk, 'get_stats', return_value=stats): 

69 self.plugin.cycle() 

70 

71 self.assertEqual('/ 10.00% (100.00GB)', self.plugin.full_text) 

72 self.assertEqual('/ 10.00%', self.plugin.short_text) 

73 self.assertIsNone(self.plugin.color) 

74 self.assertIsNone(self.plugin.background) 

75 

76 def test_warning(self): 

77 stats = { 

78 'size': '100.00GB', 

79 'usage_p': 92.0 

80 } 

81 with mock.patch.object(Disk, 'get_stats', return_value=stats): 

82 self.plugin.cycle() 

83 self.assertEqual(self.plugin.warning_color, self.plugin.color) 

84 self.assertEqual(self.plugin.warning_background, self.plugin.background) 

85 self.assertEqual('#ffff00', self.plugin.color) 

86 self.assertIsNone(self.plugin.background) 

87 

88 def test_critical(self): 

89 stats = { 

90 'size': '100.00GB', 

91 'usage_p': 100.0 

92 } 

93 with mock.patch.object(Disk, 'get_stats', return_value=stats): 

94 self.plugin.cycle() 

95 self.assertEqual(self.plugin.critical_color, self.plugin.color) 

96 self.assertEqual(self.plugin.critical_background, self.plugin.background) 

97 self.assertIsNone(self.plugin.color) 

98 self.assertEqual('#ff0000', self.plugin.background)