|
@@ -15,6 +15,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
+import contextlib
|
|
|
import unittest.mock
|
|
|
|
|
|
import pytest
|
|
@@ -36,10 +37,12 @@ def spidev_mock(tmp_path):
|
|
|
self._file = path.open("w+")
|
|
|
|
|
|
def fileno(self):
|
|
|
- return self._file.fileno()
|
|
|
+
|
|
|
+ return self._file.fileno() if self._file else -1
|
|
|
|
|
|
def close(self):
|
|
|
self._file.close()
|
|
|
+ self._file = None
|
|
|
|
|
|
return _SpiDevMock
|
|
|
|
|
@@ -47,7 +50,8 @@ def spidev_mock(tmp_path):
|
|
|
|
|
|
|
|
|
|
|
|
-def test___enter__locked(spidev_mock):
|
|
|
+@contextlib.contextmanager
|
|
|
+def _mock_hardware_access():
|
|
|
with unittest.mock.patch.object(
|
|
|
cc1101.CC1101, "_reset"
|
|
|
), unittest.mock.patch.object(
|
|
@@ -59,6 +63,11 @@ def test___enter__locked(spidev_mock):
|
|
|
"get_main_radio_control_state_machine_state",
|
|
|
return_value=cc1101.MainRadioControlStateMachineState.IDLE,
|
|
|
):
|
|
|
+ yield
|
|
|
+
|
|
|
+
|
|
|
+def test_context_lock(spidev_mock):
|
|
|
+ with _mock_hardware_access():
|
|
|
with unittest.mock.patch("spidev.SpiDev", spidev_mock):
|
|
|
transceiver = cc1101.CC1101(lock_spi_device=True)
|
|
|
with transceiver:
|
|
@@ -75,18 +84,8 @@ def test___enter__locked(spidev_mock):
|
|
|
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,
|
|
|
- ):
|
|
|
+def test_context_no_lock(spidev_mock):
|
|
|
+ with _mock_hardware_access():
|
|
|
with unittest.mock.patch("spidev.SpiDev", spidev_mock):
|
|
|
transceiver = cc1101.CC1101(lock_spi_device=False)
|
|
|
with transceiver:
|
|
@@ -94,3 +93,47 @@ def test___enter__not_locked(spidev_mock):
|
|
|
transceiver2 = cc1101.CC1101(lock_spi_device=True)
|
|
|
with transceiver2:
|
|
|
pass
|
|
|
+
|
|
|
+
|
|
|
+def test_unlock_spi_device(spidev_mock):
|
|
|
+ with _mock_hardware_access():
|
|
|
+ with unittest.mock.patch("spidev.SpiDev", spidev_mock):
|
|
|
+ transceiver = cc1101.CC1101(lock_spi_device=True)
|
|
|
+ transceiver2 = cc1101.CC1101(lock_spi_device=True)
|
|
|
+ with transceiver:
|
|
|
+ with pytest.raises(BlockingIOError):
|
|
|
+ with transceiver2:
|
|
|
+ pass
|
|
|
+ transceiver.unlock_spi_device()
|
|
|
+ with transceiver2:
|
|
|
+ pass
|
|
|
+
|
|
|
+
|
|
|
+def test_unlock_spi_device_double(spidev_mock):
|
|
|
+ with _mock_hardware_access():
|
|
|
+ with unittest.mock.patch("spidev.SpiDev", spidev_mock):
|
|
|
+ transceiver = cc1101.CC1101(lock_spi_device=True)
|
|
|
+
|
|
|
+ with transceiver:
|
|
|
+ transceiver.unlock_spi_device()
|
|
|
+ transceiver.unlock_spi_device()
|
|
|
+
|
|
|
+
|
|
|
+def test_unlock_spi_device_outside_context(spidev_mock):
|
|
|
+ with _mock_hardware_access():
|
|
|
+ with unittest.mock.patch("spidev.SpiDev", spidev_mock):
|
|
|
+ transceiver = cc1101.CC1101(lock_spi_device=True)
|
|
|
+
|
|
|
+ transceiver.unlock_spi_device()
|
|
|
+ with transceiver:
|
|
|
+ pass
|
|
|
+ transceiver.unlock_spi_device()
|
|
|
+
|
|
|
+
|
|
|
+def test_unlock_spi_device_no_lock(spidev_mock):
|
|
|
+ with _mock_hardware_access():
|
|
|
+ with unittest.mock.patch("spidev.SpiDev", spidev_mock):
|
|
|
+ transceiver = cc1101.CC1101(lock_spi_device=False)
|
|
|
+ with transceiver:
|
|
|
+
|
|
|
+ transceiver.unlock_spi_device()
|