test_cli_export_config.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. # python-cc1101 - Python Library to Transmit RF Signals via CC1101 Transceivers
  2. #
  3. # Copyright (C) 2020 Fabian Peter Hammerle <fabian@hammerle.me>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. import logging
  18. import unittest.mock
  19. import pytest
  20. import cc1101._cli
  21. from cc1101.options import PacketLengthMode
  22. # pylint: disable=protected-access
  23. @pytest.mark.parametrize(
  24. ("args", "packet_length_mode", "checksum_disabled"),
  25. (
  26. ([""], None, False),
  27. (["", "--packet-length-mode", "variable"], PacketLengthMode.VARIABLE, False),
  28. (["", "--disable-checksum"], None, True),
  29. ),
  30. )
  31. def test_configure_device(args, packet_length_mode, checksum_disabled):
  32. # pylint: disable=too-many-arguments
  33. with unittest.mock.patch("cc1101.CC1101") as transceiver_class_mock:
  34. with unittest.mock.patch("sys.argv", args):
  35. cc1101._cli._export_config()
  36. transceiver_class_mock.assert_called_once_with(lock_spi_device=True)
  37. transceiver_mock = transceiver_class_mock().__enter__()
  38. if packet_length_mode is None:
  39. transceiver_mock.__enter__().set_packet_length_mode.assert_not_called()
  40. else:
  41. transceiver_mock.set_packet_length_mode.assert_called_once_with(
  42. packet_length_mode
  43. )
  44. transceiver_mock.set_packet_length_bytes.assert_not_called()
  45. if checksum_disabled:
  46. transceiver_mock.disable_checksum.assert_called_once_with()
  47. else:
  48. transceiver_mock.disable_checksum.assert_not_called()
  49. @pytest.mark.parametrize(
  50. ("args", "output_power_settings"),
  51. (
  52. ([""], None),
  53. (["", "-p", "192"], [192]),
  54. (["", "--output-power", "192"], [192]),
  55. (["", "-p", "0", "192"], [0, 192]), # OOK
  56. (
  57. ["", "-p", "3", "15", "30", "39", "80", "129", "203", "194"],
  58. [3, 15, 30, 39, 80, 129, 203, 194],
  59. ),
  60. ),
  61. )
  62. def test_configure_device_output_power_settings(args, output_power_settings):
  63. with unittest.mock.patch("cc1101.CC1101") as transceiver_mock:
  64. with unittest.mock.patch("sys.argv", args):
  65. cc1101._cli._export_config()
  66. if output_power_settings is None:
  67. transceiver_mock().__enter__().set_output_power.assert_not_called()
  68. else:
  69. transceiver_mock().__enter__().set_output_power.assert_called_with(
  70. output_power_settings
  71. )
  72. def test_export_python_list(capsys, caplog):
  73. with unittest.mock.patch("cc1101.CC1101") as transceiver_mock:
  74. transceiver_mock().__enter__().get_configuration_register_values.return_value = {
  75. cc1101.addresses.ConfigurationRegisterAddress.IOCFG2: 0x29,
  76. cc1101.addresses.ConfigurationRegisterAddress.IOCFG1: 0x2E,
  77. }
  78. transceiver_mock().__enter__()._get_patable.return_value = [0xC6] + [0] * 7
  79. with unittest.mock.patch("sys.argv", [""]):
  80. with caplog.at_level(logging.INFO):
  81. cc1101._cli._export_config()
  82. assert caplog.record_tuples == [
  83. ("cc1101._cli", 20, str(transceiver_mock().__enter__()))
  84. ]
  85. out, err = capsys.readouterr()
  86. assert not err
  87. assert (
  88. out
  89. == "[\n0b00101001, # 0x29 IOCFG2\n0b00101110, # 0x2e IOCFG1\n]\n"
  90. + "# PATABLE = (0xc6, 0, 0, 0, 0, 0, 0, 0)\n"
  91. )
  92. @pytest.mark.parametrize(
  93. ("args", "root_log_level", "log_format"),
  94. (
  95. ([], logging.INFO, "%(message)s"),
  96. (
  97. ["--debug"],
  98. logging.DEBUG,
  99. "%(asctime)s:%(levelname)s:%(name)s:%(funcName)s:" + "%(message)s",
  100. ),
  101. ),
  102. )
  103. def test_root_log_level(args, root_log_level, log_format):
  104. with unittest.mock.patch("cc1101.CC1101"), unittest.mock.patch(
  105. "sys.argv", [""] + args
  106. ), unittest.mock.patch("logging.basicConfig") as logging_basic_config_mock:
  107. cc1101._cli._export_config()
  108. assert logging_basic_config_mock.call_count == 1
  109. assert logging_basic_config_mock.call_args[1]["level"] == root_log_level
  110. assert logging_basic_config_mock.call_args[1]["format"] == log_format
  111. def test_logging(caplog):
  112. with unittest.mock.patch("sys.argv", [""]), unittest.mock.patch(
  113. "cc1101.CC1101"
  114. ) as transceiver_mock, caplog.at_level(logging.DEBUG):
  115. transceiver_mock().__enter__().__str__.return_value = "dummystr"
  116. cc1101._cli._export_config()
  117. assert len(caplog.records) == 2
  118. assert caplog.records[0].levelno == logging.DEBUG
  119. assert caplog.records[0].name == "cc1101._cli"
  120. assert caplog.records[0].message.startswith(
  121. "args=Namespace(base_frequency_hertz=None, "
  122. )
  123. assert caplog.record_tuples[1] == ("cc1101._cli", logging.INFO, "dummystr")