Remove isClose from tests in favor of pytest.approx

This commit is contained in:
Matthias 2022-08-31 08:18:28 +00:00
parent 10e0d53860
commit 4aec2db14d
5 changed files with 30 additions and 37 deletions

View File

@ -1,4 +1,3 @@
from math import isclose
from pathlib import Path from pathlib import Path
from unittest.mock import MagicMock from unittest.mock import MagicMock
@ -269,7 +268,7 @@ def test_create_cum_profit(testdatadir):
"cum_profits", timeframe="5m") "cum_profits", timeframe="5m")
assert "cum_profits" in cum_profits.columns assert "cum_profits" in cum_profits.columns
assert cum_profits.iloc[0]['cum_profits'] == 0 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): def test_create_cum_profit1(testdatadir):
@ -287,7 +286,7 @@ def test_create_cum_profit1(testdatadir):
"cum_profits", timeframe="5m") "cum_profits", timeframe="5m")
assert "cum_profits" in cum_profits.columns assert "cum_profits" in cum_profits.columns
assert cum_profits.iloc[0]['cum_profits'] == 0 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.'): with pytest.raises(ValueError, match='Trade dataframe empty.'):
create_cum_profit(df.set_index('date'), bt_data[bt_data["pair"] == 'NOTAPAIR'], create_cum_profit(df.set_index('date'), bt_data[bt_data["pair"] == 'NOTAPAIR'],

View File

@ -2,7 +2,6 @@ import copy
import logging import logging
from copy import deepcopy from copy import deepcopy
from datetime import datetime, timedelta, timezone from datetime import datetime, timedelta, timezone
from math import isclose
from random import randint from random import randint
from unittest.mock import MagicMock, Mock, PropertyMock, patch from unittest.mock import MagicMock, Mock, PropertyMock, patch
@ -407,10 +406,10 @@ def test__get_stake_amount_limit(mocker, default_conf) -> None:
# min # min
result = exchange.get_min_pair_stake_amount('ETH/BTC', 1, stoploss) result = exchange.get_min_pair_stake_amount('ETH/BTC', 1, stoploss)
expected_result = 2 * (1 + 0.05) / (1 - abs(stoploss)) expected_result = 2 * (1 + 0.05) / (1 - abs(stoploss))
assert isclose(result, expected_result) assert pytest.approx(result) == expected_result
# With Leverage # With Leverage
result = exchange.get_min_pair_stake_amount('ETH/BTC', 1, stoploss, 3.0) 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 # max
result = exchange.get_max_pair_stake_amount('ETH/BTC', 2) result = exchange.get_max_pair_stake_amount('ETH/BTC', 2)
assert result == 10000 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) result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, stoploss)
expected_result = 2 * 2 * (1 + 0.05) / (1 - abs(stoploss)) expected_result = 2 * 2 * (1 + 0.05) / (1 - abs(stoploss))
assert isclose(result, expected_result) assert pytest.approx(result) == expected_result
# With Leverage # With Leverage
result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, stoploss, 5.0) 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 # max
result = exchange.get_max_pair_stake_amount('ETH/BTC', 2) result = exchange.get_max_pair_stake_amount('ETH/BTC', 2)
assert result == 20000 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) result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, stoploss)
expected_result = max(2, 2 * 2) * (1 + 0.05) / (1 - abs(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 # With Leverage
result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, stoploss, 10) 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) # min amount and cost are set (amount is minial)
markets["ETH/BTC"]["limits"] = { 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) result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, stoploss)
expected_result = max(8, 2 * 2) * (1 + 0.05) / (1 - abs(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 # With Leverage
result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, stoploss, 7.0) 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 # Max
result = exchange.get_max_pair_stake_amount('ETH/BTC', 2) result = exchange.get_max_pair_stake_amount('ETH/BTC', 2)
assert result == 1000 assert result == 1000
result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, -0.4) result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, -0.4)
expected_result = max(8, 2 * 2) * 1.5 expected_result = max(8, 2 * 2) * 1.5
assert isclose(result, expected_result) assert pytest.approx(result) == expected_result
# With Leverage # With Leverage
result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, -0.4, 8.0) 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 # Max
result = exchange.get_max_pair_stake_amount('ETH/BTC', 2) result = exchange.get_max_pair_stake_amount('ETH/BTC', 2)
assert result == 1000 assert result == 1000
@ -482,10 +481,10 @@ def test__get_stake_amount_limit(mocker, default_conf) -> None:
# Really big stoploss # Really big stoploss
result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, -1) result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, -1)
expected_result = max(8, 2 * 2) * 1.5 expected_result = max(8, 2 * 2) * 1.5
assert isclose(result, expected_result) assert pytest.approx(result) == expected_result
# With Leverage # With Leverage
result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, -1, 12.0) 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 # Max
result = exchange.get_max_pair_stake_amount('ETH/BTC', 2) result = exchange.get_max_pair_stake_amount('ETH/BTC', 2)
assert result == 1000 assert result == 1000
@ -501,7 +500,7 @@ def test__get_stake_amount_limit(mocker, default_conf) -> None:
# Contract size 0.01 # Contract size 0.01
result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, -1) 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 # Max
result = exchange.get_max_pair_stake_amount('ETH/BTC', 2) result = exchange.get_max_pair_stake_amount('ETH/BTC', 2)
assert result == 10 assert result == 10
@ -513,7 +512,7 @@ def test__get_stake_amount_limit(mocker, default_conf) -> None:
) )
# With Leverage, Contract size 10 # With Leverage, Contract size 10
result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, -1, 12.0) 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 # Max
result = exchange.get_max_pair_stake_amount('ETH/BTC', 2) result = exchange.get_max_pair_stake_amount('ETH/BTC', 2)
assert result == 10000 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) orders = exchange.get_trades_for_order(order_id, 'ETH/USDT:USDT', since)
assert len(orders) == 1 assert len(orders) == 1
assert orders[0]['price'] == 165 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 assert api_mock.fetch_my_trades.call_count == 1
# since argument should be # since argument should be
assert isinstance(api_mock.fetch_my_trades.call_args[0][1], int) 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 since=unix_time
) )
assert (isclose(expected_fees, fees_from_datetime)) assert pytest.approx(expected_fees) == fees_from_datetime
assert (isclose(expected_fees, fees_from_unix_time)) assert pytest.approx(expected_fees) == fees_from_unix_time
ccxt_exceptionhandlers( ccxt_exceptionhandlers(
mocker, mocker,
@ -4514,7 +4513,7 @@ def test_liquidation_price(
default_conf['liquidation_buffer'] = 0.0 default_conf['liquidation_buffer'] = 0.0
exchange = get_patched_exchange(mocker, default_conf, id=exchange_name) exchange = get_patched_exchange(mocker, default_conf, id=exchange_name)
exchange.get_maintenance_ratio_and_amt = MagicMock(return_value=(mm_ratio, maintenance_amt)) 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', pair='DOGE/USDT',
open_rate=open_rate, open_rate=open_rate,
is_short=is_short, is_short=is_short,
@ -4523,7 +4522,7 @@ def test_liquidation_price(
upnl_ex_1=upnl_ex_1, upnl_ex_1=upnl_ex_1,
amount=amount, amount=amount,
stake_amount=open_rate * amount, stake_amount=open_rate * amount,
), 2), expected) ), 2)) == expected
def test_get_max_pair_stake_amount( 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/BUSD", 1.0) == 20.0
assert exchange.get_max_leverage("BNB/USDT", 100.0) == 75.0 assert exchange.get_max_leverage("BNB/USDT", 100.0) == 75.0
assert exchange.get_max_leverage("BTC/USDT", 170.30) == 125.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 pytest.approx(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/USDT", 1500)) == 33.333333333333333
assert exchange.get_max_leverage("BTC/USDT", 300000000) == 2.0 assert exchange.get_max_leverage("BTC/USDT", 300000000) == 2.0
assert exchange.get_max_leverage("BTC/USDT", 600000000) == 1.0 # Last tier assert exchange.get_max_leverage("BTC/USDT", 600000000) == 1.0 # Last tier
@ -5145,7 +5144,7 @@ def test_get_liquidation_price(
else: else:
buffer_amount = liquidation_buffer * abs(open_rate - expected_liq) buffer_amount = liquidation_buffer * abs(open_rate - expected_liq)
expected_liq = expected_liq - buffer_amount if is_short else expected_liq + buffer_amount 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', [ @pytest.mark.parametrize('contract_size,order_amount', [

View File

@ -1,5 +1,3 @@
from math import isclose
import pytest import pytest
from freqtrade.leverage import interest from freqtrade.leverage import interest
@ -30,9 +28,9 @@ twentyfive_hours = FtPrecise(25.0)
def test_interest(exchange, interest_rate, hours, expected): def test_interest(exchange, interest_rate, hours, expected):
borrowed = FtPrecise(60.0) borrowed = FtPrecise(60.0)
assert isclose(interest( assert pytest.approx(float(interest(
exchange_name=exchange, exchange_name=exchange,
borrowed=borrowed, borrowed=borrowed,
rate=FtPrecise(interest_rate), rate=FtPrecise(interest_rate),
hours=hours hours=hours
), expected) ))) == expected

View File

@ -1,5 +1,3 @@
from math import isclose
import numpy as np import numpy as np
import pandas as pd import pandas as pd
import pytest import pytest
@ -165,7 +163,7 @@ def test_stoploss_from_open():
or (side == 'short' and expected_stop_price < current_price)): or (side == 'short' and expected_stop_price < current_price)):
assert stoploss == 0 assert stoploss == 0
else: 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(): def test_stoploss_from_absolute():

View File

@ -1,7 +1,6 @@
# pragma pylint: disable=missing-docstring, C0103 # pragma pylint: disable=missing-docstring, C0103
import logging import logging
from datetime import datetime, timedelta, timezone from datetime import datetime, timedelta, timezone
from math import isclose
from pathlib import Path from pathlib import Path
from types import FunctionType from types import FunctionType
from unittest.mock import MagicMock from unittest.mock import MagicMock
@ -630,9 +629,9 @@ def test_calc_open_close_trade_price(
trade.open_rate = 2.0 trade.open_rate = 2.0
trade.close_rate = 2.2 trade.close_rate = 2.2
trade.recalc_open_trade_value() trade.recalc_open_trade_value()
assert isclose(trade._calc_open_trade_value(trade.amount, trade.open_rate), open_value) assert pytest.approx(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 pytest.approx(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_profit(trade.close_rate)) == round(profit, 8)
assert pytest.approx(trade.calc_profit_ratio(trade.close_rate)) == profit_ratio assert pytest.approx(trade.calc_profit_ratio(trade.close_rate)) == profit_ratio