8
8
from jsonschema .compat import NativeIO
9
9
from jsonschema .exceptions import SchemaError
10
10
11
- JSON_DIR_PATH = os .path .join (os .path .dirname (__file__ ), "json_files" )
12
11
13
12
def fake_validator (* errors ):
14
13
errors = list (reversed (errors ))
@@ -70,15 +69,40 @@ def test_find_validator_in_jsonschema(self):
70
69
71
70
72
71
class TestCLI (TestCase ):
72
+ instance_file_1 = "foo1.json"
73
+ instance_file_2 = "foo2.json"
74
+ schema_file = "schema.json"
75
+
76
+ def setUp (self ):
77
+ cli .open = self .fake_open
78
+ self .addCleanup (delattr , cli , "open" )
79
+
80
+ def fake_open (self , path ):
81
+ if path == self .instance_file_1 :
82
+ contents = "1"
83
+ elif path == self .instance_file_2 :
84
+ contents = "25"
85
+ elif path == self .schema_file :
86
+ contents = """
87
+ {
88
+ "anyOf": [
89
+ {"minimum": 20},
90
+ {"type": "string"},
91
+ {"required": true}
92
+ ]
93
+ }
94
+ """
95
+ else : # pragma: no cover
96
+ self .fail ("What is {!r}" .format (path ))
97
+ return NativeIO (contents )
98
+
73
99
def test_draft3_schema_draft4_validator (self ):
74
100
stdout , stderr = NativeIO (), NativeIO ()
75
101
exit_code = cli .run (
76
102
{
77
103
"validator" : Draft4Validator ,
78
- "schema" : os .path .join (JSON_DIR_PATH , "schema1.json" ),
79
- "instances" : [
80
- os .path .join (JSON_DIR_PATH , "instance1.json" ),
81
- ],
104
+ "schema" : "schema.json" ,
105
+ "instances" : ["foo1.json" ],
82
106
"error_format" : "{error.message}" ,
83
107
"output" : "plain" ,
84
108
},
@@ -94,10 +118,8 @@ def test_successful_validation(self):
94
118
exit_code = cli .run (
95
119
{
96
120
"validator" : fake_validator (),
97
- "schema" : os .path .join (JSON_DIR_PATH , "schema2.json" ),
98
- "instances" : [
99
- os .path .join (JSON_DIR_PATH , "instance2.json" ),
100
- ],
121
+ "schema" : "schema.json" ,
122
+ "instances" : ["foo2.json" ],
101
123
"error_format" : "{error.message}" ,
102
124
"output" : "plain" ,
103
125
},
@@ -114,10 +136,8 @@ def test_unsuccessful_validation(self):
114
136
exit_code = cli .run (
115
137
{
116
138
"validator" : fake_validator ([error ]),
117
- "schema" : os .path .join (JSON_DIR_PATH , "schema2.json" ),
118
- "instances" : [
119
- os .path .join (JSON_DIR_PATH , "instance1.json" ),
120
- ],
139
+ "schema" : "schema.json" ,
140
+ "instances" : ["foo1.json" ],
121
141
"error_format" : "{error.instance} - {error.message}" ,
122
142
"output" : "plain" ,
123
143
},
@@ -138,11 +158,8 @@ def test_unsuccessful_validation_multiple_instances(self):
138
158
exit_code = cli .run (
139
159
{
140
160
"validator" : fake_validator (first_errors , second_errors ),
141
- "schema" : os .path .join (JSON_DIR_PATH , "schema2.json" ),
142
- "instances" : [
143
- os .path .join (JSON_DIR_PATH , "instance1.json" ),
144
- os .path .join (JSON_DIR_PATH , "instance2.json" ),
145
- ],
161
+ "schema" : "schema.json" ,
162
+ "instances" : ["foo1.json" , "foo2.json" ],
146
163
"error_format" : "{error.instance} - {error.message}" ,
147
164
"output" : "plain" ,
148
165
},
@@ -173,7 +190,7 @@ def test_piping(self):
173
190
exit_code = cli .run (
174
191
{
175
192
"validator" : fake_validator (),
176
- "schema" : os . path . join ( JSON_DIR_PATH , "schema2 .json") ,
193
+ "schema" : "schema .json" ,
177
194
"instances" : [],
178
195
"error_format" : "{error.message}" ,
179
196
"output" : "plain" ,
0 commit comments