test_spi.py 3.6 KB

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