Skip to content

Commit 1bfbb90

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

File tree

1 file changed

+45
-26
lines changed

1 file changed

+45
-26
lines changed

jsonschema/tests/test_cli.py

Lines changed: 45 additions & 26 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,87 @@ 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 = """
699-
{"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)
699+
{"$ref": "%s#definitions/num"}
700+
""" % (os.path.basename(schema_file.name))
701+
tmp_schema = """{"definitions": {"num": {"type": "integer"}}}"""
702+
schema_file.write(tmp_schema)
704703
finally:
705704
schema_file.close()
706705

707706
file_prefix = "file:///{}/" if "nt" == os.name else "file://{}/"
708-
absolute_path = file_prefix.format(absolute_path)
707+
absolute_dir_path = file_prefix.format(
708+
os.path.dirname(schema_file.name)
709+
)
709710
self.assertOutputs(
710-
files=dict(some_schema=schema, some_instance='{"KEY1": "1"}'),
711+
files=dict(some_schema=schema, some_instance='1'),
711712
argv=[
712713
"-i", "some_instance",
713-
"--base-uri", absolute_path,
714+
"--base-uri", absolute_dir_path,
714715
"some_schema",
715716
],
716717
stdout="",
717718
stderr="",
718719
)
719720

720721
def test_failure_validate_with_specifying_base_uri_absolute_path(self):
721-
absolute_path = os.getcwd()
722722
try:
723723
schema_file = tempfile.NamedTemporaryFile(
724724
mode='w+',
725-
prefix='schema',
726-
suffix='.json',
727-
dir=absolute_path,
728725
delete=False
729726
)
730727
self.addCleanup(os.remove, schema_file.name)
731728
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)
729+
{"$ref": "%s#definitions/num"}
730+
""" % (os.path.basename(schema_file.name))
731+
tmp_schema = """{"definitions": {"num": {"type": "integer"}}}"""
732+
schema_file.write(tmp_schema)
737733
finally:
738734
schema_file.close()
739735

740736
file_prefix = "file:///{}/" if "nt" == os.name else "file://{}/"
741-
absolute_path = file_prefix.format(absolute_path)
737+
absolute_dir_path = file_prefix.format(
738+
os.path.dirname(schema_file.name)
739+
)
742740
self.assertOutputs(
743-
files=dict(some_schema=schema, some_instance='{"KEY1": 1}'),
741+
files=dict(some_schema=schema, some_instance='"1"'),
744742
argv=[
745743
"-i", "some_instance",
746-
"--base-uri", absolute_path,
744+
"--base-uri", absolute_dir_path,
747745
"some_schema",
748746
],
749747
exit_code=1,
750748
stdout="",
751-
stderr="1: 1 is not of type 'string'\n",
749+
stderr="1: '1' is not of type 'integer'\n",
752750
)
753751

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

0 commit comments

Comments
 (0)