Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdsellers committed Feb 19, 2021
1 parent 403a5f0 commit 2f5b7b2
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 64 deletions.
26 changes: 26 additions & 0 deletions tests/unit_tests/data/test_data_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

import unittest

from nautilus_trader.data.base import Data
from nautilus_trader.data.base import DataCacheFacade
from nautilus_trader.data.base import DataType
from nautilus_trader.model.enums import PriceType
from nautilus_trader.model.identifiers import Symbol
from nautilus_trader.model.identifiers import Venue
Expand All @@ -27,6 +29,30 @@
AUDUSD_SIM = TestInstrumentProvider.default_fx_ccy(Symbol("AUD/USD", SIM))


class DataTypeTests(unittest.TestCase):

def test_data_type_instantiation(self):
# Arrange
# Act
data_type = DataType(str, {"Type": "NEWS_FLASH"})

# Assert
self.assertEqual(str, data_type.type)
self.assertEqual({"Type": "NEWS_FLASH"}, data_type.metadata)
self.assertEqual("<str> {'Type': 'NEWS_FLASH'}", str(data_type))
self.assertEqual("DataType(type=str, metadata={'Type': 'NEWS_FLASH'})", repr(data_type))

def test_data_instantiation(self):
# Arrange
# Act
data_type = DataType(str, {"Type": "NEWS_FLASH"})
data = Data(data_type, "SOME_NEWS_HEADLINE")

# Assert
self.assertEqual(data_type, data.data_type)
self.assertEqual("SOME_NEWS_HEADLINE", data.data)


class DataCacheFacadeTests(unittest.TestCase):

def setUp(self):
Expand Down
156 changes: 94 additions & 62 deletions tests/unit_tests/data/test_data_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,68 +707,100 @@ def test_execute_unsubscribe_order_book_interval_then_removes_handler(self):
# Assert
self.assertEqual([], self.data_engine.subscribed_order_books)

def test_process_order_book_when_subscriber_then_sends_to_registered_handler(self):
pass
# TODO: WIP
# # Arrange
# self.data_engine.register_client(self.binance_client)
# self.binance_client.connect()
#
# handler = []
# subscribe = Subscribe(
# provider=BINANCE.value,
# data_type=Instrument,
# metadata={"Symbol": ETHUSDT_BINANCE.symbol},
# handler=handler.append,
# command_id=self.uuid_factory.generate(),
# command_timestamp=self.clock.utc_now(),
# )
#
# self.data_engine.execute(subscribe)
#
# # Act
# self.data_engine.process(ETHUSDT_BINANCE)
#
# # Assert
# self.assertEqual([ETHUSDT_BINANCE], handler)

def test_process_order_book_when_subscribers_then_sends_to_registered_handlers(self):
pass
# TODO: WIP
# # Arrange
# self.data_engine.register_client(self.binance_client)
# self.binance_client.connect()
#
# handler1 = []
# subscribe1 = Subscribe(
# provider=BINANCE.value,
# data_type=Instrument,
# metadata={"Symbol": ETHUSDT_BINANCE.symbol},
# handler=handler1.append,
# command_id=self.uuid_factory.generate(),
# command_timestamp=self.clock.utc_now(),
# )
#
# handler2 = []
# subscribe2 = Subscribe(
# provider=BINANCE.value,
# data_type=Instrument,
# metadata={"Symbol": ETHUSDT_BINANCE.symbol},
# handler=handler2.append,
# command_id=self.uuid_factory.generate(),
# command_timestamp=self.clock.utc_now(),
# )
#
# self.data_engine.execute(subscribe1)
# self.data_engine.execute(subscribe2)
#
# # Act
# self.data_engine.process(ETHUSDT_BINANCE)
#
# # Assert
# self.assertEqual([ETHUSDT_BINANCE.symbol], self.data_engine.subscribed_instruments)
# self.assertEqual([ETHUSDT_BINANCE], handler1)
# self.assertEqual([ETHUSDT_BINANCE], handler2)
def test_process_order_book_when_one_subscriber_then_sends_to_registered_handler(self):
# Arrange
self.data_engine.register_client(self.binance_client)
self.binance_client.connect()

order_book = OrderBook(
symbol=ETHUSDT_BINANCE.symbol,
level=2,
depth=25,
price_precision=2,
size_precision=5,
bids=[],
asks=[],
update_id=0,
timestamp=0,
)

handler = []
subscribe = Subscribe(
provider=BINANCE.value,
data_type=DataType(OrderBook, {
"Symbol": ETHUSDT_BINANCE.symbol,
"Level": 2,
"Depth": 25,
"Interval": 0, # Streaming
}),
handler=handler.append,
command_id=self.uuid_factory.generate(),
command_timestamp=self.clock.utc_now(),
)

self.data_engine.execute(subscribe)

# Act
self.data_engine.process(order_book)

# Assert
self.assertEqual(order_book, handler[0])

def test_process_order_book_when_multiple_subscribers_then_sends_to_registered_handlers(self):
# Arrange
self.data_engine.register_client(self.binance_client)
self.binance_client.connect()

order_book = OrderBook(
symbol=ETHUSDT_BINANCE.symbol,
level=2,
depth=25,
price_precision=2,
size_precision=5,
bids=[],
asks=[],
update_id=0,
timestamp=0,
)

handler1 = []
subscribe1 = Subscribe(
provider=BINANCE.value,
data_type=DataType(OrderBook, {
"Symbol": ETHUSDT_BINANCE.symbol,
"Level": 2,
"Depth": 25,
"Interval": 0, # Streaming
}),
handler=handler1.append,
command_id=self.uuid_factory.generate(),
command_timestamp=self.clock.utc_now(),
)

handler2 = []
subscribe2 = Subscribe(
provider=BINANCE.value,
data_type=DataType(OrderBook, {
"Symbol": ETHUSDT_BINANCE.symbol,
"Level": 2,
"Depth": 25,
"Interval": 0, # Streaming
}),
handler=handler2.append,
command_id=self.uuid_factory.generate(),
command_timestamp=self.clock.utc_now(),
)

self.data_engine.execute(subscribe1)
self.data_engine.execute(subscribe2)

# Act
self.data_engine.process(order_book)

# Assert
self.assertEqual([ETHUSDT_BINANCE.symbol], self.data_engine.subscribed_order_books)
self.assertEqual(order_book, handler1[0])
self.assertEqual(order_book, handler2[0])

def test_execute_subscribe_for_quote_ticks(self):
# Arrange
Expand Down
4 changes: 2 additions & 2 deletions tests/unit_tests/live/test_live_execution_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ async def run_test():
self.loop.run_until_complete(run_test())

# TODO: WIP
# def test_resolve_state_with_multiple_active_orders_resolved_correctly1(self):
# def test_reconcile_state_with_multiple_active_orders_resolved_correctly1(self):
# async def run_test():
# # Arrange
# self.exec_engine.start()
Expand Down Expand Up @@ -393,7 +393,7 @@ async def run_test():
# self.exec_engine.process(TestStubs.event_order_submitted(order2))
#
# # Act
# await self.exec_engine.resolve_state()
# await self.exec_engine.reconcile_state()
# self.exec_engine.stop()
#
# self.loop.run_until_complete(run_test())

0 comments on commit 2f5b7b2

Please sign in to comment.