Browse Source

added tests for library

Fabian Peter Hammerle 3 years ago
parent
commit
79372be6c3
3 changed files with 103 additions and 0 deletions
  1. 34 0
      tests/test_cc1101_config.py
  2. 24 0
      tests/test_encode.py
  3. 45 0
      tests/test_remote_control.py

+ 34 - 0
tests/test_cc1101_config.py

@@ -0,0 +1,34 @@
+import unittest.mock
+
+import cc1101
+import pytest
+
+import intertechno_cc1101
+
+# pylint: disable=protected-access
+
+
+@pytest.mark.parametrize(
+    "payload",
+    (
+        b"\x04\x00\xa0\xa0\x82\xa0\x82\x82\x82\x82\xa0\xa0\xa0\x82\x82\xa0\xa0"
+        b"\xa0\xa0\x82\xa0\x82\xa0\xa0\x82\x82\x82\xa0\xa0\x82\xa0\xa0\xa0\xa0\x80",
+    ),
+)
+def test__cc1101_transmit(payload):
+    with unittest.mock.patch("cc1101.CC1101") as transceiver_class_mock:
+        intertechno_cc1101._cc1101_transmit(payload=payload)
+    transceiver_class_mock.assert_called_once_with(lock_spi_device=True)
+    method_calls = transceiver_class_mock().__enter__().mock_calls
+    assert method_calls[:-3] == [
+        unittest.mock.call.set_base_frequency_hertz(433930000),
+        unittest.mock.call.set_symbol_rate_baud(3942),
+        unittest.mock.call.set_sync_mode(cc1101.SyncMode.NO_PREAMBLE_AND_SYNC_WORD),
+        unittest.mock.call.set_packet_length_mode(cc1101.PacketLengthMode.FIXED),
+        unittest.mock.call.set_packet_length_bytes(35),
+        unittest.mock.call.disable_checksum(),
+        unittest.mock.call.set_output_power((0, 0xC6)),
+    ]
+    transmit_call = method_calls[-3]
+    assert transmit_call == ("transmit", (payload,), {})
+    assert all(transmit_call == t for t in method_calls[-2:])

+ 24 - 0
tests/test_encode.py

@@ -0,0 +1,24 @@
+import pytest
+
+import intertechno_cc1101
+
+# pylint: disable=protected-access
+
+
+@pytest.mark.parametrize(
+    ("message", "payload"),
+    (
+        (
+            (12345678 << 6) | (0b01 << 4) | 0b0000,
+            b"\x04\x00\xa0\xa0\x82\xa0\x82\x82\x82\x82\xa0\xa0\xa0\x82\x82\xa0\xa0"
+            b"\xa0\xa0\x82\xa0\x82\xa0\xa0\x82\x82\x82\xa0\xa0\x82\xa0\xa0\xa0\xa0\x80",
+        ),
+        (
+            (12345678 << 6) | (0b00 << 4) | 0b0111,
+            b"\x04\x00\xa0\xa0\x82\xa0\x82\x82\x82\x82\xa0\xa0\xa0\x82\x82\xa0\xa0"
+            b"\xa0\xa0\x82\xa0\x82\xa0\xa0\x82\x82\x82\xa0\xa0\xa0\xa0\x82\x82\x82\x80",
+        ),
+    ),
+)
+def test__encode_message(message, payload):
+    assert intertechno_cc1101._encode_message(message) == payload

+ 45 - 0
tests/test_remote_control.py

@@ -0,0 +1,45 @@
+import unittest.mock
+
+import pytest
+
+import intertechno_cc1101
+
+# pylint: disable=protected-access
+
+
+@pytest.mark.parametrize(
+    ("address", "button_index", "command", "expected_message"),
+    ((12345678, 0, 0b00, 790123392), (2 ** 26 - 1, 0b1111, 0b01, 4294967263)),
+)
+def test__send_command(address, button_index, command, expected_message):
+    remote_control = intertechno_cc1101.RemoteControl(address=address)
+    with unittest.mock.patch(
+        "intertechno_cc1101._encode_message", return_value=b"dummy"
+    ) as encode_message_mock, unittest.mock.patch(
+        "intertechno_cc1101._cc1101_transmit"
+    ) as transmit_mock:
+        remote_control._send_command(button_index=button_index, command=command)
+    encode_message_mock.assert_called_once_with(expected_message)
+    transmit_mock.assert_called_once_with(b"dummy")
+
+
+@pytest.mark.parametrize("address", [12345678])
+@pytest.mark.parametrize("button_index", [7])
+def test_turn_on(address, button_index):
+    remote_control = intertechno_cc1101.RemoteControl(address=address)
+    with unittest.mock.patch.object(
+        remote_control, "_send_command"
+    ) as send_command_mock:
+        remote_control.turn_on(button_index=button_index)
+    send_command_mock.assert_called_once_with(button_index=button_index, command=0b01)
+
+
+@pytest.mark.parametrize("address", [12345678])
+@pytest.mark.parametrize("button_index", [7])
+def test_turn_off(address, button_index):
+    remote_control = intertechno_cc1101.RemoteControl(address=address)
+    with unittest.mock.patch.object(
+        remote_control, "_send_command"
+    ) as send_command_mock:
+        remote_control.turn_off(button_index=button_index)
+    send_command_mock.assert_called_once_with(button_index=button_index, command=0b00)