7
7
import json
8
8
9
9
if sys .version_info [:2 ] < (2 , 7 ): # pragma: no cover
10
- from unittest2 import TestCase , skipIf
10
+ import unittest2 as unittest
11
11
else :
12
- from unittest import TestCase , skipIf
12
+ import unittest
13
13
14
14
try :
15
15
from unittest import mock
@@ -35,7 +35,7 @@ def test_case(self):
35
35
return test_case
36
36
37
37
38
- def load_json_cases (tests_glob , basedir = os .path .dirname (__file__ )):
38
+ def load_json_cases (tests_glob , basedir = os .path .dirname (__file__ ), skip = None ):
39
39
def add_test_methods (test_class ):
40
40
for filename in glob .iglob (os .path .join (basedir , tests_glob )):
41
41
validating , _ = os .path .splitext (os .path .basename (filename ))
@@ -60,14 +60,19 @@ def add_test_methods(test_class):
60
60
test_name = test_name .encode ("utf-8" )
61
61
a_test .__name__ = test_name
62
62
63
+ if skip is not None and skip (case ):
64
+ a_test = unittest .skip ("Checker not present." )(
65
+ a_test
66
+ )
67
+
63
68
setattr (test_class , test_name , a_test )
64
69
65
70
return test_class
66
71
return add_test_methods
67
72
68
73
69
74
class BytesMixin (object ):
70
- @skipIf (PY3 , "The JSON module in Python 3 always produces unicode" )
75
+ @unittest . skipIf (PY3 , "In Python 3 json.load always produces unicode" )
71
76
def test_string_a_bytestring_is_a_string (self ):
72
77
self .validator_class ({"type" : "string" }).validate (b"foo" )
73
78
@@ -87,18 +92,10 @@ def test_it_can_validate_with_decimals(self):
87
92
validator .validate (invalid )
88
93
89
94
90
- class AnyTypeMixin (object ):
91
- def test_any_type_is_valid_for_type_any (self ):
92
- validator = self .validator_class ({"type" : "any" })
93
- validator .validate (mock .Mock ())
94
-
95
-
96
- @load_json_cases ("json/tests/draft3/optional/bignum.json" )
97
- class BigNumMixin (object ):
98
- pass
99
-
100
-
101
- @load_json_cases ("json/tests/draft3/optional/format.json" )
95
+ @load_json_cases (
96
+ "json/tests/draft3/optional/format.json" ,
97
+ skip = lambda case : case ["schema" ]["format" ] not in FormatChecker .checkers
98
+ )
102
99
class FormatMixin (object ):
103
100
104
101
validator_kwargs = {"format_checker" : FormatChecker ()}
@@ -125,11 +122,15 @@ def test_it_validates_formats_if_a_checker_is_provided(self):
125
122
126
123
127
124
@load_json_cases ("json/tests/draft3/*.json" )
128
- class TestDraft3 (
129
- TestCase , BytesMixin , DecimalMixin , AnyTypeMixin , FormatMixin , BigNumMixin ,
130
- ):
125
+ @ load_json_cases ( "json/tests/draft3/optional/bignum.json" )
126
+ class TestDraft3 ( unittest . TestCase , BytesMixin , DecimalMixin , FormatMixin ):
127
+
131
128
validator_class = Draft3Validator
132
129
130
+ def test_any_type_is_valid_for_type_any (self ):
131
+ validator = self .validator_class ({"type" : "any" })
132
+ validator .validate (mock .Mock ())
133
+
133
134
# TODO: we're in need of more meta schema tests
134
135
def test_invalid_properties (self ):
135
136
with self .assertRaises (SchemaError ):
@@ -140,7 +141,7 @@ def test_minItems_invalid_string(self):
140
141
validate ([1 ], {"minItems" : "1" }) # needs to be an integer
141
142
142
143
143
- class TestIterErrors (TestCase ):
144
+ class TestIterErrors (unittest . TestCase ):
144
145
def setUp (self ):
145
146
self .validator = Draft3Validator ({})
146
147
@@ -174,7 +175,7 @@ def test_iter_errors_multiple_failures_one_validator(self):
174
175
self .assertEqual (len (errors ), 4 )
175
176
176
177
177
- class TestValidationErrorMessages (TestCase ):
178
+ class TestValidationErrorMessages (unittest . TestCase ):
178
179
def message_for (self , instance , schema , * args , ** kwargs ):
179
180
with self .assertRaises (ValidationError ) as e :
180
181
validate (instance , schema , * args , ** kwargs )
@@ -248,7 +249,7 @@ def test_invalid_format(self):
248
249
self .assertIn ("is not a" , message )
249
250
250
251
251
- class TestValidationErrorDetails (TestCase ):
252
+ class TestValidationErrorDetails (unittest . TestCase ):
252
253
def setUp (self ):
253
254
self .validator = Draft3Validator ({})
254
255
@@ -314,7 +315,7 @@ def test_multiple_nesting(self):
314
315
self .assertEqual (e6 .validator , "enum" )
315
316
316
317
317
- class TestErrorTree (TestCase ):
318
+ class TestErrorTree (unittest . TestCase ):
318
319
def setUp (self ):
319
320
self .validator = Draft3Validator ({})
320
321
@@ -356,7 +357,7 @@ def test_children_have_their_errors_dicts_built(self):
356
357
self .assertEqual (tree ["bar" ][0 ].errors , {"foo" : e1 , "quux" : e2 })
357
358
358
359
359
- class TestDraft3Validator (TestCase ):
360
+ class TestDraft3Validator (unittest . TestCase ):
360
361
def setUp (self ):
361
362
self .instance = mock .Mock ()
362
363
self .schema = {}
@@ -422,7 +423,7 @@ def test_is_type_raises_exception_for_unknown_type(self):
422
423
self .validator .is_type ("foo" , object ())
423
424
424
425
425
- class TestRefResolver (TestCase ):
426
+ class TestRefResolver (unittest . TestCase ):
426
427
def setUp (self ):
427
428
self .base_uri = ""
428
429
self .referrer = {}
@@ -472,7 +473,7 @@ def test_it_can_construct_a_base_uri_from_a_schema_without_id(self):
472
473
self .assertEqual (resolver .referrer , schema )
473
474
474
475
475
- class TestFormatChecker (TestCase ):
476
+ class TestFormatChecker (unittest . TestCase ):
476
477
def setUp (self ):
477
478
self .fn = mock .Mock ()
478
479
0 commit comments