class SmarketsTestCase(unittest.TestCase): "Tests for the `smarkets.Smarkets` client object" def setUp(self): "Patch the `Session` object for mock use" self.session_patcher = patch('smarkets.streaming_api.session.Session') self.mock_session_cls = self.session_patcher.start() self.mock_session = self.mock_session_cls.return_value self.client = StreamingAPIClient(self.mock_session) def tearDown(self): "Stop the patcher" self.session_patcher.stop() self.mock_session_cls = None self.mock_session = None self.client = None def test_login_ok(self): "Test the `Smarkets.login` method" self.client.session.next_frame = read_session_buff_gen( SUCCESSFUL_LOGIN_RESPONSE_RAW) self.client.login() self.assertTrue(self.client.check_login()) def test_login_ok_async(self): "Test the `Smarkets.login` method" self.client.login(False) self.client.session.next_frame = read_session_buff_gen( SUCCESSFUL_LOGIN_RESPONSE_RAW + PAYLOAD_THROTTLE_LIMITS_RAW + PAYLOAD_THROTTLE_LIMITS_RAW) self.client.read() self.assertTrue(self.client.check_login()) def test_login_unauthorized(self): "Test the `Smarkets.login` method for unauthorized" self.client.session.next_frame = read_session_buff_gen( UNAUTHORIZED_LOGIN_RESPONSE_RAW) try: self.client.login() except LoginError as ex: self.mock_session.disconnect.assert_called_once() self.assertEquals(eto.LOGOUT_UNAUTHORISED, ex.reason) self.assertEquals(ex.reason_msg, 'LOGOUT_UNAUTHORISED') return assert False def test_login_ok_then_timeout(self): "Test the `Smarkets.login` method for a sequence of messages" self.client.session.next_frame = read_session_buff_gen( SUCCESSFUL_LOGIN_RESPONSE_RAW + PAYLOAD_THROTTLE_LIMITS_RAW + LOGOUT_HEARTBEAT_TIMEOUT_RAW) try: self.client.login() except LoginError as ex: self.mock_session.disconnect.assert_called_once() self.assertEquals(eto.LOGOUT_HEARTBEAT_TIMEOUT, ex.reason) self.assertEquals(ex.reason_msg, 'LOGOUT_HEARTBEAT_TIMEOUT') return assert False def test_login_noresponse(self): "Test the `Smarkets.login` when no login response has been received" self.client.session.next_frame = read_session_buff_gen('') try: self.client.login() except LoginTimeout: return assert False def test_logout_norecv(self): "Test the `Smarkets.logout` method" self.client.logout(False) self.assertEquals(self.mock_session.method_calls, [('logout', (), {}), ('disconnect', (), {})]) def test_each_instance_has_separate_callbacks(self): client_a, client_b = (StreamingAPIClient('_') for i in range(2)) handler = Handler() client_a.add_handler('seto.order_accepted', handler) eq_(handler.call_count, 0) client_a.callbacks['seto.order_accepted'](message='irrelevant') eq_(handler.call_count, 1) client_b.callbacks['seto.order_accepted'](message='also irrelevant') eq_(handler.call_count, 1) def test_add_bad_handler(self): "Test trying to add a bad handler either as a global or normal" for bad_handler in (50, 'foo', False, True, u'foo', 1.2, 1): self.assertRaises(ValueError, self.client.add_handler, 'eto.pong', bad_handler) self.assertRaises(ValueError, self.client.add_global_handler, bad_handler) def test_add_unknown_handler(self): "Test trying to add a handler for an unknown callback name" handler = lambda: None self.assertRaises(InvalidCallbackError, self.client.add_handler, 'foo', handler) self.assertRaises(InvalidCallbackError, self.client.del_handler, 'foo', handler) @staticmethod def _login_response(): "Create a dummy login response payload" payload = seto.Payload() payload.eto_payload.seq = 1 payload.eto_payload.type = eto.PAYLOAD_LOGIN_RESPONSE payload.eto_payload.login_response.session = 'session' payload.eto_payload.login_response.reset = 2 return payload
class SmarketsTestCase(unittest.TestCase): "Tests for the `smarkets.Smarkets` client object" def setUp(self): "Patch the `Session` object for mock use" self.session_patcher = patch('smarkets.streaming_api.session.Session') self.mock_session_cls = self.session_patcher.start() self.mock_session = self.mock_session_cls.return_value self.client = StreamingAPIClient(self.mock_session) def tearDown(self): "Stop the patcher" self.session_patcher.stop() self.mock_session_cls = None self.mock_session = None self.client = None def test_login(self): "Test the `Smarkets.login` method" payload = self._login_response() self.mock_session.next_frame.return_value = payload response = Mock() self.client.add_handler('eto.login_response', response) self.client.login() self.assertEquals( self.mock_session.method_calls, [('connect', (), {}), ('next_frame', (), {})]) response.assert_called_once_with(payload) def test_login_norecv(self): "Test the `Smarkets.login` method" payload = self._login_response() self.mock_session.next_frame.return_value = payload response = Mock() self.client.add_handler('eto.login_response', response) self.client.login(False) self.assertEquals( self.mock_session.method_calls, [('connect', (), {})]) self.assertFalse(response.called) self.mock_session.reset_mock() self.client.read() self.assertEquals( self.mock_session.method_calls, [('next_frame', (), {})]) response.assert_called_once_with(payload) def test_logout(self): "Test the `Smarkets.logout` method" self.client.logout() self.assertEquals( self.mock_session.method_calls, [('logout', (), {}), ('next_frame', (), {}), ('disconnect', (), {})]) def test_logout_norecv(self): "Test the `Smarkets.logout` method" self.client.logout(False) self.assertEquals( self.mock_session.method_calls, [('logout', (), {}), ('disconnect', (), {})]) def test_each_instance_has_separate_callbacks(self): client_a, client_b = (StreamingAPIClient('_') for i in range(2)) handler = Handler() client_a.add_handler('seto.http_found', handler) eq_(handler.call_count, 0) client_a.callbacks['seto.http_found']('irrelevant') eq_(handler.call_count, 1) client_b.callbacks['seto.http_found']('also irrelevant') eq_(handler.call_count, 1) def test_flush(self): "Test the `Smarkets.flush` method" self.client.flush() self.assertEquals( self.mock_session.method_calls, [('flush', (), {})]) def test_order(self): "Test the `Smarkets.order` method" market_id = self.client.str_to_uuid128('1c024') contract_id = self.client.str_to_uuid128('1cccc') with self._clear_send(): order = OrderCreate() order.price = 2500 order.quantity = 10000 order.side = BUY order.market = market_id order.contract = contract_id order.validate_new() self.client.send(order) def test_order_cancel(self): "Test the `Smarkets.order_cancel` method" order_id = self.client.str_to_uuid128('1fff0') with self._clear_send(): self.client.send(OrderCancel(order_id)) def test_ping(self): "Test the `Smarkets.ping` method" with self._clear_send(): self.client.ping() def test_subscribe(self): "Test the `Smarkets.subscribe` method" market_id = self.client.str_to_uuid128('1c024') with self._clear_send(): self.client.subscribe(market_id) def test_unsubscribe(self): "Test the `Smarkets.unsubscribe` method" market_id = self.client.str_to_uuid128('1c024') with self._clear_send(): self.client.unsubscribe(market_id) def test_add_bad_handler(self): "Test trying to add a bad handler either as a global or normal" for bad_handler in ( 50, 'foo', False, True, u'foo', 1.2, 1L): self.assertRaises( ValueError, self.client.add_handler, 'eto.pong', bad_handler) self.assertRaises( ValueError, self.client.add_global_handler, bad_handler) def test_add_unknown_handler(self): "Test trying to add a handler for an unknown callback name" handler = lambda: None self.assertRaises( InvalidCallbackError, self.client.add_handler, 'foo', handler) self.assertRaises( InvalidCallbackError, self.client.del_handler, 'foo', handler) @contextmanager def _clear_send(self): """ Shortcut for asserting that the outgoing payload is cleared and sent via the session """ self.mock_session.out_payload.Clear = Mock() yield self.mock_session.send.assert_called_once_with(True) self.assertEquals(1, self.mock_session.out_payload.Clear.call_count) @staticmethod def _login_response(): "Create a dummy login response payload" payload = seto.Payload() payload.eto_payload.seq = 1 payload.eto_payload.type = eto.PAYLOAD_LOGIN_RESPONSE payload.eto_payload.login_response.session = 'session' payload.eto_payload.login_response.reset = 2 return payload
import logging logging.basicConfig(level=logging.DEBUG) from smarkets.streaming_api.api import SessionSettings, Session, StreamingAPIClient username = '******' password = '******' settings = SessionSettings(username, password) settings.host = 'stream.smarkets.com' session = Session(settings) client = StreamingAPIClient(session) client.login()
class SmarketsTestCase(unittest.TestCase): "Tests for the `smarkets.Smarkets` client object" def setUp(self): "Patch the `Session` object for mock use" self.session_patcher = patch('smarkets.streaming_api.session.Session') self.mock_session_cls = self.session_patcher.start() self.mock_session = self.mock_session_cls.return_value self.client = StreamingAPIClient(self.mock_session) def tearDown(self): "Stop the patcher" self.session_patcher.stop() self.mock_session_cls = None self.mock_session = None self.client = None def test_login_ok(self): "Test the `Smarkets.login` method" self.client.session.next_frame = read_session_buff_gen(SUCCESSFUL_LOGIN_RESPONSE_RAW) self.client.login() self.assertTrue(self.client.check_login()) def test_login_ok_async(self): "Test the `Smarkets.login` method" self.client.login(False) self.client.session.next_frame = read_session_buff_gen( SUCCESSFUL_LOGIN_RESPONSE_RAW + PAYLOAD_THROTTLE_LIMITS_RAW + PAYLOAD_THROTTLE_LIMITS_RAW) self.client.read() self.assertTrue(self.client.check_login()) def test_login_unauthorized(self): "Test the `Smarkets.login` method for unauthorized" self.client.session.next_frame = read_session_buff_gen(UNAUTHORIZED_LOGIN_RESPONSE_RAW) try: self.client.login() except LoginError as ex: self.mock_session.disconnect.assert_called_once() self.assertEquals(eto.LOGOUT_UNAUTHORISED, ex.reason) self.assertEquals(ex.reason_msg, 'LOGOUT_UNAUTHORISED') return assert False def test_login_ok_then_timeout(self): "Test the `Smarkets.login` method for a sequence of messages" self.client.session.next_frame = read_session_buff_gen( SUCCESSFUL_LOGIN_RESPONSE_RAW + PAYLOAD_THROTTLE_LIMITS_RAW + LOGOUT_HEARTBEAT_TIMEOUT_RAW) try: self.client.login() except LoginError as ex: self.mock_session.disconnect.assert_called_once() self.assertEquals(eto.LOGOUT_HEARTBEAT_TIMEOUT, ex.reason) self.assertEquals(ex.reason_msg, 'LOGOUT_HEARTBEAT_TIMEOUT') return assert False def test_login_noresponse(self): "Test the `Smarkets.login` when no login response has been received" self.client.session.next_frame = read_session_buff_gen('') try: self.client.login() except LoginTimeout: return assert False def test_logout_norecv(self): "Test the `Smarkets.logout` method" self.client.logout(False) self.assertEquals( self.mock_session.method_calls, [('logout', (), {}), ('disconnect', (), {})]) def test_each_instance_has_separate_callbacks(self): client_a, client_b = (StreamingAPIClient('_') for i in range(2)) handler = Handler() client_a.add_handler('seto.order_accepted', handler) eq_(handler.call_count, 0) client_a.callbacks['seto.order_accepted'](message='irrelevant') eq_(handler.call_count, 1) client_b.callbacks['seto.order_accepted'](message='also irrelevant') eq_(handler.call_count, 1) def test_add_bad_handler(self): "Test trying to add a bad handler either as a global or normal" for bad_handler in ( 50, 'foo', False, True, u'foo', 1.2, 1): self.assertRaises( ValueError, self.client.add_handler, 'eto.pong', bad_handler) self.assertRaises( ValueError, self.client.add_global_handler, bad_handler) def test_add_unknown_handler(self): "Test trying to add a handler for an unknown callback name" handler = lambda: None self.assertRaises( InvalidCallbackError, self.client.add_handler, 'foo', handler) self.assertRaises( InvalidCallbackError, self.client.del_handler, 'foo', handler) @staticmethod def _login_response(): "Create a dummy login response payload" payload = seto.Payload() payload.eto_payload.seq = 1 payload.eto_payload.type = eto.PAYLOAD_LOGIN_RESPONSE payload.eto_payload.login_response.session = 'session' payload.eto_payload.login_response.reset = 2 return payload