summaryrefslogtreecommitdiff
path: root/jsonschema/tests/test_cli.py
diff options
context:
space:
mode:
Diffstat (limited to 'jsonschema/tests/test_cli.py')
-rw-r--r--jsonschema/tests/test_cli.py129
1 files changed, 128 insertions, 1 deletions
diff --git a/jsonschema/tests/test_cli.py b/jsonschema/tests/test_cli.py
index 2bf0ac4..3b73dce 100644
--- a/jsonschema/tests/test_cli.py
+++ b/jsonschema/tests/test_cli.py
@@ -684,7 +684,7 @@ class TestCLI(TestCase):
stderr="",
)
- def test_successful_validation_with_specifying_base_uri(self):
+ def test_successful_validate_with_specifying_base_uri_relative_path(self):
try:
schema_file = tempfile.NamedTemporaryFile(
mode='w+',
@@ -710,6 +710,133 @@ class TestCLI(TestCase):
stderr="",
)
+ def test_failure_validate_with_specifying_base_uri_relative_path(self):
+ try:
+ schema_file = tempfile.NamedTemporaryFile(
+ mode='w+',
+ prefix='schema',
+ suffix='.json',
+ dir='..',
+ delete=False
+ )
+ self.addCleanup(os.remove, schema_file.name)
+ schema = """
+ {"type": "object", "properties": {"KEY1":
+ {"$ref": %s%s#definitions/schemas"}},
+ "definitions": {"schemas": {"type": "string"}}}
+ """ % ("\"", os.path.basename(schema_file.name))
+ schema_file.write(schema)
+ finally:
+ schema_file.close()
+
+ self.assertOutputs(
+ files=dict(some_schema=schema, some_instance='{"KEY1": 1}'),
+ argv=["-i", "some_instance", "--base-uri", "..", "some_schema"],
+ exit_code=1,
+ stdout="",
+ stderr="1: 1 is not of type 'string'\n",
+ )
+
+ def test_successful_validate_with_specifying_base_uri_absolute_path(self):
+ absolute_path = os.getcwd()
+ try:
+ schema_file = tempfile.NamedTemporaryFile(
+ mode='w+',
+ prefix='schema',
+ suffix='.json',
+ dir=absolute_path,
+ delete=False
+ )
+ self.addCleanup(os.remove, schema_file.name)
+ schema = """
+ {"type": "object", "properties": {"KEY1":
+ {"$ref": %s%s#definitions/schemas"}},
+ "definitions": {"schemas": {"type": "string"}}}
+ """ % ("\"", os.path.basename(schema_file.name))
+ schema_file.write(schema)
+ finally:
+ schema_file.close()
+
+ self.assertOutputs(
+ files=dict(some_schema=schema, some_instance='{"KEY1": "1"}'),
+ argv=[
+ "-i", "some_instance",
+ "--base-uri", absolute_path,
+ "some_schema",
+ ],
+ stdout="",
+ stderr="",
+ )
+
+ def test_failure_validate_with_specifying_base_uri_absolute_path(self):
+ absolute_path = os.getcwd()
+ try:
+ schema_file = tempfile.NamedTemporaryFile(
+ mode='w+',
+ prefix='schema',
+ suffix='.json',
+ dir=absolute_path,
+ delete=False
+ )
+ self.addCleanup(os.remove, schema_file.name)
+ schema = """
+ {"type": "object", "properties": {"KEY1":
+ {"$ref": %s%s#definitions/schemas"}},
+ "definitions": {"schemas": {"type": "string"}}}
+ """ % ("\"", os.path.basename(schema_file.name))
+ schema_file.write(schema)
+ finally:
+ schema_file.close()
+
+ self.assertOutputs(
+ files=dict(some_schema=schema, some_instance='{"KEY1": 1}'),
+ argv=[
+ "-i", "some_instance",
+ "--base-uri", absolute_path,
+ "some_schema",
+ ],
+ exit_code=1,
+ stdout="",
+ stderr="1: 1 is not of type 'string'\n",
+ )
+
+ def test_successful_validate_with_specifying_base_uri_remote_path(self):
+ schema = """
+ {"type": "object", "properties": {
+ "KEY1":{"$ref": "organization.json"}}}
+ """
+ self.assertOutputs(
+ files=dict(some_schema=schema,
+ some_instance='{"KEY1": {"name": "remote"}}'
+ ),
+ argv=[
+ "-i", "some_instance",
+ "--base-uri", "https://project-open-data.cio.gov/v1.1/schema/",
+ "some_schema",
+ ],
+ stdout="",
+ stderr="",
+ )
+
+ def test_failure_validate_with_specifying_base_uri_remote_path(self):
+ schema = """
+ {"type": "object", "properties": {
+ "KEY1":{"$ref": "organization.json"}}}
+ """
+ self.assertOutputs(
+ files=dict(some_schema=schema,
+ some_instance='{"KEY1": {"fail": "remote"}}'
+ ),
+ argv=[
+ "-i", "some_instance",
+ "--base-uri", "https://project-open-data.cio.gov/v1.1/schema/",
+ "some_schema",
+ ],
+ exit_code=1,
+ stdout="",
+ stderr="{'fail': 'remote'}: 'name' is a required property\n",
+ )
+
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
# latest version was used, given that the call to validator_for