test_gpio.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # python-cc1101 - Python Library to Transmit RF Signals via CC1101 Transceivers
  2. #
  3. # Copyright (C) 2021 Fabian Peter Hammerle <fabian@hammerle.me>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. import pathlib
  18. import re
  19. import unittest.mock
  20. import pytest
  21. import cc1101._gpio
  22. # pylint: disable=protected-access
  23. @pytest.mark.parametrize(
  24. ("chip_selector", "chip_selector_formatted"),
  25. (
  26. (0, "/dev/gpiochip0"),
  27. (1, "/dev/gpiochip1"),
  28. ("/dev/gpiochip0", "/dev/gpiochip0"),
  29. (pathlib.Path("/dev/gpiochip1"), "/dev/gpiochip1"),
  30. ),
  31. )
  32. def test_get_line_permission_error(chip_selector, chip_selector_formatted):
  33. with unittest.mock.patch(
  34. "gpiod.chip",
  35. side_effect=PermissionError("[Errno 13] Permission denied: '/dev/gpiochip0'"),
  36. ):
  37. with pytest.raises(
  38. PermissionError,
  39. match=r"^Failed to access GPIO chip {}\.".format(
  40. re.escape(chip_selector_formatted)
  41. ),
  42. ):
  43. cc1101._gpio.get_line(chip_selector=chip_selector, line_name="GPIO24")
  44. def test_get_line_file_not_found():
  45. with unittest.mock.patch(
  46. "gpiod.chip",
  47. side_effect=FileNotFoundError(
  48. "[Errno 2] No such file or directory: 'cannot open GPIO device /dev/gpiochip21'"
  49. ),
  50. ):
  51. with pytest.raises(
  52. FileNotFoundError, match=r"^Failed to find GPIO chip /dev/gpiochip21\."
  53. ):
  54. cc1101._gpio.get_line(chip_selector=21, line_name="GPIO24")
  55. def test_get_line_cannot_open():
  56. with unittest.mock.patch(
  57. "gpiod.chip",
  58. side_effect=OSError("[Errno 0] Success: 'cannot open GPIO device 42'"),
  59. ):
  60. with pytest.raises(
  61. FileNotFoundError, match=r"^Failed to find GPIO chip /dev/gpiochip42\."
  62. ):
  63. cc1101._gpio.get_line(chip_selector=42, line_name="GPIO24")
  64. def test_get_line_type_error():
  65. with unittest.mock.patch(
  66. "gpiod.chip",
  67. side_effect=TypeError("iter() returned non-iterator of type 'NoneType'"),
  68. ):
  69. with pytest.raises(
  70. FileNotFoundError, match=r"^Failed to find GPIO chip /dev/gpiochip815\."
  71. ):
  72. cc1101._gpio.get_line(chip_selector="/dev/gpiochip815", line_name="GPIO24")
  73. class _LineMock:
  74. # pylint: disable=too-few-public-methods
  75. def __init__(self, holding: bool):
  76. self._holding = holding
  77. @property
  78. def name(self) -> str:
  79. if not self._holding:
  80. raise RuntimeError("object not holding a GPIO line handle")
  81. return "dummy"
  82. @pytest.mark.parametrize("chip_selector", ("/dev/gpiochip0",))
  83. @pytest.mark.parametrize("line_name", ("GPIO24", "GPIO25"))
  84. def test_get_line(chip_selector, line_name):
  85. with unittest.mock.patch("gpiod.chip") as chip_mock:
  86. chip_mock().find_line.return_value = _LineMock(holding=True)
  87. chip_mock.reset_mock()
  88. assert isinstance(
  89. cc1101._gpio.get_line(chip_selector=chip_selector, line_name=line_name),
  90. _LineMock,
  91. )
  92. chip_mock.assert_called_once_with(chip_selector)
  93. chip_mock().find_line.assert_called_once_with(name=line_name)
  94. @pytest.mark.parametrize("chip_selector", ("/dev/gpiochip0",))
  95. @pytest.mark.parametrize("line_name", ("GPIO24", "GPIO25"))
  96. def test_get_line_unknown_line(chip_selector, line_name):
  97. with unittest.mock.patch("gpiod.chip") as chip_mock:
  98. chip_mock().find_line.return_value = _LineMock(holding=False)
  99. with pytest.raises(
  100. ValueError,
  101. match=r"Failed to find GPIO line with name {}\.".format(
  102. re.escape(repr(line_name))
  103. ),
  104. ):
  105. cc1101._gpio.get_line(chip_selector=chip_selector, line_name=line_name)