1
+ import json
2
+
1
3
from jsonschema import Draft4Validator , ValidationError , cli
2
4
from jsonschema .compat import StringIO
3
5
from jsonschema .tests .compat import mock , unittest
@@ -17,6 +19,11 @@ def iter_errors(self, instance):
17
19
return FakeValidator
18
20
19
21
22
+ def serialize_instances (* instances ):
23
+ """:Return: a string with `instances` serialized to JSON, one per line."""
24
+ return '\n ' .join (json .dumps (instance , separators = (',' , ':' )) for instance in instances ) + '\n '
25
+
26
+
20
27
class TestParser (unittest .TestCase ):
21
28
FakeValidator = fake_validator ()
22
29
@@ -108,3 +115,31 @@ def test_unsuccessful_validation_multiple_instances(self):
108
115
self .assertFalse (stdout .getvalue ())
109
116
self .assertEqual (stderr .getvalue (), "1 - 9\t 1 - 8\t 2 - 7\t " )
110
117
self .assertEqual (exit_code , 1 )
118
+
119
+ def test_filter_valid (self ):
120
+ serialized = serialize_instances (1 , [2 ], {3 : 4 })
121
+ stdin , stdout , stderr = StringIO (serialized ), StringIO (), StringIO ()
122
+ exit_code = cli .run ({
123
+ "validator" : fake_validator (),
124
+ "schema" : {},
125
+ "error_format" : "{error.message}" ,
126
+ "filter" : True ,
127
+ }, stdin = stdin , stdout = stdout , stderr = stderr )
128
+ self .assertFalse (stderr .getvalue ())
129
+ self .assertEquals (stdout .getvalue (), serialized )
130
+ self .assertEqual (exit_code , 0 )
131
+
132
+ def test_filter_invalid (self ):
133
+ schema = {"type" : "object" }
134
+ serialized_in = serialize_instances ({}, [2 ], {3 : 4 })
135
+ serialized_out = serialize_instances ({}, {3 : 4 })
136
+ stdin , stdout , stderr = StringIO (serialized_in ), StringIO (), StringIO ()
137
+ exit_code = cli .run ({
138
+ "validator" : Draft4Validator ,
139
+ "schema" : schema ,
140
+ "error_format" : "{error.message}" ,
141
+ "filter" : True ,
142
+ }, stdin = stdin , stdout = stdout , stderr = stderr )
143
+ self .assertEquals (stderr .getvalue (), "[2] is not of type 'object'" )
144
+ self .assertEquals (stdout .getvalue (), serialized_out )
145
+ self .assertEqual (exit_code , 1 )
0 commit comments