-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_diskspacemonitor.py
More file actions
81 lines (68 loc) · 3.75 KB
/
Copy pathtest_diskspacemonitor.py
File metadata and controls
81 lines (68 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""Unit test for disk space monitor"""
import unittest
import sys
from collections import namedtuple
from unittest.mock import Mock
import diskspacemonitor
class TestDiskSpaceMonitor(unittest.TestCase):
"""Testing Disk Space Monitor"""
def test_parse_args(self):
"""Testing arg verification"""
test_args = ["too", "many", "args"]
self.assertEqual(diskspacemonitor.parse_args(test_args), 0)
test_args = ["correct"]
self.assertEqual(diskspacemonitor.parse_args(test_args), 1)
test_args = ["also", "correct"]
self.assertEqual(diskspacemonitor.parse_args(test_args), 1)
def test_get_notification_percentages_nofile(self):
"""Test get a list of the notifications"""
percentages = diskspacemonitor.get_notification_percentages("")
self.assertGreater(len(percentages), 0)
for index in range(len(percentages) - 1):
self.assertLess(percentages[index], percentages[index+1])
def test_get_notification_percentages(self):
"""Test get a list of the notifications"""
percentages = diskspacemonitor.get_notification_percentages("prefs.txt")
self.assertGreater(len(percentages), 0)
for index in range(len(percentages) - 1):
self.assertLess(percentages[index], percentages[index+1])
def test_get_last_notification_value_no_file(self):
"""Test what the last notification was without a file"""
self.assertEqual(diskspacemonitor.get_last_notification_value("nofile"), 100)
def test_get_last_notification_value_with_file(self):
"""Test what the last notification was with a file"""
self.assertEqual(diskspacemonitor.get_last_notification_value("testfile.txt"), 50)
# Do a test for file
def test_parse_threshold(self):
"""Test if a value falls less than a range"""
testlist = [10, 20, 90]
self.assertEqual(diskspacemonitor.parse_threshold_list(testlist, 5), 10)
self.assertEqual(diskspacemonitor.parse_threshold_list(testlist, 15), 20)
self.assertEqual(diskspacemonitor.parse_threshold_list(testlist, 21), 90)
self.assertEqual(diskspacemonitor.parse_threshold_list(testlist, 100), 100)
def test_set_last_notification(self):
"""Test if a value falls less than a range"""
diskspacemonitor.set_last_notification("writetest.txt", 50)
testval = diskspacemonitor.get_last_notification_value("writetest.txt")
self.assertEqual(50, testval)
def test_process_disk_space(self):
"""Test the main crux of the code"""
percentages = [90, 75, 10, 25, 5, 50, 15]
percentages = sorted(percentages)
TestType = namedtuple('Point', ['used', 'total', 'free'])
testval = TestType(0, 200, 200)
self.assertEqual(diskspacemonitor.process_disk_space(testval, 0, percentages), -100)
testval = TestType(150, 200, 50)
self.assertEqual(diskspacemonitor.process_disk_space(testval, 0, percentages), -25)
# Testing that the last val was 50, new val is at 50% threshold, so return 0
self.assertEqual(diskspacemonitor.process_disk_space(testval, 50, percentages), 25)
testval = TestType(100, 200, 100)
# Testing that the last val was 75, new val is at 50% threshold, so return 1
self.assertEqual(diskspacemonitor.process_disk_space(testval, 75, percentages), 50)
# Testing that the last val was less than the new one - disk has been emptied
testval = TestType(100, 200, 100)
self.assertEqual(diskspacemonitor.process_disk_space(testval, 10, percentages), -50)
if __name__ == '__main__':
SUITE = unittest.TestLoader().loadTestsFromTestCase(TestDiskSpaceMonitor)
RESULT = unittest.TextTestRunner(verbosity=2).run(SUITE)
sys.exit(not RESULT.wasSuccessful())