Python library for working with the PD Buddy Sink Serial Console Configuration Interface
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

__init__.py 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. """Unit tests for the top-level pdbuddy classes"""
  2. import unittest
  3. import pdbuddy
  4. class SinkTestCase(unittest.TestCase):
  5. def setUp(self):
  6. # Get devices
  7. pdbs_devices = list(pdbuddy.Sink.get_devices())
  8. # If there are no devices, skip the test
  9. if len(pdbs_devices) == 0:
  10. self.skipTest("No PD Buddy Sink devices found")
  11. # Open the first device
  12. self.pdbs = pdbuddy.Sink(pdbs_devices[0])
  13. self.obj_valid = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
  14. flags=pdbuddy.SinkFlags.NONE, v=15000, i=3000)
  15. self.obj_valid_gb = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
  16. flags=pdbuddy.SinkFlags.GIVEBACK, v=15000, i=3000)
  17. def tearDown(self):
  18. # Close the connection to the PD Buddy Sink
  19. self.pdbs.close()
  20. def test_identify(self):
  21. self.pdbs.identify()
  22. def test_help(self):
  23. help_text = self.pdbs.help()
  24. self.assertTrue(len(help_text) > 0)
  25. self.assertTrue(len(help_text[0]) > 0)
  26. def test_license(self):
  27. license_text = self.pdbs.license()
  28. self.assertTrue(len(license_text) > 0)
  29. self.assertTrue(len(license_text[0]) > 0)
  30. def test_set_tmpcfg_valid(self):
  31. self.pdbs.set_tmpcfg(self.obj_valid)
  32. self.assertEqual(self.pdbs.get_tmpcfg(), self.obj_valid)
  33. def test_set_tmpcfg_valid_gb(self):
  34. self.pdbs.set_tmpcfg(self.obj_valid_gb)
  35. self.assertEqual(self.pdbs.get_tmpcfg(), self.obj_valid_gb)
  36. def test_write(self):
  37. self.test_set_tmpcfg_valid()
  38. self.pdbs.write()
  39. self.assertEqual(self.pdbs.get_cfg(), self.obj_valid)
  40. def test_get_cfg_index(self):
  41. self.assertIsInstance(self.pdbs.get_cfg(0), pdbuddy.SinkConfig)
  42. def test_get_cfg_index_bad(self):
  43. with self.assertRaises(IndexError):
  44. self.pdbs.get_cfg(-1)
  45. def test_load(self):
  46. # Write obj_valid to flash
  47. self.test_write()
  48. # Write obj_valid_gb to tmpcfg
  49. self.test_set_tmpcfg_valid_gb()
  50. self.assertNotEqual(self.pdbs.get_cfg(), self.pdbs.get_tmpcfg())
  51. # Load flash to tmpcfg
  52. self.pdbs.load()
  53. self.assertEqual(self.pdbs.get_cfg(), self.pdbs.get_tmpcfg())
  54. def test_erase(self):
  55. self.pdbs.erase()
  56. with self.assertRaises(KeyError):
  57. self.pdbs.load()
  58. def test_context_manager(self):
  59. self.pdbs.close()
  60. with pdbuddy.Sink(list(pdbuddy.Sink.get_devices())[0]) as pdbs:
  61. # Test something with the conext manager. For example, this is
  62. # essentially test_get_cfg_index.
  63. self.assertIsInstance(pdbs.get_cfg(0), pdbuddy.SinkConfig)
  64. class SinkConfigTestCase(unittest.TestCase):
  65. def setUp(self):
  66. self.obj_none = pdbuddy.SinkConfig()
  67. self.obj_empty = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.EMPTY)
  68. self.obj_valid = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
  69. flags=pdbuddy.SinkFlags.NONE, v=15000, i=3000)
  70. self.obj_invalid = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.INVALID,
  71. flags=pdbuddy.SinkFlags.NONE, v=15000, i=3000)
  72. self.obj_valid_gb = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
  73. flags=pdbuddy.SinkFlags.GIVEBACK, v=15000, i=3000)
  74. self.obj_valid_5v = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
  75. flags=pdbuddy.SinkFlags.NONE, v=5000, i=3000)
  76. self.obj_valid_1a = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
  77. flags=pdbuddy.SinkFlags.NONE, v=15000, i=1000)
  78. def test_str_none(self):
  79. self.assertEqual(str(self.obj_none), "No configuration")
  80. def test_str_empty(self):
  81. self.assertEqual(str(self.obj_empty), "status: empty")
  82. def test_str_valid(self):
  83. self.assertEqual(str(self.obj_valid),
  84. "status: valid\nflags: (none)\nv: 15.00 V\ni: 3.00 A")
  85. def test_str_invalid(self):
  86. self.assertEqual(str(self.obj_invalid),
  87. "status: invalid\nflags: (none)\nv: 15.00 V\ni: 3.00 A")
  88. def test_str_valid_gb(self):
  89. self.assertEqual(str(self.obj_valid_gb),
  90. "status: valid\nflags: GiveBack\nv: 15.00 V\ni: 3.00 A")
  91. def test_eq_none(self):
  92. self.assertTrue(self.obj_none == pdbuddy.SinkConfig())
  93. def test_eq_valid(self):
  94. # Set invalid object as valid
  95. self.obj_invalid.status = pdbuddy.SinkStatus.VALID
  96. self.assertTrue(self.obj_valid == self.obj_invalid)
  97. def test_eq_wrong_type(self):
  98. self.assertFalse(self.obj_none == "hello world")
  99. def test_eq_wrong_status(self):
  100. self.assertFalse(self.obj_valid == self.obj_invalid)
  101. def test_eq_wrong_flags(self):
  102. self.assertFalse(self.obj_valid == self.obj_valid_gb)
  103. def test_eq_wrong_voltage(self):
  104. self.assertFalse(self.obj_valid == self.obj_valid_5v)
  105. def test_eq_wrong_current(self):
  106. self.assertFalse(self.obj_valid == self.obj_valid_1a)
  107. def test_ne_none(self):
  108. self.assertFalse(self.obj_none != pdbuddy.SinkConfig())
  109. def test_ne_valid(self):
  110. # Set invalid object as valid
  111. self.obj_invalid.status = pdbuddy.SinkStatus.VALID
  112. self.assertFalse(self.obj_valid != self.obj_invalid)
  113. def test_ne_wrong_type(self):
  114. self.assertTrue(self.obj_none != "hello world")
  115. def test_ne_wrong_status(self):
  116. self.assertTrue(self.obj_valid != self.obj_invalid)
  117. def test_hash_identical(self):
  118. self.assertEqual(hash(self.obj_none), hash(pdbuddy.SinkConfig()))
  119. def test_hash_different(self):
  120. self.assertNotEqual(hash(self.obj_none), hash(self.obj_valid))
  121. def test_from_text_none(self):
  122. ft_none = pdbuddy.SinkConfig.from_text([])
  123. self.assertEqual(ft_none, self.obj_none)
  124. def test_from_text_empty(self):
  125. ft_empty = pdbuddy.SinkConfig.from_text([b"status: empty"])
  126. self.assertEqual(ft_empty, self.obj_empty)
  127. def test_from_text_valid(self):
  128. ft_valid = pdbuddy.SinkConfig.from_text([b"status: valid",
  129. b"flags: (none)",
  130. b"v: 15.00 V",
  131. b"i: 3.00 A"])
  132. self.assertEqual(ft_valid, self.obj_valid)
  133. def test_from_text_invalid(self):
  134. ft_invalid = pdbuddy.SinkConfig.from_text([b"status: invalid",
  135. b"flags: (none)",
  136. b"v: 15.00 V",
  137. b"i: 3.00 A"])
  138. self.assertEqual(ft_invalid, self.obj_invalid)
  139. def test_from_text_valid_gb(self):
  140. ft_valid_gb = pdbuddy.SinkConfig.from_text([b"status: valid",
  141. b"flags: GiveBack",
  142. b"v: 15.00 V",
  143. b"i: 3.00 A"])
  144. self.assertEqual(ft_valid_gb, self.obj_valid_gb)
  145. def test_from_text_valid_5v(self):
  146. ft_valid_5v = pdbuddy.SinkConfig.from_text([b"status: valid",
  147. b"flags: (none)",
  148. b"v: 5.00 V",
  149. b"i: 3.00 A"])
  150. self.assertEqual(ft_valid_5v, self.obj_valid_5v)
  151. def test_from_text_valid_1a(self):
  152. ft_valid_1a = pdbuddy.SinkConfig.from_text([b"status: valid",
  153. b"flags: (none)",
  154. b"v: 15.00 V",
  155. b"i: 1.00 A"])
  156. self.assertEqual(ft_valid_1a, self.obj_valid_1a)
  157. def test_from_text_invalid_index(self):
  158. with self.assertRaises(IndexError):
  159. pdbuddy.SinkConfig.from_text([b"Invalid index"])
  160. def test_from_text_no_configuration(self):
  161. ft_no_config = pdbuddy.SinkConfig.from_text([b"No configuration"])
  162. self.assertEqual(ft_no_config, self.obj_none)
  163. def test_from_text_valid_extra(self):
  164. ft_valid = pdbuddy.SinkConfig.from_text([b"status: valid",
  165. b"flags: (none)",
  166. b"This is an extra line, which shouldn't hurt anything.",
  167. b"v: 15.00 V",
  168. b"i: 3.00 A"])
  169. self.assertEqual(ft_valid, self.obj_valid)