From 28a7598673966b73c1789a9fdd03dbd8bd9a02b7 Mon Sep 17 00:00:00 2001 From: Julian Berman Date: Thu, 7 Jul 2022 18:20:24 +0200 Subject: [PATCH] Run best match tests on the latest validator. --- jsonschema/tests/test_exceptions.py | 145 +++++++++++++--------------- 1 file changed, 67 insertions(+), 78 deletions(-) diff --git a/jsonschema/tests/test_exceptions.py b/jsonschema/tests/test_exceptions.py index dc5a664da..c57c10b94 100644 --- a/jsonschema/tests/test_exceptions.py +++ b/jsonschema/tests/test_exceptions.py @@ -1,33 +1,32 @@ from unittest import TestCase import textwrap -from jsonschema import Draft4Validator, exceptions +from jsonschema import exceptions +from jsonschema.validators import _LATEST_VERSION class TestBestMatch(TestCase): - def best_match(self, errors): - errors = list(errors) - best = exceptions.best_match(errors) + def best_match_of(self, instance, schema): + errors = list(_LATEST_VERSION(schema).iter_errors(instance)) + best = exceptions.best_match(iter(errors)) reversed_best = exceptions.best_match(reversed(errors)) - msg = "Didn't return a consistent best match!\nGot: {0}\n\nThen: {1}" self.assertEqual( - best._contents(), reversed_best._contents(), - msg=msg.format(best, reversed_best), + best._contents(), + reversed_best._contents(), + f"No consistent best match!\nGot: {best}\n\nThen: {reversed_best}", ) return best def test_shallower_errors_are_better_matches(self): - validator = Draft4Validator( - { - "properties": { - "foo": { - "minProperties": 2, - "properties": {"bar": {"type": "object"}}, - }, + schema = { + "properties": { + "foo": { + "minProperties": 2, + "properties": {"bar": {"type": "object"}}, }, }, - ) - best = self.best_match(validator.iter_errors({"foo": {"bar": []}})) + } + best = self.best_match_of(instance={"foo": {"bar": []}}, schema=schema) self.assertEqual(best.validator, "minProperties") def test_oneOf_and_anyOf_are_weak_matches(self): @@ -36,14 +35,12 @@ def test_oneOf_and_anyOf_are_weak_matches(self): match a part of. """ - validator = Draft4Validator( - { - "minProperties": 2, - "anyOf": [{"type": "string"}, {"type": "number"}], - "oneOf": [{"type": "string"}, {"type": "number"}], - }, - ) - best = self.best_match(validator.iter_errors({})) + schema = { + "minProperties": 2, + "anyOf": [{"type": "string"}, {"type": "number"}], + "oneOf": [{"type": "string"}, {"type": "number"}], + } + best = self.best_match_of(instance={}, schema=schema) self.assertEqual(best.validator, "minProperties") def test_if_the_most_relevant_error_is_anyOf_it_is_traversed(self): @@ -56,19 +53,17 @@ def test_if_the_most_relevant_error_is_anyOf_it_is_traversed(self): relevant one. """ - validator = Draft4Validator( - { - "properties": { - "foo": { - "anyOf": [ - {"type": "string"}, - {"properties": {"bar": {"type": "array"}}}, - ], - }, + schema = { + "properties": { + "foo": { + "anyOf": [ + {"type": "string"}, + {"properties": {"bar": {"type": "array"}}}, + ], }, }, - ) - best = self.best_match(validator.iter_errors({"foo": {"bar": 12}})) + } + best = self.best_match_of(instance={"foo": {"bar": 12}}, schema=schema) self.assertEqual(best.validator_value, "array") def test_if_the_most_relevant_error_is_oneOf_it_is_traversed(self): @@ -81,19 +76,17 @@ def test_if_the_most_relevant_error_is_oneOf_it_is_traversed(self): relevant one. """ - validator = Draft4Validator( - { - "properties": { - "foo": { - "oneOf": [ - {"type": "string"}, - {"properties": {"bar": {"type": "array"}}}, - ], - }, + schema = { + "properties": { + "foo": { + "oneOf": [ + {"type": "string"}, + {"properties": {"bar": {"type": "array"}}}, + ], }, }, - ) - best = self.best_match(validator.iter_errors({"foo": {"bar": 12}})) + } + best = self.best_match_of(instance={"foo": {"bar": 12}}, schema=schema) self.assertEqual(best.validator_value, "array") def test_if_the_most_relevant_error_is_allOf_it_is_traversed(self): @@ -102,48 +95,44 @@ def test_if_the_most_relevant_error_is_allOf_it_is_traversed(self): error from the context, because all schemas here must match anyways. """ - validator = Draft4Validator( - { - "properties": { - "foo": { - "allOf": [ - {"type": "string"}, - {"properties": {"bar": {"type": "array"}}}, - ], - }, + schema = { + "properties": { + "foo": { + "allOf": [ + {"type": "string"}, + {"properties": {"bar": {"type": "array"}}}, + ], }, }, - ) - best = self.best_match(validator.iter_errors({"foo": {"bar": 12}})) + } + best = self.best_match_of(instance={"foo": {"bar": 12}}, schema=schema) self.assertEqual(best.validator_value, "string") def test_nested_context_for_oneOf(self): - validator = Draft4Validator( - { - "properties": { - "foo": { - "oneOf": [ - {"type": "string"}, - { - "oneOf": [ - {"type": "string"}, - { - "properties": { - "bar": {"type": "array"}, - }, + schema = { + "properties": { + "foo": { + "oneOf": [ + {"type": "string"}, + { + "oneOf": [ + {"type": "string"}, + { + "properties": { + "bar": {"type": "array"}, }, - ], - }, - ], - }, + }, + ], + }, + ], }, }, - ) - best = self.best_match(validator.iter_errors({"foo": {"bar": 12}})) + } + best = self.best_match_of(instance={"foo": {"bar": 12}}, schema=schema) self.assertEqual(best.validator_value, "array") def test_one_error(self): - validator = Draft4Validator({"minProperties": 2}) + validator = _LATEST_VERSION({"minProperties": 2}) error, = validator.iter_errors({}) self.assertEqual( exceptions.best_match(validator.iter_errors({})).validator, @@ -151,7 +140,7 @@ def test_one_error(self): ) def test_no_errors(self): - validator = Draft4Validator({}) + validator = _LATEST_VERSION({}) self.assertIsNone(exceptions.best_match(validator.iter_errors({})))