Browse Source

Test the Sink class

It's at 100% branch coverage.  Woohoo!
Clara Hobbs 7 years ago
parent
commit
a4308fe7b0
1 changed files with 79 additions and 0 deletions
  1. 79
    0
      test_pdbuddy/__init__.py

+ 79
- 0
test_pdbuddy/__init__.py View File

@@ -5,6 +5,85 @@ import unittest
5 5
 import pdbuddy
6 6
 
7 7
 
8
+class SinkTestCase(unittest.TestCase):
9
+
10
+    def setUp(self):
11
+        # Get devices
12
+        pdbs_devices = list(pdbuddy.Sink.get_devices())
13
+        # If there are no devices, skip the test
14
+        if len(pdbs_devices) == 0:
15
+            self.skipTest("No PD Buddy Sink devices found")
16
+        # Open the first device
17
+        self.pdbs = pdbuddy.Sink(pdbs_devices[0])
18
+
19
+        self.obj_valid = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
20
+                flags=pdbuddy.SinkFlags.NONE, v=15000, i=3000)
21
+        self.obj_valid_gb = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
22
+                flags=pdbuddy.SinkFlags.GIVEBACK, v=15000, i=3000)
23
+
24
+    def tearDown(self):
25
+        # Close the connection to the PD Buddy Sink
26
+        self.pdbs.close()
27
+
28
+    def test_identify(self):
29
+        self.pdbs.identify()
30
+
31
+    def test_help(self):
32
+        help_text = self.pdbs.help()
33
+        self.assertTrue(len(help_text) > 0)
34
+        self.assertTrue(len(help_text[0]) > 0)
35
+
36
+    def test_license(self):
37
+        license_text = self.pdbs.license()
38
+        self.assertTrue(len(license_text) > 0)
39
+        self.assertTrue(len(license_text[0]) > 0)
40
+
41
+    def test_set_tmpcfg_valid(self):
42
+        self.pdbs.set_tmpcfg(self.obj_valid)
43
+        self.assertEqual(self.pdbs.get_tmpcfg(), self.obj_valid)
44
+
45
+    def test_set_tmpcfg_valid_gb(self):
46
+        self.pdbs.set_tmpcfg(self.obj_valid_gb)
47
+        self.assertEqual(self.pdbs.get_tmpcfg(), self.obj_valid_gb)
48
+
49
+    def test_write(self):
50
+        self.test_set_tmpcfg_valid()
51
+        self.pdbs.write()
52
+        self.assertEqual(self.pdbs.get_cfg(), self.obj_valid)
53
+
54
+    def test_get_cfg_index(self):
55
+        self.assertIsInstance(self.pdbs.get_cfg(0), pdbuddy.SinkConfig)
56
+
57
+    def test_get_cfg_index_bad(self):
58
+        with self.assertRaises(IndexError):
59
+            self.pdbs.get_cfg(-1)
60
+
61
+    def test_load(self):
62
+        # Write obj_valid to flash
63
+        self.test_write()
64
+        # Write obj_valid_gb to tmpcfg
65
+        self.test_set_tmpcfg_valid_gb()
66
+
67
+        self.assertNotEqual(self.pdbs.get_cfg(), self.pdbs.get_tmpcfg())
68
+
69
+        # Load flash to tmpcfg
70
+        self.pdbs.load()
71
+
72
+        self.assertEqual(self.pdbs.get_cfg(), self.pdbs.get_tmpcfg())
73
+
74
+    def test_erase(self):
75
+        self.pdbs.erase()
76
+        with self.assertRaises(KeyError):
77
+            self.pdbs.load()
78
+
79
+    def test_context_manager(self):
80
+        self.pdbs.close()
81
+        with pdbuddy.Sink(list(pdbuddy.Sink.get_devices())[0]) as pdbs:
82
+            # Test something with the conext manager.  For example, this is
83
+            # essentially test_get_cfg_index.
84
+            self.assertIsInstance(pdbs.get_cfg(0), pdbuddy.SinkConfig)
85
+
86
+
8 87
 class SinkConfigTestCase(unittest.TestCase):
9 88
 
10 89
     def setUp(self):

Loading…
Cancel
Save