test_lock.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # python-cc1101 - Python Library to Transmit RF Signals via C1101 Transceivers
  2. #
  3. # Copyright (C) 2020 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 unittest.mock
  18. import pytest
  19. import cc1101
  20. import cc1101.addresses
  21. # pylint: disable=protected-access
  22. @pytest.fixture(scope="function")
  23. def spidev_mock(tmp_path):
  24. class _SpiDevMock:
  25. def __init__(self):
  26. self._file = None
  27. def open(self, bus, select):
  28. path = tmp_path.joinpath("spidev{}.{}~".format(bus, select))
  29. self._file = path.open("w+")
  30. def fileno(self):
  31. return self._file.fileno()
  32. def close(self):
  33. self._file.close()
  34. return _SpiDevMock
  35. # pylint: disable=redefined-outer-name; using fixture
  36. def test___enter__locked(spidev_mock):
  37. with unittest.mock.patch.object(
  38. cc1101.CC1101, "_reset"
  39. ), unittest.mock.patch.object(
  40. cc1101.CC1101, "_verify_chip"
  41. ), unittest.mock.patch.object(
  42. cc1101.CC1101, "_configure_defaults"
  43. ), unittest.mock.patch.object(
  44. cc1101.CC1101,
  45. "get_main_radio_control_state_machine_state",
  46. return_value=cc1101.MainRadioControlStateMachineState.IDLE,
  47. ):
  48. with unittest.mock.patch("spidev.SpiDev", spidev_mock):
  49. transceiver = cc1101.CC1101(lock_spi_device=True)
  50. with transceiver:
  51. with unittest.mock.patch("spidev.SpiDev", spidev_mock):
  52. transceiver2 = cc1101.CC1101(lock_spi_device=True)
  53. with pytest.raises(BlockingIOError):
  54. with transceiver2:
  55. pass
  56. with unittest.mock.patch("spidev.SpiDev", spidev_mock):
  57. transceiver3 = cc1101.CC1101(lock_spi_device=False)
  58. with transceiver3:
  59. pass
  60. with transceiver2: # closing unlocks
  61. pass
  62. def test___enter__not_locked(spidev_mock):
  63. with unittest.mock.patch.object(
  64. cc1101.CC1101, "_reset"
  65. ), unittest.mock.patch.object(
  66. cc1101.CC1101, "_verify_chip"
  67. ), unittest.mock.patch.object(
  68. cc1101.CC1101, "_configure_defaults"
  69. ), unittest.mock.patch.object(
  70. cc1101.CC1101,
  71. "get_main_radio_control_state_machine_state",
  72. return_value=cc1101.MainRadioControlStateMachineState.IDLE,
  73. ):
  74. with unittest.mock.patch("spidev.SpiDev", spidev_mock):
  75. transceiver = cc1101.CC1101(lock_spi_device=False)
  76. with transceiver:
  77. with unittest.mock.patch("spidev.SpiDev", spidev_mock):
  78. transceiver2 = cc1101.CC1101(lock_spi_device=True)
  79. with transceiver2:
  80. pass