Browse Source

increase test coverage

Fabian Peter Hammerle 3 years ago
parent
commit
ba8cb371c9
3 changed files with 96 additions and 1 deletions
  1. 1 1
      .github/workflows/python.yml
  2. 18 0
      tests/test_config.py
  3. 77 0
      tests/test_spi.py

+ 1 - 1
.github/workflows/python.yml

@@ -50,7 +50,7 @@ jobs:
       env:
         PYTHON_VERSION: ${{ matrix.python-version }}
     - run: pipenv graph
-    - run: pipenv run pytest --cov=cc1101 --cov-report=term-missing --cov-fail-under=72
+    - run: pipenv run pytest --cov=cc1101 --cov-report=term-missing --cov-fail-under=81
     - run: pipenv run pylint --load-plugins=pylint_import_requirements cc1101
     # https://github.com/PyCQA/pylint/issues/352
     - run: pipenv run pylint tests/*

+ 18 - 0
tests/test_config.py

@@ -113,6 +113,24 @@ def test__get_filter_bandwidth_hertz(transceiver, mdmcfg4, real):
     transceiver._spi.xfer.assert_called_once_with([0x10 | 0x80, 0])
 
 
+@pytest.mark.parametrize(
+    ("mdmcfg4", "symbol_rate_exponent"), [(0b1001100, 12), (0b10011001, 9)]
+)
+def test__get_symbol_rate_exponent(transceiver, mdmcfg4, symbol_rate_exponent):
+    transceiver._spi.xfer.return_value = [15, mdmcfg4]
+    assert transceiver._get_symbol_rate_exponent() == symbol_rate_exponent
+    transceiver._spi.xfer.assert_called_once_with([0x10 | 0x80, 0])
+
+
+@pytest.mark.parametrize(
+    ("mdmcfg3", "symbol_rate_mantissa"), [(0b00100010, 34), (0b10101010, 170)]
+)
+def test__get_symbol_rate_mantissa(transceiver, mdmcfg3, symbol_rate_mantissa):
+    transceiver._spi.xfer.return_value = [15, mdmcfg3]
+    assert transceiver._get_symbol_rate_mantissa() == symbol_rate_mantissa
+    transceiver._spi.xfer.assert_called_once_with([0x11 | 0x80, 0])
+
+
 _SYMBOL_RATE_MANTISSA_EXPONENT_REAL_PARAMS = [
     # > The default values give a data rate of 115.051 kBaud
     # > (closest setting to 115.2 kBaud), assuming a 26.0 MHz crystal.

+ 77 - 0
tests/test_spi.py

@@ -0,0 +1,77 @@
+# python-cc1101 - Python Library to Transmit RF Signals via C1101 Transceivers
+#
+# Copyright (C) 2020 Fabian Peter Hammerle <fabian@hammerle.me>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+import unittest.mock
+
+import cc1101
+import cc1101.addresses
+import cc1101.options
+
+# pylint: disable=protected-access
+
+
+def test__read_status_register(transceiver):
+    transceiver._spi.xfer.return_value = [0, 20]
+    transceiver._read_status_register(cc1101.addresses.StatusRegisterAddress.VERSION)
+    transceiver._spi.xfer.assert_called_once_with([0x31 | 0xC0, 0])
+
+
+def test__command_strobe(transceiver):
+    transceiver._spi.xfer.return_value = [15]
+    transceiver._command_strobe(cc1101.addresses.StrobeAddress.STX)
+    transceiver._spi.xfer.assert_called_once_with([0x35 | 0x00])
+
+
+def test__reset(transceiver):
+    transceiver._spi.xfer.return_value = [15]
+    transceiver._reset()
+    transceiver._spi.xfer.assert_called_once_with([0x30 | 0x00])
+
+
+def test___enter__(transceiver):
+    with unittest.mock.patch.object(
+        transceiver, "_read_status_register"
+    ) as read_status_register_mock, unittest.mock.patch.object(
+        transceiver, "_reset"
+    ) as reset_mock, unittest.mock.patch.object(
+        transceiver, "_set_modulation_format"
+    ) as set_modulation_format_mock, unittest.mock.patch.object(
+        transceiver, "_set_power_amplifier_setting_index"
+    ) as set_pa_setting_mock, unittest.mock.patch.object(
+        transceiver, "_disable_data_whitening"
+    ) as disable_whitening_mock, unittest.mock.patch.object(
+        transceiver, "_write_burst"
+    ) as write_burst_mock, unittest.mock.patch.object(
+        transceiver,
+        "get_main_radio_control_state_machine_state",
+        return_value=cc1101.MainRadioControlStateMachineState.IDLE,
+    ):
+        read_status_register_mock.side_effect = lambda r: {
+            cc1101.addresses.StatusRegisterAddress.PARTNUM: 0,
+            cc1101.addresses.StatusRegisterAddress.VERSION: 0x14,
+        }[r]
+        with transceiver as transceiver_context:
+            assert transceiver == transceiver_context
+            transceiver._spi.open.assert_called_once_with(0, 0)
+            assert transceiver._spi.max_speed_hz == 55700
+            reset_mock.assert_called_once_with()
+            set_modulation_format_mock.assert_called_once_with(
+                cc1101.options.ModulationFormat.ASK_OOK
+            )
+            set_pa_setting_mock.assert_called_once_with(1)
+            disable_whitening_mock.assert_called_once_with()
+            write_burst_mock.assert_called_once_with(0x18, [0b010100])