From db93f41f7d639cb18c3c7c69dd90ef53c3307947 Mon Sep 17 00:00:00 2001 From: Zac Hatfield-Dodds Date: Sun, 3 Mar 2024 01:57:25 -0800 Subject: [PATCH] Another test for #105 --- tests/test_from_schema.py | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/tests/test_from_schema.py b/tests/test_from_schema.py index 7a56d53..1ad4553 100644 --- a/tests/test_from_schema.py +++ b/tests/test_from_schema.py @@ -21,6 +21,7 @@ from hypothesis.errors import FailedHealthCheck, HypothesisWarning, InvalidArgument from hypothesis.internal.compat import PYPY from hypothesis.internal.reflection import proxies +from hypothesis.strategies._internal.regex import IncompatibleWithAlphabet from hypothesis_jsonschema._canonicalise import ( HypothesisRefResolutionError, @@ -311,10 +312,19 @@ def inner(*args, **kwargs): try: f(*args, **kwargs) assert name not in RECURSIVE_REFS - except jsonschema.exceptions._RefResolutionError as err: + except ( + jsonschema.exceptions._RefResolutionError, + wre := getattr(jsonschema.exceptions, "_WrappedReferencingError", ()), + ) as err: + if isinstance(err, wre) and isinstance( + err._wrapped, jsonschema.exceptions._Unresolvable + ): + pytest.xfail() if ( isinstance(err, HypothesisRefResolutionError) - or isinstance(err._cause, HypothesisRefResolutionError) + or isinstance( + getattr(err, "_cause", None), HypothesisRefResolutionError + ) ) and ( "does not fetch remote references" in str(err) or name in RECURSIVE_REFS @@ -360,6 +370,28 @@ def test_cannot_generate_for_empty_test_suite_schema(name): strat.example() +@pytest.mark.parametrize("name", to_name_params(suite)) +@settings( + suppress_health_check=[HealthCheck.too_slow, HealthCheck.data_too_large], + deadline=None, + max_examples=20, +) +@given(data=st.data()) +@xfail_on_reference_resolve_error +def test_constrained_alphabet_generation(data, name): + note(f"{suite[name]=}") + try: + value = data.draw(from_schema(suite[name], codec="ascii")) + except IncompatibleWithAlphabet: + pytest.skip() + note(f"{value=}") + try: + jsonschema.validate(value, suite[name]) + except jsonschema.exceptions.SchemaError: + jsonschema.Draft4Validator(suite[name]).validate(value) + json.dumps(value).encode("ascii") + + # This schema has overlapping patternProperties - this is OK, so long as they're # merged or otherwise handled correctly, with the exception of the key "ab" which # would have to be both an integer and a string (and is thus disallowed).