summaryrefslogtreecommitdiff
path: root/jsonschema
diff options
context:
space:
mode:
authorJulian Berman <Julian@GrayVines.com>2023-02-23 15:01:46 +0200
committerJulian Berman <Julian@GrayVines.com>2023-02-23 15:01:46 +0200
commit3801d9aea0e044ff8b190bff750594aa18dee67e (patch)
tree633416325abeb3c08f84ad047354c3ecdc25de9a /jsonschema
parent787dbc9a143b0631024999a2a3dd56619d42b39d (diff)
downloadjsonschema-3801d9aea0e044ff8b190bff750594aa18dee67e.tar.gz
Improve error messages for deprecation tests when they fail.
Diffstat (limited to 'jsonschema')
-rw-r--r--jsonschema/tests/test_deprecations.py144
1 files changed, 37 insertions, 107 deletions
diff --git a/jsonschema/tests/test_deprecations.py b/jsonschema/tests/test_deprecations.py
index 5370599..224f459 100644
--- a/jsonschema/tests/test_deprecations.py
+++ b/jsonschema/tests/test_deprecations.py
@@ -12,15 +12,11 @@ class TestDeprecations(TestCase):
As of v4.0.0, __version__ is deprecated in favor of importlib.metadata.
"""
- with self.assertWarns(DeprecationWarning) as w:
+ message = "Accessing jsonschema.__version__ is deprecated"
+ with self.assertWarnsRegex(DeprecationWarning, message) as w:
from jsonschema import __version__ # noqa
self.assertEqual(w.filename, __file__)
- self.assertTrue(
- str(w.warning).startswith(
- "Accessing jsonschema.__version__ is deprecated",
- ),
- )
def test_validators_ErrorTree(self):
"""
@@ -28,15 +24,11 @@ class TestDeprecations(TestCase):
deprecated in favor of doing so from jsonschema.exceptions.
"""
- with self.assertWarns(DeprecationWarning) as w:
+ message = "Importing ErrorTree from jsonschema.validators is "
+ with self.assertWarnsRegex(DeprecationWarning, message) as w:
from jsonschema.validators import ErrorTree # noqa
self.assertEqual(w.filename, __file__)
- self.assertTrue(
- str(w.warning).startswith(
- "Importing ErrorTree from jsonschema.validators is deprecated",
- ),
- )
def test_validators_validators(self):
"""
@@ -44,16 +36,12 @@ class TestDeprecations(TestCase):
deprecated.
"""
- with self.assertWarns(DeprecationWarning) as w:
+ message = "Accessing jsonschema.validators.validators is deprecated"
+ with self.assertWarnsRegex(DeprecationWarning, message) as w:
value = validators.validators
- self.assertEqual(value, validators._VALIDATORS)
+ self.assertEqual(value, validators._VALIDATORS)
self.assertEqual(w.filename, __file__)
- self.assertTrue(
- str(w.warning).startswith(
- "Accessing jsonschema.validators.validators is deprecated",
- ),
- )
def test_validators_meta_schemas(self):
"""
@@ -61,16 +49,12 @@ class TestDeprecations(TestCase):
deprecated.
"""
- with self.assertWarns(DeprecationWarning) as w:
+ message = "Accessing jsonschema.validators.meta_schemas is deprecated"
+ with self.assertWarnsRegex(DeprecationWarning, message) as w:
value = validators.meta_schemas
- self.assertEqual(value, validators._META_SCHEMAS)
+ self.assertEqual(value, validators._META_SCHEMAS)
self.assertEqual(w.filename, __file__)
- self.assertTrue(
- str(w.warning).startswith(
- "Accessing jsonschema.validators.meta_schemas is deprecated",
- ),
- )
def test_RefResolver_in_scope(self):
"""
@@ -78,16 +62,12 @@ class TestDeprecations(TestCase):
"""
resolver = validators._RefResolver.from_schema({})
- with self.assertWarns(DeprecationWarning) as w:
+ message = "jsonschema.RefResolver.in_scope is deprecated "
+ with self.assertWarnsRegex(DeprecationWarning, message) as w:
with resolver.in_scope("foo"):
pass
self.assertEqual(w.filename, __file__)
- self.assertTrue(
- str(w.warning).startswith(
- "jsonschema.RefResolver.in_scope is deprecated ",
- ),
- )
def test_Validator_is_valid_two_arguments(self):
"""
@@ -96,16 +76,12 @@ class TestDeprecations(TestCase):
"""
validator = validators.Draft7Validator({})
- with self.assertWarns(DeprecationWarning) as w:
+ message = "Passing a schema to Validator.is_valid is deprecated "
+ with self.assertWarnsRegex(DeprecationWarning, message) as w:
result = validator.is_valid("foo", {"type": "number"})
self.assertFalse(result)
self.assertEqual(w.filename, __file__)
- self.assertTrue(
- str(w.warning).startswith(
- "Passing a schema to Validator.is_valid is deprecated ",
- ),
- )
def test_Validator_iter_errors_two_arguments(self):
"""
@@ -114,16 +90,12 @@ class TestDeprecations(TestCase):
"""
validator = validators.Draft7Validator({})
- with self.assertWarns(DeprecationWarning) as w:
+ message = "Passing a schema to Validator.iter_errors is deprecated "
+ with self.assertWarnsRegex(DeprecationWarning, message) as w:
error, = validator.iter_errors("foo", {"type": "number"})
self.assertEqual(error.validator, "type")
self.assertEqual(w.filename, __file__)
- self.assertTrue(
- str(w.warning).startswith(
- "Passing a schema to Validator.iter_errors is deprecated ",
- ),
- )
def test_Validator_resolver(self):
"""
@@ -131,15 +103,11 @@ class TestDeprecations(TestCase):
"""
validator = validators.Draft7Validator({})
- with self.assertWarns(DeprecationWarning) as w:
+ message = "Accessing Draft7Validator.resolver is "
+ with self.assertWarnsRegex(DeprecationWarning, message) as w:
self.assertIsInstance(validator.resolver, validators._RefResolver)
self.assertEqual(w.filename, __file__)
- self.assertTrue(
- str(w.warning).startswith(
- "Accessing Draft7Validator.resolver is ",
- ),
- )
def test_RefResolver(self):
"""
@@ -167,16 +135,14 @@ class TestDeprecations(TestCase):
A future version will explicitly raise an error.
"""
- with self.assertWarns(DeprecationWarning) as w:
+ message = "Subclassing validator classes is "
+ with self.assertWarnsRegex(DeprecationWarning, message) as w:
class Subclass(validators.Draft202012Validator):
pass
self.assertEqual(w.filename, __file__)
- self.assertTrue(
- str(w.warning).startswith("Subclassing validator classes is "),
- )
- with self.assertWarns(DeprecationWarning) as w:
+ with self.assertWarnsRegex(DeprecationWarning, message) as w:
class AnotherSubclass(validators.create(meta_schema={})):
pass
@@ -188,13 +154,11 @@ class TestDeprecations(TestCase):
self.addCleanup(FormatChecker.checkers.pop, "boom", None)
- with self.assertWarns(DeprecationWarning) as w:
+ message = "FormatChecker.cls_checks "
+ with self.assertWarnsRegex(DeprecationWarning, message) as w:
FormatChecker.cls_checks("boom")
self.assertEqual(w.filename, __file__)
- self.assertTrue(
- str(w.warning).startswith("FormatChecker.cls_checks "),
- )
def test_draftN_format_checker(self):
"""
@@ -202,7 +166,8 @@ class TestDeprecations(TestCase):
in favor of Validator.FORMAT_CHECKER.
"""
- with self.assertWarns(DeprecationWarning) as w:
+ message = "Accessing jsonschema.draft202012_format_checker is "
+ with self.assertWarnsRegex(DeprecationWarning, message) as w:
from jsonschema import draft202012_format_checker # noqa
self.assertIs(
@@ -210,14 +175,9 @@ class TestDeprecations(TestCase):
validators.Draft202012Validator.FORMAT_CHECKER,
)
self.assertEqual(w.filename, __file__)
- self.assertTrue(
- str(w.warning).startswith(
- "Accessing jsonschema.draft202012_format_checker is ",
- ),
- msg=w.warning,
- )
- with self.assertWarns(DeprecationWarning) as w:
+ message = "Accessing jsonschema.draft201909_format_checker is "
+ with self.assertWarnsRegex(DeprecationWarning, message) as w:
from jsonschema import draft201909_format_checker # noqa
self.assertIs(
@@ -225,14 +185,9 @@ class TestDeprecations(TestCase):
validators.Draft201909Validator.FORMAT_CHECKER,
)
self.assertEqual(w.filename, __file__)
- self.assertTrue(
- str(w.warning).startswith(
- "Accessing jsonschema.draft201909_format_checker is ",
- ),
- msg=w.warning,
- )
- with self.assertWarns(DeprecationWarning) as w:
+ message = "Accessing jsonschema.draft7_format_checker is "
+ with self.assertWarnsRegex(DeprecationWarning, message) as w:
from jsonschema import draft7_format_checker # noqa
self.assertIs(
@@ -240,14 +195,9 @@ class TestDeprecations(TestCase):
validators.Draft7Validator.FORMAT_CHECKER,
)
self.assertEqual(w.filename, __file__)
- self.assertTrue(
- str(w.warning).startswith(
- "Accessing jsonschema.draft7_format_checker is ",
- ),
- msg=w.warning,
- )
- with self.assertWarns(DeprecationWarning) as w:
+ message = "Accessing jsonschema.draft6_format_checker is "
+ with self.assertWarnsRegex(DeprecationWarning, message) as w:
from jsonschema import draft6_format_checker # noqa
self.assertIs(
@@ -255,14 +205,9 @@ class TestDeprecations(TestCase):
validators.Draft6Validator.FORMAT_CHECKER,
)
self.assertEqual(w.filename, __file__)
- self.assertTrue(
- str(w.warning).startswith(
- "Accessing jsonschema.draft6_format_checker is ",
- ),
- msg=w.warning,
- )
- with self.assertWarns(DeprecationWarning) as w:
+ message = "Accessing jsonschema.draft4_format_checker is "
+ with self.assertWarnsRegex(DeprecationWarning, message) as w:
from jsonschema import draft4_format_checker # noqa
self.assertIs(
@@ -270,14 +215,9 @@ class TestDeprecations(TestCase):
validators.Draft4Validator.FORMAT_CHECKER,
)
self.assertEqual(w.filename, __file__)
- self.assertTrue(
- str(w.warning).startswith(
- "Accessing jsonschema.draft4_format_checker is ",
- ),
- msg=w.warning,
- )
- with self.assertWarns(DeprecationWarning) as w:
+ message = "Accessing jsonschema.draft3_format_checker is "
+ with self.assertWarnsRegex(DeprecationWarning, message) as w:
from jsonschema import draft3_format_checker # noqa
self.assertIs(
@@ -285,12 +225,6 @@ class TestDeprecations(TestCase):
validators.Draft3Validator.FORMAT_CHECKER,
)
self.assertEqual(w.filename, __file__)
- self.assertTrue(
- str(w.warning).startswith(
- "Accessing jsonschema.draft3_format_checker is ",
- ),
- msg=w.warning,
- )
with self.assertRaises(ImportError):
from jsonschema import draft1234_format_checker # noqa
@@ -300,16 +234,12 @@ class TestDeprecations(TestCase):
As of v4.17.0, importing jsonschema.cli is deprecated.
"""
- with self.assertWarns(DeprecationWarning) as w:
+ message = "The jsonschema CLI is deprecated and will be removed "
+ with self.assertWarnsRegex(DeprecationWarning, message) as w:
import jsonschema.cli
importlib.reload(jsonschema.cli)
self.assertEqual(w.filename, importlib.__file__)
- self.assertTrue(
- str(w.warning).startswith(
- "The jsonschema CLI is deprecated and will be removed ",
- ),
- )
def test_cli(self):
"""