test_spi.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 unittest.mock
  18. import pytest
  19. import cc1101
  20. import cc1101.addresses
  21. import cc1101.options
  22. # pylint: disable=protected-access
  23. @pytest.mark.parametrize("bus", [0, 1])
  24. @pytest.mark.parametrize("chip_select", [0, 2])
  25. def test___init__select_device(bus, chip_select):
  26. with unittest.mock.patch("spidev.SpiDev"):
  27. transceiver = cc1101.CC1101(spi_bus=bus, spi_chip_select=chip_select)
  28. assert transceiver._spi_bus == bus
  29. assert transceiver._spi_chip_select == chip_select
  30. assert transceiver._spi_device_path == "/dev/spidev{}.{}".format(bus, chip_select)
  31. transceiver._spi.open.side_effect = SystemExit
  32. with pytest.raises(SystemExit):
  33. with transceiver:
  34. pass
  35. transceiver._spi.open.assert_called_once_with(bus, chip_select)
  36. def test__read_status_register(transceiver):
  37. transceiver._spi.xfer.return_value = [0, 20]
  38. transceiver._read_status_register(cc1101.addresses.StatusRegisterAddress.VERSION)
  39. transceiver._spi.xfer.assert_called_once_with([0x31 | 0xC0, 0])
  40. def test__command_strobe(transceiver):
  41. transceiver._spi.xfer.return_value = [15]
  42. transceiver._command_strobe(cc1101.addresses.StrobeAddress.STX)
  43. transceiver._spi.xfer.assert_called_once_with([0x35 | 0x00])
  44. def test__reset(transceiver):
  45. transceiver._spi.xfer.return_value = [15]
  46. transceiver._reset()
  47. transceiver._spi.xfer.assert_called_once_with([0x30 | 0x00])
  48. def test___enter__(transceiver):
  49. with unittest.mock.patch.object(
  50. transceiver, "_read_status_register"
  51. ) as read_status_register_mock, unittest.mock.patch.object(
  52. transceiver, "_reset"
  53. ) as reset_mock, unittest.mock.patch.object(
  54. transceiver, "_set_modulation_format"
  55. ) as set_modulation_format_mock, unittest.mock.patch.object(
  56. transceiver, "_set_power_amplifier_setting_index"
  57. ) as set_pa_setting_mock, unittest.mock.patch.object(
  58. transceiver, "_disable_data_whitening"
  59. ) as disable_whitening_mock, unittest.mock.patch.object(
  60. transceiver, "_write_burst"
  61. ) as write_burst_mock, unittest.mock.patch.object(
  62. transceiver,
  63. "get_main_radio_control_state_machine_state",
  64. return_value=cc1101.MainRadioControlStateMachineState.IDLE,
  65. ):
  66. read_status_register_mock.side_effect = lambda r: {
  67. cc1101.addresses.StatusRegisterAddress.PARTNUM: 0,
  68. cc1101.addresses.StatusRegisterAddress.VERSION: 0x14,
  69. }[r]
  70. with transceiver as transceiver_context:
  71. assert transceiver == transceiver_context
  72. transceiver._spi.open.assert_called_once_with(0, 0)
  73. assert transceiver._spi.max_speed_hz == 55700
  74. reset_mock.assert_called_once_with()
  75. set_modulation_format_mock.assert_called_once_with(
  76. cc1101.options.ModulationFormat.ASK_OOK
  77. )
  78. set_pa_setting_mock.assert_called_once_with(1)
  79. disable_whitening_mock.assert_called_once_with()
  80. write_burst_mock.assert_called_once_with(0x18, [0b010100])
  81. @pytest.mark.parametrize("bus", [0, 1])
  82. @pytest.mark.parametrize("chip_select", [0, 2])
  83. def test___enter__permission_error(transceiver, bus, chip_select):
  84. transceiver._spi.open.side_effect = PermissionError("[Errno 13] Permission denied")
  85. transceiver._spi_bus = bus
  86. transceiver._spi_chip_select = chip_select
  87. with pytest.raises(
  88. PermissionError, match=r"\s/dev/spidev{}.{}\s".format(bus, chip_select)
  89. ):
  90. with transceiver:
  91. pass