test_cli_export_config.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. def test_export_python_list(capsys, caplog):
  50. with unittest.mock.patch("cc1101.CC1101") as transceiver_mock:
  51. transceiver_mock().__enter__().get_configuration_register_values.return_value = {
  52. cc1101.addresses.ConfigurationRegisterAddress.IOCFG2: 0x29,
  53. cc1101.addresses.ConfigurationRegisterAddress.IOCFG1: 0x2E,
  54. }
  55. transceiver_mock().__enter__()._get_patable.return_value = [0xC6] + [0] * 7
  56. with unittest.mock.patch("sys.argv", [""]):
  57. with caplog.at_level(logging.INFO):
  58. cc1101._cli._export_config()
  59. assert caplog.record_tuples == [
  60. ("cc1101._cli", 20, str(transceiver_mock().__enter__()))
  61. ]
  62. out, err = capsys.readouterr()
  63. assert not err
  64. assert (
  65. out
  66. == "[\n0b00101001, # 0x29 IOCFG2\n0b00101110, # 0x2e IOCFG1\n]\n"
  67. + "# PATABLE = (0xc6, 0, 0, 0, 0, 0, 0, 0)\n"
  68. )
  69. @pytest.mark.parametrize(
  70. ("args", "root_log_level", "log_format"),
  71. (
  72. ([], logging.INFO, "%(message)s"),
  73. (
  74. ["--debug"],
  75. logging.DEBUG,
  76. "%(asctime)s:%(levelname)s:%(name)s:%(funcName)s:" + "%(message)s",
  77. ),
  78. ),
  79. )
  80. def test_root_log_level(args, root_log_level, log_format):
  81. with unittest.mock.patch("cc1101.CC1101"), unittest.mock.patch(
  82. "sys.argv", [""] + args
  83. ), unittest.mock.patch("logging.basicConfig") as logging_basic_config_mock:
  84. cc1101._cli._export_config()
  85. assert logging_basic_config_mock.call_count == 1
  86. assert logging_basic_config_mock.call_args[1]["level"] == root_log_level
  87. assert logging_basic_config_mock.call_args[1]["format"] == log_format
  88. def test_logging(caplog):
  89. with unittest.mock.patch("sys.argv", [""]), unittest.mock.patch(
  90. "cc1101.CC1101"
  91. ) as transceiver_mock, caplog.at_level(logging.DEBUG):
  92. transceiver_mock().__enter__().__str__.return_value = "dummystr"
  93. cc1101._cli._export_config()
  94. assert caplog.record_tuples == [
  95. (
  96. "cc1101._cli",
  97. logging.DEBUG,
  98. "args=Namespace(base_frequency_hertz=None, debug=False, disable_checksum=False, "
  99. "format='python-list', packet_length_mode=None, symbol_rate_baud=None, sync_mode=None)",
  100. ),
  101. ("cc1101._cli", logging.INFO, "dummystr"),
  102. ]