1
0

test_cli_export_config.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. with transceiver_class_mock() as transceiver_mock:
  38. pass
  39. if packet_length_mode is None:
  40. transceiver_mock.set_packet_length_mode.assert_not_called()
  41. else:
  42. transceiver_mock.set_packet_length_mode.assert_called_once_with(
  43. packet_length_mode
  44. )
  45. transceiver_mock.set_packet_length_bytes.assert_not_called()
  46. if checksum_disabled: # pylint: disable=duplicate-code
  47. transceiver_mock.disable_checksum.assert_called_once_with()
  48. else:
  49. transceiver_mock.disable_checksum.assert_not_called()
  50. @pytest.mark.parametrize(
  51. ("args", "output_power_settings"),
  52. (
  53. ([""], None),
  54. (["", "-p", "192"], [192]),
  55. (["", "--output-power", "192"], [192]),
  56. (["", "-p", "0", "192"], [0, 192]), # OOK
  57. (
  58. ["", "-p", "3", "15", "30", "39", "80", "129", "203", "194"],
  59. [3, 15, 30, 39, 80, 129, 203, 194],
  60. ),
  61. ),
  62. )
  63. def test_configure_device_output_power_settings(args, output_power_settings):
  64. with unittest.mock.patch("cc1101.CC1101") as transceiver_class_mock:
  65. with unittest.mock.patch("sys.argv", args):
  66. cc1101._cli._export_config()
  67. with transceiver_class_mock() as transceiver_mock:
  68. pass
  69. if output_power_settings is None:
  70. transceiver_mock.set_output_power.assert_not_called()
  71. else:
  72. transceiver_mock.set_output_power.assert_called_with(output_power_settings)
  73. def test_export_python_list(capsys, caplog):
  74. with unittest.mock.patch("cc1101.CC1101") as transceiver_class_mock:
  75. with transceiver_class_mock() as transceiver_mock:
  76. pass
  77. transceiver_mock.get_configuration_register_values.return_value = {
  78. cc1101.addresses.ConfigurationRegisterAddress.IOCFG2: 0x29,
  79. cc1101.addresses.ConfigurationRegisterAddress.IOCFG1: 0x2E,
  80. }
  81. transceiver_mock._get_patable.return_value = [0xC6] + [0] * 7
  82. with unittest.mock.patch("sys.argv", [""]):
  83. with caplog.at_level(logging.INFO):
  84. cc1101._cli._export_config()
  85. assert caplog.record_tuples == [("cc1101._cli", 20, str(transceiver_mock))]
  86. out, err = capsys.readouterr()
  87. assert not err
  88. assert (
  89. out
  90. == "[\n0b00101001, # 0x29 IOCFG2\n0b00101110, # 0x2e IOCFG1\n]\n"
  91. + "# PATABLE = (0xc6, 0, 0, 0, 0, 0, 0, 0)\n"
  92. )
  93. @pytest.mark.parametrize(
  94. ("args", "root_log_level", "log_format"),
  95. (
  96. ([], logging.INFO, "%(message)s"),
  97. (
  98. ["--debug"],
  99. logging.DEBUG,
  100. "%(asctime)s:%(levelname)s:%(name)s:%(funcName)s:" + "%(message)s",
  101. ),
  102. ),
  103. )
  104. def test_root_log_level(args, root_log_level, log_format):
  105. with unittest.mock.patch("cc1101.CC1101"), unittest.mock.patch(
  106. "sys.argv", [""] + args
  107. ), unittest.mock.patch("logging.basicConfig") as logging_basic_config_mock:
  108. cc1101._cli._export_config()
  109. logging_basic_config_mock.assert_called_once()
  110. assert logging_basic_config_mock.call_args[1]["level"] == root_log_level
  111. assert logging_basic_config_mock.call_args[1]["format"] == log_format
  112. def test_logging(caplog):
  113. with unittest.mock.patch("sys.argv", [""]), unittest.mock.patch(
  114. "cc1101.CC1101"
  115. ) as transceiver_class_mock, caplog.at_level(logging.DEBUG):
  116. with transceiver_class_mock() as transceiver_mock:
  117. transceiver_mock.__str__.return_value = "dummystr"
  118. cc1101._cli._export_config()
  119. assert len(caplog.records) == 2
  120. assert caplog.records[0].levelno == logging.DEBUG
  121. assert caplog.records[0].name == "cc1101._cli"
  122. assert caplog.records[0].message.startswith(
  123. "args=Namespace(base_frequency_hertz=None, "
  124. )
  125. assert caplog.record_tuples[1] == ("cc1101._cli", logging.INFO, "dummystr")