async def test_prepare_df_for_update_table(table, mocker): """Если таблица c данных, то осуществляется инкрементальная загрузка.""" event = events.TradingDayEnded(date(2021, 2, 3)) 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("2020-12-14", "2021-02-03")
def test_update_cond(df, event_date, update): """Индекс обновляется при отсутствии и по окончании месяца.""" event = events.TradingDayEnded(event_date) id_ = base.create_id(ports.CPI) table = cpi.CPI(id_, df=df) assert table._update_cond(event) == update
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_new_table(table, mocker): """Если таблица без данных, то осуществляется загрузка с начала.""" event = events.TradingDayEnded(date(2021, 2, 3)) fake_gateway = mocker.AsyncMock() table._gateway = fake_gateway assert await table._prepare_df(event) == fake_gateway.return_value fake_gateway.assert_called_once_with(None, "2021-02-03")
def _new_events(self, event: events.AppStarted) -> list[domain.AbstractEvent]: """Событие окончания торгового дня.""" df: pd.DataFrame = self._df last_trading_day = df.loc[0, "till"] if last_trading_day != self._last_trading_day_old: return [events.TradingDayEnded(last_trading_day.date())] return []
def test_new_events(table, mocker): """Возвращает событие обновления курса.""" table._df = mocker.Mock() new_events = table._new_events( events.TradingDayEnded(date=date(2020, 2, 5))) assert new_events == [ events.USDUpdated( date=date(2020, 2, 5), usd=table._df.copy.return_value, ), ]
table._validate_new_df(df_new) elif raises is False: table._validate_new_df(df_new) assert table._last_trading_day_old == "test" else: table._validate_new_df(df_new) assert table._last_trading_day_old is None NEW_EVENTS_CASES = ( ( pd.DataFrame(datetime(2020, 12, 14), index=[0], columns=["till"]), datetime(2020, 12, 14), [], ), ( pd.DataFrame(datetime(2020, 12, 14), index=[0], columns=["till"]), datetime(2020, 12, 13), [events.TradingDayEnded(date(2020, 12, 14))], ), ) @pytest.mark.parametrize("df, old_date, new_events", NEW_EVENTS_CASES) def test_new_events(table, df, old_date, new_events): """Два результата в зависимости от изменения даты.""" table._df = df table._last_trading_day_old = old_date assert table._new_events("") == new_events