22from contextlib import contextmanager
33from decimal import Decimal
44from io import BytesIO
5- from unittest import TestCase
5+ from unittest import TestCase , mock
66from urllib .request import pathname2url
77import json
88import os
99import sys
1010import tempfile
1111import unittest
12+ import warnings
1213
13- from twisted .trial .unittest import SynchronousTestCase
1414import attr
1515
1616from jsonschema import FormatChecker , TypeChecker , exceptions , validators
@@ -23,7 +23,7 @@ def fail(validator, errors, instance, schema):
2323 yield exceptions .ValidationError (** each )
2424
2525
26- class TestCreateAndExtend (SynchronousTestCase ):
26+ class TestCreateAndExtend (TestCase ):
2727 def setUp (self ):
2828 self .addCleanup (
2929 self .assertEqual ,
@@ -1707,7 +1707,7 @@ class TestDraft202012Validator(ValidatorTestMixin, TestCase):
17071707 invalid = {"type" : "integer" }, "foo"
17081708
17091709
1710- class TestValidatorFor (SynchronousTestCase ):
1710+ class TestValidatorFor (TestCase ):
17111711 def test_draft_3 (self ):
17121712 schema = {"$schema" : "http://json-schema.org/draft-03/schema" }
17131713 self .assertIs (
@@ -1828,31 +1828,29 @@ def test_validator_for_custom_default(self):
18281828 self .assertIs (validators .validator_for ({}, default = None ), None )
18291829
18301830 def test_warns_if_meta_schema_specified_was_not_found (self ):
1831- self .assertWarns (
1832- category = DeprecationWarning ,
1833- message = (
1834- "The metaschema specified by $schema was not found. "
1835- "Using the latest draft to validate, but this will raise "
1836- "an error in the future."
1837- ),
1838- # https://tm.tl/9363 :'(
1839- filename = sys .modules [self .assertWarns .__module__ ].__file__ ,
1831+ with self .assertWarns (DeprecationWarning ) as cm :
1832+ validators .validator_for (schema = {"$schema" : "unknownSchema" })
18401833
1841- f = validators .validator_for ,
1842- schema = {"$schema" : "unknownSchema" },
1843- default = {},
1834+ self .assertEqual (cm .filename , __file__ )
1835+ self .assertEqual (
1836+ str (cm .warning ),
1837+ "The metaschema specified by $schema was not found. "
1838+ "Using the latest draft to validate, but this will raise "
1839+ "an error in the future." ,
18441840 )
18451841
18461842 def test_does_not_warn_if_meta_schema_is_unspecified (self ):
1847- validators .validator_for (schema = {}, default = {})
1848- self .assertFalse (self .flushWarnings ())
1843+ with warnings .catch_warnings (record = True ) as w :
1844+ warnings .simplefilter ("always" )
1845+ validators .validator_for (schema = {}, default = {})
1846+ self .assertFalse (w )
18491847
18501848
1851- class TestValidate (SynchronousTestCase ):
1849+ class TestValidate (TestCase ):
18521850 def assertUses (self , schema , Validator ):
18531851 result = []
1854- self .patch (Validator , "check_schema" , result .append )
1855- validators .validate ({}, schema )
1852+ with mock .patch . object (Validator , "check_schema" , result .append ):
1853+ validators .validate ({}, schema )
18561854 self .assertEqual (result , [schema ])
18571855
18581856 def test_draft3_validator_is_chosen (self ):
@@ -1941,7 +1939,7 @@ def test_it_uses_best_match(self):
19411939 self .assertIn ("12 is not of type" , str (e .exception ))
19421940
19431941
1944- class TestRefResolver (SynchronousTestCase ):
1942+ class TestRefResolver (TestCase ):
19451943
19461944 base_uri = ""
19471945 stored_uri = "foo://stored"
@@ -1956,14 +1954,11 @@ def setUp(self):
19561954
19571955 def test_it_does_not_retrieve_schema_urls_from_the_network (self ):
19581956 ref = validators .Draft3Validator .META_SCHEMA ["id" ]
1959- self .patch (
1960- self .resolver ,
1961- "resolve_remote" ,
1962- lambda * args , ** kwargs : self .fail ("Should not have been called!" ),
1963- )
1964- with self .resolver .resolving (ref ) as resolved :
1965- pass
1957+ with mock .patch .object (self .resolver , "resolve_remote" ) as patched :
1958+ with self .resolver .resolving (ref ) as resolved :
1959+ pass
19661960 self .assertEqual (resolved , validators .Draft3Validator .META_SCHEMA )
1961+ self .assertFalse (patched .called )
19671962
19681963 def test_it_resolves_local_refs (self ):
19691964 ref = "#/properties/foo"
0 commit comments