summaryrefslogtreecommitdiff
path: root/jsonschema/tests
diff options
context:
space:
mode:
authorwilson chen <willson.chenwx@gmail.com>2020-08-29 10:13:32 +0800
committerGitHub <noreply@github.com>2020-08-29 10:13:32 +0800
commitba914b00e2c150fc722b86e0b53cf0aa82652de7 (patch)
tree743f3c3b3c0a777acfab5775f6923c6931e57294 /jsonschema/tests
parent03868b3316ba2f17fbd091df91bcb06bd89a8552 (diff)
parent5fc5b1450a0cdb903ce226105b18169a954dec55 (diff)
downloadjsonschema-ba914b00e2c150fc722b86e0b53cf0aa82652de7.tar.gz
Merge branch 'master' into fix_issue669
Diffstat (limited to 'jsonschema/tests')
-rw-r--r--jsonschema/tests/_helpers.py2
-rw-r--r--jsonschema/tests/test_cli.py81
-rw-r--r--jsonschema/tests/test_format.py3
-rw-r--r--jsonschema/tests/test_jsonschema_test_suite.py123
4 files changed, 149 insertions, 60 deletions
diff --git a/jsonschema/tests/_helpers.py b/jsonschema/tests/_helpers.py
index 761b404..51fff4f 100644
--- a/jsonschema/tests/_helpers.py
+++ b/jsonschema/tests/_helpers.py
@@ -1,5 +1,5 @@
-from io import StringIO
from contextlib import contextmanager
+from io import StringIO
import sys
diff --git a/jsonschema/tests/test_cli.py b/jsonschema/tests/test_cli.py
index 8d2fdf7..08e92c4 100644
--- a/jsonschema/tests/test_cli.py
+++ b/jsonschema/tests/test_cli.py
@@ -7,9 +7,8 @@ import json
import os
import subprocess
import sys
-import tempfile
-from jsonschema import Draft4Validator, cli, __version__
+from jsonschema import Draft4Validator, Draft7Validator, __version__, cli
from jsonschema.exceptions import SchemaError, ValidationError
from jsonschema.tests._helpers import captured_output
from jsonschema.validators import _LATEST_VERSION, validate
@@ -676,7 +675,7 @@ class TestCLI(TestCase):
stderr="",
)
- def test_successful_validation__of_just_the_schema_pretty_output(self):
+ def test_successful_validation_of_just_the_schema_pretty_output(self):
self.assertOutputs(
files=dict(some_schema="{}", some_instance="{}"),
argv=["--output", "pretty", "-i", "some_instance", "some_schema"],
@@ -684,35 +683,54 @@ class TestCLI(TestCase):
stderr="",
)
- def test_successful_validation_with_specifying_base_uri(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()
+ 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
+ # is hidden inside the CLI, so guard that that's the case, and
+ # this test will have to be updated when versions change until
+ # we can think of a better way to ensure this behavior.
+ self.assertIs(Draft7Validator, _LATEST_VERSION)
self.assertOutputs(
- files=dict(some_schema=schema, some_instance='{"KEY1": "1"}'),
- argv=["-i", "some_instance", "--base-uri", "..", "some_schema"],
+ files=dict(some_schema='{"const": "check"}', some_instance='"a"'),
+ argv=["-i", "some_instance", "some_schema"],
+ exit_code=1,
stdout="",
- stderr="",
+ stderr="a: 'check' was expected\n",
+ )
+
+ def test_it_validates_using_draft7_when_specified(self):
+ """
+ Specifically, `const` validation applies for Draft 7.
+ """
+ schema = """
+ {
+ "$schema": "http://json-schema.org/draft-07/schema#",
+ "const": "check"
+ }
+ """
+ instance = '"foo"'
+ self.assertOutputs(
+ files=dict(some_schema=schema, some_instance=instance),
+ argv=["-i", "some_instance", "some_schema"],
+ exit_code=1,
+ stdout="",
+ stderr="foo: 'check' was expected\n",
)
- def test_real_validator(self):
+ def test_it_validates_using_draft4_when_specified(self):
+ """
+ Specifically, `const` validation *does not* apply for Draft 4.
+ """
+ schema = """
+ {
+ "$schema": "http://json-schema.org/draft-04/schema#",
+ "const": "check"
+ }
+ """
+ instance = '"foo"'
self.assertOutputs(
- files=dict(some_schema='{"minimum": 30}', some_instance="37"),
+ files=dict(some_schema=schema, some_instance=instance),
argv=["-i", "some_instance", "some_schema"],
stdout="",
stderr="",
@@ -744,15 +762,6 @@ class TestParser(TestCase):
)
self.assertIs(arguments["validator"], Draft4Validator)
- def test_latest_validator_is_the_default(self):
- arguments = cli.parse_args(
- [
- "--instance", "mem://some/instance",
- "mem://some/schema",
- ]
- )
- self.assertIs(arguments["validator"], _LATEST_VERSION)
-
def test_unknown_output(self):
# Avoid the help message on stdout
with captured_output() as (stdout, stderr):
@@ -809,4 +818,4 @@ class TestCLIIntegration(TestCase):
[sys.executable, "-m", "jsonschema", "--help"],
stderr=subprocess.STDOUT,
)
- self.assertEqual(output, output_for_help)
+ self.assertEqual(output, output_for_help) \ No newline at end of file
diff --git a/jsonschema/tests/test_format.py b/jsonschema/tests/test_format.py
index 254985f..6dba484 100644
--- a/jsonschema/tests/test_format.py
+++ b/jsonschema/tests/test_format.py
@@ -4,10 +4,9 @@ Tests for the parts of jsonschema related to the :validator:`format` property.
from unittest import TestCase
-from jsonschema import FormatError, ValidationError, FormatChecker
+from jsonschema import FormatChecker, FormatError, ValidationError
from jsonschema.validators import Draft4Validator
-
BOOM = ValueError("Boom!")
BANG = ZeroDivisionError("Bang!")
diff --git a/jsonschema/tests/test_jsonschema_test_suite.py b/jsonschema/tests/test_jsonschema_test_suite.py
index e561f3e..0f9698a 100644
--- a/jsonschema/tests/test_jsonschema_test_suite.py
+++ b/jsonschema/tests/test_jsonschema_test_suite.py
@@ -23,7 +23,6 @@ from jsonschema.tests._helpers import bug
from jsonschema.tests._suite import Suite
from jsonschema.validators import _DEPRECATED_DEFAULT_TYPES, create
-
SUITE = Suite()
DRAFT3 = SUITE.version(name="draft3")
DRAFT4 = SUITE.version(name="draft4")
@@ -88,6 +87,24 @@ else:
return
+if sys.version_info < (3, 7):
+ message = "datetime.date.fromisoformat is new in 3.7+"
+
+ def missing_date_fromisoformat(test):
+ return skip(
+ message=message,
+ subject="date",
+ description="invalidates non-padded month dates",
+ )(test) or skip(
+ message=message,
+ subject="date",
+ description="invalidates non-padded day dates",
+ )(test)
+else:
+ def missing_date_fromisoformat(test):
+ return
+
+
TestDraft3 = DRAFT3.to_unittest_testcase(
DRAFT3.tests(),
DRAFT3.format_tests(),
@@ -98,6 +115,7 @@ TestDraft3 = DRAFT3.to_unittest_testcase(
format_checker=draft3_format_checker,
skip=lambda test: (
narrow_unicode_build(test)
+ or missing_date_fromisoformat(test)
or missing_format(draft3_format_checker)(test)
or complex_email_validation(test)
or skip(
@@ -106,11 +124,6 @@ TestDraft3 = DRAFT3.to_unittest_testcase(
description="case-insensitive T and Z",
)(test)
or skip(
- message=bug(),
- subject="host-name",
- description="ends with hyphen",
- )(test)
- or skip(
message=bug(686),
subject="uniqueItems",
description="[0] and [false] are unique",
@@ -130,6 +143,16 @@ TestDraft3 = DRAFT3.to_unittest_testcase(
subject="uniqueItems",
description="nested [1] and [true] are unique",
)(test)
+ or skip(
+ message=bug(686),
+ subject="uniqueItems",
+ description='{"a": false} and {"a": 0} are unique',
+ )(test)
+ or skip(
+ message=bug(686),
+ subject="uniqueItems",
+ description='{"a": true} and {"a": 1} are unique',
+ )(test)
),
)
@@ -144,6 +167,7 @@ TestDraft4 = DRAFT4.to_unittest_testcase(
format_checker=draft4_format_checker,
skip=lambda test: (
narrow_unicode_build(test)
+ or missing_date_fromisoformat(test)
or missing_format(draft4_format_checker)(test)
or complex_email_validation(test)
or skip(
@@ -181,11 +205,6 @@ TestDraft4 = DRAFT4.to_unittest_testcase(
description="case-insensitive T and Z",
)(test)
or skip(
- message=bug(),
- subject="hostname",
- description="ends with hyphen",
- )(test)
- or skip(
message=bug(686),
subject="uniqueItems",
description="[0] and [false] are unique",
@@ -205,6 +224,16 @@ TestDraft4 = DRAFT4.to_unittest_testcase(
subject="uniqueItems",
description="nested [1] and [true] are unique",
)(test)
+ or skip(
+ message=bug(686),
+ subject="uniqueItems",
+ description='{"a": false} and {"a": 0} are unique',
+ )(test)
+ or skip(
+ message=bug(686),
+ subject="uniqueItems",
+ description='{"a": true} and {"a": 1} are unique',
+ )(test)
),
)
@@ -218,6 +247,7 @@ TestDraft6 = DRAFT6.to_unittest_testcase(
format_checker=draft6_format_checker,
skip=lambda test: (
narrow_unicode_build(test)
+ or missing_date_fromisoformat(test)
or missing_format(draft6_format_checker)(test)
or complex_email_validation(test)
or skip(
@@ -255,11 +285,6 @@ TestDraft6 = DRAFT6.to_unittest_testcase(
description="case-insensitive T and Z",
)(test)
or skip(
- message=bug(),
- subject="hostname",
- description="ends with hyphen",
- )(test)
- or skip(
message=bug(686),
subject="uniqueItems",
description="[0] and [false] are unique",
@@ -279,6 +304,36 @@ TestDraft6 = DRAFT6.to_unittest_testcase(
subject="uniqueItems",
description="nested [1] and [true] are unique",
)(test)
+ or skip(
+ message=bug(686),
+ subject="uniqueItems",
+ description='{"a": false} and {"a": 0} are unique',
+ )(test)
+ or skip(
+ message=bug(686),
+ subject="uniqueItems",
+ description='{"a": true} and {"a": 1} are unique',
+ )(test)
+ or skip(
+ message=bug(686),
+ subject="const",
+ case_description="const with [false] does not match [0]",
+ )(test)
+ or skip(
+ message=bug(686),
+ subject="const",
+ case_description="const with [true] does not match [1]",
+ )(test)
+ or skip(
+ message=bug(686),
+ subject="const",
+ case_description='const with {"a": false} does not match {"a": 0}',
+ )(test)
+ or skip(
+ message=bug(686),
+ subject="const",
+ case_description='const with {"a": true} does not match {"a": 1}',
+ )(test)
),
)
@@ -293,6 +348,7 @@ TestDraft7 = DRAFT7.to_unittest_testcase(
format_checker=draft7_format_checker,
skip=lambda test: (
narrow_unicode_build(test)
+ or missing_date_fromisoformat(test)
or missing_format(draft7_format_checker)(test)
or complex_email_validation(test)
or skip(
@@ -330,11 +386,6 @@ TestDraft7 = DRAFT7.to_unittest_testcase(
description="case-insensitive T and Z",
)(test)
or skip(
- message=bug(),
- subject="hostname",
- description="ends with hyphen",
- )(test)
- or skip(
message=bug(593),
subject="content",
valid=False,
@@ -376,6 +427,36 @@ TestDraft7 = DRAFT7.to_unittest_testcase(
subject="uniqueItems",
description="nested [1] and [true] are unique",
)(test)
+ or skip(
+ message=bug(686),
+ subject="uniqueItems",
+ description='{"a": false} and {"a": 0} are unique',
+ )(test)
+ or skip(
+ message=bug(686),
+ subject="uniqueItems",
+ description='{"a": true} and {"a": 1} are unique',
+ )(test)
+ or skip(
+ message=bug(686),
+ subject="const",
+ case_description="const with [false] does not match [0]",
+ )(test)
+ or skip(
+ message=bug(686),
+ subject="const",
+ case_description="const with [true] does not match [1]",
+ )(test)
+ or skip(
+ message=bug(686),
+ subject="const",
+ case_description='const with {"a": false} does not match {"a": 0}',
+ )(test)
+ or skip(
+ message=bug(686),
+ subject="const",
+ case_description='const with {"a": true} does not match {"a": 1}',
+ )(test)
),
)