Use regex to match logs in rest-client tests

This commit is contained in:
Matthias 2024-03-29 19:09:23 +01:00
parent d606d6d08a
commit e100a06785

View File

@ -1,3 +1,4 @@
import re
from unittest.mock import MagicMock from unittest.mock import MagicMock
import pytest import pytest
@ -6,9 +7,9 @@ from freqtrade_client.ft_client import add_arguments, main_exec
from requests.exceptions import ConnectionError from requests.exceptions import ConnectionError
def log_has(line, logs): def log_has_re(line, logs):
"""Check if line is found on some caplog's message.""" """Check if line matches some caplog's message."""
return any(line == message for message in logs.messages) return any(re.match(line, message) for message in logs.messages)
def get_rest_client(): def get_rest_client():
@ -46,7 +47,7 @@ def test_FtRestClient_call_invalid(caplog):
client._session.request = MagicMock(side_effect=ConnectionError()) client._session.request = MagicMock(side_effect=ConnectionError())
client._call('GET', '/dummytest') client._call('GET', '/dummytest')
assert log_has('Connection error', caplog) assert log_has_re('Connection error', caplog)
@pytest.mark.parametrize('method,args', [ @pytest.mark.parametrize('method,args', [
@ -139,7 +140,7 @@ def test_ft_client(mocker, capsys, caplog):
args = add_arguments(['--config', 'tests/testdata/testconfigs/nonexisting.json']) args = add_arguments(['--config', 'tests/testdata/testconfigs/nonexisting.json'])
main_exec(args) main_exec(args)
assert log_has('Could not load config file tests/testdata/testconfigs/nonexisting.json.', assert log_has_re(r'Could not load config file .*nonexisting\.json\.',
caplog) caplog)
args = add_arguments([ args = add_arguments([
@ -148,4 +149,4 @@ def test_ft_client(mocker, capsys, caplog):
'whatever' 'whatever'
]) ])
main_exec(args) main_exec(args)
assert log_has('Command whatever not defined', caplog) assert log_has_re('Command whatever not defined', caplog)