summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian Berman <Julian@GrayVines.com>2020-10-15 17:30:38 -0400
committerJulian Berman <Julian@GrayVines.com>2020-10-15 17:30:38 -0400
commitd106511d9fe06d8744ba7ecae5ab72cc19247e9f (patch)
treef6aa5a837f11ef331333b2065190290046a708a5
parent9502934ca18004712c848f0463d14a014b685420 (diff)
downloadjsonschema-d106511d9fe06d8744ba7ecae5ab72cc19247e9f.tar.gz
And now tidy up the tests.
-rw-r--r--jsonschema/tests/test_cli.py90
1 files changed, 45 insertions, 45 deletions
diff --git a/jsonschema/tests/test_cli.py b/jsonschema/tests/test_cli.py
index 4ba095f..22528fe 100644
--- a/jsonschema/tests/test_cli.py
+++ b/jsonschema/tests/test_cli.py
@@ -1,5 +1,6 @@
from io import StringIO
from json import JSONDecodeError
+from pathlib import Path
from textwrap import dedent
from unittest import TestCase
import errno
@@ -688,60 +689,40 @@ class TestCLI(TestCase):
stderr="",
)
- def test_successful_validate_with_specifying_base_uri_absolute_path(self):
- try:
- schema_file = tempfile.NamedTemporaryFile(
- mode='w+',
- delete=False
- )
- self.addCleanup(os.remove, schema_file.name)
- schema = """
- {"$ref": "%s#definitions/num"}
- """ % (os.path.basename(schema_file.name))
- tmp_schema = """{"definitions": {"num": {"type": "integer"}}}"""
- schema_file.write(tmp_schema)
- finally:
- schema_file.close()
+ def test_successful_validation_via_explicit_base_uri(self):
+ ref_schema_file = tempfile.NamedTemporaryFile(delete=False)
+ self.addCleanup(os.remove, ref_schema_file.name)
+
+ ref_path = Path(ref_schema_file.name)
+ ref_path.write_text('{"definitions": {"num": {"type": "integer"}}}')
+
+ schema = f'{{"$ref": "{ref_path.name}#definitions/num"}}'
- file_prefix = "file:///{}/" if "nt" == os.name else "file://{}/"
- absolute_dir_path = file_prefix.format(
- os.path.dirname(schema_file.name)
- )
self.assertOutputs(
files=dict(some_schema=schema, some_instance='1'),
argv=[
"-i", "some_instance",
- "--base-uri", absolute_dir_path,
+ "--base-uri", ref_path.parent.as_uri() + "/",
"some_schema",
],
stdout="",
stderr="",
)
- def test_failure_validate_with_specifying_base_uri_absolute_path(self):
- try:
- schema_file = tempfile.NamedTemporaryFile(
- mode='w+',
- delete=False
- )
- self.addCleanup(os.remove, schema_file.name)
- schema = """
- {"$ref": "%s#definitions/num"}
- """ % (os.path.basename(schema_file.name))
- tmp_schema = """{"definitions": {"num": {"type": "integer"}}}"""
- schema_file.write(tmp_schema)
- finally:
- schema_file.close()
+ def test_unsuccessful_validation_via_explicit_base_uri(self):
+ ref_schema_file = tempfile.NamedTemporaryFile(delete=False)
+ self.addCleanup(os.remove, ref_schema_file.name)
+
+ ref_path = Path(ref_schema_file.name)
+ ref_path.write_text('{"definitions": {"num": {"type": "integer"}}}')
+
+ schema = f'{{"$ref": "{ref_path.name}#definitions/num"}}'
- file_prefix = "file:///{}/" if "nt" == os.name else "file://{}/"
- absolute_dir_path = file_prefix.format(
- os.path.dirname(schema_file.name)
- )
self.assertOutputs(
files=dict(some_schema=schema, some_instance='"1"'),
argv=[
"-i", "some_instance",
- "--base-uri", absolute_dir_path,
+ "--base-uri", ref_path.parent.as_uri() + "/",
"some_schema",
],
exit_code=1,
@@ -749,11 +730,28 @@ class TestCLI(TestCase):
stderr="1: '1' is not of type 'integer'\n",
)
- def test_validate_with_specifying_invalid_base_uri(self):
- schema = """
- {"$ref": "foo.json#definitions/num"}
- """
- instance = '1'
+ def test_nonexistent_file_with_explicit_base_uri(self):
+ schema = '{"$ref": "someNonexistentFile.json#definitions/num"}'
+ instance = "1"
+
+ with self.assertRaises(RefResolutionError) as e:
+ self.assertOutputs(
+ files=dict(
+ some_schema=schema,
+ some_instance=instance,
+ ),
+ argv=[
+ "-i", "some_instance",
+ "--base-uri", Path.cwd().as_uri(),
+ "some_schema",
+ ],
+ )
+ error = str(e.exception)
+ self.assertIn("/someNonexistentFile.json'", error)
+
+ def test_invalid_exlicit_base_uri(self):
+ schema = '{"$ref": "foo.json#definitions/num"}'
+ instance = "1"
with self.assertRaises(RefResolutionError) as e:
self.assertOutputs(
@@ -763,12 +761,14 @@ class TestCLI(TestCase):
),
argv=[
"-i", "some_instance",
- "--base-uri", ".",
+ "--base-uri", "not@UR1",
"some_schema",
],
)
error = str(e.exception)
- self.assertEqual(error, "unknown url type: 'foo.json'")
+ self.assertEqual(
+ error, "unknown url type: 'foo.json'",
+ )
def test_it_validates_using_the_latest_validator_when_unspecified(self):
# There isn't a better way now I can think of to ensure that the