tests: add unit testts for orderbook, order and subscriptions

This commit is contained in:
Jacob Plaster
2019-01-24 14:19:25 +00:00
committed by Jacob Plaster
parent 77fb4b1bac
commit ac0abe54c5
7 changed files with 402 additions and 0 deletions

0
bfxapi/tests/__init__.py Normal file
View File

89
bfxapi/tests/helpers.py Normal file
View File

@@ -0,0 +1,89 @@
import time
import json
import asyncio
from .. import Client, BfxWebsocket
def get_now():
return int(round(time.time() * 1000))
class StubbedWebsocket(BfxWebsocket):
def __new__(cls, *args, **kwargs):
instance = super(StubbedWebsocket, cls).__new__(cls, *args, **kwargs)
instance.sent_items = []
instance.published_items = []
return instance
async def _main(self, host):
print ("Faking wesocket connection to {}".format(host))
def get_ws(self):
return self
async def publish(self, data):
self.published_items += [{
'time': get_now(),
'data': data
}]
# convert to string and push through the websocket
return await self.on_message(json.dumps(data))
async def publish_auth_confirmation(self):
return self.publish({"event":"auth","status":"OK","chanId":0,"userId":269499,"auth_id":"58aa0472-b1a9-4690-8ab8-300d68e66aaf","caps":{"orders":{"read":1,"write":1},"account":{"read":1,"write":0},"funding":{"read":1,"write":1},"history":{"read":1,"write":0},"wallets":{"read":1,"write":1},"withdraw":{"read":0,"write":1},"positions":{"read":1,"write":1}}})
async def send(self, data_string):
self.sent_items += [{
'time': get_now(),
'data': data_string
}]
def get_published_items(self):
return self.published_items
def get_sent_items(self):
return self.sent_items
def get_last_sent_item(self):
return self.sent_items[-1:][0]
def get_sent_items_count(self):
return len(self.sent_items)
class EventWatcher():
def __init__(self, ws, event):
self.value = None
self.event = event
ws.once(event, self._finish)
def _finish(self, value):
self.value = value or {}
@classmethod
def watch(cls, ws, event):
return EventWatcher(ws, event)
def wait_until_complete(self, max_wait_time=5):
counter = 0
while self.value == None:
if counter > 5:
raise Exception('Wait time limit exceeded for event {}'.format(self.event))
time.sleep(1)
counter += 1
return self.value
def create_stubbed_client(*args, **kwargs):
client = Client(*args, **kwargs)
# no support for rest stubbing yet
client.rest = None
client.ws = StubbedWebsocket(*args, **kwargs)
return client
async def ws_publish_auth_accepted(ws):
return await ws.publish({"event":"auth","status":"OK","chanId":0,"userId":269499,"auth_id":"58aa0472-b1a9-4690-8ab8-300d68e66aaf","caps":{"orders":{"read":1,"write":1},"account":{"read":1,"write":0},"funding":{"read":1,"write":1},"history":{"read":1,"write":0},"wallets":{"read":1,"write":1},"withdraw":{"read":0,"write":1},"positions":{"read":1,"write":1}}})
async def ws_publish_connection_init(ws):
return await ws.publish({"event":"info","version":2,"serverId":"748c00f2-250b-46bb-8519-ce1d7d68e4f0","platform":{"status":1}})
async def ws_publish_conf_accepted(ws, flags_code):
return await ws.publish({"event":"conf","status":"OK","flags":flags_code})

View File

@@ -0,0 +1,24 @@
import sys
sys.path.append('../components')
from bfxapi import Decimal
def test_precision():
assert str(Decimal(0.00000123456789)) == "0.00000123456789"
assert str(Decimal("0.00000123456789")) == "0.00000123456789"
def test_float_operations():
assert str(Decimal(0.0002) * 0.02) == "0.000004"
assert str(0.02 * Decimal(0.0002)) == "0.000004"
assert str(Decimal(0.0002) / 0.02) == "0.01"
assert str(0.02 / Decimal(0.0002)) == "0.01"
assert str(0.02 + Decimal(0.0002)) == "0.0202"
assert str(Decimal(0.0002) + 0.02) == "0.0202"
assert str(0.02 - Decimal(0.0002)) == "-0.0198"
assert str(Decimal(0.0002) - 0.02) == "-0.0198"
assert str(0.01 // Decimal(0.0004)) == "0"
assert str(Decimal(0.0004) // 0.01) == "0"

View File

@@ -0,0 +1,34 @@
import pytest
from .helpers import create_stubbed_client, ws_publish_connection_init, ws_publish_conf_accepted
@pytest.mark.asyncio
async def test_checksum_generation():
client = create_stubbed_client()
symbol = "tXRPBTC"
# publsh connection created message
await ws_publish_connection_init(client.ws)
# publish checksum flag accepted
await ws_publish_conf_accepted(client.ws, 131072)
# subscribe to order book
await client.ws.subscribe('book', symbol)
## send subscription accepted
chanId = 123
await client.ws.publish({"event":"subscribed","channel":"book","chanId": chanId,"symbol": symbol,"prec":"P0","freq":"F0","len":"25","pair": symbol})
## send orderbook snapshot
await client.ws.publish([chanId, [[0.0000886,1,1060.55466114],[0.00008859,1,1000],[0.00008858,1,2713.47159343],[0.00008857,1,4276.92870916],[0.00008856,2,6764.75562319],
[0.00008854,1,5641.48532401],[0.00008853,1,2255.92632223],[0.0000885,1,2256.69584601],[0.00008848,2,3630.3],[0.00008845,1,28195.70625766],
[0.00008844,1,15571.7],[0.00008843,1,2500],[0.00008841,1,64196.16117814],[0.00008838,1,7500],[0.00008837,2,2764.12999012],[0.00008834,2,10886.476298],
[0.00008831,1,20000],[0.0000883,1,1000],[0.00008829,2,2517.22175358],[0.00008828,1,450.45],[0.00008827,1,13000],[0.00008824,1,1500],[0.0000882,1,300],
[0.00008817,1,3000],[0.00008816,1,100],[0.00008864,1,-481.8549041],[0.0000887,2,-2141.77009092],[0.00008871,1,-2256.45433182],[0.00008872,1,-2707.58122743],
[0.00008874,1,-5640.31794092],[0.00008876,1,-29004.93294912],[0.00008878,1,-2500],[0.0000888,1,-20000],[0.00008881,2,-2880.15595827],[0.00008882,1,-27705.42933984],
[0.00008883,1,-4509.83708214],[0.00008884,1,-1500],[0.00008885,1,-2500],[0.00008888,1,-902.91405442],[0.00008889,1,-900],[0.00008891,1,-7500],
[0.00008894,1,-775.08564697],[0.00008896,1,-150],[0.00008899,3,-11628.02590049],[0.000089,2,-1299.7],[0.00008902,2,-4841.8],[0.00008904,3,-25320.46250083],
[0.00008909,1,-14000],[0.00008913,1,-123947.999],[0.00008915,2,-28019.6]]])
## send some more price updates
await client.ws.publish([chanId,[0.00008915,0,-1]])
await client.ws.publish([chanId,[0.00008837,1,56.54876269]])
await client.ws.publish([chanId,[0.00008873,1,-15699.9]])
## check checksum is the same as expected
expected_checksum = 30026640
actual_checksum = client.ws.orderBooks[symbol].checksum()
assert expected_checksum == actual_checksum

View File

@@ -0,0 +1,112 @@
import pytest
import json
from .helpers import (create_stubbed_client, ws_publish_auth_accepted, ws_publish_connection_init,
EventWatcher)
@pytest.mark.asyncio
async def test_submit_order():
client = create_stubbed_client()
# publsh connection created message
await ws_publish_connection_init(client.ws)
## send auth accepted
await ws_publish_auth_accepted(client.ws)
## send new order
await client.ws.submit_order('tBTCUSD', 19000, 0.01, 'EXCHANGE MARKET')
last_sent = client.ws.get_last_sent_item()
sent_order_array = json.loads(last_sent['data'])
assert sent_order_array[1] == "on"
sent_order_json = sent_order_array[3]
assert sent_order_json['type'] == "EXCHANGE MARKET"
assert sent_order_json['symbol'] == "tBTCUSD"
assert sent_order_json['amount'] == "0.01"
assert sent_order_json['price'] == "19000"
@pytest.mark.asyncio
async def test_submit_update_order():
client = create_stubbed_client()
# publsh connection created message
await ws_publish_connection_init(client.ws)
## send auth accepted
await ws_publish_auth_accepted(client.ws)
## send new order
await client.ws.update_order(123, price=100, amount=0.01, hidden=True)
last_sent = client.ws.get_last_sent_item()
sent_order_array = json.loads(last_sent['data'])
assert sent_order_array[1] == "ou"
sent_order_json = sent_order_array[3]
# {"id": 123, "price": "100", "amount": "0.01", "flags": 64}
assert sent_order_json['id'] == 123
assert sent_order_json['price'] == "100"
assert sent_order_json['amount'] == "0.01"
assert sent_order_json['flags'] == 64
@pytest.mark.asyncio
async def test_submit_cancel_order():
client = create_stubbed_client()
# publsh connection created message
await ws_publish_connection_init(client.ws)
## send auth accepted
await ws_publish_auth_accepted(client.ws)
## send new order
await client.ws.cancel_order(123)
last_sent = client.ws.get_last_sent_item()
sent_order_array = json.loads(last_sent['data'])
assert sent_order_array[1] == "oc"
sent_order_json = sent_order_array[3]
assert sent_order_json['id'] == 123
@pytest.mark.asyncio
async def test_events_on_new_order():
client = create_stubbed_client()
# publsh connection created message
await ws_publish_connection_init(client.ws)
## send auth accepted
await ws_publish_auth_accepted(client.ws)
## look for new order confirmation
o_new = EventWatcher.watch(client.ws, 'order_new')
await client.ws.publish([0,"on",[1151718504,None,1548262833910,"tBTCUSD",1548262833379,1548262833410,-1,-1,"EXCHANGE LIMIT",None,None,None,0,"ACTIVE",None,None,15980,0,0,0,None,None,None,0,0,None,None,None,"API>BFX",None,None,None]])
new_res = o_new.wait_until_complete()
assert new_res.amount_orig == -1
assert new_res.amount_filled == 0
assert new_res.price == 15980
assert new_res.type == 'EXCHANGE LIMIT'
## look for order update confirmation
o_update = EventWatcher.watch(client.ws, 'order_update')
await client.ws.publish([0,"ou",[1151718504,None,1548262833910,"tBTCUSD",1548262833379,1548262846964,-0.5,-1,"EXCHANGE LIMIT",None,None,None,0,"PARTIALLY FILLED @ 15980.0(-0.5)",None,None,15980,15980,0,0,None,None,None,0,0,None,None,None,"API>BFX",None,None,None]])
update_res = o_update.wait_until_complete()
assert update_res.amount_orig == -1
assert float(update_res.amount_filled) == -0.5
assert update_res.price == 15980
assert update_res.type == 'EXCHANGE LIMIT'
## look for closed notification
o_closed = EventWatcher.watch(client.ws, 'order_closed')
await client.ws.publish([0,"oc",[1151718504,None,1548262833910,"tBTCUSD",1548262833379,1548262888016,0,-1,"EXCHANGE LIMIT",None,None,None,0,"EXECUTED @ 15980.0(-0.5): was PARTIALLY FILLED @ 15980.0(-0.5)",None,None,15980,15980,0,0,None,None,None,0,0,None,None,None,"API>BFX",None,None,None]])
closed_res = o_closed.wait_until_complete()
assert new_res.amount_orig == -1
assert new_res.amount_filled == 0
assert new_res.price == 15980
assert new_res.type == 'EXCHANGE LIMIT'
@pytest.mark.asyncio
async def test_events_on_cancel_order():
client = create_stubbed_client()
# publsh connection created message
await ws_publish_connection_init(client.ws)
## send auth accepted
await ws_publish_auth_accepted(client.ws)
## Create new order
await client.ws.publish([0,"on",[1151718565,None,1548325124885,"tBTCUSD",1548325123435,1548325123460,1,1,"EXCHANGE LIMIT",None,None,None,0,"ACTIVE",None,None,10,0,0,0,None,None,None,0,0,None,None,None,"API>BFX",None,None,None]])
## look for order closed confirmation
o_close = EventWatcher.watch(client.ws, 'order_closed')
await client.ws.publish([0,"oc",[1151718565,None,1548325124885,"tBTCUSD",1548325123435,1548325123548,1,1,"EXCHANGE LIMIT",None,None,None,0,"CANCELED",None,None,10,0,0,0,None,None,None,0,0,None,None,None,"API>BFX",None,None,None]])
close_res = o_close.wait_until_complete()
assert close_res.amount_orig == 1
assert float(close_res.amount_filled) == 0
assert close_res.price == 10
assert close_res.type == 'EXCHANGE LIMIT'

View File

@@ -0,0 +1,141 @@
import pytest
import json
import asyncio
from .helpers import (create_stubbed_client, ws_publish_connection_init, EventWatcher)
@pytest.mark.asyncio
async def test_submit_subscribe():
client = create_stubbed_client()
symb = 'tXRPBTC'
# publsh connection created message
await ws_publish_connection_init(client.ws)
# Create new subscription to orderbook
await client.ws.subscribe('book', symb)
last_sent = client.ws.get_last_sent_item()
sent_sub = json.loads(last_sent['data'])
# {'time': 1548327054030, 'data': '{"event": "subscribe", "channel": "book", "symbol": "tXRPBTC"}'}
assert sent_sub['event'] == "subscribe"
assert sent_sub['channel'] == "book"
assert sent_sub['symbol'] == symb
# create new subscription to trades
await client.ws.subscribe('trades', symb)
last_sent = client.ws.get_last_sent_item()
sent_sub = json.loads(last_sent['data'])
# {'event': 'subscribe', 'channel': 'trades', 'symbol': 'tBTCUSD'}
assert sent_sub['event'] == 'subscribe'
assert sent_sub['channel'] == 'trades'
assert sent_sub['symbol'] == symb
# create new subscription to candles
await client.ws.subscribe('candles', symb, timeframe='1m')
last_sent = client.ws.get_last_sent_item()
sent_sub = json.loads(last_sent['data'])
#{'event': 'subscribe', 'channel': 'candles', 'symbol': 'tBTCUSD', 'key': 'trade:1m:tBTCUSD'}
assert sent_sub['event'] == 'subscribe'
assert sent_sub['channel'] == 'candles'
assert sent_sub['key'] == 'trade:1m:{}'.format(symb)
@pytest.mark.asyncio
async def test_event_subscribe():
client = create_stubbed_client()
symb = 'tXRPBTC'
pair = 'XRPBTC'
# publsh connection created message
await ws_publish_connection_init(client.ws)
# create a new subscription
await client.ws.subscribe('trades', symb)
# announce subscription was successful
sub_watch = EventWatcher.watch(client.ws, 'subscribed')
await client.ws.publish({"event":"subscribed","channel":"trades","chanId":2,"symbol":symb,"pair":pair})
s_res = sub_watch.wait_until_complete()
assert s_res.channel_name == 'trades'
assert s_res.symbol == symb
assert s_res.is_subscribed_bool == True
assert s_res.chan_id == 2
@pytest.mark.asyncio
async def test_submit_unsubscribe():
client = create_stubbed_client()
symb = 'tXRPBTC'
pair = 'XRPBTC'
# publsh connection created message
await ws_publish_connection_init(client.ws)
# create new subscription to trades
await client.ws.subscribe('trades', symb)
# announce subscription was successful
sub_watch = EventWatcher.watch(client.ws, 'subscribed')
await client.ws.publish({"event":"subscribed","channel":"trades","chanId":2,"symbol":symb,"pair":pair})
s_res = sub_watch.wait_until_complete()
# unsubscribe from channel
await s_res.unsubscribe()
last_sent = client.ws.get_last_sent_item()
sent_unsub = json.loads(last_sent['data'])
# {'event': 'unsubscribe', 'chanId': 2}
assert sent_unsub['event'] == 'unsubscribe'
assert sent_unsub['chanId'] == 2
@pytest.mark.asyncio
async def test_event_unsubscribe():
client = create_stubbed_client()
symb = 'tXRPBTC'
pair = 'XRPBTC'
# publish connection created message
await ws_publish_connection_init(client.ws)
# create new subscription to trades
await client.ws.subscribe('trades', symb)
# announce subscription was successful
sub_watch = EventWatcher.watch(client.ws, 'subscribed')
await client.ws.publish({"event":"subscribed","channel":"trades","chanId":2,"symbol":symb,"pair":pair})
s_res = sub_watch.wait_until_complete()
# unsubscribe from channel
await s_res.unsubscribe()
last_sent = client.ws.get_last_sent_item()
sent_unsub = json.loads(last_sent['data'])
# publish confirmation of unsubscribe
unsub_watch = EventWatcher.watch(client.ws, 'unsubscribed')
await client.ws.publish({"event":"unsubscribed","status":"OK","chanId":2})
unsub_res = unsub_watch.wait_until_complete()
assert s_res.channel_name == 'trades'
assert s_res.symbol == symb
assert s_res.is_subscribed_bool == False
assert s_res.chan_id == 2
@pytest.mark.asyncio
async def test_submit_resubscribe():
client = create_stubbed_client()
symb = 'tXRPBTC'
pair = 'XRPBTC'
# publish connection created message
await ws_publish_connection_init(client.ws)
# request two new subscriptions
await client.ws.subscribe('book', symb)
await client.ws.subscribe('trades', symb)
# confirm subscriptions
await client.ws.publish({"event":"subscribed","channel":"trades","chanId":2,"symbol":symb,"pair":pair})
await client.ws.publish({"event":"subscribed","channel":"book","chanId":3,"symbol":symb,"prec":"P0","freq":"F0","len":"25","pair":pair})
# call resubscribe all
await client.ws.resubscribe_all()
## assert that 2 unsubscribe requests were sent
last_sent = client.ws.get_sent_items()[-2:]
for i in last_sent:
data = json.loads(i['data'])
assert data['event'] == 'unsubscribe'
assert (data['chanId'] == 2 or data['chanId'] == 3)
## confirm unsubscriptions
await client.ws.publish({"event":"unsubscribed","status":"OK","chanId":2})
await client.ws.publish({"event":"unsubscribed","status":"OK","chanId":3})
## confirm subscriptions
# await client.ws.publish({"event":"subscribed","channel":"trades","chanId":2,"symbol":symb,"pair":pair})
# await client.ws.publish({"event":"subscribed","channel":"book","chanId":3,"symbol":symb,"prec":"P0","freq":"F0","len":"25","pair":pair})
# wait for emit of event
n_last_sent = client.ws.get_sent_items()[-2:]
for i in n_last_sent:
data = json.loads(i['data'])
# print (data)
assert data['event'] == 'subscribe'
assert (data['channel'] == 'book' or data['channel'] == 'trades')
assert data['symbol'] == symb

View File

@@ -9,3 +9,5 @@ disable=too-few-public-methods,
len-as-condition, len-as-condition,
too-many-instance-attributes, too-many-instance-attributes,
invalid-name invalid-name
ignore=tests