Skip to content

Commit 272b4f2

Browse files
committed
Remove uses of SynchronousTestCase.
The warnings module still leaves what to be desired vis a vis cleaning up state, but everything at least passes this way now, and this removes a testing dependency.
1 parent 832888f commit 272b4f2

File tree

1 file changed

+24
-29
lines changed

1 file changed

+24
-29
lines changed

Diff for: jsonschema/tests/test_validators.py

+24-29
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
from contextlib import contextmanager
33
from decimal import Decimal
44
from io import BytesIO
5-
from unittest import TestCase
5+
from unittest import TestCase, mock
66
from urllib.request import pathname2url
77
import json
88
import os
99
import sys
1010
import tempfile
1111
import unittest
12+
import warnings
1213

13-
from twisted.trial.unittest import SynchronousTestCase
1414
import attr
1515

1616
from 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

Comments
 (0)