From 028b8ee534ad73179858d5c3e4f5f9cd0c3176dc Mon Sep 17 00:00:00 2001 From: Nicola Coretti Date: Tue, 9 Jul 2024 12:16:22 +0200 Subject: [PATCH] Add tests for http transport errors (#149) --------- Co-authored-by: Christoph Pirkl <4711730+kaklakariada@users.noreply.github.com> --- .../integration/http_transport_errors_test.py | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 test/integration/http_transport_errors_test.py diff --git a/test/integration/http_transport_errors_test.py b/test/integration/http_transport_errors_test.py new file mode 100644 index 0000000..d4b7cae --- /dev/null +++ b/test/integration/http_transport_errors_test.py @@ -0,0 +1,78 @@ +import os +import time +import pytest +import shutil +import pyexasol + + +@pytest.fixture +def dev_null(): + with open(os.devnull, "wb") as f: + yield f + + +@pytest.fixture +def statement(): + yield "SELECT * FROM USERS LIMIT 1000" + + +@pytest.fixture +def invalid_statement(): + yield "SELECT * FROM DoesNotExist LIMIT 1000" + + +@pytest.mark.etl +@pytest.mark.exceptions +def test_sql_error(connection, dev_null, invalid_statement): + def export_cb(pipe, dst, **kwargs): + shutil.copyfileobj(pipe, dev_null) + + with pytest.raises(pyexasol.exceptions.ExaQueryError): + connection.export_to_callback(export_cb, None, invalid_statement) + + +@pytest.mark.etl +@pytest.mark.exceptions +def test_abort_query(connection, dev_null, statement): + def export_cb(pipe, dst, **kwargs): + connection.abort_query() + # Make sure abort has time to propagate + time.sleep(1) + shutil.copyfileobj(pipe, dev_null) + + with pytest.raises(pyexasol.exceptions.ExaError): + connection.export_to_callback(export_cb, None, statement) + + +@pytest.mark.etl +@pytest.mark.exceptions +def test_error_in_export_callback(connection, statement): + error_msg = "Error from callback" + + def export_cb(pipe, dst, **kwargs): + raise Exception(error_msg) + + with pytest.raises(Exception, match=error_msg): + connection.export_to_callback(export_cb, None, statement) + + +@pytest.mark.etl +@pytest.mark.exceptions +def test_error_in_import_callback(connection, statement): + def import_cb(pipe, src, **kwargs): + raise Exception() + + with pytest.raises(pyexasol.exceptions.ExaQueryError): + connection.import_from_callback(import_cb, None, "users") + + +@pytest.mark.etl +@pytest.mark.exceptions +def test_closed_ws_connection(connection, dev_null, statement): + def import_cb(pipe, src, **kwargs): + connection.close(disconnect=False) + time.sleep(1) + shutil.copyfileobj(pipe, dev_null) + + with pytest.raises(pyexasol.exceptions.ExaError): + connection.import_from_callback(import_cb, None, "users")