# Path: hbmqtt/adapters.py
# class BufferReader(ReaderAdapter):
#     """
#     Byte Buffer reader adapter
#     This adapter simply adapt reading a byte buffer.
#     """
#     def __init__(self, buffer: bytes):
#         self._stream = io.BytesIO(buffer)
# 
#     @asyncio.coroutine
#     def read(self, n=-1) -> bytes:
#         return self._stream.read(n)


import asyncio
import unittest
from hbmqtt.mqtt.publish import PublishPacket, PublishVariableHeader, PublishPayload
from hbmqtt.adapters import BufferReader
from hbmqtt.mqtt.constants import QOS_0, QOS_1, QOS_2
# Copyright (c) 2015 Nicolas JOUANIN
#
# See the file license.txt for copying permission.



class PublishPacketTest(unittest.TestCase):
    def setUp(self):
        self.loop = asyncio.new_event_loop()

    def test_from_stream_qos_0(self):
        data = b'\x31\x11\x00\x05topic0123456789'
        stream = BufferReader(data)
        message = self.loop.run_until_complete(PublishPacket.from_stream(stream))
        self.assertEqual(message.variable_header.topic_name, 'topic')
        self.assertEqual(message.variable_header.packet_id, None)
        self.assertFalse((message.fixed_header.flags >> 1) & 0x03)
        self.assertTrue(message.fixed_header.flags & 0x01)
        self.assertTrue(message.payload.data, b'0123456789')

    def test_from_stream_qos_2(self):
        data = b'\x37\x13\x00\x05topic\x00\x0a0123456789'

# Path: hbmqtt/adapters.py
# class BufferReader(ReaderAdapter):
#     """
#     Byte Buffer reader adapter
#     This adapter simply adapt reading a byte buffer.
#     """
#     def __init__(self, buffer: bytes):
#         self._stream = io.BytesIO(buffer)
# 
#     @asyncio.coroutine
#     def read(self, n=-1) -> bytes:
#         return self._stream.read(n)


# Path: tests/mqtt/test_publish.py
import asyncio
import unittest
from hbmqtt.mqtt.publish import PublishPacket, PublishVariableHeader, PublishPayload
from hbmqtt.adapters import BufferReader
from hbmqtt.mqtt.constants import QOS_0, QOS_1, QOS_2

# Copyright (c) 2015 Nicolas JOUANIN
#
# See the file license.txt for copying permission.



class PublishPacketTest(unittest.TestCase):
    def setUp(self):
        self.loop = asyncio.new_event_loop()

    def test_from_stream_qos_0(self):
        data = b'\x31\x11\x00\x05topic0123456789'
        stream = BufferReader(data)
        message = self.loop.run_until_complete(PublishPacket.from_stream(stream))
        self.assertEqual(message.variable_header.topic_name, 'topic')
        self.assertEqual(message.variable_header.packet_id, None)
        self.assertFalse((message.fixed_header.flags >> 1) & 0x03)
        self.assertTrue(message.fixed_header.flags & 0x01)
        self.assertTrue(message.payload.data, b'0123456789')

    def test_from_stream_qos_2(self):
        data = b'\x37\x13\x00\x05topic\x00\x0a0123456789'

        stream = BufferReader(data)