def test_remove(self): parse_command('remove stock 123; remove stock 123 10') msg = self.handler.receive() self.assertEqual(msg.title, 'vault_request', 'Remove command should produce vault_request message') self.assertEqual(msg.msg, 'remove_stock', 'Remove command should produce a remove_stock message') self.assertTrue(msg.payload == ManagedStock('None', table_id=123, shares=-1)) msg = self.handler.receive() self.assertTrue(msg.payload == ManagedStock('None', table_id=123, shares=10))
def test_add(self): parse_command('add stock AAPL shares=10; add stock AAPL 1 3.0; add stock AAPL shares=10 price=4.50') msg = self.handler.receive() self.assertEqual(msg.title, 'vault_request', 'Add command should produce vault_request message') self.assertEqual(msg.msg, 'add_stock', 'Add command should produce an add_stock message') self.assertTrue(msg.payload == ManagedStock('AAPL', shares=10, last_price=-1)) msg = self.handler.receive() self.assertTrue(msg.payload == ManagedStock('AAPL', shares=1, last_price=3)) msg = self.handler.receive() self.assertTrue(msg.payload == ManagedStock('AAPL', shares=10, last_price=4.5))
def parse_stock_descriptor(stock_descriptor: str) -> tuple: spaces = stock_descriptor.strip().count(' ') info = stock_descriptor.strip().split(' ') if spaces == 4: d = ManagedStock(info[0], shares=int(info[1])) l = LimitDescriptor(d.table_id, info[2], float(info[3]), float(info[4])) elif spaces == 3: d = ManagedStock(info[0], shares=int(info[1])) l = LimitDescriptor(d.table_id, '%', float(info[3]), float(info[2])) elif spaces == 1: d = ManagedStock(info[0], 0.95, 1.05, int(info[1])) l = LimitDescriptor(d.table_id, '%', 0.95, 1.05) else: print('Incorrect stocks.txt format') exit(1) return d, l
def handle(self, handler: MessageHandler, data: dict) -> None: handler.send( Message( 'vault_request', 'update_stock', ManagedStock(data['acronym'], data['id'], data['shares'], data['price'])))