Sfoglia il codice sorgente

add tests for _format_patable, get_base_frequency_hertz & set_base_frequency_hertz

Fabian Peter Hammerle 10 mesi fa
parent
commit
20f58813cf

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

@@ -57,7 +57,7 @@ jobs:
       env:
         PYTHON_VERSION: ${{ matrix.python-version }}
     - run: pipenv graph
-    - run: pipenv run pytest --cov="$(cat *.egg-info/top_level.txt)" --cov-report=term-missing --cov-fail-under=95
+    - run: pipenv run pytest --cov="$(cat *.egg-info/top_level.txt)" --cov-report=term-missing --cov-fail-under=96
     - run: pipenv run pylint "$(cat *.egg-info/top_level.txt)"
     # https://github.com/PyCQA/pylint/issues/352
     - run: pipenv run pylint tests/*

+ 54 - 0
tests/config/test_0x0d_0x0e_0x0f_freq.py

@@ -0,0 +1,54 @@
+# python-cc1101 - Python Library to Transmit RF Signals via CC1101 Transceivers
+#
+# Copyright (C) 2024 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 typing
+
+import pytest
+
+import cc1101
+
+# pylint: disable=protected-access
+
+
+@pytest.mark.parametrize(
+    ("freq210", "frequency_hertz"),
+    [((0x10, 0xB0, 0x71), 433920000), ((0x21, 0x62, 0x76), 868000000)],
+)
+def test_get_base_frequency_hertz(
+    transceiver: cc1101.CC1101,
+    freq210: typing.Tuple[int, int, int],
+    frequency_hertz: int,
+) -> None:
+    transceiver._spi.xfer.return_value = [0] + list(freq210)
+    assert transceiver.get_base_frequency_hertz() == pytest.approx(
+        frequency_hertz, abs=170
+    )
+    transceiver._spi.xfer.assert_called_once_with([0x0D | 0xC0, 0, 0, 0])
+
+
+@pytest.mark.parametrize(
+    ("freq210", "frequency_hertz"),
+    [((0x10, 0xB0, 0x71), 433920000), ((0x21, 0x62, 0x76), 868000000)],
+)
+def test_set_base_frequency_hertz(
+    transceiver: cc1101.CC1101,
+    freq210: typing.Tuple[int, int, int],
+    frequency_hertz: int,
+) -> None:
+    transceiver._spi.xfer.return_value = [15] * (1 + 3)
+    transceiver.set_base_frequency_hertz(frequency_hertz)
+    transceiver._spi.xfer.assert_called_once_with([0x0D | 0x40] + list(freq210))

+ 16 - 0
tests/test_config.py

@@ -15,6 +15,7 @@
 # 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 typing
 import unittest.mock
 import warnings
 
@@ -158,6 +159,21 @@ def test_set_base_frequency_hertz_low_warning(transceiver, freq_hz, warn):
         assert not caught_warnings
 
 
+@pytest.mark.parametrize(
+    ("settings", "insert_spaces", "expected_str"),
+    [
+        ((198,), True, "(0xc6,)"),
+        ((198,), False, "(0xc6,)"),
+        ((0, 198), True, "(0, 0xc6)"),
+        ((21, 42, 17), False, "(0x15,0x2a,0x11)"),
+    ],
+)
+def test__format_patable(
+    settings: typing.List[int], insert_spaces: bool, expected_str: str
+) -> None:
+    assert cc1101._format_patable(settings, insert_spaces=insert_spaces) == expected_str
+
+
 @pytest.mark.parametrize(
     ("patable", "patable_index", "power_levels"),
     (