Skip to content

Commit

Permalink
more custom validation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ric-evans committed Dec 6, 2024
1 parent 82c4721 commit fb35077
Showing 1 changed file with 52 additions and 4 deletions.
56 changes: 52 additions & 4 deletions tests/unit_server/arghandler_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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!"
)

0 comments on commit fb35077

Please sign in to comment.