From bb6562113454f15cec054aba49b9501360045c5b Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 27 Dec 2021 17:14:59 +0100 Subject: [PATCH] Simplify test, simplify "log_*" selection --- tests/conftest.py | 15 ++------------- tests/exchange/test_exchange.py | 27 ++++++++++++++++++++++----- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 3fc1256ef..9cd1332d4 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -48,11 +48,6 @@ def pytest_configure(config): setattr(config.option, 'markexpr', 'not longrun') -def log_contains(line, logs): - """Check if line is contained in some caplog's message.""" - return any(line in message for message in logs.messages) - - def log_has(line, logs): """Check if line is found on some caplog's message.""" return any(line == message for message in logs.messages) @@ -63,20 +58,14 @@ def log_has_re(line, logs): return any(re.match(line, message) for message in logs.messages) -def num_log_contains(line, logs): - """Check how many times line is contained in caplog's messages.""" - # We want to check how many times line ('foo', for example) is contained in caplog's messages. - return sum(line in message for message in logs.messages) - - def num_log_has(line, logs): """Check how many times line is found in caplog's messages.""" - return sum(line == message for logger_name, level, message in logs.record_tuples) + return sum(line == message for message in logs.messages) def num_log_has_re(line, logs): """Check how many times line matches caplog's messages.""" - return sum(re.match(line, message) for logger_name, level, message in logs.record_tuples) + return sum(bool(re.match(line, message)) for message in logs.messages) def get_args(args): diff --git a/tests/exchange/test_exchange.py b/tests/exchange/test_exchange.py index 7bd62c838..071f4e2b8 100644 --- a/tests/exchange/test_exchange.py +++ b/tests/exchange/test_exchange.py @@ -20,8 +20,7 @@ from freqtrade.exchange.exchange import (market_is_active, timeframe_to_minutes, timeframe_to_next_date, timeframe_to_prev_date, timeframe_to_seconds) from freqtrade.resolvers.exchange_resolver import ExchangeResolver -from tests.conftest import (get_mock_coro, get_patched_exchange, log_contains, log_has, log_has_re, - num_log_contains) +from tests.conftest import get_mock_coro, get_patched_exchange, log_has, log_has_re, num_log_has_re # Make sure to always keep one exchange here which is NOT subclassed!! @@ -1752,13 +1751,31 @@ async def test__async_kucoin_get_candle_history(default_conf, mocker, caplog): exchange = get_patched_exchange(mocker, default_conf, api_mock, id="kucoin") msg = "Kucoin 429 error, avoid triggering DDosProtection backoff delay" - assert not log_contains(msg, caplog) + assert not num_log_has_re(msg, caplog) for _ in range(3): with pytest.raises(DDosProtection, match=r'429 Too Many Requests'): await exchange._async_get_candle_history( - "ETH/BTC", "5m", (arrow.utcnow().int_timestamp - 2000) * 1000, count=1) - assert num_log_contains(msg, caplog) == 1 + "ETH/BTC", "5m", (arrow.utcnow().int_timestamp - 2000) * 1000, count=3) + assert num_log_has_re(msg, caplog) == 3 + + caplog.clear() + # Test regular non-kucoin message + api_mock.fetch_ohlcv = MagicMock(side_effect=ccxt.DDoSProtection( + "kucoin GET https://openapi-v2.kucoin.com/api/v1/market/candles?" + "symbol=ETH-BTC&type=5min&startAt=1640268735&endAt=1640418735" + "429 Too Many Requests" '{"code":"2222222","msg":"Too Many Requests"}')) + + msg = r'_async_get_candle_history\(\) returned exception: .*' + msg2 = r'Applying DDosProtection backoff delay: .*' + with patch('freqtrade.exchange.common.asyncio.sleep', get_mock_coro(None)): + for _ in range(3): + with pytest.raises(DDosProtection, match=r'429 Too Many Requests'): + await exchange._async_get_candle_history( + "ETH/BTC", "5m", (arrow.utcnow().int_timestamp - 2000) * 1000, count=3) + # Expect the "returned exception" message 12 times (4 retries * 3 (loop)) + assert num_log_has_re(msg, caplog) == 12 + assert num_log_has_re(msg2, caplog) == 9 @pytest.mark.asyncio