Пример #1
0
def test_defer_messages(buffering_broker, mock):
    buffering_broker.subscribe(mock)
    m = message.test()
    buffering_broker.deliver_message(m)
    mock.receive_message.assert_not_called()
    buffering_broker.flush()
    mock.receive_message.assert_called_once_with(m)
Пример #2
0
class TestMessagesToTheserial:
    @mark.parametrize(
        "message,expected_result",
        [(message.test(), "test\r\n"),
         (message.asdhsakhdahskdhas(asd="assda"), "asdhsakhdahskdhas\r\n")])
    def test_message_with_only_name(self, message, expected_result):
        result = message_to_serial(message)
        assert result == expected_result
Пример #3
0
class TestMessagesFromTheserial:
    @mark.parametrize("expected_result,message",
                      [(message.test(), "test\r\n"),
                       (message.asdhsakhdahskdhas(), "asdhsakhdahskdhas\r\n")])
    def test_message_with_only_name(self, message, expected_result):
        result = message_from_serial(message)
        assert result == expected_result

    @mark.parametrize(
        "expected_result,message",
        [(message.log(level="debug",
                      text="test test debug"), "[debug]test test debug\r\n"),
         (message.log(level="error", text="Oh No!"), "[error] Oh No!\r\n")])
    def test_handle_log_messages(self, message, expected_result):
        result = message_from_serial(message)
        assert result == expected_result
Пример #4
0
class TestMessageAdapter:
    @fixture
    def serial(self):
        return Mock()

    @fixture
    def adapter(self, serial):
        return SerialMessageAdapter(serial)

    @fixture
    def subscriber(self, adapter):
        subscriber = Mock()
        adapter.subscribe(subscriber)
        return subscriber

    def test_sending_message_transforms_it_into_bytes(self, adapter, serial):
        adapter.receive_message(message.test())
        # see https://pythonhosted.org/pyserial/pyserial_api.html#serial.Serial.write
        serial.write.assert_called_once_with(b"test\r\n")

    def test_messages_arrive_in_order(self, adapter, serial):
        adapter.receive_message(message.test1())
        adapter.receive_message(message.test2())
        serial.write.assert_has_calls([call(b"test1\r\n"), call(b"test2\r\n")])

    @mark.parametrize("message,bytes",
                      [(message.test(), b"test\r\n"),
                       (message.asdhsakhdahskdhas(), b"asdhsakhdahskdhas\r\n")]
                      )
    def test_reading_one_line(self, adapter, serial, bytes, message, mock):
        serial.in_waiting.side_effect = [len(bytes), 0]
        serial.readline.return_value = bytes
        adapter.deliver_message = mock
        adapter.flush()
        serial.readline.assert_called_once()
        mock.assert_called_once_with(message)

    def test_no_message_to_deliver(self, adapter, serial, mock):
        serial.in_waiting = 0
        adapter.deliver_message = mock
        adapter.flush()
Пример #5
0
 def test_sending_message_transforms_it_into_bytes(self, adapter, serial):
     adapter.receive_message(message.test())
     # see https://pythonhosted.org/pyserial/pyserial_api.html#serial.Serial.write
     serial.write.assert_called_once_with(b"test\r\n")
Пример #6
0
def test_subscribers_receive_messages(broker, mock):
    broker.subscribe(mock)
    m = message.test()
    broker.deliver_message(m)
    mock.receive_message.assert_called_once_with(m)
Пример #7
0
    pass


p = NestingObj(attr={"name": "test"})
p.save()
assert p.attr["name"] == "test"
print("can nest dicts")

heading("Test class factory")

print(Object.factory("Channel_") is Object.factory("Channel_"))

heading("chat via broker - run several instances")

from openbookscanner.broker import ParseBroker
from openbookscanner.message import message


class MessageReceiver:
    def receive_message(self, message):
        pprint(message)


broker = ParseBroker("test")
broker.subscribe(MessageReceiver())

while True:
    text = input("Type and press ENTER to chat: ")
    broker.deliver_message(message.test(text=text))
    broker.receive_messages()
def test_subscribers_receive_messages(parse_broker, mock):
    parse_broker.subscribe(mock)
    m = message.test()
    parse_broker.deliver_message(m)
    parse_broker.flush()
    mock.receive_message.assert_called_once_with(m)