From 3b446c76b7d1f045def7a26b755b2f6276da1861 Mon Sep 17 00:00:00 2001 From: Anish Umale Date: Wed, 17 Apr 2024 19:31:16 +0530 Subject: [PATCH 1/2] rearrange imports in test_errors_codes.py --- mathesar/tests/rpc/exceptions/test_error_codes.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/mathesar/tests/rpc/exceptions/test_error_codes.py b/mathesar/tests/rpc/exceptions/test_error_codes.py index 66d6babb72..a50f01b521 100644 --- a/mathesar/tests/rpc/exceptions/test_error_codes.py +++ b/mathesar/tests/rpc/exceptions/test_error_codes.py @@ -1,12 +1,12 @@ -from http.client import CannotSendRequest +import pytest -from django.core.exceptions import FieldDoesNotExist from psycopg.errors import BadCopyFileFormat -import pytest +from django.core.exceptions import FieldDoesNotExist +from mathesar.utils.connections import BadInstallationTarget +from db.functions.exceptions import UnknownDBFunctionID from sqlalchemy.exc import IntegrityError +from http.client import CannotSendRequest -from db.functions.exceptions import UnknownDBFunctionID -from mathesar.utils.connections import BadInstallationTarget import mathesar.rpc.exceptions.error_codes as err_codes From ab69b487f5bb45d5ea94b173c08a10f12406a80f Mon Sep 17 00:00:00 2001 From: Anish Umale Date: Thu, 18 Apr 2024 00:58:06 +0530 Subject: [PATCH 2/2] add tests for rpc exception decorator --- .../rpc/exceptions/test_error_handler.py | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 mathesar/tests/rpc/exceptions/test_error_handler.py diff --git a/mathesar/tests/rpc/exceptions/test_error_handler.py b/mathesar/tests/rpc/exceptions/test_error_handler.py new file mode 100644 index 0000000000..f701131343 --- /dev/null +++ b/mathesar/tests/rpc/exceptions/test_error_handler.py @@ -0,0 +1,37 @@ +import pytest + +from modernrpc.exceptions import RPCException +from psycopg.errors import BadCopyFileFormat +from django.core.exceptions import FieldDoesNotExist +from mathesar.utils.connections import BadInstallationTarget +from db.functions.exceptions import UnknownDBFunctionID +from http.client import CannotSendRequest + +from mathesar.rpc.exceptions.handlers import handle_rpc_exceptions + + +@handle_rpc_exceptions +def fake_func(exception): + raise exception + + +@pytest.mark.parametrize( + "example_err,expect_code", [ + (AssertionError, -31002), + (BadCopyFileFormat, -30009), + (FieldDoesNotExist, -29030), + (BadInstallationTarget, -28002), + (UnknownDBFunctionID, -27024), + (CannotSendRequest, -25031), + ] +) +def test_handle_rpc_exceptions(example_err, expect_code): + """ + Tests whether a function wrapped by `handle_rpc_exceptions` always raises a `RPCException` + for any given exception raised inside the function. + """ + with pytest.raises(RPCException) as rpc_exception: + fake_func(example_err) + assert hasattr(fake_func, '__wrapped__') + assert example_err.__name__ in rpc_exception.value.args[0] + assert str(expect_code) in rpc_exception.value.args[0]