Browse Source

add tests for "spi_max_speed_hz" argument of CC1101's constructor

https://github.com/fphammerle/python-cc1101/pull/129#issuecomment-1533761687
Fabian Peter Hammerle 1 year ago
parent
commit
0f90b4ca39
2 changed files with 16 additions and 1 deletions
  1. 1 1
      cc1101/__init__.py
  2. 15 0
      tests/test_spi.py

+ 1 - 1
cc1101/__init__.py

@@ -181,7 +181,7 @@ class CC1101:
             >>>     # lock removed
         """
         self._spi = spidev.SpiDev()
-        self._spi_max_speed_hz = int(spi_max_speed_hz)
+        self._spi_max_speed_hz = spi_max_speed_hz
         self._spi_bus = int(spi_bus)
         # > The BCM2835 core common to all Raspberry Pi devices has 3 SPI Controllers:
         # > SPI0, with two hardware chip selects, [...]

+ 15 - 0
tests/test_spi.py

@@ -35,6 +35,7 @@ def test___init__select_device(bus, chip_select):
     assert transceiver._spi_bus == bus
     assert transceiver._spi_chip_select == chip_select
     assert transceiver._spi_device_path == f"/dev/spidev{bus}.{chip_select}"
+    assert transceiver._spi_max_speed_hz == 55700
     transceiver._spi.open.side_effect = SystemExit
     with pytest.raises(SystemExit):
         with transceiver:
@@ -87,6 +88,7 @@ def test___enter__(transceiver, chip_version):
             assert transceiver == transceiver_context
             transceiver._spi.open.assert_called_once_with(0, 0)
             assert transceiver._spi.max_speed_hz == 55700
+            assert transceiver._spi.max_speed_hz == transceiver._spi_max_speed_hz
             reset_mock.assert_called_once_with()
             set_modulation_format_mock.assert_called_once_with(
                 cc1101.options.ModulationFormat.ASK_OOK
@@ -99,6 +101,19 @@ def test___enter__(transceiver, chip_version):
             ]
 
 
+@pytest.mark.parametrize("spi_max_speed_hz", [55700, 500000])
+def test___enter__spi_max_speed(spi_max_speed_hz):
+    with unittest.mock.patch("spidev.SpiDev"):
+        transceiver = cc1101.CC1101(spi_max_speed_hz=spi_max_speed_hz)
+    assert transceiver._spi_max_speed_hz == spi_max_speed_hz
+    with unittest.mock.patch.object(
+        transceiver, "_reset", side_effect=SystemExit
+    ), pytest.raises(SystemExit):
+        with transceiver:
+            pass
+    assert transceiver._spi.max_speed_hz == spi_max_speed_hz
+
+
 def test___enter___unsupported_partnum(transceiver):
     with unittest.mock.patch.object(
         transceiver, "_read_status_register"