From 15ddd2f5d902007ca2c303180473b87375955671 Mon Sep 17 00:00:00 2001 From: Julian Berman Date: Wed, 25 Aug 2021 11:33:12 +0100 Subject: [PATCH] Bit of coverage tweaking. --- jsonschema/tests/test_cli.py | 6 ++-- .../tests/test_jsonschema_test_suite.py | 6 ++-- jsonschema/tests/test_types.py | 31 ++++++++++++------- 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/jsonschema/tests/test_cli.py b/jsonschema/tests/test_cli.py index eafc05d6c..f9c8c621e 100644 --- a/jsonschema/tests/test_cli.py +++ b/jsonschema/tests/test_cli.py @@ -10,9 +10,9 @@ import sys import tempfile -try: +try: # pragma: no cover from importlib import metadata -except ImportError: +except ImportError: # pragma: no cover import importlib_metadata as metadata from pyrsistent import m @@ -36,7 +36,7 @@ def __init__(self, *args, **kwargs): def iter_errors(self, instance): if errors: return errors.pop() - return [] + return [] # pragma: no cover @classmethod def check_schema(self, schema): diff --git a/jsonschema/tests/test_jsonschema_test_suite.py b/jsonschema/tests/test_jsonschema_test_suite.py index 23a34c9af..6a367ddd5 100644 --- a/jsonschema/tests/test_jsonschema_test_suite.py +++ b/jsonschema/tests/test_jsonschema_test_suite.py @@ -42,7 +42,7 @@ def skipper(test): def missing_format(checker): - def missing_format(test): + def missing_format(test): # pragma: no cover schema = test.schema if ( schema is True @@ -93,7 +93,7 @@ def narrow_unicode_build(test): # pragma: no cover return -if sys.version_info < (3, 9): +if sys.version_info < (3, 9): # pragma: no cover message = "Rejecting leading zeros is 3.9+" allowed_leading_zeros = skip( message=message, @@ -103,7 +103,7 @@ def narrow_unicode_build(test): # pragma: no cover ), ) else: - def allowed_leading_zeros(test): + def allowed_leading_zeros(test): # pragma: no cover return diff --git a/jsonschema/tests/test_types.py b/jsonschema/tests/test_types.py index 21e8312cc..8a4537c7e 100644 --- a/jsonschema/tests/test_types.py +++ b/jsonschema/tests/test_types.py @@ -28,18 +28,6 @@ def is_object_or_named_tuple(checker, instance): return is_namedtuple(instance) -def coerce_named_tuple(fn): - def coerced(validator, value, instance, schema): - if is_namedtuple(instance): - instance = instance._asdict() - return fn(validator, value, instance, schema) - return coerced - - -required = coerce_named_tuple(_validators.required) -properties = coerce_named_tuple(_validators.properties) - - class TestTypeChecker(TestCase): def test_is_type(self): checker = TypeChecker({"two": equals_2}) @@ -139,6 +127,9 @@ def int_or_str_int(checker, instance): with self.assertRaises(ValidationError): validator.validate(4.4) + with self.assertRaises(ValidationError): + validator.validate("foo") + def test_object_can_be_extended(self): schema = {"type": "object"} @@ -179,6 +170,16 @@ def test_object_extensions_can_handle_custom_validators(self): "object", is_object_or_named_tuple, ) + def coerce_named_tuple(fn): + def coerced(validator, value, instance, schema): + if is_namedtuple(instance): + instance = instance._asdict() + return fn(validator, value, instance, schema) + return coerced + + required = coerce_named_tuple(_validators.required) + properties = coerce_named_tuple(_validators.properties) + CustomValidator = extend( Draft4Validator, type_checker=type_checker, @@ -194,6 +195,12 @@ def test_object_extensions_can_handle_custom_validators(self): with self.assertRaises(ValidationError): validator.validate(Point(x="not an integer", y=5)) + # As well as still handle objects. + validator.validate({"x": 4, "y": 5}) + + with self.assertRaises(ValidationError): + validator.validate({"x": "not an integer", "y": 5}) + def test_unknown_type(self): with self.assertRaises(UnknownType) as e: Draft4Validator({}).is_type(12, "some unknown type")