diff --git a/tests/unit_server/arghandler_test.py b/tests/unit_server/arghandler_test.py index 73512349..96bce31a 100644 --- a/tests/unit_server/arghandler_test.py +++ b/tests/unit_server/arghandler_test.py @@ -540,15 +540,21 @@ def test_220__argparse_nargs(argument_source: str) -> None: test_130__duplicates(argument_source) +class MyError(Exception): + """Used below.""" + + @pytest.mark.parametrize( "argument_source", [QUERY_ARGUMENTS, JSON_BODY_ARGUMENTS], ) @pytest.mark.parametrize( "exc", - [TypeError, ValueError, argparse.ArgumentTypeError, argparse.ArgumentError], + [TypeError, ValueError, argparse.ArgumentError, RuntimeError, IndexError, MyError], ) -def test_230__argparse_catch_most__error(argument_source: str, exc: Exception) -> None: +def test_230__argparse_custom_validation__error( + argument_source: str, exc: Exception +) -> None: """Test `argument_source` arguments using argparse's advanced options.""" args: Dict[str, Any] = { "foo": "True", @@ -574,11 +580,53 @@ def _error_it(_: Any, exc: Exception): arg, type=lambda x: _error_it( x, - exc("it's a bad value"), # type: ignore + exc("it's a bad value but you won't see this message anyway..."), # type: ignore ), ) with pytest.raises(tornado.web.HTTPError) as e: arghand.parse_args() - assert str(e.value) == "HTTP 400: argument foo: it's a bad value" + assert str(e.value) == "HTTP 400: argument foo: invalid type" + + +@pytest.mark.parametrize( + "argument_source", + [QUERY_ARGUMENTS, JSON_BODY_ARGUMENTS], +) +def test_235__argparse_custom_validation__argumenttypeerror__error( + argument_source: str, +) -> None: + """Test `argument_source` arguments using argparse's advanced options.""" + args: Dict[str, Any] = { + "foo": "True", + } + if argument_source == JSON_BODY_ARGUMENTS: + args = { + "foo": [1, 2, 3], + } + + # set up ArgumentHandler + arghand = setup_argument_handler( + argument_source, + args, + ) + + def _error_it(): + raise argparse.ArgumentTypeError("it's a bad value and you *will* see this!") + + for arg, _ in args.items(): + print() + print(arg) + arghand.add_argument( + arg, + type=_error_it, + ) + + with pytest.raises(tornado.web.HTTPError) as e: + arghand.parse_args() + + assert ( + str(e.value) + == "HTTP 400: argument foo: it's a bad value and you *will* see this!" + )