1
1
#! /usr/bin/env python
2
- from glob import glob
3
2
from unittest import TestCase , skipIf
3
+ import fnmatch
4
4
import json
5
5
import logging
6
6
import os
@@ -11,34 +11,9 @@ except ImportError:
11
11
jsonschema = None
12
12
13
13
14
- SUITE_ROOT_DIR = os .path .join (os .path .dirname (__file__ ), os .pardir , "tests" )
15
-
16
14
logging .basicConfig (level = logging .INFO )
17
15
18
-
19
- def groups (paths ):
20
- for path in paths :
21
- with open (path ) as test_file :
22
- groups = json .load (test_file )
23
- for group in groups :
24
- yield group
25
-
26
-
27
- def cases (paths ):
28
- for test_group in groups (paths ):
29
- for test in test_group ["tests" ]:
30
- test ["schema" ] = test_group ["schema" ]
31
- yield test
32
-
33
-
34
- def errortree_messages (et , path = "" ):
35
- for e in et .errors .values ():
36
- yield "%s: %s" % (path , e )
37
- for key in et :
38
- for msg in errortree_messages (et [key ], "%s/%s" % (path , key )):
39
- yield msg
40
-
41
-
16
+ SUITE_ROOT_DIR = os .path .join (os .path .dirname (__file__ ), os .pardir , "tests" )
42
17
TESTSUITE_SCHEMA = {
43
18
"$schema" : "http://json-schema.org/draft-03/schema#" ,
44
19
"type" : "array" ,
@@ -67,43 +42,73 @@ TESTSUITE_SCHEMA = {
67
42
}
68
43
69
44
45
+ def files (paths ):
46
+ for path in paths :
47
+ with open (path ) as test_file :
48
+ yield json .load (test_file )
49
+
50
+
51
+ def groups (paths ):
52
+ for test_file in files (paths ):
53
+ for group in test_file :
54
+ yield group
55
+
56
+
57
+ def cases (paths ):
58
+ for test_group in groups (paths ):
59
+ for test in test_group ["tests" ]:
60
+ test ["schema" ] = test_group ["schema" ]
61
+ yield test
62
+
63
+
64
+ def collect (root_dir ):
65
+ for root , dirs , files in os .walk (root_dir ):
66
+ for filename in fnmatch .filter (files , "*.json" ):
67
+ yield os .path .join (root , filename )
68
+
69
+
70
70
class SanityTests (TestCase ):
71
71
@classmethod
72
72
def setUpClass (cls ):
73
73
logging .info ("Looking for tests in %s" , SUITE_ROOT_DIR )
74
- cls .test_files = []
75
- for root , dirs , files in os .walk (SUITE_ROOT_DIR ):
76
- cls .test_files .extend (glob (os .path .join (root , "*.json" )))
74
+ cls .test_files = list (collect (SUITE_ROOT_DIR ))
77
75
logging .info ("Found %s test files" , len (cls .test_files ))
78
76
assert cls .test_files , "Didn't find the test files!"
79
77
80
78
def test_all_files_are_valid_json (self ):
81
- for test_path in self .test_files :
82
- with open (test_path ) as test_file :
83
- json .load (test_file )
84
-
85
- @skipIf (jsonschema is None , "Validation library not present! Skipping." )
86
- def test_all_schemas_are_valid (self ):
87
- for test_case in cases (self .test_files ):
88
- # XXX: Add the other versions for the other drafts
89
- jsonschema .Draft3Validator .check_schema (test_case ["schema" ])
79
+ for path in self .test_files :
80
+ with open (path ) as test_file :
81
+ try :
82
+ json .load (test_file )
83
+ except ValueError as error :
84
+ self .fail ("%s contains invalid JSON (%s)" % (path , error ))
90
85
91
86
def test_all_descriptions_are_unique (self ):
92
87
for group in groups (self .test_files ):
93
88
descriptions = {test ["description" ] for test in group ["tests" ]}
94
- self .assertEqual (len (descriptions ), len (group ["tests" ]))
89
+ self .assertEqual (
90
+ len (descriptions ),
91
+ len (group ["tests" ]),
92
+ "%r contains a duplicate description" % (group ,)
93
+ )
95
94
95
+ @skipIf (jsonschema is None , "Validation library not present! Skipping." )
96
+ def test_all_schemas_are_valid (self ):
97
+ for case in cases (self .test_files ):
98
+ # XXX: Add the other versions for the other drafts
99
+ try :
100
+ jsonschema .Draft3Validator .check_schema (case ["schema" ])
101
+ except jsonschema .SchemaError as error :
102
+ self .fail ("%s contains an invalid schema (%s)" % (case , error ))
103
+
104
+ @skipIf (jsonschema is None , "Validation library not present! Skipping." )
96
105
def test_suites_are_valid (self ):
97
106
validator = jsonschema .Draft3Validator (TESTSUITE_SCHEMA )
98
- for root , dirs , files in os .walk (SUITE_ROOT_DIR ):
99
- for f in glob (os .path .join (root , "*.json" )):
100
- with open (f ) as test_file :
101
- test_data = json .load (test_file )
102
- errortree = jsonschema .ErrorTree (validator .iter_errors (test_data ))
103
- msg = "Test suite '%s' does not follow the test format:\n " % f
104
- msg += "\n " .join ("\t " + e for e in errortree_messages (errortree ))
105
- assert not len (errortree ), msg
106
-
107
+ for tests in files (self .test_files ):
108
+ try :
109
+ validator .validate (tests )
110
+ except jsonschema .ValidationError as error :
111
+ self .fail (str (error ))
107
112
108
113
109
114
if __name__ == "__main__" :
0 commit comments