From b294318d0f0ffb9dadf6fdba398db9325a406757 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 4 Jun 2024 19:05:27 +0200 Subject: [PATCH] Update tests for simplified init --- tests/exchange/test_exchange.py | 76 +++++++++++++++------------------ 1 file changed, 35 insertions(+), 41 deletions(-) diff --git a/tests/exchange/test_exchange.py b/tests/exchange/test_exchange.py index 2664e20df..4fd7d941c 100644 --- a/tests/exchange/test_exchange.py +++ b/tests/exchange/test_exchange.py @@ -527,20 +527,18 @@ def test__load_async_markets(default_conf, mocker, caplog): assert exchange._api_async.load_markets.call_count == 1 caplog.set_level(logging.DEBUG) - exchange._api_async.load_markets = Mock(side_effect=ccxt.BaseError("deadbeef")) - exchange._load_async_markets() - - assert log_has("Could not load async markets. Reason: deadbeef", caplog) + exchange._api_async.load_markets = get_mock_coro(side_effect=ccxt.BaseError("deadbeef")) + with pytest.raises(ccxt.BaseError, match="deadbeef"): + exchange._load_async_markets() def test__load_markets(default_conf, mocker, caplog): caplog.set_level(logging.INFO) api_mock = MagicMock() - api_mock.load_markets = MagicMock(side_effect=ccxt.BaseError("SomeError")) + api_mock.load_markets = get_mock_coro(side_effect=ccxt.BaseError("SomeError")) mocker.patch(f"{EXMS}._init_ccxt", MagicMock(return_value=api_mock)) mocker.patch(f"{EXMS}.validate_pairs") mocker.patch(f"{EXMS}.validate_timeframes") - mocker.patch(f"{EXMS}._load_async_markets") mocker.patch(f"{EXMS}.validate_stakecurrency") mocker.patch(f"{EXMS}.validate_pricing") Exchange(default_conf) @@ -548,7 +546,7 @@ def test__load_markets(default_conf, mocker, caplog): expected_return = {"ETH/BTC": "available"} api_mock = MagicMock() - api_mock.load_markets = MagicMock(return_value=expected_return) + api_mock.load_markets = get_mock_coro(return_value=expected_return) mocker.patch(f"{EXMS}._init_ccxt", MagicMock(return_value=api_mock)) default_conf["exchange"]["pair_whitelist"] = ["ETH/BTC"] ex = Exchange(default_conf) @@ -563,12 +561,12 @@ def test_reload_markets(default_conf, mocker, caplog, time_machine): start_dt = dt_now() time_machine.move_to(start_dt, tick=False) api_mock = MagicMock() - api_mock.load_markets = MagicMock(return_value=initial_markets) + api_mock.load_markets = get_mock_coro(return_value=initial_markets) default_conf["exchange"]["markets_refresh_interval"] = 10 exchange = get_patched_exchange( mocker, default_conf, api_mock, id="binance", mock_markets=False ) - exchange._load_async_markets = MagicMock() + lam_spy = mocker.spy(exchange, "_load_async_markets") assert exchange._last_markets_refresh == dt_ts() assert exchange.markets == initial_markets @@ -577,30 +575,32 @@ def test_reload_markets(default_conf, mocker, caplog, time_machine): # less than 10 minutes have passed, no reload exchange.reload_markets() assert exchange.markets == initial_markets - assert exchange._load_async_markets.call_count == 0 + assert lam_spy.call_count == 0 - api_mock.load_markets = MagicMock(return_value=updated_markets) + api_mock.load_markets = get_mock_coro(return_value=updated_markets) # more than 10 minutes have passed, reload is executed time_machine.move_to(start_dt + timedelta(minutes=11), tick=False) exchange.reload_markets() assert exchange.markets == updated_markets - assert exchange._load_async_markets.call_count == 1 + assert lam_spy.call_count == 1 assert log_has("Performing scheduled market reload..", caplog) # Not called again - exchange._load_async_markets.reset_mock() + lam_spy.reset_mock() exchange.reload_markets() - assert exchange._load_async_markets.call_count == 0 + assert lam_spy.call_count == 0 def test_reload_markets_exception(default_conf, mocker, caplog): caplog.set_level(logging.DEBUG) api_mock = MagicMock() - api_mock.load_markets = MagicMock(side_effect=ccxt.NetworkError("LoadError")) + api_mock.load_markets = get_mock_coro(side_effect=ccxt.NetworkError("LoadError")) default_conf["exchange"]["markets_refresh_interval"] = 10 - exchange = get_patched_exchange(mocker, default_conf, api_mock, id="binance") + exchange = get_patched_exchange( + mocker, default_conf, api_mock, id="binance", mock_markets=False + ) exchange._last_markets_refresh = 2 # less than 10 minutes have passed, no reload @@ -613,7 +613,7 @@ def test_reload_markets_exception(default_conf, mocker, caplog): def test_validate_stakecurrency(default_conf, stake_currency, mocker, caplog): default_conf["stake_currency"] = stake_currency api_mock = MagicMock() - type(api_mock).load_markets = MagicMock( + type(api_mock).load_markets = get_mock_coro( return_value={ "ETH/BTC": {"quote": "BTC"}, "LTC/BTC": {"quote": "BTC"}, @@ -624,7 +624,6 @@ def test_validate_stakecurrency(default_conf, stake_currency, mocker, caplog): mocker.patch(f"{EXMS}._init_ccxt", MagicMock(return_value=api_mock)) mocker.patch(f"{EXMS}.validate_pairs") mocker.patch(f"{EXMS}.validate_timeframes") - mocker.patch(f"{EXMS}._load_async_markets") mocker.patch(f"{EXMS}.validate_pricing") Exchange(default_conf) @@ -632,7 +631,7 @@ def test_validate_stakecurrency(default_conf, stake_currency, mocker, caplog): def test_validate_stakecurrency_error(default_conf, mocker, caplog): default_conf["stake_currency"] = "XRP" api_mock = MagicMock() - type(api_mock).load_markets = MagicMock( + type(api_mock).load_markets = get_mock_coro( return_value={ "ETH/BTC": {"quote": "BTC"}, "LTC/BTC": {"quote": "BTC"}, @@ -643,14 +642,13 @@ def test_validate_stakecurrency_error(default_conf, mocker, caplog): mocker.patch(f"{EXMS}._init_ccxt", MagicMock(return_value=api_mock)) mocker.patch(f"{EXMS}.validate_pairs") mocker.patch(f"{EXMS}.validate_timeframes") - mocker.patch(f"{EXMS}._load_async_markets") with pytest.raises( ConfigurationError, match=r"XRP is not available as stake on .*Available currencies are: BTC, ETH, USDT", ): Exchange(default_conf) - type(api_mock).load_markets = MagicMock(side_effect=ccxt.NetworkError("No connection.")) + type(api_mock).load_markets = get_mock_coro(side_effect=ccxt.NetworkError("No connection.")) mocker.patch(f"{EXMS}._init_ccxt", MagicMock(return_value=api_mock)) with pytest.raises( @@ -697,20 +695,20 @@ def test_get_pair_base_currency(default_conf, mocker, pair, expected): def test_validate_pairs(default_conf, mocker): api_mock = MagicMock() - type(api_mock).load_markets = MagicMock( - return_value={ - "ETH/BTC": {"quote": "BTC"}, - "LTC/BTC": {"quote": "BTC"}, - "XRP/BTC": {"quote": "BTC"}, - "NEO/BTC": {"quote": "BTC"}, - } - ) id_mock = PropertyMock(return_value="test_exchange") type(api_mock).id = id_mock mocker.patch(f"{EXMS}._init_ccxt", MagicMock(return_value=api_mock)) mocker.patch(f"{EXMS}.validate_timeframes") - mocker.patch(f"{EXMS}._load_async_markets") + mocker.patch( + f"{EXMS}._load_async_markets", + return_value={ + "ETH/BTC": {"quote": "BTC"}, + "LTC/BTC": {"quote": "BTC"}, + "XRP/BTC": {"quote": "BTC"}, + "NEO/BTC": {"quote": "BTC"}, + }, + ) mocker.patch(f"{EXMS}.validate_stakecurrency") mocker.patch(f"{EXMS}.validate_pricing") # test exchange.validate_pairs directly @@ -754,7 +752,7 @@ def test_validate_pairs_exception(default_conf, mocker, caplog): def test_validate_pairs_restricted(default_conf, mocker, caplog): api_mock = MagicMock() - type(api_mock).load_markets = MagicMock( + type(api_mock).load_markets = get_mock_coro( return_value={ "ETH/BTC": {"quote": "BTC"}, "LTC/BTC": {"quote": "BTC"}, @@ -764,7 +762,6 @@ def test_validate_pairs_restricted(default_conf, mocker, caplog): ) mocker.patch(f"{EXMS}._init_ccxt", MagicMock(return_value=api_mock)) mocker.patch(f"{EXMS}.validate_timeframes") - mocker.patch(f"{EXMS}._load_async_markets") mocker.patch(f"{EXMS}.validate_pricing") mocker.patch(f"{EXMS}.validate_stakecurrency") @@ -777,9 +774,9 @@ def test_validate_pairs_restricted(default_conf, mocker, caplog): ) -def test_validate_pairs_stakecompatibility(default_conf, mocker, caplog): +def test_validate_pairs_stakecompatibility(default_conf, mocker): api_mock = MagicMock() - type(api_mock).load_markets = MagicMock( + type(api_mock).load_markets = get_mock_coro( return_value={ "ETH/BTC": {"quote": "BTC"}, "LTC/BTC": {"quote": "BTC"}, @@ -790,17 +787,16 @@ def test_validate_pairs_stakecompatibility(default_conf, mocker, caplog): ) mocker.patch(f"{EXMS}._init_ccxt", MagicMock(return_value=api_mock)) mocker.patch(f"{EXMS}.validate_timeframes") - mocker.patch(f"{EXMS}._load_async_markets") mocker.patch(f"{EXMS}.validate_stakecurrency") mocker.patch(f"{EXMS}.validate_pricing") Exchange(default_conf) -def test_validate_pairs_stakecompatibility_downloaddata(default_conf, mocker, caplog): +def test_validate_pairs_stakecompatibility_downloaddata(default_conf, mocker): api_mock = MagicMock() default_conf["stake_currency"] = "" - type(api_mock).load_markets = MagicMock( + type(api_mock).load_markets = get_mock_coro( return_value={ "ETH/BTC": {"quote": "BTC"}, "LTC/BTC": {"quote": "BTC"}, @@ -811,7 +807,6 @@ def test_validate_pairs_stakecompatibility_downloaddata(default_conf, mocker, ca ) mocker.patch(f"{EXMS}._init_ccxt", MagicMock(return_value=api_mock)) mocker.patch(f"{EXMS}.validate_timeframes") - mocker.patch(f"{EXMS}._load_async_markets") mocker.patch(f"{EXMS}.validate_stakecurrency") mocker.patch(f"{EXMS}.validate_pricing") @@ -819,10 +814,10 @@ def test_validate_pairs_stakecompatibility_downloaddata(default_conf, mocker, ca assert type(api_mock).load_markets.call_count == 1 -def test_validate_pairs_stakecompatibility_fail(default_conf, mocker, caplog): +def test_validate_pairs_stakecompatibility_fail(default_conf, mocker): default_conf["exchange"]["pair_whitelist"].append("HELLO-WORLD") api_mock = MagicMock() - type(api_mock).load_markets = MagicMock( + type(api_mock).load_markets = get_mock_coro( return_value={ "ETH/BTC": {"quote": "BTC"}, "LTC/BTC": {"quote": "BTC"}, @@ -833,7 +828,6 @@ def test_validate_pairs_stakecompatibility_fail(default_conf, mocker, caplog): ) mocker.patch(f"{EXMS}._init_ccxt", MagicMock(return_value=api_mock)) mocker.patch(f"{EXMS}.validate_timeframes") - mocker.patch(f"{EXMS}._load_async_markets") mocker.patch(f"{EXMS}.validate_stakecurrency") with pytest.raises(OperationalException, match=r"Stake-currency 'BTC' not compatible with.*"):