ruff format: Update test strategies

This commit is contained in:
Matthias
2024-05-12 15:41:07 +02:00
parent 099b1fc8c4
commit 8c7d80b78e
23 changed files with 420 additions and 462 deletions

View File

@@ -9,33 +9,32 @@ from tests.conftest import create_mock_trades_usdt, log_has
def test_binance_mig_data_conversion(default_conf_usdt, tmp_path, testdatadir):
# call doing nothing (spot mode)
migrate_binance_futures_data(default_conf_usdt)
default_conf_usdt['trading_mode'] = 'futures'
pair_old = 'XRP_USDT'
pair_unified = 'XRP_USDT_USDT'
futures_src = testdatadir / 'futures'
futures_dst = tmp_path / 'futures'
default_conf_usdt["trading_mode"] = "futures"
pair_old = "XRP_USDT"
pair_unified = "XRP_USDT_USDT"
futures_src = testdatadir / "futures"
futures_dst = tmp_path / "futures"
futures_dst.mkdir()
files = [
'-1h-mark.feather',
'-1h-futures.feather',
'-8h-funding_rate.feather',
'-8h-mark.feather',
"-1h-mark.feather",
"-1h-futures.feather",
"-8h-funding_rate.feather",
"-8h-mark.feather",
]
# Copy files to tmpdir and rename to old naming
for file in files:
fn_after = futures_dst / f'{pair_old}{file}'
shutil.copy(futures_src / f'{pair_unified}{file}', fn_after)
fn_after = futures_dst / f"{pair_old}{file}"
shutil.copy(futures_src / f"{pair_unified}{file}", fn_after)
default_conf_usdt['datadir'] = tmp_path
default_conf_usdt["datadir"] = tmp_path
# Migrate files to unified namings
migrate_binance_futures_data(default_conf_usdt)
for file in files:
fn_after = futures_dst / f'{pair_unified}{file}'
fn_after = futures_dst / f"{pair_unified}{file}"
assert fn_after.exists()
@@ -47,19 +46,19 @@ def test_binance_mig_db_conversion(default_conf_usdt, fee, caplog):
create_mock_trades_usdt(fee, None)
for t in Trade.get_trades():
t.trading_mode = 'FUTURES'
t.exchange = 'binance'
t.trading_mode = "FUTURES"
t.exchange = "binance"
Trade.commit()
default_conf_usdt['trading_mode'] = 'futures'
default_conf_usdt["trading_mode"] = "futures"
migrate_binance_futures_names(default_conf_usdt)
assert log_has('Migrating binance futures pairs in database.', caplog)
assert log_has("Migrating binance futures pairs in database.", caplog)
def test_migration_wrapper(default_conf_usdt, mocker):
default_conf_usdt['trading_mode'] = 'futures'
binmock = mocker.patch('freqtrade.util.migrations.migrate_binance_futures_data')
funding_mock = mocker.patch('freqtrade.util.migrations.migrate_funding_fee_timeframe')
default_conf_usdt["trading_mode"] = "futures"
binmock = mocker.patch("freqtrade.util.migrations.migrate_binance_futures_data")
funding_mock = mocker.patch("freqtrade.util.migrations.migrate_funding_fee_timeframe")
migrate_data(default_conf_usdt)
assert binmock.call_count == 1

View File

@@ -1,82 +1,82 @@
from freqtrade.util import FtPrecise
ws = FtPrecise('-1.123e-6')
ws = FtPrecise('-1.123e-6')
xs = FtPrecise('0.00000002')
ys = FtPrecise('69696900000')
zs = FtPrecise('0')
ws = FtPrecise("-1.123e-6")
ws = FtPrecise("-1.123e-6")
xs = FtPrecise("0.00000002")
ys = FtPrecise("69696900000")
zs = FtPrecise("0")
def test_FtPrecise():
assert ys * xs == '1393.938'
assert xs * ys == '1393.938'
assert ys * xs == "1393.938"
assert xs * ys == "1393.938"
assert ys + xs == '69696900000.00000002'
assert xs + ys == '69696900000.00000002'
assert xs - ys == '-69696899999.99999998'
assert ys - xs == '69696899999.99999998'
assert xs / ys == '0'
assert ys / xs == '3484845000000000000'
assert ys + xs == "69696900000.00000002"
assert xs + ys == "69696900000.00000002"
assert xs - ys == "-69696899999.99999998"
assert ys - xs == "69696899999.99999998"
assert xs / ys == "0"
assert ys / xs == "3484845000000000000"
assert ws * xs == '-0.00000000000002246'
assert xs * ws == '-0.00000000000002246'
assert ws * xs == "-0.00000000000002246"
assert xs * ws == "-0.00000000000002246"
assert ws + xs == '-0.000001103'
assert xs + ws == '-0.000001103'
assert ws + xs == "-0.000001103"
assert xs + ws == "-0.000001103"
assert xs - ws == '0.000001143'
assert ws - xs == '-0.000001143'
assert xs - ws == "0.000001143"
assert ws - xs == "-0.000001143"
assert xs / ws == '-0.017809439002671415'
assert ws / xs == '-56.15'
assert xs / ws == "-0.017809439002671415"
assert ws / xs == "-56.15"
assert zs * ws == '0'
assert zs * xs == '0'
assert zs * ys == '0'
assert ws * zs == '0'
assert xs * zs == '0'
assert ys * zs == '0'
assert zs * ws == "0"
assert zs * xs == "0"
assert zs * ys == "0"
assert ws * zs == "0"
assert xs * zs == "0"
assert ys * zs == "0"
assert zs + ws == '-0.000001123'
assert zs + xs == '0.00000002'
assert zs + ys == '69696900000'
assert ws + zs == '-0.000001123'
assert xs + zs == '0.00000002'
assert ys + zs == '69696900000'
assert zs + ws == "-0.000001123"
assert zs + xs == "0.00000002"
assert zs + ys == "69696900000"
assert ws + zs == "-0.000001123"
assert xs + zs == "0.00000002"
assert ys + zs == "69696900000"
assert abs(FtPrecise('-500.1')) == '500.1'
assert abs(FtPrecise('213')) == '213'
assert abs(FtPrecise("-500.1")) == "500.1"
assert abs(FtPrecise("213")) == "213"
assert abs(FtPrecise('-500.1')) == '500.1'
assert -FtPrecise('213') == '-213'
assert abs(FtPrecise("-500.1")) == "500.1"
assert -FtPrecise("213") == "-213"
assert FtPrecise('10.1') % FtPrecise('0.5') == '0.1'
assert FtPrecise('5550') % FtPrecise('120') == '30'
assert FtPrecise("10.1") % FtPrecise("0.5") == "0.1"
assert FtPrecise("5550") % FtPrecise("120") == "30"
assert FtPrecise('-0.0') == FtPrecise('0')
assert FtPrecise('5.534000') == FtPrecise('5.5340')
assert FtPrecise("-0.0") == FtPrecise("0")
assert FtPrecise("5.534000") == FtPrecise("5.5340")
assert min(FtPrecise('-3.1415'), FtPrecise('-2')) == '-3.1415'
assert min(FtPrecise("-3.1415"), FtPrecise("-2")) == "-3.1415"
assert max(FtPrecise('3.1415'), FtPrecise('-2')) == '3.1415'
assert max(FtPrecise("3.1415"), FtPrecise("-2")) == "3.1415"
assert FtPrecise('2') > FtPrecise('1.2345')
assert not FtPrecise('-3.1415') > FtPrecise('-2')
assert not FtPrecise('3.1415') > FtPrecise('3.1415')
assert FtPrecise.string_gt('3.14150000000000000000001', '3.1415')
assert FtPrecise("2") > FtPrecise("1.2345")
assert not FtPrecise("-3.1415") > FtPrecise("-2")
assert not FtPrecise("3.1415") > FtPrecise("3.1415")
assert FtPrecise.string_gt("3.14150000000000000000001", "3.1415")
assert FtPrecise('3.1415') >= FtPrecise('3.1415')
assert FtPrecise('3.14150000000000000000001') >= FtPrecise('3.1415')
assert FtPrecise("3.1415") >= FtPrecise("3.1415")
assert FtPrecise("3.14150000000000000000001") >= FtPrecise("3.1415")
assert not FtPrecise('3.1415') < FtPrecise('3.1415')
assert not FtPrecise("3.1415") < FtPrecise("3.1415")
assert FtPrecise('3.1415') <= FtPrecise('3.1415')
assert FtPrecise('3.1415') <= FtPrecise('3.14150000000000000000001')
assert FtPrecise("3.1415") <= FtPrecise("3.1415")
assert FtPrecise("3.1415") <= FtPrecise("3.14150000000000000000001")
assert FtPrecise(213) == '213'
assert FtPrecise(-213) == '-213'
assert str(FtPrecise(-213)) == '-213'
assert FtPrecise(213.2) == '213.2'
assert FtPrecise(213) == "213"
assert FtPrecise(-213) == "-213"
assert str(FtPrecise(-213)) == "-213"
assert FtPrecise(213.2) == "213.2"
assert float(FtPrecise(213.2)) == 213.2
assert float(FtPrecise(-213.2)) == -213.2

View File

@@ -49,16 +49,18 @@ def test_dt_ts_none():
def test_dt_utc():
assert dt_utc(2023, 5, 5) == datetime(2023, 5, 5, tzinfo=timezone.utc)
assert dt_utc(2023, 5, 5, 0, 0, 0, 555500) == datetime(2023, 5, 5, 0, 0, 0, 555500,
tzinfo=timezone.utc)
assert dt_utc(2023, 5, 5, 0, 0, 0, 555500) == datetime(
2023, 5, 5, 0, 0, 0, 555500, tzinfo=timezone.utc
)
@pytest.mark.parametrize('as_ms', [True, False])
@pytest.mark.parametrize("as_ms", [True, False])
def test_dt_from_ts(as_ms):
multi = 1000 if as_ms else 1
assert dt_from_ts(1683244800.0 * multi) == datetime(2023, 5, 5, tzinfo=timezone.utc)
assert dt_from_ts(1683244800.5555 * multi) == datetime(2023, 5, 5, 0, 0, 0, 555500,
tzinfo=timezone.utc)
assert dt_from_ts(1683244800.5555 * multi) == datetime(
2023, 5, 5, 0, 0, 0, 555500, tzinfo=timezone.utc
)
# As int
assert dt_from_ts(1683244800 * multi) == datetime(2023, 5, 5, tzinfo=timezone.utc)
# As milliseconds
@@ -73,18 +75,18 @@ def test_dt_floor_day():
def test_shorten_date() -> None:
str_data = '1 day, 2 hours, 3 minutes, 4 seconds ago'
str_shorten_data = '1 d, 2 h, 3 min, 4 sec ago'
str_data = "1 day, 2 hours, 3 minutes, 4 seconds ago"
str_shorten_data = "1 d, 2 h, 3 min, 4 sec ago"
assert shorten_date(str_data) == str_shorten_data
def test_dt_humanize() -> None:
assert dt_humanize_delta(dt_now()) == 'now'
assert dt_humanize_delta(dt_now() - timedelta(minutes=50)) == '50 minutes ago'
assert dt_humanize_delta(dt_now() - timedelta(hours=16)) == '16 hours ago'
assert dt_humanize_delta(dt_now() - timedelta(hours=16, minutes=30)) == '16 hours ago'
assert dt_humanize_delta(dt_now() - timedelta(days=16, hours=10, minutes=25)) == '16 days ago'
assert dt_humanize_delta(dt_now() - timedelta(minutes=50)) == '50 minutes ago'
assert dt_humanize_delta(dt_now()) == "now"
assert dt_humanize_delta(dt_now() - timedelta(minutes=50)) == "50 minutes ago"
assert dt_humanize_delta(dt_now() - timedelta(hours=16)) == "16 hours ago"
assert dt_humanize_delta(dt_now() - timedelta(hours=16, minutes=30)) == "16 hours ago"
assert dt_humanize_delta(dt_now() - timedelta(days=16, hours=10, minutes=25)) == "16 days ago"
assert dt_humanize_delta(dt_now() - timedelta(minutes=50)) == "50 minutes ago"
def test_format_ms_time() -> None:
@@ -93,20 +95,19 @@ def test_format_ms_time() -> None:
date = format_ms_time(date_in_epoch_ms)
assert isinstance(date, str)
res = datetime(2018, 4, 10, 18, 2, 1, tzinfo=timezone.utc)
assert date == res.strftime('%Y-%m-%dT%H:%M:%S')
assert date == '2018-04-10T18:02:01'
assert date == res.strftime("%Y-%m-%dT%H:%M:%S")
assert date == "2018-04-10T18:02:01"
res = datetime(2017, 12, 13, 8, 2, 1, tzinfo=timezone.utc)
# Date 2017-12-13 08:02:01
date_in_epoch_ms = 1513152121000
assert format_ms_time(date_in_epoch_ms) == res.strftime('%Y-%m-%dT%H:%M:%S')
assert format_ms_time(date_in_epoch_ms) == res.strftime("%Y-%m-%dT%H:%M:%S")
def test_format_date() -> None:
date = datetime(2023, 9, 1, 5, 2, 3, 455555, tzinfo=timezone.utc)
assert format_date(date) == '2023-09-01 05:02:03'
assert format_date(None) == ''
assert format_date(date) == "2023-09-01 05:02:03"
assert format_date(None) == ""
date = datetime(2021, 9, 30, 22, 59, 3, 455555, tzinfo=timezone.utc)
assert format_date(date) == '2021-09-30 22:59:03'
assert format_date(None) == ''
assert format_date(date) == "2021-09-30 22:59:03"
assert format_date(None) == ""

View File

@@ -2,36 +2,35 @@ from freqtrade.util import decimals_per_coin, fmt_coin, round_value
def test_decimals_per_coin():
assert decimals_per_coin('USDT') == 3
assert decimals_per_coin('EUR') == 3
assert decimals_per_coin('BTC') == 8
assert decimals_per_coin('ETH') == 5
assert decimals_per_coin("USDT") == 3
assert decimals_per_coin("EUR") == 3
assert decimals_per_coin("BTC") == 8
assert decimals_per_coin("ETH") == 5
def test_fmt_coin():
assert fmt_coin(222.222222, 'USDT') == '222.222 USDT'
assert fmt_coin(222.2, 'USDT', keep_trailing_zeros=True) == '222.200 USDT'
assert fmt_coin(222.2, 'USDT') == '222.2 USDT'
assert fmt_coin(222.12745, 'EUR') == '222.127 EUR'
assert fmt_coin(0.1274512123, 'BTC') == '0.12745121 BTC'
assert fmt_coin(0.1274512123, 'ETH') == '0.12745 ETH'
assert fmt_coin(222.222222, "USDT") == "222.222 USDT"
assert fmt_coin(222.2, "USDT", keep_trailing_zeros=True) == "222.200 USDT"
assert fmt_coin(222.2, "USDT") == "222.2 USDT"
assert fmt_coin(222.12745, "EUR") == "222.127 EUR"
assert fmt_coin(0.1274512123, "BTC") == "0.12745121 BTC"
assert fmt_coin(0.1274512123, "ETH") == "0.12745 ETH"
assert fmt_coin(222.222222, 'USDT', False) == '222.222'
assert fmt_coin(222.2, 'USDT', False) == '222.2'
assert fmt_coin(222.00, 'USDT', False) == '222'
assert fmt_coin(222.12745, 'EUR', False) == '222.127'
assert fmt_coin(0.1274512123, 'BTC', False) == '0.12745121'
assert fmt_coin(0.1274512123, 'ETH', False) == '0.12745'
assert fmt_coin(222.2, 'USDT', False, True) == '222.200'
assert fmt_coin(222.222222, "USDT", False) == "222.222"
assert fmt_coin(222.2, "USDT", False) == "222.2"
assert fmt_coin(222.00, "USDT", False) == "222"
assert fmt_coin(222.12745, "EUR", False) == "222.127"
assert fmt_coin(0.1274512123, "BTC", False) == "0.12745121"
assert fmt_coin(0.1274512123, "ETH", False) == "0.12745"
assert fmt_coin(222.2, "USDT", False, True) == "222.200"
def test_round_value():
assert round_value(222.222222, 3) == '222.222'
assert round_value(222.2, 3) == '222.2'
assert round_value(222.00, 3) == '222'
assert round_value(222.12745, 3) == '222.127'
assert round_value(0.1274512123, 8) == '0.12745121'
assert round_value(0.1274512123, 5) == '0.12745'
assert round_value(222.2, 3, True) == '222.200'
assert round_value(222.2, 0, True) == '222'
assert round_value(222.222222, 3) == "222.222"
assert round_value(222.2, 3) == "222.2"
assert round_value(222.00, 3) == "222"
assert round_value(222.12745, 3) == "222.127"
assert round_value(0.1274512123, 8) == "0.12745121"
assert round_value(0.1274512123, 5) == "0.12745"
assert round_value(222.2, 3, True) == "222.200"
assert round_value(222.2, 0, True) == "222"

View File

@@ -4,22 +4,21 @@ from freqtrade.util.migrations import migrate_funding_fee_timeframe
def test_migrate_funding_rate_timeframe(default_conf_usdt, tmp_path, testdatadir):
copytree(testdatadir / 'futures', tmp_path / 'futures')
file_4h = tmp_path / 'futures' / 'XRP_USDT_USDT-4h-funding_rate.feather'
file_8h = tmp_path / 'futures' / 'XRP_USDT_USDT-8h-funding_rate.feather'
file_1h = tmp_path / 'futures' / 'XRP_USDT_USDT-1h-futures.feather'
copytree(testdatadir / "futures", tmp_path / "futures")
file_4h = tmp_path / "futures" / "XRP_USDT_USDT-4h-funding_rate.feather"
file_8h = tmp_path / "futures" / "XRP_USDT_USDT-8h-funding_rate.feather"
file_1h = tmp_path / "futures" / "XRP_USDT_USDT-1h-futures.feather"
file_8h.rename(file_4h)
assert file_1h.exists()
assert file_4h.exists()
assert not file_8h.exists()
default_conf_usdt['datadir'] = tmp_path
default_conf_usdt["datadir"] = tmp_path
# Inactive on spot trading ...
migrate_funding_fee_timeframe(default_conf_usdt, None)
default_conf_usdt['trading_mode'] = 'futures'
default_conf_usdt["trading_mode"] = "futures"
migrate_funding_fee_timeframe(default_conf_usdt, None)

View File

@@ -6,10 +6,8 @@ from freqtrade.util import MeasureTime
def test_measure_time():
callback = MagicMock()
with time_machine.travel("2021-09-01 05:00:00 +00:00", tick=False) as t:
measure = MeasureTime(callback, 5, ttl=60)
with measure:
pass

View File

@@ -4,31 +4,29 @@ from freqtrade.util import PeriodicCache
def test_ttl_cache():
with time_machine.travel("2021-09-01 05:00:00 +00:00", tick=False) as t:
cache = PeriodicCache(5, ttl=60)
cache1h = PeriodicCache(5, ttl=3600)
assert cache.timer() == 1630472400.0
cache['a'] = 1235
cache1h['a'] = 555123
assert 'a' in cache
assert 'a' in cache1h
cache["a"] = 1235
cache1h["a"] = 555123
assert "a" in cache
assert "a" in cache1h
t.move_to("2021-09-01 05:00:59 +00:00")
assert 'a' in cache
assert 'a' in cache1h
assert "a" in cache
assert "a" in cache1h
# Cache expired
t.move_to("2021-09-01 05:01:00 +00:00")
assert 'a' not in cache
assert 'a' in cache1h
assert "a" not in cache
assert "a" in cache1h
t.move_to("2021-09-01 05:59:59 +00:00")
assert 'a' not in cache
assert 'a' in cache1h
assert "a" not in cache
assert "a" in cache1h
t.move_to("2021-09-01 06:00:00 +00:00")
assert 'a' not in cache
assert 'a' not in cache1h
assert "a" not in cache
assert "a" not in cache1h

View File

@@ -5,15 +5,16 @@ from freqtrade.util import render_template, render_template_with_fallback
def test_render_template_fallback():
from jinja2.exceptions import TemplateNotFound
with pytest.raises(TemplateNotFound):
val = render_template(
templatefile='subtemplates/indicators_does-not-exist.j2',
templatefile="subtemplates/indicators_does-not-exist.j2",
arguments={},
)
val = render_template_with_fallback(
templatefile='strategy_subtemplates/indicators_does-not-exist.j2',
templatefallbackfile='strategy_subtemplates/indicators_minimal.j2',
templatefile="strategy_subtemplates/indicators_does-not-exist.j2",
templatefallbackfile="strategy_subtemplates/indicators_minimal.j2",
)
assert isinstance(val, str)
assert 'if self.dp' in val
assert "if self.dp" in val