test_transmit.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import unittest.mock
  2. import pytest
  3. import cc1101.options
  4. def test_transmit_empty_payload(transceiver):
  5. with unittest.mock.patch.object(
  6. transceiver,
  7. "get_packet_length_mode",
  8. return_value=cc1101.options.PacketLengthMode.VARIABLE,
  9. ), unittest.mock.patch.object(
  10. transceiver, "get_packet_length_bytes", return_value=21
  11. ):
  12. with pytest.raises(ValueError, match=r"\bempty\b"):
  13. transceiver.transmit([])
  14. @pytest.mark.parametrize("payload", (b"\0\x01\x02", [0, 127]))
  15. def test_transmit_first_null(transceiver, payload):
  16. with unittest.mock.patch.object(
  17. transceiver,
  18. "get_packet_length_mode",
  19. return_value=cc1101.options.PacketLengthMode.VARIABLE,
  20. ), unittest.mock.patch.object(
  21. transceiver, "get_packet_length_bytes", return_value=21
  22. ):
  23. with pytest.raises(ValueError, match=r"\bfirst byte\b.*\bmust not be null\b"):
  24. transceiver.transmit(payload)
  25. @pytest.mark.parametrize(
  26. ("max_packet_length", "payload"),
  27. ((3, "\x04\x01\x02\x03"), (4, [7, 21, 42, 0, 0, 1, 2, 3])),
  28. )
  29. def test_transmit_exceeding_max_length(transceiver, max_packet_length, payload):
  30. with unittest.mock.patch.object(
  31. transceiver,
  32. "get_packet_length_mode",
  33. return_value=cc1101.options.PacketLengthMode.VARIABLE,
  34. ), unittest.mock.patch.object(
  35. transceiver, "get_packet_length_bytes", return_value=max_packet_length
  36. ):
  37. with pytest.raises(
  38. ValueError, match=r"\bpayload exceeds maximum payload length\b"
  39. ):
  40. transceiver.transmit(payload)
  41. @pytest.mark.parametrize(
  42. ("packet_length", "payload"),
  43. ((3, "\x04\x01\x02\x03"), (4, [7, 21, 42, 0, 0, 1, 2, 3])),
  44. )
  45. def test_transmit_unexpected_payload_len(transceiver, packet_length, payload):
  46. with unittest.mock.patch.object(
  47. transceiver,
  48. "get_packet_length_mode",
  49. return_value=cc1101.options.PacketLengthMode.FIXED,
  50. ), unittest.mock.patch.object(
  51. transceiver, "get_packet_length_bytes", return_value=packet_length
  52. ):
  53. with pytest.raises(ValueError, match=r"\bpayload length\b"):
  54. transceiver.transmit(payload)