amqtt/tests/mqtt/test_pubcomp.py

44 wiersze
1.2 KiB
Python
Czysty Zwykły widok Historia

import asyncio
2015-06-17 15:28:36 +00:00
import unittest
import pytest
2015-06-17 15:28:36 +00:00
2021-03-27 12:16:42 +00:00
from amqtt.adapters import BufferReader
from amqtt.errors import AMQTTError
from amqtt.mqtt import MQTTFixedHeader, PUBLISH
from amqtt.mqtt.pubcomp import PacketIdVariableHeader, PubcompPacket
2015-06-17 15:28:36 +00:00
2015-06-17 15:28:36 +00:00
class PubcompPacketTest(unittest.TestCase):
def setUp(self):
self.loop = asyncio.new_event_loop()
def test_from_stream(self):
data = b"\x70\x02\x00\x0a"
stream = BufferReader(data)
2015-06-17 15:28:36 +00:00
message = self.loop.run_until_complete(PubcompPacket.from_stream(stream))
assert message.variable_header.packet_id == 10
2015-06-17 15:28:36 +00:00
def test_to_bytes(self):
variable_header = PacketIdVariableHeader(10)
2015-06-17 15:28:36 +00:00
publish = PubcompPacket(variable_header=variable_header)
out = publish.to_bytes()
assert out == b"p\x02\x00\n"
def test_incorrect_fixed_header():
header = MQTTFixedHeader(PUBLISH, 0x00)
with pytest.raises(AMQTTError):
_ = PubcompPacket(fixed=header)
@pytest.mark.parametrize("prop", [
"packet_id"
])
def test_empty_variable_header(prop):
packet = PubcompPacket()
with pytest.raises(ValueError):
assert getattr(packet, prop) is not None
with pytest.raises(ValueError):
assert setattr(packet, prop, "a value")