Python library for working with the PD Buddy Sink Serial Console Configuration Interface
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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