Parcourir la source

Add unit tests for SinkConfig

100% statement coverage for the SinkConfig class, except for __repr__
which is mostly for debugging anyway.
Clara Hobbs il y a 7 ans
Parent
révision
fe57912c2a
1 fichiers modifiés avec 144 ajouts et 0 suppressions
  1. 144
    0
      test_pdbuddy/__init__.py

+ 144
- 0
test_pdbuddy/__init__.py Voir le fichier

@@ -0,0 +1,144 @@
1
+"""Unit tests for the top-level pdbuddy classes"""
2
+
3
+import unittest
4
+
5
+import pdbuddy
6
+
7
+
8
+class SinkConfigTestCase(unittest.TestCase):
9
+
10
+    def setUp(self):
11
+        self.obj_none = pdbuddy.SinkConfig()
12
+        self.obj_empty = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.EMPTY)
13
+        self.obj_valid = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
14
+                flags=pdbuddy.SinkFlags.NONE, v=15000, i=3000)
15
+        self.obj_invalid = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.INVALID,
16
+                flags=pdbuddy.SinkFlags.NONE, v=15000, i=3000)
17
+        self.obj_valid_gb = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
18
+                flags=pdbuddy.SinkFlags.GIVEBACK, v=15000, i=3000)
19
+        self.obj_valid_5v = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
20
+                flags=pdbuddy.SinkFlags.NONE, v=5000, i=3000)
21
+        self.obj_valid_1a = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
22
+                flags=pdbuddy.SinkFlags.NONE, v=15000, i=1000)
23
+
24
+    def test_str_none(self):
25
+        self.assertEqual(str(self.obj_none), "No configuration")
26
+
27
+    def test_str_empty(self):
28
+        self.assertEqual(str(self.obj_empty), "status: empty")
29
+
30
+    def test_str_valid(self):
31
+        self.assertEqual(str(self.obj_valid),
32
+                "status: valid\nflags: (none)\nv: 15.00 V\ni: 3.00 A")
33
+
34
+    def test_str_invalid(self):
35
+        self.assertEqual(str(self.obj_invalid),
36
+                "status: invalid\nflags: (none)\nv: 15.00 V\ni: 3.00 A")
37
+
38
+    def test_str_valid_gb(self):
39
+        self.assertEqual(str(self.obj_valid_gb),
40
+                "status: valid\nflags: GiveBack\nv: 15.00 V\ni: 3.00 A")
41
+
42
+    def test_eq_none(self):
43
+        self.assertTrue(self.obj_none == pdbuddy.SinkConfig())
44
+
45
+    def test_eq_valid(self):
46
+        # Set invalid object as valid
47
+        self.obj_invalid.status = pdbuddy.SinkStatus.VALID
48
+
49
+        self.assertTrue(self.obj_valid == self.obj_invalid)
50
+
51
+    def test_eq_wrong_type(self):
52
+        self.assertFalse(self.obj_none == "hello world")
53
+
54
+    def test_eq_wrong_status(self):
55
+        self.assertFalse(self.obj_valid == self.obj_invalid)
56
+
57
+    def test_eq_wrong_flags(self):
58
+        self.assertFalse(self.obj_valid == self.obj_valid_gb)
59
+
60
+    def test_eq_wrong_voltage(self):
61
+        self.assertFalse(self.obj_valid == self.obj_valid_5v)
62
+
63
+    def test_eq_wrong_current(self):
64
+        self.assertFalse(self.obj_valid == self.obj_valid_1a)
65
+
66
+    def test_ne_none(self):
67
+        self.assertFalse(self.obj_none != pdbuddy.SinkConfig())
68
+
69
+    def test_ne_valid(self):
70
+        # Set invalid object as valid
71
+        self.obj_invalid.status = pdbuddy.SinkStatus.VALID
72
+
73
+        self.assertFalse(self.obj_valid != self.obj_invalid)
74
+
75
+    def test_ne_wrong_type(self):
76
+        self.assertTrue(self.obj_none != "hello world")
77
+
78
+    def test_ne_wrong_status(self):
79
+        self.assertTrue(self.obj_valid != self.obj_invalid)
80
+
81
+    def test_hash_identical(self):
82
+        self.assertEqual(hash(self.obj_none), hash(pdbuddy.SinkConfig()))
83
+
84
+    def test_hash_different(self):
85
+        self.assertNotEqual(hash(self.obj_none), hash(self.obj_valid))
86
+
87
+    def test_from_text_none(self):
88
+        ft_none = pdbuddy.SinkConfig.from_text([])
89
+        self.assertEqual(ft_none, self.obj_none)
90
+
91
+    def test_from_text_empty(self):
92
+        ft_empty = pdbuddy.SinkConfig.from_text([b"status: empty"])
93
+        self.assertEqual(ft_empty, self.obj_empty)
94
+
95
+    def test_from_text_valid(self):
96
+        ft_valid = pdbuddy.SinkConfig.from_text([b"status: valid",
97
+                b"flags: (none)",
98
+                b"v: 15.00 V",
99
+                b"i: 3.00 A"])
100
+        self.assertEqual(ft_valid, self.obj_valid)
101
+
102
+    def test_from_text_invalid(self):
103
+        ft_invalid = pdbuddy.SinkConfig.from_text([b"status: invalid",
104
+                b"flags: (none)",
105
+                b"v: 15.00 V",
106
+                b"i: 3.00 A"])
107
+        self.assertEqual(ft_invalid, self.obj_invalid)
108
+
109
+    def test_from_text_valid_gb(self):
110
+        ft_valid_gb = pdbuddy.SinkConfig.from_text([b"status: valid",
111
+                b"flags: GiveBack",
112
+                b"v: 15.00 V",
113
+                b"i: 3.00 A"])
114
+        self.assertEqual(ft_valid_gb, self.obj_valid_gb)
115
+
116
+    def test_from_text_valid_5v(self):
117
+        ft_valid_5v = pdbuddy.SinkConfig.from_text([b"status: valid",
118
+                b"flags: (none)",
119
+                b"v: 5.00 V",
120
+                b"i: 3.00 A"])
121
+        self.assertEqual(ft_valid_5v, self.obj_valid_5v)
122
+
123
+    def test_from_text_valid_1a(self):
124
+        ft_valid_1a = pdbuddy.SinkConfig.from_text([b"status: valid",
125
+                b"flags: (none)",
126
+                b"v: 15.00 V",
127
+                b"i: 1.00 A"])
128
+        self.assertEqual(ft_valid_1a, self.obj_valid_1a)
129
+
130
+    def test_from_text_invalid_index(self):
131
+        with self.assertRaises(IndexError):
132
+            pdbuddy.SinkConfig.from_text([b"Invalid index"])
133
+
134
+    def test_from_text_no_configuration(self):
135
+        ft_no_config = pdbuddy.SinkConfig.from_text([b"No configuration"])
136
+        self.assertEqual(ft_no_config, self.obj_none)
137
+
138
+    def test_from_text_valid_extra(self):
139
+        ft_valid = pdbuddy.SinkConfig.from_text([b"status: valid",
140
+                b"flags: (none)",
141
+                b"This is an extra line, which shouldn't hurt anything.",
142
+                b"v: 15.00 V",
143
+                b"i: 3.00 A"])
144
+        self.assertEqual(ft_valid, self.obj_valid)

Chargement…
Annuler
Enregistrer