def create_register_from_warehouse(self, stocks: List[Stock], start_epoch_s: float, end_epoch_s: float) -> \ SymbolDayRegister: sdr = SymbolDayRegister() moments = self.warehouse_client.get_moments(stocks, start_epoch_s, end_epoch_s) sdr.add_moments(moments) return sdr
class AddMomentToRegisterTestCase(unittest.TestCase): def setUp(self): self.symbol_day_register = SymbolDayRegister() def test_add_moment(self): moment = Moment.from_dict(STOCKS_DAILY['AAPL']['records'][0]) moment.symbol = 'AAPL' self.symbol_day_register.add_moment(moment) self.assertIn('AAPL', self.symbol_day_register.series.keys()) self.assertEqual(moment, self.symbol_day_register.series['AAPL'].get_moments()[0]) def test_add_multiple_moments(self): moments = list() for symbol in ('AAPL', 'BYND'): _chunk = [Moment.from_dict(x) for x in STOCKS_DAILY[symbol]['records']] for moment in _chunk: moment.symbol = symbol moments += _chunk moments_shuffled = moments.copy() random.shuffle(moments_shuffled) self.assertNotEqual(moments, moments_shuffled) self.symbol_day_register.add_moments(moments_shuffled) actual_moments = list() for symbol in ('AAPL', 'BYND'): actual_moments += self.symbol_day_register.series[symbol].get_moments() self.assertEqual(moments, actual_moments)
def _populate_today_register(self, stocks: List[Stock]): register = SymbolDayRegister() moments = list() for stock in stocks: prices = self.data_ingest_manager.market_client.get_real_time_price( [stock]) for price in prices: moment_data = { 'epoch_s': self.today_epoch_s, 'exchange': price['exchange'], 'symbol': price['symbol'], 'close': float(price['price']) } moment = Moment(**moment_data) moments.append(moment) register.add_moments(moments) return register
class GetFromRegisterTestCase(unittest.TestCase): def setUp(self): self.symbol_day_register = SymbolDayRegister() def test_get(self): moments = list() for symbol in ('AAPL', 'BYND'): _chunk = [Moment.from_dict(x) for x in STOCKS_DAILY[symbol]['records']] for moment in _chunk: moment.symbol = symbol moments += _chunk self.symbol_day_register.add_moments(moments) for symbol in ('AAPL', 'BYND'): for _ in range(3): i = random.randint(0, len(STOCKS_DAILY[symbol]['records']) - 1) moment = Moment.from_dict(STOCKS_DAILY[symbol]['records'][i]) moment.symbol = symbol self.assertEqual(moment, self.symbol_day_register.get(symbol, moment.epoch_s))