test_gpio.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import pathlib
  2. import re
  3. import unittest.mock
  4. import pytest
  5. import cc1101._gpio
  6. # pylint: disable=protected-access
  7. @pytest.mark.parametrize(
  8. ("chip_selector", "chip_selector_formatted"),
  9. (
  10. (0, "/dev/gpiochip0"),
  11. (1, "/dev/gpiochip1"),
  12. ("/dev/gpiochip0", "/dev/gpiochip0"),
  13. (pathlib.Path("/dev/gpiochip1"), "/dev/gpiochip1"),
  14. ),
  15. )
  16. def test_get_line_permission_error(chip_selector, chip_selector_formatted):
  17. with unittest.mock.patch(
  18. "gpiod.chip",
  19. side_effect=PermissionError("[Errno 13] Permission denied: '/dev/gpiochip0'"),
  20. ):
  21. with pytest.raises(
  22. PermissionError,
  23. match=r"^Failed to access GPIO chip {}\.".format(
  24. re.escape(chip_selector_formatted)
  25. ),
  26. ):
  27. cc1101._gpio.get_line(chip_selector=chip_selector, line_name="GPIO24")
  28. def test_get_line_file_not_found():
  29. with unittest.mock.patch(
  30. "gpiod.chip",
  31. side_effect=FileNotFoundError(
  32. "[Errno 2] No such file or directory: 'cannot open GPIO device /dev/gpiochip21'"
  33. ),
  34. ):
  35. with pytest.raises(
  36. FileNotFoundError, match=r"^Failed to find GPIO chip /dev/gpiochip21\."
  37. ):
  38. cc1101._gpio.get_line(chip_selector=21, line_name="GPIO24")
  39. def test_get_line_cannot_open():
  40. with unittest.mock.patch(
  41. "gpiod.chip",
  42. side_effect=OSError("[Errno 0] Success: 'cannot open GPIO device 42'"),
  43. ):
  44. with pytest.raises(
  45. FileNotFoundError, match=r"^Failed to find GPIO chip /dev/gpiochip42\."
  46. ):
  47. cc1101._gpio.get_line(chip_selector=42, line_name="GPIO24")
  48. def test_get_line_type_error():
  49. with unittest.mock.patch(
  50. "gpiod.chip",
  51. side_effect=TypeError("iter() returned non-iterator of type 'NoneType'"),
  52. ):
  53. with pytest.raises(
  54. FileNotFoundError, match=r"^Failed to find GPIO chip /dev/gpiochip815\."
  55. ):
  56. cc1101._gpio.get_line(chip_selector="/dev/gpiochip815", line_name="GPIO24")
  57. class _LineMock:
  58. # pylint: disable=too-few-public-methods
  59. def __init__(self, holding: bool):
  60. self._holding = holding
  61. @property
  62. def name(self) -> str:
  63. if not self._holding:
  64. raise RuntimeError("object not holding a GPIO line handle")
  65. return "dummy"
  66. @pytest.mark.parametrize("chip_selector", ("/dev/gpiochip0",))
  67. @pytest.mark.parametrize("line_name", ("GPIO24", "GPIO25"))
  68. def test_get_line(chip_selector, line_name):
  69. with unittest.mock.patch("gpiod.chip") as chip_mock:
  70. chip_mock().find_line.return_value = _LineMock(holding=True)
  71. chip_mock.reset_mock()
  72. assert isinstance(
  73. cc1101._gpio.get_line(chip_selector=chip_selector, line_name=line_name),
  74. _LineMock,
  75. )
  76. chip_mock.assert_called_once_with(chip_selector)
  77. chip_mock().find_line.assert_called_once_with(name=line_name)
  78. @pytest.mark.parametrize("chip_selector", ("/dev/gpiochip0",))
  79. @pytest.mark.parametrize("line_name", ("GPIO24", "GPIO25"))
  80. def test_get_line_unknown_line(chip_selector, line_name):
  81. with unittest.mock.patch("gpiod.chip") as chip_mock:
  82. chip_mock().find_line.return_value = _LineMock(holding=False)
  83. with pytest.raises(
  84. ValueError,
  85. match=r"Failed to find GPIO line with name {}\.".format(
  86. re.escape(repr(line_name))
  87. ),
  88. ):
  89. cc1101._gpio.get_line(chip_selector=chip_selector, line_name=line_name)