Skip to content

Commit ef8c46b

Browse files
RomainTTRomain Taprest
authored andcommitted
[tests] Update existing CLI tests for the new implementation.
Schema and instances can no longer be given to run() as objects. They MUST be paths to existing files. That is why a new directory has been made, containing JSON files.
1 parent 1e352e8 commit ef8c46b

File tree

5 files changed

+49
-27
lines changed

5 files changed

+49
-27
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
25
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"anyOf": [
3+
{"minimum": 20},
4+
{"type": "string"},
5+
{"required": true}
6+
]
7+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

jsonschema/tests/test_cli.py

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
import json
33
import subprocess
44
import sys
5+
import os
56

67
from jsonschema import Draft4Validator, ValidationError, cli, __version__
78
from jsonschema.compat import NativeIO
89
from jsonschema.exceptions import SchemaError
910

11+
JSON_DIR_PATH = os.path.join(os.path.dirname(__file__), "json_files")
1012

1113
def fake_validator(*errors):
1214
errors = list(reversed(errors))
@@ -70,32 +72,34 @@ def test_find_validator_in_jsonschema(self):
7072
class TestCLI(TestCase):
7173
def test_draft3_schema_draft4_validator(self):
7274
stdout, stderr = NativeIO(), NativeIO()
73-
with self.assertRaises(SchemaError):
74-
cli.run(
75-
{
76-
"validator": Draft4Validator,
77-
"schema": {
78-
"anyOf": [
79-
{"minimum": 20},
80-
{"type": "string"},
81-
{"required": True},
82-
],
83-
},
84-
"instances": [1],
85-
"error_format": "{error.message}",
86-
},
87-
stdout=stdout,
88-
stderr=stderr,
89-
)
75+
exit_code = cli.run(
76+
{
77+
"validator": Draft4Validator,
78+
"schema": os.path.join(JSON_DIR_PATH, "schema1.json"),
79+
"instances": [
80+
os.path.join(JSON_DIR_PATH, "instance1.json"),
81+
],
82+
"error_format": "{error.message}",
83+
"output": "plain",
84+
},
85+
stdout=stdout,
86+
stderr=stderr,
87+
)
88+
self.assertFalse(stdout.getvalue())
89+
self.assertTrue(stderr.getvalue())
90+
self.assertEqual(exit_code, 0)
9091

9192
def test_successful_validation(self):
9293
stdout, stderr = NativeIO(), NativeIO()
9394
exit_code = cli.run(
9495
{
9596
"validator": fake_validator(),
96-
"schema": {},
97-
"instances": [1],
97+
"schema": os.path.join(JSON_DIR_PATH, "schema2.json"),
98+
"instances": [
99+
os.path.join(JSON_DIR_PATH, "instance2.json"),
100+
],
98101
"error_format": "{error.message}",
102+
"output": "plain",
99103
},
100104
stdout=stdout,
101105
stderr=stderr,
@@ -110,15 +114,18 @@ def test_unsuccessful_validation(self):
110114
exit_code = cli.run(
111115
{
112116
"validator": fake_validator([error]),
113-
"schema": {},
114-
"instances": [1],
117+
"schema": os.path.join(JSON_DIR_PATH, "schema2.json"),
118+
"instances": [
119+
os.path.join(JSON_DIR_PATH, "instance1.json"),
120+
],
115121
"error_format": "{error.instance} - {error.message}",
122+
"output": "plain",
116123
},
117124
stdout=stdout,
118125
stderr=stderr,
119126
)
120127
self.assertFalse(stdout.getvalue())
121-
self.assertEqual(stderr.getvalue(), "1 - I am an error!")
128+
self.assertEqual(stderr.getvalue(), "1 - I am an error!\n")
122129
self.assertEqual(exit_code, 1)
123130

124131
def test_unsuccessful_validation_multiple_instances(self):
@@ -131,15 +138,19 @@ def test_unsuccessful_validation_multiple_instances(self):
131138
exit_code = cli.run(
132139
{
133140
"validator": fake_validator(first_errors, second_errors),
134-
"schema": {},
135-
"instances": [1, 2],
136-
"error_format": "{error.instance} - {error.message}\t",
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+
],
146+
"error_format": "{error.instance} - {error.message}",
147+
"output": "plain",
137148
},
138149
stdout=stdout,
139150
stderr=stderr,
140151
)
141152
self.assertFalse(stdout.getvalue())
142-
self.assertEqual(stderr.getvalue(), "1 - 9\t1 - 8\t2 - 7\t")
153+
self.assertEqual(stderr.getvalue(), "1 - 9\n1 - 8\n2 - 7\n")
143154
self.assertEqual(exit_code, 1)
144155

145156
def test_license(self):
@@ -162,9 +173,10 @@ def test_piping(self):
162173
exit_code = cli.run(
163174
{
164175
"validator": fake_validator(),
165-
"schema": {},
176+
"schema": os.path.join(JSON_DIR_PATH, "schema2.json"),
166177
"instances": [],
167178
"error_format": "{error.message}",
179+
"output": "plain",
168180
},
169181
stdout=stdout,
170182
stderr=stderr,

0 commit comments

Comments
 (0)