예제 #1
0
def test_to_bytes_returns_full_message_as_bytes():
    # Given
    message_nb = bytes([0, 0])
    payload = bytes([48, 48, 48, 48])
    msg_id = bytes([48, 48, 48, 48])
    topic = bytes([1, 0, 0, 0])

    # When
    msg = UDPMessage(subtopic=message_nb,
                     payload=payload,
                     code=msg_id,
                     topic=topic)
    expected_result = msg.full_content + msg.crc

    # Then
    assert msg.to_bytes() == expected_result
예제 #2
0
def test_from_bytes_returns_none_if_message_is_corrupted():
    # Given
    message_nb = bytes([2, 0])
    payload = bytes([49, 49, 49, 49])
    msg_id = bytes([48, 48, 48, 48])
    topic = bytes([1, 0, 0, 0])

    # When
    msg = UDPMessage(subtopic=message_nb,
                     payload=payload,
                     code=msg_id,
                     topic=topic)
    msg.crc = bytes()
    result = UDPMessage.from_bytes(msg.to_bytes())

    # Then
    assert result is None
예제 #3
0
def test_from_bytes_correctly_load_a_message():
    # Given
    message_nb = bytes(UDPMessage.MSG_NUMBER_LENGTH * [1])
    payload = bytes([49, 49, 49, 49])
    msg_id = bytes([48, 48, 48, 48])
    topic = bytes([1, 0, 0, 0])

    # When
    msg = UDPMessage(subtopic=message_nb,
                     payload=payload,
                     code=msg_id,
                     topic=topic)
    result = UDPMessage.from_bytes(msg.to_bytes())

    # Then
    assert result.msg_id == msg_id
    assert result.time_creation == msg.time_creation
    assert result.topic == topic
    assert result.message_nb == message_nb
    assert result.payload == payload
    assert result.crc == msg.crc