2
2
from contextlib import contextmanager
3
3
from decimal import Decimal
4
4
from io import BytesIO
5
- from unittest import TestCase
5
+ from unittest import TestCase , mock
6
6
from urllib .request import pathname2url
7
7
import json
8
8
import os
9
9
import sys
10
10
import tempfile
11
11
import unittest
12
+ import warnings
12
13
13
- from twisted .trial .unittest import SynchronousTestCase
14
14
import attr
15
15
16
16
from jsonschema import FormatChecker , TypeChecker , exceptions , validators
@@ -23,7 +23,7 @@ def fail(validator, errors, instance, schema):
23
23
yield exceptions .ValidationError (** each )
24
24
25
25
26
- class TestCreateAndExtend (SynchronousTestCase ):
26
+ class TestCreateAndExtend (TestCase ):
27
27
def setUp (self ):
28
28
self .addCleanup (
29
29
self .assertEqual ,
@@ -1707,7 +1707,7 @@ class TestDraft202012Validator(ValidatorTestMixin, TestCase):
1707
1707
invalid = {"type" : "integer" }, "foo"
1708
1708
1709
1709
1710
- class TestValidatorFor (SynchronousTestCase ):
1710
+ class TestValidatorFor (TestCase ):
1711
1711
def test_draft_3 (self ):
1712
1712
schema = {"$schema" : "http://json-schema.org/draft-03/schema" }
1713
1713
self .assertIs (
@@ -1828,31 +1828,29 @@ def test_validator_for_custom_default(self):
1828
1828
self .assertIs (validators .validator_for ({}, default = None ), None )
1829
1829
1830
1830
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" })
1840
1833
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." ,
1844
1840
)
1845
1841
1846
1842
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 )
1849
1847
1850
1848
1851
- class TestValidate (SynchronousTestCase ):
1849
+ class TestValidate (TestCase ):
1852
1850
def assertUses (self , schema , Validator ):
1853
1851
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 )
1856
1854
self .assertEqual (result , [schema ])
1857
1855
1858
1856
def test_draft3_validator_is_chosen (self ):
@@ -1941,7 +1939,7 @@ def test_it_uses_best_match(self):
1941
1939
self .assertIn ("12 is not of type" , str (e .exception ))
1942
1940
1943
1941
1944
- class TestRefResolver (SynchronousTestCase ):
1942
+ class TestRefResolver (TestCase ):
1945
1943
1946
1944
base_uri = ""
1947
1945
stored_uri = "foo://stored"
@@ -1956,14 +1954,11 @@ def setUp(self):
1956
1954
1957
1955
def test_it_does_not_retrieve_schema_urls_from_the_network (self ):
1958
1956
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
1966
1960
self .assertEqual (resolved , validators .Draft3Validator .META_SCHEMA )
1961
+ self .assertFalse (patched .called )
1967
1962
1968
1963
def test_it_resolves_local_refs (self ):
1969
1964
ref = "#/properties/foo"
0 commit comments