bbgo_origin/python/tests/test_grpc.py

67 lines
1.6 KiB
Python
Raw Normal View History

2022-03-07 04:06:16 +00:00
from concurrent import futures
import bbgo_pb2
import bbgo_pb2_grpc
2022-03-07 04:06:16 +00:00
import grpc
import pytest
from bbgo import MarketService
from bbgo import TradingService
2022-04-09 09:24:34 +00:00
from tests.servicer import TestTradingServicer
2022-03-07 04:06:16 +00:00
@pytest.fixture
2022-04-09 09:24:34 +00:00
def address(host='[::]', port=50051):
2022-03-07 04:06:16 +00:00
return f'{host}:{port}'
@pytest.fixture
2022-04-09 09:24:34 +00:00
def channel(address):
return grpc.insecure_channel(address)
2022-03-07 04:06:16 +00:00
@pytest.fixture
2022-04-09 09:24:34 +00:00
def trading_service(channel):
trading_service_stub = bbgo_pb2_grpc.TradingServiceStub(channel)
return TradingService(trading_service_stub)
2022-03-07 04:06:16 +00:00
@pytest.fixture
2022-04-09 09:24:34 +00:00
def market_service(channel):
market_service_stub = bbgo_pb2_grpc.MarketDataServiceStub(channel)
return MarketService(market_service_stub)
@pytest.fixture
def test_trading_servicer(address, max_workers=1):
2022-03-07 04:06:16 +00:00
server = grpc.server(futures.ThreadPoolExecutor(max_workers))
2022-04-09 09:24:34 +00:00
servicer = TestTradingServicer()
bbgo_pb2_grpc.add_TradingServiceServicer_to_server(servicer, server)
server.add_insecure_port(address)
2022-03-07 04:06:16 +00:00
server.start()
yield server
server.stop(grace=None)
2022-04-09 09:24:34 +00:00
def test_submit_order(trading_service, test_trading_servicer):
2022-03-07 04:06:16 +00:00
exchange = 'max'
symbol = 'BTCUSDT'
side = bbgo_pb2.Side.BUY
quantity = 0.01
order_type = bbgo_pb2.OrderType.LIMIT
2022-04-09 09:24:34 +00:00
response = trading_service.submit_order(
2022-03-07 04:06:16 +00:00
exchange=exchange,
symbol=symbol,
side=side,
quantity=quantity,
order_type=order_type,
)
order = response.order
assert order.exchange == exchange
assert order.symbol == symbol
assert order.side == side
assert order.quantity == quantity