def test_setConfig(self): self.interface.handleRequest(6, 'bl4p.setconfig', {'values': { 'foo': 'xx', 'bar': 'yy' }}) self.assertEqual(self.output.buffer, b'') self.client.handleIncomingMessage.assert_called_once_with( messages.SetConfigCommand( commandID=6, values={ 'foo': 'xx', 'bar': 'yy' }, )) self.interface.handleMessage( messages.PluginCommandResult( commandID=2, result=None, )) self.checkJSONOutput({ 'jsonrpc': '2.0', 'id': 2, 'result': None, })
def test_handleGetConfigCommand(self): self.backend.storage = MockStorage(test=self) self.backend.BL4PAddress = 'BL4PAddress' MS = MockStorage(test=self) MS.configuration['bl4p.apiKey'] = 'foo' MS.configuration['bl4p.apiSecret'] = 'bar' self.backend.configuration = configuration.Configuration(MS) cmd = Mock() cmd.commandID = 42 self.backend.handleGetConfigCommand(cmd) self.assertEqual(self.outgoingMessages, [messages.PluginCommandResult( commandID=42, result=\ { 'values': { 'bl4p.url' : '', 'bl4p.apiKey' : 'foo', 'bl4p.apiSecret' : 'bar', 'bl4p.signingPrivateKey': '', } } )])
def test_handleSellCommand(self): self.backend.storage = MockStorage(test=self) self.backend.BL4PAddress = 'BL4PAddress' cmd = Mock() cmd.commandID = 42 cmd.amount = 123 cmd.limitRate = 20000 with patch.object(backend.ordertask, 'OrderTask', MockOrderTask): self.backend.handleSellCommand(cmd) self.assertEqual(set(self.backend.orderTasks.keys()), set([61])) self.assertEqual(set(self.backend.storage.sellOrders.keys()), set([61])) ot = self.backend.orderTasks[61] stored = self.backend.storage.sellOrders[61] self.assertTrue(isinstance(ot, MockOrderTask)) self.assertEqual(ot.client, self.client) self.assertEqual(ot.storage, self.backend.storage) self.assertTrue(ot.started) self.assertTrue(isinstance(ot.order, order.SellOrder)) self.assertEqual(stored['amount'], ot.order.amount) self.assertEqual(stored['limitRate'], ot.order.limitRate) self.assertEqual(stored['amount'], 123) self.assertEqual(stored['limitRate'], 20000) self.assertEqual( self.outgoingMessages, [messages.PluginCommandResult(commandID=42, result=None)])
def test_handleListCommand(self): class BuyOrder: pass class SellOrder: pass class Task: def __init__(self, cls, info): self.order = cls() self.info = info def getListInfo(self): return {'info': self.info} self.backend.orderTasks = \ { 41: Task(BuyOrder, 'buy btc'), 51: Task(SellOrder, 'sell bcash'), 52: Task(SellOrder, 'sell pizza'), } cmd = Mock() cmd.commandID = 42 with patch.object(backend, 'BuyOrder', BuyOrder): with patch.object(backend, 'SellOrder', SellOrder): self.backend.handleListCommand(cmd) self.assertEqual(self.outgoingMessages, [messages.PluginCommandResult( commandID=42, result=\ { 'buy' : [ {'info': 'buy btc'}, ], 'sell': [ {'info': 'sell bcash'}, {'info': 'sell pizza'}, ], } )]) self.backend.orderTasks = \ { 41: Task(Mock, 'foobar'), } with patch.object(backend, 'BuyOrder', BuyOrder): with patch.object(backend, 'SellOrder', SellOrder): with self.assertRaises(Exception): self.backend.handleListCommand(cmd)
def handleSellCommand(self, cmd: messages.SellCommand) -> None: ID = SellOrder.create( self.storage, limitRate=cmd.limitRate, amount=cmd.amount, ) #type: int order = SellOrder(self.storage, ID, self.BL4PAddress) #type: SellOrder self.addOrder(order) self.client.handleOutgoingMessage( messages.PluginCommandResult(commandID=cmd.commandID, result=None))
def handleCancelCommand(self, cmd: messages.CancelCommand) -> None: try: task = self.orderTasks[cmd.orderID] except KeyError: self.client.handleOutgoingMessage( messages.PluginCommandError( commandID=cmd.commandID, code=2, message='There is no active order with ID ' + str(cmd.orderID))) return task.cancel() self.client.handleOutgoingMessage( messages.PluginCommandResult(commandID=cmd.commandID, result=None))
def handleListCommand(self, cmd: messages.ListCommand) -> None: sell = [] #type: List[Dict[str, Any]] buy = [] #type: List[Dict[str, Any]] for task in self.orderTasks.values(): order = task.getListInfo() #type: Dict[str, Any] if isinstance(task.order, SellOrder): sell.append(order) elif isinstance(task.order, BuyOrder): buy.append(order) else: raise Exception('Found an order of unknown type') self.client.handleOutgoingMessage( messages.PluginCommandResult(commandID=cmd.commandID, result={ 'sell': sell, 'buy': buy }))
def test_Cancel(self): self.interface.handleRequest(6, 'bl4p.cancel', {'orderID': 42}) self.assertEqual(self.output.buffer, b'') self.client.handleIncomingMessage.assert_called_once_with( messages.CancelCommand( commandID=6, orderID=42, )) self.interface.handleMessage( messages.PluginCommandResult( commandID=6, result=None, )) self.checkJSONOutput({ 'jsonrpc': '2.0', 'id': 6, 'result': None, })
def test_handleCancelCommand(self): self.backend.orderTasks = {41: Mock()} cmd = Mock() cmd.commandID = 42 cmd.orderID = 41 self.backend.handleCancelCommand(cmd) self.backend.orderTasks[41].cancel.assert_called_with() self.assertEqual( self.outgoingMessages, [messages.PluginCommandResult(commandID=42, result=None)]) self.outgoingMessages = [] cmd.orderID = 40 self.backend.handleCancelCommand(cmd) self.assertEqual(self.outgoingMessages, [ messages.PluginCommandError( commandID=42, code=2, message='There is no active order with ID 40') ])
def test_List(self): self.interface.handleRequest(6, 'bl4p.list', {}) self.assertEqual(self.output.buffer, b'') self.client.handleIncomingMessage.assert_called_once_with( messages.ListCommand(commandID=6, )) self.interface.handleMessage( messages.PluginCommandResult( commandID=6, result={ 'sell': [], 'buy': [] }, )) self.checkJSONOutput({ 'jsonrpc': '2.0', 'id': 6, 'result': { 'sell': [], 'buy': [] }, })
def test_Sell(self): self.interface.handleRequest(2, 'bl4p.sell', { 'limit_rate': 42, 'amount': 6 }) self.assertEqual(self.output.buffer, b'') self.client.handleIncomingMessage.assert_called_once_with( messages.SellCommand( commandID=2, limitRate=42, amount=6, )) self.interface.handleMessage( messages.PluginCommandResult( commandID=2, result=None, )) self.checkJSONOutput({ 'jsonrpc': '2.0', 'id': 2, 'result': None, })
def test_handleSetConfigCommand(self): self.backend.storage = MockStorage(test=self) self.backend.BL4PAddress = 'BL4PAddress' MS = MockStorage(test=self) self.backend.configuration = configuration.Configuration(MS) cmd = Mock() cmd.commandID = 42 cmd.values = {'bl4p.apiKey': 'foo', 'bl4p.apiSecret': 'bar'} self.backend.handleSetConfigCommand(cmd) self.assertEqual(self.backend.configuration.getValue('bl4p.apiKey'), 'foo') self.assertEqual(self.backend.configuration.getValue('bl4p.apiSecret'), 'bar') self.assertEqual(MS.configuration['bl4p.apiKey'], 'foo') self.assertEqual(MS.configuration['bl4p.apiSecret'], 'bar') self.assertEqual( self.outgoingMessages, [messages.PluginCommandResult(commandID=42, result=None)])
def handleGetConfigCommand(self, cmd: messages.GetConfigCommand) -> None: values = self.configuration.getAllValues() #type: Dict[str, str] self.client.handleOutgoingMessage( messages.PluginCommandResult(commandID=cmd.commandID, result={'values': values}))
def handleSetConfigCommand(self, cmd: messages.SetConfigCommand) -> None: for k, v in cmd.values.items(): self.configuration.setValue(k, v) self.client.handleOutgoingMessage( messages.PluginCommandResult(commandID=cmd.commandID, result=None))