test_cli_export_config.py 4.1 KB

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