Improve tests around timezone

This commit is contained in:
Matthias
2023-05-14 08:42:30 +02:00
parent 66a97ff45d
commit bbce738523
5 changed files with 20 additions and 20 deletions
+5 -5
View File
@@ -1,5 +1,5 @@
import random import random
from datetime import datetime, timedelta from datetime import datetime, timedelta, timezone
import pytest import pytest
@@ -24,8 +24,8 @@ def generate_mock_trade(pair: str, fee: float, is_open: bool,
stake_amount=0.01, stake_amount=0.01,
fee_open=fee, fee_open=fee,
fee_close=fee, fee_close=fee,
open_date=datetime.utcnow() - timedelta(minutes=min_ago_open or 200), open_date=datetime.now(timezone.utc) - timedelta(minutes=min_ago_open or 200),
close_date=datetime.utcnow() - timedelta(minutes=min_ago_close or 30), close_date=datetime.now(timezone.utc) - timedelta(minutes=min_ago_close or 30),
open_rate=open_rate, open_rate=open_rate,
is_open=is_open, is_open=is_open,
amount=0.01 / open_rate, amount=0.01 / open_rate,
@@ -87,9 +87,9 @@ def test_protectionmanager(mocker, default_conf):
for handler in freqtrade.protections._protection_handlers: for handler in freqtrade.protections._protection_handlers:
assert handler.name in constants.AVAILABLE_PROTECTIONS assert handler.name in constants.AVAILABLE_PROTECTIONS
if not handler.has_global_stop: if not handler.has_global_stop:
assert handler.global_stop(datetime.utcnow(), '*') is None assert handler.global_stop(datetime.now(timezone.utc), '*') is None
if not handler.has_local_stop: if not handler.has_local_stop:
assert handler.stop_per_pair('XRP/BTC', datetime.utcnow(), '*') is None assert handler.stop_per_pair('XRP/BTC', datetime.now(timezone.utc), '*') is None
@pytest.mark.parametrize('timeframe,expected,protconf', [ @pytest.mark.parametrize('timeframe,expected,protconf', [
+2 -3
View File
@@ -261,8 +261,7 @@ def test_rpc_status_table(default_conf, ticker, fee, mocker) -> None:
assert isnan(fiat_profit_sum) assert isnan(fiat_profit_sum)
def test__rpc_timeunit_profit(default_conf_usdt, ticker, fee, def test__rpc_timeunit_profit(default_conf_usdt, ticker, fee, markets, mocker) -> None:
limit_buy_order, limit_sell_order, markets, mocker) -> None:
mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock()) mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock())
mocker.patch.multiple( mocker.patch.multiple(
EXMS, EXMS,
@@ -295,7 +294,7 @@ def test__rpc_timeunit_profit(default_conf_usdt, ticker, fee,
assert day['starting_balance'] in (pytest.approx(1062.37), pytest.approx(1066.46)) assert day['starting_balance'] in (pytest.approx(1062.37), pytest.approx(1066.46))
assert day['fiat_value'] in (0.0, ) assert day['fiat_value'] in (0.0, )
# ensure first day is current date # ensure first day is current date
assert str(days['data'][0]['date']) == str(datetime.utcnow().date()) assert str(days['data'][0]['date']) == str(datetime.now(timezone.utc).date())
# Try invalid data # Try invalid data
with pytest.raises(RPCException, match=r'.*must be an integer greater than 0*'): with pytest.raises(RPCException, match=r'.*must be an integer greater than 0*'):
+2 -2
View File
@@ -601,7 +601,7 @@ def test_api_daily(botclient, mocker, ticker, fee, markets):
assert len(rc.json()['data']) == 7 assert len(rc.json()['data']) == 7
assert rc.json()['stake_currency'] == 'BTC' assert rc.json()['stake_currency'] == 'BTC'
assert rc.json()['fiat_display_currency'] == 'USD' assert rc.json()['fiat_display_currency'] == 'USD'
assert rc.json()['data'][0]['date'] == str(datetime.utcnow().date()) assert rc.json()['data'][0]['date'] == str(datetime.now(timezone.utc).date())
@pytest.mark.parametrize('is_short', [True, False]) @pytest.mark.parametrize('is_short', [True, False])
@@ -1224,7 +1224,7 @@ def test_api_force_entry(botclient, mocker, fee, endpoint):
stake_amount=1, stake_amount=1,
open_rate=0.245441, open_rate=0.245441,
open_order_id="123456", open_order_id="123456",
open_date=datetime.utcnow(), open_date=datetime.now(timezone.utc),
is_open=False, is_open=False,
is_short=False, is_short=False,
fee_close=fee.return_value, fee_close=fee.return_value,
+8 -7
View File
@@ -52,7 +52,7 @@ def default_conf(default_conf) -> dict:
@pytest.fixture @pytest.fixture
def update(): def update():
message = Message(0, datetime.utcnow(), Chat(0, 0)) message = Message(0, datetime.now(timezone.utc), Chat(0, 0))
_update = Update(0, message=message) _update = Update(0, message=message)
return _update return _update
@@ -213,7 +213,7 @@ async def test_authorized_only_unauthorized(default_conf, mocker, caplog) -> Non
patch_exchange(mocker) patch_exchange(mocker)
caplog.set_level(logging.DEBUG) caplog.set_level(logging.DEBUG)
chat = Chat(0xdeadbeef, 0) chat = Chat(0xdeadbeef, 0)
message = Message(randint(1, 100), datetime.utcnow(), chat) message = Message(randint(1, 100), datetime.now(timezone.utc), chat)
update = Update(randint(1, 100), message=message) update = Update(randint(1, 100), message=message)
default_conf['telegram']['enabled'] = False default_conf['telegram']['enabled'] = False
@@ -520,7 +520,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.utcnow().date()) 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 ' 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]
@@ -533,8 +533,9 @@ 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.utcnow().date()) 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((datetime.utcnow() - timedelta(days=5)).date()) in msg_mock.call_args_list[0][0][0] assert 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]
@@ -608,7 +609,7 @@ async def test_weekly_handle(default_conf_usdt, update, ticker, fee, mocker, tim
assert "Weekly Profit over the last 2 weeks (starting from Monday)</b>:" \ assert "Weekly Profit over the last 2 weeks (starting from Monday)</b>:" \
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.utcnow().date() today = datetime.now(timezone.utc).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]
@@ -677,7 +678,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.utcnow().date() today = datetime.now(timezone.utc).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]
+3 -3
View File
@@ -1,4 +1,4 @@
from datetime import datetime from datetime import datetime, timezone
import pytest import pytest
from pandas import DataFrame from pandas import DataFrame
@@ -43,12 +43,12 @@ def test_strategy_test_v3(dataframe_1m, fee, is_short, side):
assert strategy.confirm_trade_entry(pair='ETH/BTC', order_type='limit', amount=0.1, assert strategy.confirm_trade_entry(pair='ETH/BTC', order_type='limit', amount=0.1,
rate=20000, time_in_force='gtc', rate=20000, time_in_force='gtc',
current_time=datetime.utcnow(), current_time=datetime.now(timezone.utc),
side=side, entry_tag=None) is True side=side, entry_tag=None) is True
assert strategy.confirm_trade_exit(pair='ETH/BTC', trade=trade, order_type='limit', amount=0.1, assert strategy.confirm_trade_exit(pair='ETH/BTC', trade=trade, order_type='limit', amount=0.1,
rate=20000, time_in_force='gtc', exit_reason='roi', rate=20000, time_in_force='gtc', exit_reason='roi',
sell_reason='roi', sell_reason='roi',
current_time=datetime.utcnow(), current_time=datetime.now(timezone.utc),
side=side) is True side=side) is True
assert strategy.custom_stoploss(pair='ETH/BTC', trade=trade, current_time=datetime.now(), assert strategy.custom_stoploss(pair='ETH/BTC', trade=trade, current_time=datetime.now(),