def test_on_message(self): client = BcexClient([Symbol.ALGOUSD]) client._check_message_seqnum = Mock() channels = [ "dummy_channel", Channel.TRADES, Channel.TICKER, Channel.PRICES, Channel.AUTH, Channel.BALANCES, Channel.HEARTBEAT, Channel.SYMBOLS, Channel.L2, Channel.L3, ] # set up the mocks mocks = [Mock() for _ in range(len(channels))] client._on_message_unsupported = mocks[0] client._on_market_trade_updates = mocks[1] client._on_ticker_updates = mocks[2] client._on_price_updates = mocks[3] client._on_auth_updates = mocks[4] client._on_balance_updates = mocks[5] client._on_heartbeat_updates = mocks[6] client._on_symbols_updates = mocks[7] client._on_l2_updates = mocks[8] client._on_l3_updates = mocks[9] # checks msg = {} for i, ch in enumerate(channels): msg.update({"channel": ch}) client.on_message(json.dumps(msg)) assert client._check_message_seqnum.call_count == i + 1 for j in range(i): assert mocks[j].call_count == 1 for j in range(i + 1, len(channels)): assert mocks[j].call_count == 0 assert mocks[i].call_args[0][0] == msg client.on_message(None)
def test_on_l2_updates(self): client = BcexClient(symbols=["BTC-USD"]) msg = { "seqnum": 2, "event": "snapshot", "channel": "l2", "symbol": "BTC-USD", "bids": [ { "px": 8723.45, "qty": 1.45, "num": 2 }, { "px": 8124.45, "qty": 123.45, "num": 1 }, ], "asks": [ { "px": 8730.0, "qty": 1.55, "num": 2 }, { "px": 8904.45, "qty": 13.66, "num": 2 }, ], } client._on_l2_updates(msg) assert client.l2_book["BTC-USD"] == { "bids": SortedDict({ 8124.45: 123.45, 8723.45: 1.45 }), "asks": SortedDict({ 8730.0: 1.55, 8904.45: 13.66 }), } msg = { "seqnum": 3, "event": "updated", "channel": "l2", "symbol": "BTC-USD", "bids": [{ "px": 8723.45, "qty": 1.1, "num": 1 }], "asks": [], } client._on_l2_updates(msg) assert client.l2_book["BTC-USD"] == { "bids": SortedDict({ 8124.45: 123.45, 8723.45: 1.1 }), "asks": SortedDict({ 8730.0: 1.55, 8904.45: 13.66 }), } msg = { "seqnum": 3, "event": "updated", "channel": "l2", "symbol": "BTC-USD", "bids": [{ "px": 8124.45, "qty": 0, "num": 1 }], "asks": [], } client._on_l2_updates(msg) assert client.l2_book["BTC-USD"] == { "bids": SortedDict({8723.45: 1.1}), "asks": SortedDict({ 8730.0: 1.55, 8904.45: 13.66 }), } msg = { "seqnum": 2, "event": "snapshot", "channel": "l2", "symbol": "BTC-USD", "bids": [{ "px": 8723.45, "qty": 1.45, "num": 2 }], "asks": [{ "px": 8730.0, "qty": 1.55, "num": 2 }], } client._on_l2_updates(msg) assert client.l2_book["BTC-USD"] == { "bids": SortedDict({8723.45: 1.45}), "asks": SortedDict({8730.0: 1.55}), }