async def test_trading_day_ended(mocker): """В конце торгового дня должны обновляться базовые данные. Создаются события для обновления индексов и осуществляется обновление трех таблиц. """ dispatcher = handlers.EventHandlersDispatcher() event = events.TradingDayEnded(date(2020, 12, 22)) fake_repo = mocker.Mock() mocker.patch.object( handlers, "_load_by_id_and_handle_event", side_effect=[["a"], ["rf"], ["b", "c"], ["usd"]], ) assert await dispatcher.handle_event(event, fake_repo) == [ events.IndexCalculated("MCFTRR", event.date), events.IndexCalculated("MEOGTRR", event.date), events.IndexCalculated("IMOEX", event.date), events.IndexCalculated("RVI", event.date), "a", "rf", "b", "c", "usd", ]
async def test_prepare_df_for_update_table(table, mocker): """Если таблица c данных, то осуществляется инкрементальная загрузка.""" event = events.IndexCalculated("TICKER", date(2020, 12, 15)) fake_gateway = mocker.AsyncMock() fake_gateway.return_value = pd.DataFrame( [2, 1], index=[ datetime(2020, 12, 14), datetime(2020, 12, 15), ], ) table._gateway = fake_gateway table._df = pd.DataFrame( [3, 2], index=[ datetime(2020, 12, 13), datetime(2020, 12, 14), ], ) new_df = await table._prepare_df(event) pd.testing.assert_frame_equal( new_df, pd.DataFrame( [3, 2, 1], index=[ datetime(2020, 12, 13), datetime(2020, 12, 14), datetime(2020, 12, 15), ], ), ) fake_gateway.assert_called_once_with("TICKER", "2020-12-14", "2020-12-15")
async def test_prepare_df_for_new_table(table, mocker): """Если таблица без данных, то осуществляется загрузка с начала.""" event = events.IndexCalculated("TICKER", date(2020, 12, 15)) fake_gateway = mocker.AsyncMock() table._gateway = fake_gateway assert await table._prepare_df(event) == fake_gateway.return_value fake_gateway.assert_called_once_with("TICKER", None, "2020-12-15")
async def trading_day_ended( self, event: events.TradingDayEnded, repo: AnyTableRepo, ) -> list[domain.AbstractEvent]: """Запускает обновление необходимых таблиц в конце торгового дня и создает дочерние события.""" table_groups = [ports.CPI, ports.DIV_NEW, ports.USD] table_ids = [base.create_id(group) for group in table_groups] aws = [ _load_by_id_and_handle_event(repo, id_, event) for id_ in table_ids ] return [ events.IndexCalculated("MCFTRR", event.date), events.IndexCalculated("IMOEX", event.date), events.IndexCalculated("RVI", event.date), *itertools.chain.from_iterable(await asyncio.gather(*aws)), ]
async def test_index_calculated(mocker): """Требуется обновить таблицу с индексом.""" dispatcher = handlers.EventHandlersDispatcher() event = events.IndexCalculated("IND1", date(2020, 12, 22)) fake_repo = mocker.Mock() fake_loader_and_handler = mocker.patch.object( handlers, "_load_by_id_and_handle_event") assert await dispatcher.handle_event( event, fake_repo) is fake_loader_and_handler.return_value fake_loader_and_handler.assert_called_once_with( fake_repo, base.create_id(ports.INDEX, "IND1"), event, )