From 2c80388b40bca3a77d765801eb96722f992340ab Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 31 Jan 2021 19:49:14 +0100 Subject: [PATCH] Fix valueerror in case of empty array files --- freqtrade/data/history/jsondatahandler.py | 8 ++++++-- tests/commands/test_commands.py | 2 +- tests/data/test_history.py | 14 +++++++++++++- tests/rpc/test_rpc_apiserver.py | 2 +- tests/testdata/NOPAIR_XXX-4m.json | 1 + 5 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 tests/testdata/NOPAIR_XXX-4m.json diff --git a/freqtrade/data/history/jsondatahandler.py b/freqtrade/data/history/jsondatahandler.py index 9122170d5..301d228a8 100644 --- a/freqtrade/data/history/jsondatahandler.py +++ b/freqtrade/data/history/jsondatahandler.py @@ -86,8 +86,12 @@ class JsonDataHandler(IDataHandler): filename = self._pair_data_filename(self._datadir, pair, timeframe) if not filename.exists(): return DataFrame(columns=self._columns) - pairdata = read_json(filename, orient='values') - pairdata.columns = self._columns + try: + pairdata = read_json(filename, orient='values') + pairdata.columns = self._columns + except ValueError: + logger.error(f"Could not load data for {pair}.") + return DataFrame(columns=self._columns) pairdata = pairdata.astype(dtype={'open': 'float', 'high': 'float', 'low': 'float', 'close': 'float', 'volume': 'float'}) pairdata['date'] = to_datetime(pairdata['date'], diff --git a/tests/commands/test_commands.py b/tests/commands/test_commands.py index f8ecc8218..d655174b3 100644 --- a/tests/commands/test_commands.py +++ b/tests/commands/test_commands.py @@ -1181,7 +1181,7 @@ def test_start_list_data(testdatadir, capsys): pargs['config'] = None start_list_data(pargs) captured = capsys.readouterr() - assert "Found 16 pair / timeframe combinations." in captured.out + assert "Found 17 pair / timeframe combinations." in captured.out assert "\n| Pair | Timeframe |\n" in captured.out assert "\n| UNITTEST/BTC | 1m, 5m, 8m, 30m |\n" in captured.out diff --git a/tests/data/test_history.py b/tests/data/test_history.py index 99b22adda..353cfc6f7 100644 --- a/tests/data/test_history.py +++ b/tests/data/test_history.py @@ -646,7 +646,7 @@ def test_datahandler_ohlcv_get_available_data(testdatadir): ('ZEC/BTC', '5m'), ('UNITTEST/BTC', '1m'), ('ADA/BTC', '5m'), ('ETC/BTC', '5m'), ('NXT/BTC', '5m'), ('DASH/BTC', '5m'), ('XRP/ETH', '1m'), ('XRP/ETH', '5m'), ('UNITTEST/BTC', '30m'), - ('UNITTEST/BTC', '8m')} + ('UNITTEST/BTC', '8m'), ('NOPAIR/XXX', '4m')} paircombs = JsonGzDataHandler.ohlcv_get_available_data(testdatadir) assert set(paircombs) == {('UNITTEST/BTC', '8m')} @@ -672,6 +672,18 @@ def test_jsondatahandler_ohlcv_purge(mocker, testdatadir): assert unlinkmock.call_count == 1 +def test_jsondatahandler_ohlcv_load(testdatadir, caplog): + dh = JsonDataHandler(testdatadir) + df = dh.ohlcv_load('XRP/ETH', '5m') + assert len(df) == 711 + + # Failure case (empty array) + df1 = dh.ohlcv_load('NOPAIR/XXX', '4m') + assert len(df1) == 0 + assert log_has("Could not load data for NOPAIR/XXX.", caplog) + assert df.columns.equals(df1.columns) + + def test_jsondatahandler_trades_load(testdatadir, caplog): dh = JsonGzDataHandler(testdatadir) logmsg = "Old trades format detected - converting" diff --git a/tests/rpc/test_rpc_apiserver.py b/tests/rpc/test_rpc_apiserver.py index b050e5694..71f1b3172 100644 --- a/tests/rpc/test_rpc_apiserver.py +++ b/tests/rpc/test_rpc_apiserver.py @@ -1109,7 +1109,7 @@ def test_list_available_pairs(botclient): rc = client_get(client, f"{BASE_URI}/available_pairs") assert_response(rc) - assert rc.json()['length'] == 12 + assert rc.json()['length'] == 13 assert isinstance(rc.json()['pairs'], list) rc = client_get(client, f"{BASE_URI}/available_pairs?timeframe=5m") diff --git a/tests/testdata/NOPAIR_XXX-4m.json b/tests/testdata/NOPAIR_XXX-4m.json new file mode 100644 index 000000000..fe51488c7 --- /dev/null +++ b/tests/testdata/NOPAIR_XXX-4m.json @@ -0,0 +1 @@ +[]