Skip to content

Commit 040b95a

Browse files
committed
Optimizing test cases and add invalid '--base-uri' test
1 parent a0ec5e9 commit 040b95a

File tree

1 file changed

+47
-25
lines changed

1 file changed

+47
-25
lines changed

jsonschema/tests/test_cli.py

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
import tempfile
1111

1212
from jsonschema import Draft4Validator, Draft7Validator, __version__, cli
13-
from jsonschema.exceptions import SchemaError, ValidationError
13+
from jsonschema.exceptions import (
14+
RefResolutionError,
15+
SchemaError,
16+
ValidationError,
17+
)
1418
from jsonschema.tests._helpers import captured_output
1519
from jsonschema.validators import _LATEST_VERSION, validate
1620

@@ -685,72 +689,90 @@ def test_successful_validation_of_just_the_schema_pretty_output(self):
685689
)
686690

687691
def test_successful_validate_with_specifying_base_uri_absolute_path(self):
688-
absolute_path = os.getcwd()
689692
try:
690693
schema_file = tempfile.NamedTemporaryFile(
691694
mode='w+',
692-
prefix='schema',
693-
suffix='.json',
694-
dir=absolute_path,
695695
delete=False
696696
)
697697
self.addCleanup(os.remove, schema_file.name)
698698
schema = """
699699
{"type": "object", "properties": {"KEY1":
700-
{"$ref": %s%s#definitions/schemas"}},
701-
"definitions": {"schemas": {"type": "string"}}}
702-
""" % ("\"", os.path.basename(schema_file.name))
703-
schema_file.write(schema)
700+
{"$ref": "%s#definitions/num"}}}
701+
""" % (os.path.basename(schema_file.name))
702+
tmp_schema = """{"definitions": {"num": {"type": "integer"}}}"""
703+
schema_file.write(tmp_schema)
704704
finally:
705705
schema_file.close()
706706

707707
file_prefix = "file:///{}/" if "nt" == os.name else "file://{}/"
708-
absolute_path = file_prefix.format(absolute_path)
708+
absolute_dir_path = file_prefix.format(
709+
os.path.dirname(schema_file.name)
710+
)
709711
self.assertOutputs(
710-
files=dict(some_schema=schema, some_instance='{"KEY1": "1"}'),
712+
files=dict(some_schema=schema, some_instance='{"KEY1": 1}'),
711713
argv=[
712714
"-i", "some_instance",
713-
"--base-uri", absolute_path,
715+
"--base-uri", absolute_dir_path,
714716
"some_schema",
715717
],
716718
stdout="",
717719
stderr="",
718720
)
719721

720722
def test_failure_validate_with_specifying_base_uri_absolute_path(self):
721-
absolute_path = os.getcwd()
722723
try:
723724
schema_file = tempfile.NamedTemporaryFile(
724725
mode='w+',
725-
prefix='schema',
726-
suffix='.json',
727-
dir=absolute_path,
728726
delete=False
729727
)
730728
self.addCleanup(os.remove, schema_file.name)
731729
schema = """
732-
{"type": "object", "properties": {"KEY1":
733-
{"$ref": %s%s#definitions/schemas"}},
734-
"definitions": {"schemas": {"type": "string"}}}
735-
""" % ("\"", os.path.basename(schema_file.name))
736-
schema_file.write(schema)
730+
{"type": "object", "properties": {"KEY1":
731+
{"$ref": "%s#definitions/num"}}}
732+
""" % (os.path.basename(schema_file.name))
733+
tmp_schema = """{"definitions": {"num": {"type": "integer"}}}"""
734+
schema_file.write(tmp_schema)
737735
finally:
738736
schema_file.close()
739737

740738
file_prefix = "file:///{}/" if "nt" == os.name else "file://{}/"
741-
absolute_path = file_prefix.format(absolute_path)
739+
absolute_dir_path = file_prefix.format(
740+
os.path.dirname(schema_file.name)
741+
)
742742
self.assertOutputs(
743-
files=dict(some_schema=schema, some_instance='{"KEY1": 1}'),
743+
files=dict(some_schema=schema, some_instance='{"KEY1": "1"}'),
744744
argv=[
745745
"-i", "some_instance",
746-
"--base-uri", absolute_path,
746+
"--base-uri", absolute_dir_path,
747747
"some_schema",
748748
],
749749
exit_code=1,
750750
stdout="",
751-
stderr="1: 1 is not of type 'string'\n",
751+
stderr="1: '1' is not of type 'integer'\n",
752752
)
753753

754+
def test_validate_with_specifying_invalid_base_uri(self):
755+
schema = """
756+
{"type": "object", "properties": {"KEY1":
757+
{"$ref": "foo.json#definitions/num"}}}
758+
"""
759+
instance = '{"KEY1": 1}'
760+
761+
with self.assertRaises(RefResolutionError) as e:
762+
self.assertOutputs(
763+
files=dict(
764+
some_schema=schema,
765+
some_instance=instance,
766+
),
767+
argv=[
768+
"-i", "some_instance",
769+
"--base-uri", ".",
770+
"some_schema",
771+
],
772+
)
773+
error = str(e.exception)
774+
self.assertEqual(error, "unknown url type: 'foo.json'")
775+
754776
def test_it_validates_using_the_latest_validator_when_unspecified(self):
755777
# There isn't a better way now I can think of to ensure that the
756778
# latest version was used, given that the call to validator_for

0 commit comments

Comments
 (0)