From 4aec2db14dadb7e2fec6a6d883a6a7f3e48dd311 Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 31 Aug 2022 08:18:28 +0000 Subject: [PATCH] Remove isClose from tests in favor of pytest.approx --- tests/data/test_btanalysis.py | 5 ++- tests/exchange/test_exchange.py | 45 ++++++++++++------------- tests/leverage/test_interest.py | 6 ++-- tests/strategy/test_strategy_helpers.py | 4 +-- tests/test_persistence.py | 7 ++-- 5 files changed, 30 insertions(+), 37 deletions(-) diff --git a/tests/data/test_btanalysis.py b/tests/data/test_btanalysis.py index 977140ebb..72084d067 100644 --- a/tests/data/test_btanalysis.py +++ b/tests/data/test_btanalysis.py @@ -1,4 +1,3 @@ -from math import isclose from pathlib import Path from unittest.mock import MagicMock @@ -269,7 +268,7 @@ def test_create_cum_profit(testdatadir): "cum_profits", timeframe="5m") assert "cum_profits" in cum_profits.columns assert cum_profits.iloc[0]['cum_profits'] == 0 - assert isclose(cum_profits.iloc[-1]['cum_profits'], 8.723007518796964e-06) + assert pytest.approx(cum_profits.iloc[-1]['cum_profits']) == 8.723007518796964e-06 def test_create_cum_profit1(testdatadir): @@ -287,7 +286,7 @@ def test_create_cum_profit1(testdatadir): "cum_profits", timeframe="5m") assert "cum_profits" in cum_profits.columns assert cum_profits.iloc[0]['cum_profits'] == 0 - assert isclose(cum_profits.iloc[-1]['cum_profits'], 8.723007518796964e-06) + assert pytest.approx(cum_profits.iloc[-1]['cum_profits']) == 8.723007518796964e-06 with pytest.raises(ValueError, match='Trade dataframe empty.'): create_cum_profit(df.set_index('date'), bt_data[bt_data["pair"] == 'NOTAPAIR'], diff --git a/tests/exchange/test_exchange.py b/tests/exchange/test_exchange.py index 800aa5381..5456b3098 100644 --- a/tests/exchange/test_exchange.py +++ b/tests/exchange/test_exchange.py @@ -2,7 +2,6 @@ import copy import logging from copy import deepcopy from datetime import datetime, timedelta, timezone -from math import isclose from random import randint from unittest.mock import MagicMock, Mock, PropertyMock, patch @@ -407,10 +406,10 @@ def test__get_stake_amount_limit(mocker, default_conf) -> None: # min result = exchange.get_min_pair_stake_amount('ETH/BTC', 1, stoploss) expected_result = 2 * (1 + 0.05) / (1 - abs(stoploss)) - assert isclose(result, expected_result) + assert pytest.approx(result) == expected_result # With Leverage result = exchange.get_min_pair_stake_amount('ETH/BTC', 1, stoploss, 3.0) - assert isclose(result, expected_result / 3) + assert pytest.approx(result) == expected_result / 3 # max result = exchange.get_max_pair_stake_amount('ETH/BTC', 2) assert result == 10000 @@ -426,10 +425,10 @@ def test__get_stake_amount_limit(mocker, default_conf) -> None: ) result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, stoploss) expected_result = 2 * 2 * (1 + 0.05) / (1 - abs(stoploss)) - assert isclose(result, expected_result) + assert pytest.approx(result) == expected_result # With Leverage result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, stoploss, 5.0) - assert isclose(result, expected_result / 5) + assert pytest.approx(result) == expected_result / 5 # max result = exchange.get_max_pair_stake_amount('ETH/BTC', 2) assert result == 20000 @@ -445,10 +444,10 @@ def test__get_stake_amount_limit(mocker, default_conf) -> None: ) result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, stoploss) expected_result = max(2, 2 * 2) * (1 + 0.05) / (1 - abs(stoploss)) - assert isclose(result, expected_result) + assert pytest.approx(result) == expected_result # With Leverage result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, stoploss, 10) - assert isclose(result, expected_result / 10) + assert pytest.approx(result) == expected_result / 10 # min amount and cost are set (amount is minial) markets["ETH/BTC"]["limits"] = { @@ -461,20 +460,20 @@ def test__get_stake_amount_limit(mocker, default_conf) -> None: ) result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, stoploss) expected_result = max(8, 2 * 2) * (1 + 0.05) / (1 - abs(stoploss)) - assert isclose(result, expected_result) + assert pytest.approx(result) == expected_result # With Leverage result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, stoploss, 7.0) - assert isclose(result, expected_result / 7.0) + assert pytest.approx(result) == expected_result / 7.0 # Max result = exchange.get_max_pair_stake_amount('ETH/BTC', 2) assert result == 1000 result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, -0.4) expected_result = max(8, 2 * 2) * 1.5 - assert isclose(result, expected_result) + assert pytest.approx(result) == expected_result # With Leverage result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, -0.4, 8.0) - assert isclose(result, expected_result / 8.0) + assert pytest.approx(result) == expected_result / 8.0 # Max result = exchange.get_max_pair_stake_amount('ETH/BTC', 2) assert result == 1000 @@ -482,10 +481,10 @@ def test__get_stake_amount_limit(mocker, default_conf) -> None: # Really big stoploss result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, -1) expected_result = max(8, 2 * 2) * 1.5 - assert isclose(result, expected_result) + assert pytest.approx(result) == expected_result # With Leverage result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, -1, 12.0) - assert isclose(result, expected_result / 12) + assert pytest.approx(result) == expected_result / 12 # Max result = exchange.get_max_pair_stake_amount('ETH/BTC', 2) assert result == 1000 @@ -501,7 +500,7 @@ def test__get_stake_amount_limit(mocker, default_conf) -> None: # Contract size 0.01 result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, -1) - assert isclose(result, expected_result * 0.01) + assert pytest.approx(result) == expected_result * 0.01 # Max result = exchange.get_max_pair_stake_amount('ETH/BTC', 2) assert result == 10 @@ -513,7 +512,7 @@ def test__get_stake_amount_limit(mocker, default_conf) -> None: ) # With Leverage, Contract size 10 result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, -1, 12.0) - assert isclose(result, (expected_result / 12) * 10.0) + assert pytest.approx(result) == (expected_result / 12) * 10.0 # Max result = exchange.get_max_pair_stake_amount('ETH/BTC', 2) assert result == 10000 @@ -3239,7 +3238,7 @@ def test_get_trades_for_order(default_conf, mocker, exchange_name, trading_mode, orders = exchange.get_trades_for_order(order_id, 'ETH/USDT:USDT', since) assert len(orders) == 1 assert orders[0]['price'] == 165 - assert isclose(orders[0]['amount'], amount) + assert pytest.approx(orders[0]['amount']) == amount assert api_mock.fetch_my_trades.call_count == 1 # since argument should be assert isinstance(api_mock.fetch_my_trades.call_args[0][1], int) @@ -3776,8 +3775,8 @@ def test__get_funding_fees_from_exchange(default_conf, mocker, exchange_name): since=unix_time ) - assert (isclose(expected_fees, fees_from_datetime)) - assert (isclose(expected_fees, fees_from_unix_time)) + assert pytest.approx(expected_fees) == fees_from_datetime + assert pytest.approx(expected_fees) == fees_from_unix_time ccxt_exceptionhandlers( mocker, @@ -4514,7 +4513,7 @@ def test_liquidation_price( default_conf['liquidation_buffer'] = 0.0 exchange = get_patched_exchange(mocker, default_conf, id=exchange_name) exchange.get_maintenance_ratio_and_amt = MagicMock(return_value=(mm_ratio, maintenance_amt)) - assert isclose(round(exchange.get_liquidation_price( + assert pytest.approx(round(exchange.get_liquidation_price( pair='DOGE/USDT', open_rate=open_rate, is_short=is_short, @@ -4523,7 +4522,7 @@ def test_liquidation_price( upnl_ex_1=upnl_ex_1, amount=amount, stake_amount=open_rate * amount, - ), 2), expected) + ), 2)) == expected def test_get_max_pair_stake_amount( @@ -4868,8 +4867,8 @@ def test_get_max_leverage_futures(default_conf, mocker, leverage_tiers): assert exchange.get_max_leverage("BNB/BUSD", 1.0) == 20.0 assert exchange.get_max_leverage("BNB/USDT", 100.0) == 75.0 assert exchange.get_max_leverage("BTC/USDT", 170.30) == 125.0 - assert isclose(exchange.get_max_leverage("BNB/BUSD", 99999.9), 5.000005) - assert isclose(exchange.get_max_leverage("BNB/USDT", 1500), 33.333333333333333) + assert pytest.approx(exchange.get_max_leverage("BNB/BUSD", 99999.9)) == 5.000005 + assert pytest.approx(exchange.get_max_leverage("BNB/USDT", 1500)) == 33.333333333333333 assert exchange.get_max_leverage("BTC/USDT", 300000000) == 2.0 assert exchange.get_max_leverage("BTC/USDT", 600000000) == 1.0 # Last tier @@ -5145,7 +5144,7 @@ def test_get_liquidation_price( else: buffer_amount = liquidation_buffer * abs(open_rate - expected_liq) expected_liq = expected_liq - buffer_amount if is_short else expected_liq + buffer_amount - assert isclose(expected_liq, liq) + assert pytest.approx(expected_liq) == liq @pytest.mark.parametrize('contract_size,order_amount', [ diff --git a/tests/leverage/test_interest.py b/tests/leverage/test_interest.py index 7bdf4c2f0..64e99b6b4 100644 --- a/tests/leverage/test_interest.py +++ b/tests/leverage/test_interest.py @@ -1,5 +1,3 @@ -from math import isclose - import pytest from freqtrade.leverage import interest @@ -30,9 +28,9 @@ twentyfive_hours = FtPrecise(25.0) def test_interest(exchange, interest_rate, hours, expected): borrowed = FtPrecise(60.0) - assert isclose(interest( + assert pytest.approx(float(interest( exchange_name=exchange, borrowed=borrowed, rate=FtPrecise(interest_rate), hours=hours - ), expected) + ))) == expected diff --git a/tests/strategy/test_strategy_helpers.py b/tests/strategy/test_strategy_helpers.py index 244fd3919..a7c2da26a 100644 --- a/tests/strategy/test_strategy_helpers.py +++ b/tests/strategy/test_strategy_helpers.py @@ -1,5 +1,3 @@ -from math import isclose - import numpy as np import pandas as pd import pytest @@ -165,7 +163,7 @@ def test_stoploss_from_open(): or (side == 'short' and expected_stop_price < current_price)): assert stoploss == 0 else: - assert isclose(stop_price, expected_stop_price, rel_tol=0.00001) + assert pytest.approx(stop_price) == expected_stop_price def test_stoploss_from_absolute(): diff --git a/tests/test_persistence.py b/tests/test_persistence.py index 2460fde68..f16c8b054 100644 --- a/tests/test_persistence.py +++ b/tests/test_persistence.py @@ -1,7 +1,6 @@ # pragma pylint: disable=missing-docstring, C0103 import logging from datetime import datetime, timedelta, timezone -from math import isclose from pathlib import Path from types import FunctionType from unittest.mock import MagicMock @@ -630,9 +629,9 @@ def test_calc_open_close_trade_price( trade.open_rate = 2.0 trade.close_rate = 2.2 trade.recalc_open_trade_value() - assert isclose(trade._calc_open_trade_value(trade.amount, trade.open_rate), open_value) - assert isclose(trade.calc_close_trade_value(trade.close_rate), close_value) - assert isclose(trade.calc_profit(trade.close_rate), round(profit, 8)) + assert pytest.approx(trade._calc_open_trade_value(trade.amount, trade.open_rate)) == open_value + assert pytest.approx(trade.calc_close_trade_value(trade.close_rate)) == close_value + assert pytest.approx(trade.calc_profit(trade.close_rate)) == round(profit, 8) assert pytest.approx(trade.calc_profit_ratio(trade.close_rate)) == profit_ratio