Debian Codegraph @

tests/unit_tests.py

raw symbols SCIP
1 import unittest
2 import argparse
3 from discus import StatsFactory, SizeFormatter, DiskData, Disk
4 from discus import Mount, parse_options, read_mounts, opts
5
6
7 class StatsFactoryTests(unittest.TestCase):
8 """Unit tests for the StatsFactory class"""
9 def test_getStatsReservedTrue(self):
10 """Normal use case with reserved == True."""
11 factory = StatsFactory(True)
12 s = factory.getStats("/")
13 self.assertNotEqual(s.total, 0, "a size of 0 for / is unlikely")
14 self.assertNotEqual(s.free, 0, "0 bytes free for / is unlikely")
15 self.assertNotEqual(s.used, 0, "0 bytes used for / is unlikely")
16
17 def test_getStatsReservedFalse(self):
18 """Normal use case with reserved == False."""
19 factory = StatsFactory(False)
20 s = factory.getStats("/")
21 self.assertNotEqual(s.total, 0, "a size of 0 for / is unlikely")
22 self.assertNotEqual(s.free, 0, "0 bytes free for / is unlikely")
23 self.assertNotEqual(s.used, 0, "0 bytes used for / is unlikely")
24 self.assertEqual(s.total, s.free + s.used, "total != free + used")
25
26 def test_getStatsFailure(self):
27 """Failure use case, non existent mount point."""
28 factory = StatsFactory(False)
29 raised = False
30 try:
31 factory.getStats("non existent mount point")
32 except Exception:
33 raised = True
34 self.assertTrue(raised)
35
36
37 class SizeFormatterTests(unittest.TestCase):
38 """Unit tests for the SizeFormatter class"""
39
40 def test_manual_format(self):
41 """Format sizes in manual format mode."""
42 # kilo bytes
43 opts = SizeFormatter.Options(smart=False, divisor=0)
44 sf = SizeFormatter(*opts)
45 # TODO, this is not correct, if the converted size is exact, then there
46 # should be no .0 part, but to implement that, we have to manipulate
47 # sizes internally in bytes, not in kilo bytes
48 self.assertEqual(sf.format(124684), "124684.0 KB", "124684 in KB fail")
49 # mega bytes
50 opts = SizeFormatter.Options(smart=False, divisor=1)
51 sf = SizeFormatter(*opts)
52 self.assertEqual(sf.format(124684), "121.8 MB", "124684 in MB fail")
53 # giga bytes
54 opts = SizeFormatter.Options(smart=False, divisor=2)
55 sf = SizeFormatter(*opts)
56 self.assertEqual(sf.format(124684), "0.1 GB", "124684 in GB fail")
57 # tera bytes
58 opts = SizeFormatter.Options(smart=False, divisor=3)
59 sf = SizeFormatter(*opts)
60 self.assertEqual(sf.format(pow(1024, 3)), "1.0 TB", "1 TB in TB fail")
61 self.assertEqual(sf.format(0), "0 TB", "0 TB in TB fail")
62
63 def test_smart_format(self):
64 """Format sizes in smart format mode."""
65 opts = SizeFormatter.Options(smart=True)
66 sf = SizeFormatter(*opts)
67 self.assertEqual(sf.format(124684), "121.8 MB", "124684 fail")
68 self.assertEqual(sf.format(1024), "1.0 MB", "1024 fail")
69 self.assertEqual(sf.format(1), "1.0 KB", "1 fail")
70 self.assertEqual(sf.format(0), "0 KB", "0 fail")
71 self.assertEqual(sf.format(999), "999.0 KB", "999 fail")
72 self.assertEqual(sf.format(1000), "1.0 MB", "1000 fail")
73
74
75 class DiskDataTests(unittest.TestCase):
76 """Unit tests for the DiskData class"""
77
78 def test_get(self):
79 """Tests for the get method."""
80 sf = SizeFormatter(*SizeFormatter.Options())
81 mount = Mount("/", "/dev/sda1")
82 d = DiskData.get(StatsFactory.Stats(1000000, 1000000 - 50000, 50000),
83 5.0,
84 mount,
85 sf)
86 self.assertEqual(d.percent, "5.0%", "percent doesn't match")
87 self.assertEqual(d.total, "976.6 KB", "total doesn't match")
88 self.assertEqual(d.used, "48.8 KB", "used doesn't match")
89 self.assertEqual(d.free, "927.7 KB", "free doesn't match")
90 self.assertEqual(d.mount, mount.mount, "mount doesn't match")
91 self.assertEqual(d.device, mount.device, "device doesn't match")
92
93
94 class DiskTests(unittest.TestCase):
95 """Unit tests for the Disk class"""
96 def test_graph(self):
97 opts["color"] = ""
98 self.assertEqual(Disk.graph(10, 14), " [*---------]")
99 self.assertEqual(Disk.graph(20, 14), " [**--------]")
100 self.assertEqual(Disk.graph(50, 14), " [*****-----]")
101 self.assertEqual(Disk.graph(80, 14), " [********--]")
102 self.assertEqual(Disk.graph(100, 14), " [**********]")
103
104
105 class ParseOptionsTests(unittest.TestCase):
106 """Tests for the parse_options function."""
107 def test_device(self):
108 """Test for the --device option."""
109 options = parse_options([])
110 self.assertFalse(options.device)
111 options = parse_options(["-d"])
112 self.assertTrue(options.device)
113 options = parse_options(["--device"])
114 self.assertTrue(options.device)
115
116 def test_color(self):
117 """Test for the --color option."""
118 options = parse_options([])
119 self.assertTrue(options.color)
120 options = parse_options(["-c"])
121 self.assertFalse(options.color)
122 options = parse_options(["--color"])
123 self.assertFalse(options.color)
124
125 def test_gigabytes(self):
126 """Test for the --gigabytes option."""
127 options = parse_options([])
128 self.assertEqual(options.divisor, None)
129 options = parse_options(["-g"])
130 self.assertEqual(options.divisor, 2)
131 options = parse_options(["--gigabytes"])
132 self.assertEqual(options.divisor, 2)
133
134 def test_kilobytes(self):
135 """Test for the --kilobytes option."""
136 options = parse_options([])
137 self.assertEqual(options.divisor, None)
138 options = parse_options(["-k"])
139 self.assertEqual(options.divisor, 0)
140 options = parse_options(["--kilobytes"])
141 self.assertEqual(options.divisor, 0)
142
143 def test_megabytes(self):
144 """Test for the --megabytes option."""
145 options = parse_options([])
146 self.assertEqual(options.divisor, None)
147 options = parse_options(["-m"])
148 self.assertEqual(options.divisor, 1)
149 options = parse_options(["--megabytes"])
150 self.assertEqual(options.divisor, 1)
151
152 def test_places(self):
153 options = parse_options([])
154 self.assertEqual(options.places, None)
155 options = parse_options(["-p", "0"])
156 self.assertEqual(options.places, 0)
157 options = parse_options(["-p", "9"])
158 self.assertEqual(options.places, 9)
159 with self.assertRaises((SystemExit, argparse.ArgumentError)):
160 parse_options(["-p", "10"])
161 with self.assertRaises((SystemExit, argparse.ArgumentError)):
162 parse_options(["-p", "-1"])
163
164 def test_reserved(self):
165 """ Test for the --reserved option."""
166 options = parse_options([])
167 self.assertFalse(options.reserved)
168 options = parse_options(["-r"])
169 self.assertTrue(options.reserved)
170 options = parse_options(["--reserved"])
171 self.assertTrue(options.reserved)
172
173 def test_smart(self):
174 options = parse_options([])
175 self.assertTrue(options.smart)
176 options = parse_options(["-s"])
177 self.assertFalse(options.smart)
178 options = parse_options(["--smart"])
179 self.assertFalse(options.smart)
180
181 def test_terabytes(self):
182 """Test for the --terabytes option."""
183 options = parse_options([])
184 self.assertEqual(options.divisor, None)
185 options = parse_options(["-t"])
186 self.assertEqual(options.divisor, 3)
187 options = parse_options(["--terabytes"])
188 self.assertEqual(options.divisor, 3)
189
190
191 class ReadMountsTests(unittest.TestCase):
192 """Tests for the read_mounts function."""
193 def test_simple_mtab(self):
194 """Test with a simple mtabe consisting of only one line."""
195 mounts = read_mounts("tests/mtab.oneline", [])
196 self.assertEqual(len(mounts), 1, "one device should be detected")
197 self.assertEqual(mounts[0].device, "/dev/sda2")
198 self.assertEqual(mounts[0].mount, "/")
199
200 def test_bug_291276(self):
201 """Test to reproduce the debian bug 291276."""
202 mounts = read_mounts("tests/mtab.291276", [])
203 self.assertEqual(len(mounts), 1, "one mount point should be detected")
204 self.assertEqual(mounts[0].mount, "/media/ACER UFD")