tests: simplify telegram test setup

This commit is contained in:
Matthias
2025-03-16 13:42:15 +01:00
parent 623f27341f
commit d543feb032
+20 -14
View File
@@ -6,7 +6,7 @@ import asyncio
import logging import logging
import re import re
import threading import threading
from datetime import datetime, timedelta, timezone from datetime import timedelta
from functools import reduce from functools import reduce
from random import choice, randint from random import choice, randint
from string import ascii_uppercase from string import ascii_uppercase
@@ -16,7 +16,7 @@ import pytest
import time_machine import time_machine
from pandas import DataFrame from pandas import DataFrame
from sqlalchemy import select from sqlalchemy import select
from telegram import Chat, Message, ReplyKeyboardMarkup, Update from telegram import Chat, Message, ReplyKeyboardMarkup, Update, User
from telegram.error import BadRequest, NetworkError, TelegramError from telegram.error import BadRequest, NetworkError, TelegramError
from freqtrade import __version__ from freqtrade import __version__
@@ -67,7 +67,12 @@ def default_conf(default_conf) -> dict:
@pytest.fixture @pytest.fixture
def update(): def update():
message = Message(0, datetime.now(timezone.utc), Chat(1235, 0)) message = Message(
0,
dt_now(),
Chat(1235, 0),
from_user=User(5432, "test", is_bot=False),
)
_update = Update(0, message=message) _update = Update(0, message=message)
return _update return _update
@@ -232,8 +237,12 @@ async def test_authorized_only(default_conf, mocker, caplog, update) -> None:
async def test_authorized_only_unauthorized(default_conf, mocker, caplog) -> None: async def test_authorized_only_unauthorized(default_conf, mocker, caplog) -> None:
patch_exchange(mocker) patch_exchange(mocker)
caplog.set_level(logging.DEBUG) caplog.set_level(logging.DEBUG)
chat = Chat(0xDEADBEEF, 0) message = Message(
message = Message(randint(1, 100), datetime.now(timezone.utc), chat) randint(1, 100),
dt_now(),
Chat(0xDEADBEEF, 0),
from_user=User(5432, "test", is_bot=False),
)
update = Update(randint(1, 100), message=message) update = Update(randint(1, 100), message=message)
default_conf["telegram"]["enabled"] = False default_conf["telegram"]["enabled"] = False
@@ -638,7 +647,7 @@ async def test_daily_handle(default_conf_usdt, update, ticker, fee, mocker, time
assert msg_mock.call_count == 1 assert msg_mock.call_count == 1
assert "Daily Profit over the last 2 days</b>:" in msg_mock.call_args_list[0][0][0] assert "Daily Profit over the last 2 days</b>:" in msg_mock.call_args_list[0][0][0]
assert "Day " in msg_mock.call_args_list[0][0][0] assert "Day " in msg_mock.call_args_list[0][0][0]
assert str(datetime.now(timezone.utc).date()) in msg_mock.call_args_list[0][0][0] assert str(dt_now().date()) in msg_mock.call_args_list[0][0][0]
assert " 6.83 USDT" in msg_mock.call_args_list[0][0][0] assert " 6.83 USDT" in msg_mock.call_args_list[0][0][0]
assert " 7.51 USD" in msg_mock.call_args_list[0][0][0] assert " 7.51 USD" in msg_mock.call_args_list[0][0][0]
assert "(2)" in msg_mock.call_args_list[0][0][0] assert "(2)" in msg_mock.call_args_list[0][0][0]
@@ -651,11 +660,8 @@ async def test_daily_handle(default_conf_usdt, update, ticker, fee, mocker, time
await telegram._daily(update=update, context=context) await telegram._daily(update=update, context=context)
assert msg_mock.call_count == 1 assert msg_mock.call_count == 1
assert "Daily Profit over the last 7 days</b>:" in msg_mock.call_args_list[0][0][0] assert "Daily Profit over the last 7 days</b>:" in msg_mock.call_args_list[0][0][0]
assert str(datetime.now(timezone.utc).date()) in msg_mock.call_args_list[0][0][0] assert str(dt_now().date()) in msg_mock.call_args_list[0][0][0]
assert ( assert str((dt_now() - timedelta(days=5)).date()) in msg_mock.call_args_list[0][0][0]
str((datetime.now(timezone.utc) - timedelta(days=5)).date())
in msg_mock.call_args_list[0][0][0]
)
assert " 6.83 USDT" in msg_mock.call_args_list[0][0][0] assert " 6.83 USDT" in msg_mock.call_args_list[0][0][0]
assert " 7.51 USD" in msg_mock.call_args_list[0][0][0] assert " 7.51 USD" in msg_mock.call_args_list[0][0][0]
assert "(2)" in msg_mock.call_args_list[0][0][0] assert "(2)" in msg_mock.call_args_list[0][0][0]
@@ -725,7 +731,7 @@ async def test_weekly_handle(default_conf_usdt, update, ticker, fee, mocker, tim
in msg_mock.call_args_list[0][0][0] in msg_mock.call_args_list[0][0][0]
) )
assert "Monday " in msg_mock.call_args_list[0][0][0] assert "Monday " in msg_mock.call_args_list[0][0][0]
today = datetime.now(timezone.utc).date() today = dt_now().date()
first_iso_day_of_current_week = today - timedelta(days=today.weekday()) first_iso_day_of_current_week = today - timedelta(days=today.weekday())
assert str(first_iso_day_of_current_week) in msg_mock.call_args_list[0][0][0] assert str(first_iso_day_of_current_week) in msg_mock.call_args_list[0][0][0]
assert " 2.74 USDT" in msg_mock.call_args_list[0][0][0] assert " 2.74 USDT" in msg_mock.call_args_list[0][0][0]
@@ -793,7 +799,7 @@ async def test_monthly_handle(default_conf_usdt, update, ticker, fee, mocker, ti
assert msg_mock.call_count == 1 assert msg_mock.call_count == 1
assert "Monthly Profit over the last 2 months</b>:" in msg_mock.call_args_list[0][0][0] assert "Monthly Profit over the last 2 months</b>:" in msg_mock.call_args_list[0][0][0]
assert "Month " in msg_mock.call_args_list[0][0][0] assert "Month " in msg_mock.call_args_list[0][0][0]
today = datetime.now(timezone.utc).date() today = dt_now().date()
current_month = f"{today.year}-{today.month:02} " current_month = f"{today.year}-{today.month:02} "
assert current_month in msg_mock.call_args_list[0][0][0] assert current_month in msg_mock.call_args_list[0][0][0]
assert " 2.74 USDT" in msg_mock.call_args_list[0][0][0] assert " 2.74 USDT" in msg_mock.call_args_list[0][0][0]
@@ -898,7 +904,7 @@ async def test_telegram_profit_handle(
trade.orders.append(oobj) trade.orders.append(oobj)
trade.update_trade(oobj) trade.update_trade(oobj)
trade.close_date = datetime.now(timezone.utc) trade.close_date = dt_now()
trade.is_open = False trade.is_open = False
Trade.commit() Trade.commit()