|
@@ -0,0 +1,96 @@
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+import unittest.mock
|
|
|
+
|
|
|
+import pytest
|
|
|
+
|
|
|
+import cc1101
|
|
|
+import cc1101.addresses
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+@pytest.fixture(scope="function")
|
|
|
+def spidev_mock(tmp_path):
|
|
|
+ class _SpiDevMock:
|
|
|
+ def __init__(self):
|
|
|
+ self._file = None
|
|
|
+
|
|
|
+ def open(self, bus, select):
|
|
|
+ path = tmp_path.joinpath("spidev{}.{}~".format(bus, select))
|
|
|
+ self._file = path.open("w+")
|
|
|
+
|
|
|
+ def fileno(self):
|
|
|
+ return self._file.fileno()
|
|
|
+
|
|
|
+ def close(self):
|
|
|
+ self._file.close()
|
|
|
+
|
|
|
+ return _SpiDevMock
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+def test___enter__locked(spidev_mock):
|
|
|
+ with unittest.mock.patch.object(
|
|
|
+ cc1101.CC1101, "_reset"
|
|
|
+ ), unittest.mock.patch.object(
|
|
|
+ cc1101.CC1101, "_verify_chip"
|
|
|
+ ), unittest.mock.patch.object(
|
|
|
+ cc1101.CC1101, "_configure_defaults"
|
|
|
+ ), unittest.mock.patch.object(
|
|
|
+ cc1101.CC1101,
|
|
|
+ "get_main_radio_control_state_machine_state",
|
|
|
+ return_value=cc1101.MainRadioControlStateMachineState.IDLE,
|
|
|
+ ):
|
|
|
+ with unittest.mock.patch("spidev.SpiDev", spidev_mock):
|
|
|
+ transceiver = cc1101.CC1101(lock_spi_device=True)
|
|
|
+ with transceiver:
|
|
|
+ with unittest.mock.patch("spidev.SpiDev", spidev_mock):
|
|
|
+ transceiver2 = cc1101.CC1101(lock_spi_device=True)
|
|
|
+ with pytest.raises(BlockingIOError):
|
|
|
+ with transceiver2:
|
|
|
+ pass
|
|
|
+ with unittest.mock.patch("spidev.SpiDev", spidev_mock):
|
|
|
+ transceiver3 = cc1101.CC1101(lock_spi_device=False)
|
|
|
+ with transceiver3:
|
|
|
+ pass
|
|
|
+ with transceiver2:
|
|
|
+ pass
|
|
|
+
|
|
|
+
|
|
|
+def test___enter__not_locked(spidev_mock):
|
|
|
+ with unittest.mock.patch.object(
|
|
|
+ cc1101.CC1101, "_reset"
|
|
|
+ ), unittest.mock.patch.object(
|
|
|
+ cc1101.CC1101, "_verify_chip"
|
|
|
+ ), unittest.mock.patch.object(
|
|
|
+ cc1101.CC1101, "_configure_defaults"
|
|
|
+ ), unittest.mock.patch.object(
|
|
|
+ cc1101.CC1101,
|
|
|
+ "get_main_radio_control_state_machine_state",
|
|
|
+ return_value=cc1101.MainRadioControlStateMachineState.IDLE,
|
|
|
+ ):
|
|
|
+ with unittest.mock.patch("spidev.SpiDev", spidev_mock):
|
|
|
+ transceiver = cc1101.CC1101(lock_spi_device=False)
|
|
|
+ with transceiver:
|
|
|
+ with unittest.mock.patch("spidev.SpiDev", spidev_mock):
|
|
|
+ transceiver2 = cc1101.CC1101(lock_spi_device=True)
|
|
|
+ with transceiver2:
|
|
|
+ pass
|