Beispiel #1
0
def test_protocol_send_message():
    prot = Protocol()
    prot.writer = mock.MagicMock()
    prot.writer.write = mock.MagicMock()

    prot.send_message(Outgoing.REQ_CONTRACT_DATA, 1, "foo", [42])
    prot.writer.write.assert_called_once_with(b'\x00\x00\x00\r'
                                              b'9\x00'
                                              b'1\x00'
                                              b'foo\x00'
                                              b'1\x0042\x00')
Beispiel #2
0
def test_protocol_futures_error(caplog):
    prot = Protocol()
    r_id, fut = prot.make_future()
    prot._handle_err_msg(r_id, 404, "Not found")
    ex = fut.exception()
    assert ex.error_code == 404
    assert ex.error_message == "Not found"

    assert caplog.records == []

    prot._handle_err_msg(r_id, 404, "Not found")
    assert len(caplog.records) == 1
    assert caplog.records[
        0].message == 'Received error#404 from TWS: Not found'
Beispiel #3
0
def test_protocol_check_dispatch_nohandler(caplog):
    protocol = Protocol()

    protocol.version = ProtocolVersion.MIN_CLIENT
    protocol.dispatch_message(["2", "10", "42"])

    assert caplog.records == []

    with caplog.at_level('DEBUG'):
        protocol.dispatch_message(["2", "10", "42"])

    assert len(caplog.records) == 1
    assert caplog.records[0].message == (
        "no handler for IncomingMessage(Incoming.TICK_SIZE, 10, "
        "'42', protocol_version=ProtocolVersion.MIN_CLIENT) (v10)")
Beispiel #4
0
def test_protocol_check_dispatch_versioned():
    protocol = Protocol()
    is_called_arg = None

    def mocked(arg: int):
        nonlocal is_called_arg
        is_called_arg = arg

    protocol.version = ProtocolVersion.MIN_CLIENT
    protocol._handle_tick_size_v10 = mocked
    protocol.dispatch_message(["2", "10", "42"])

    assert is_called_arg == 42
Beispiel #5
0
def test_protocol_check_dispatch(caplog):
    protocol = Protocol()
    is_called_arg = None

    def mocked(arg: int):
        nonlocal is_called_arg
        is_called_arg = arg

    protocol._handle_tick_size = mocked
    protocol.version = ProtocolVersion.MIN_CLIENT
    protocol.dispatch_message(["2", "10", "42"])

    assert is_called_arg == 42

    assert caplog.records == []

    with caplog.at_level('DEBUG'):
        del protocol._handle_tick_size
        protocol.dispatch_message(["2", "10", "42"])
        assert len(caplog.records) == 1
        assert caplog.records[0].message == (
            "no handler for IncomingMessage(Incoming.TICK_SIZE, 10, "
            "'42', protocol_version=ProtocolVersion.MIN_CLIENT) (v10)")
Beispiel #6
0
def test_protocol_check_feature():
    prot = Protocol()
    with pytest.raises(ib_async.errors.NotConnectedError) as e:
        prot.check_feature(ProtocolVersion(115), "not yet connected")

    prot.version = ProtocolVersion(110)

    prot.check_feature(ProtocolVersion(105), "105 feature")

    with pytest.raises(ib_async.errors.OutdatedServerError) as e:
        prot.check_feature(ProtocolVersion(115), "115 feature")

    assert e.value.error_code == 502
    assert str(
        e.value
    ) == "The TWS is out of date and must be upgraded. It does not support 115 feature."

    with pytest.raises(ib_async.errors.OutdatedServerError) as e:
        prot.check_feature(ProtocolVersion(115))

    assert e.value.error_code == 502
    assert str(e.value) == "The TWS is out of date and must be upgraded."
Beispiel #7
0
def test_protocol_futures():
    prot = Protocol()
    r_id, fut = prot.make_future()
    prot.resolve_future(r_id, "Test")
    assert fut.result() == "Test"