def setUp(self) -> None: mock_client = mock.Mock(paper_trade=False) self.mock_trade = mock.Mock( client=mock_client, market_id="1.1", selection_id=123, info={} ) self.mock_order_type = mock.Mock(info={}) self.order = BaseOrder( self.mock_trade, "BACK", self.mock_order_type, 1, context={1: 2} )
def setUp(self) -> None: mock_client = mock.Mock(paper_trade=False) self.mock_trade = mock.Mock(client=mock_client) self.mock_order_type = mock.Mock() self.order = BaseOrder(self.mock_trade, "BACK", self.mock_order_type, 1) config.simulated = True
def test_current_order_simulated(self, mock_config): mock_config.simulated = True order = BaseOrder(mock.Mock(), "", mock.Mock()) self.assertTrue(order.simulated) self.assertTrue(order._simulated)
class BaseOrderTest(unittest.TestCase): def setUp(self) -> None: mock_client = mock.Mock(paper_trade=False) self.mock_trade = mock.Mock(client=mock_client, market_id="1.1", selection_id=123, info={}) self.mock_order_type = mock.Mock(info={}) self.order = BaseOrder(self.mock_trade, "BACK", self.mock_order_type, 1, context={1: 2}) def test_init(self): self.assertIsNotNone(self.order.id) self.assertEqual(self.order.trade, self.mock_trade) self.assertEqual(self.order.side, "BACK") self.assertEqual(self.order.order_type, self.mock_order_type) self.assertEqual(self.order.handicap, 1) self.assertEqual( self.order.lookup, (self.order.market_id, self.order.selection_id, self.order.handicap), ) self.assertIsNone(self.order.runner_status) self.assertIsNone(self.order.status) self.assertEqual(self.order.status_log, []) self.assertIsNone(self.order.violation_msg) self.assertEqual(self.order.context, {1: 2}) self.assertIsNone(self.order.bet_id) self.assertIsNone(self.order.EXCHANGE) self.assertEqual(self.order.update_data, {}) self.assertIsNone(self.order.publish_time) self.assertIsNotNone(self.order.date_time_created) self.assertIsNone(self.order.date_time_execution_complete) self.assertFalse(self.order.simulated) self.assertFalse(self.order._simulated) @mock.patch("flumine.order.order.BaseOrder.info") def test__update_status(self, mock_info): self.mock_trade.complete = True self.order._update_status(OrderStatus.EXECUTION_COMPLETE) self.assertEqual(self.order.status_log, [OrderStatus.EXECUTION_COMPLETE]) self.assertEqual(self.order.status, OrderStatus.EXECUTION_COMPLETE) self.mock_trade.complete_trade.assert_called() @mock.patch("flumine.order.order.BaseOrder._update_status") def test_placing(self, mock__update_status): self.order.placing() mock__update_status.assert_called_with(OrderStatus.PENDING) @mock.patch("flumine.order.order.BaseOrder._update_status") def test_executable(self, mock__update_status): self.order.update_data = {123: 456} self.order.executable() mock__update_status.assert_called_with(OrderStatus.EXECUTABLE) self.assertEqual(self.order.update_data, {}) @mock.patch("flumine.order.order.BaseOrder._update_status") def test_execution_complete(self, mock__update_status): self.order.update_data = {123: 456} self.order.execution_complete() mock__update_status.assert_called_with(OrderStatus.EXECUTION_COMPLETE) self.assertIsNotNone(self.order.date_time_execution_complete) self.assertEqual(self.order.update_data, {}) @mock.patch("flumine.order.order.BaseOrder._update_status") def test_cancelling(self, mock__update_status): self.order.cancelling() mock__update_status.assert_called_with(OrderStatus.CANCELLING) @mock.patch("flumine.order.order.BaseOrder._update_status") def test_updating(self, mock__update_status): self.order.updating() mock__update_status.assert_called_with(OrderStatus.UPDATING) @mock.patch("flumine.order.order.BaseOrder._update_status") def test_replacing(self, mock__update_status): self.order.replacing() mock__update_status.assert_called_with(OrderStatus.REPLACING) @mock.patch("flumine.order.order.BaseOrder._update_status") def test_lapsed(self, mock__update_status): self.order.update_data = {123: 456} self.order.lapsed() mock__update_status.assert_called_with(OrderStatus.LAPSED) self.assertEqual(self.order.update_data, {}) @mock.patch("flumine.order.order.BaseOrder._update_status") def test_voided(self, mock__update_status): self.order.update_data = {123: 456} self.order.voided() mock__update_status.assert_called_with(OrderStatus.VOIDED) self.assertEqual(self.order.update_data, {}) @mock.patch("flumine.order.order.BaseOrder._update_status") def test_violation(self, mock__update_status): self.order.update_data = {123: 456} self.order.violation("the murder capital") mock__update_status.assert_called_with(OrderStatus.VIOLATION) self.assertEqual(self.order.update_data, {}) self.assertEqual(self.order.violation_msg, "the murder capital") def test_place(self): with self.assertRaises(NotImplementedError): self.order.place(123) def test_cancel(self): with self.assertRaises(NotImplementedError): self.order.cancel() def test_update(self): with self.assertRaises(NotImplementedError): self.order.update("PERSIST") def test_replace(self): with self.assertRaises(NotImplementedError): self.order.replace(20.0) def test_create_place_instruction(self): with self.assertRaises(NotImplementedError): self.order.create_place_instruction() def test_create_cancel_instruction(self): with self.assertRaises(NotImplementedError): self.order.create_cancel_instruction() def test_create_update_instruction(self): with self.assertRaises(NotImplementedError): self.order.create_update_instruction() def test_create_replace_instruction(self): with self.assertRaises(NotImplementedError): self.order.create_replace_instruction() def test_update_current_order(self): mock_current_order = mock.Mock() self.order.update_current_order(mock_current_order) self.assertEqual(self.order.responses.current_order, mock_current_order) def test_current_order(self): self.assertIsNone(self.order.current_order) mock_responses = mock.Mock() mock_responses.current_order = None self.order.responses = mock_responses self.assertEqual(self.order.current_order, mock_responses.place_response) mock_responses.current_order = 1 self.assertEqual(self.order.current_order, 1) @mock.patch("flumine.backtest.simulated.config") def test_current_order_simulated(self, mock_config): mock_config.simulated = True order = BaseOrder(mock.Mock(), "", mock.Mock()) self.assertTrue(order.simulated) self.assertTrue(order._simulated) def test_complete(self): self.order.status = None self.assertFalse(self.order.complete) for s in [ OrderStatus.PENDING, OrderStatus.CANCELLING, OrderStatus.UPDATING, OrderStatus.REPLACING, OrderStatus.EXECUTABLE, ]: self.order.status = s self.assertFalse(self.order.complete) for s in [ OrderStatus.EXECUTION_COMPLETE, OrderStatus.EXPIRED, OrderStatus.VOIDED, OrderStatus.LAPSED, OrderStatus.VIOLATION, ]: self.order.status = s self.assertTrue(self.order.complete) def test_average_price_matched(self): with self.assertRaises(NotImplementedError): assert self.order.average_price_matched def test_size_matched(self): with self.assertRaises(NotImplementedError): assert self.order.size_matched def test_size_remaining(self): with self.assertRaises(NotImplementedError): assert self.order.size_remaining def test_size_cancelled(self): with self.assertRaises(NotImplementedError): assert self.order.size_cancelled def test_size_lapsed(self): with self.assertRaises(NotImplementedError): assert self.order.size_lapsed def test_size_voided(self): with self.assertRaises(NotImplementedError): assert self.order.size_voided def test_elapsed_seconds(self): self.assertIsNone(self.order.elapsed_seconds) mock_responses = mock.Mock() mock_responses.date_time_placed = datetime.datetime.utcnow() self.order.responses = mock_responses self.assertGreaterEqual(self.order.elapsed_seconds, 0) def test_elapsed_seconds_executable(self): self.assertIsNone(self.order.elapsed_seconds_executable) mock_responses = mock.Mock() mock_responses.date_time_placed = datetime.datetime.utcnow() self.order.responses = mock_responses self.order.date_time_execution_complete = datetime.datetime.utcnow() self.assertGreaterEqual(self.order.elapsed_seconds_executable, 0) def test_market_id(self): self.assertEqual(self.order.market_id, self.mock_trade.market_id) def test_selection_id(self): self.assertEqual(self.order.selection_id, self.mock_trade.selection_id) def test_lookup(self): self.assertEqual( self.order.lookup, (self.mock_trade.market_id, self.mock_trade.selection_id, 1), ) def test_repr(self): self.assertEqual(repr(self.order), "Order None: None") def test_set_and_get_sep(self): self.order.sep = "a" self.assertEqual("a", self.order.sep) def test_customer_order_ref(self): self.order.trade.strategy.name_hash = "my_name_hash" self.order.id = 1234 self.assertEqual("my_name_hash-1234", self.order.customer_order_ref) self.order.sep = "I" self.assertEqual("my_name_hashI1234", self.order.customer_order_ref) self.order.sep = "O" self.assertEqual("my_name_hashO1234", self.order.customer_order_ref)
def setUp(self) -> None: self.mock_trade = mock.Mock() self.mock_order_type = mock.Mock() self.order = BaseOrder(self.mock_trade, "BACK", self.mock_order_type, 1)