summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian Berman <Julian@GrayVines.com>2013-04-20 21:21:09 -0400
committerJulian Berman <Julian@GrayVines.com>2013-04-20 21:21:17 -0400
commita27655cdf0422e81c528ee24c6ebbfad8bb49a06 (patch)
tree6a14476e16d5b14141454cc9e07d3778e3bf0c19
parent7151a9c83bf319e420972ebb055cc4f0361403f1 (diff)
downloadjsonschema-a27655cdf0422e81c528ee24c6ebbfad8bb49a06.tar.gz
Fix doctests.
-rw-r--r--docs/errors.rst127
-rw-r--r--tox.ini10
2 files changed, 74 insertions, 63 deletions
diff --git a/docs/errors.rst b/docs/errors.rst
index 65bdbf4..7e0dde2 100644
--- a/docs/errors.rst
+++ b/docs/errors.rst
@@ -76,26 +76,31 @@ raised.
These attributes can be clarified with a short example:
-.. code-block:: python
-
- >>> schema = {
- ... "items": {
- ... "anyOf": [
- ... {"type": "string", "maxLength": 2},
- ... {"type": "integer", "minimum": 5}
- ... ]
- ... }
- ... }
- >>> instance = [{}, 3, "foo"]
- >>> v = Draft4Validator(schema)
- >>> errors = sorted(v.iter_errors(instance), key=lambda e: e.path)
-
-The error messages in this situation are not very helpful on their own:
-
-.. code-block:: python
-
- >>> for error in errors:
- ... print(error.message)
+.. testcode::
+
+ schema = {
+ "items": {
+ "anyOf": [
+ {"type": "string", "maxLength": 2},
+ {"type": "integer", "minimum": 5}
+ ]
+ }
+ }
+ instance = [{}, 3, "foo"]
+ v = Draft4Validator(schema)
+ errors = sorted(v.iter_errors(instance), key=lambda e: e.path)
+
+The error messages in this situation are not very helpful on their own.
+
+.. testcode::
+
+ for error in errors:
+ print(error.message)
+
+outputs:
+
+.. testoutput::
+
{} is not valid under any of the given schemas
3 is not valid under any of the given schemas
'foo' is not valid under any of the given schemas
@@ -105,10 +110,13 @@ out which elements in the instance correspond to each of the errors. In
this example, :attr:`~ValidationError.path` will have only one element, which
will be the index in our list.
-.. code-block:: python
+.. testcode::
+
+ for error in errors:
+ print(list(error.path))
+
+.. testoutput::
- >>> for error in errors:
- ... print(list(error.path))
[0]
[1]
[2]
@@ -126,11 +134,14 @@ exactly in the schema each of these errors come from. In the case of sub-errors
from the :attr:`~ValidationError.context` attribute, this path will be relative
to the :attr:`~ValidationError.schema_path` of the parent error.
-.. code-block:: python
+.. testcode::
+
+ for error in errors:
+ for suberror in sorted(error.context, key=lambda e: e.schema_path):
+ print(list(suberror.schema_path), suberror.message, sep=", ")
+
+.. testoutput::
- >>> for error in errors:
- ... for suberror in sorted(error.context, key=lambda e: e.schema_path):
- ... print(list(suberror.schema_path), suberror.message, sep=", ")
[0, 'type'], {} is not of type 'string'
[1, 'type'], {} is not of type 'integer'
[0, 'type'], 3 is not of type 'string'
@@ -141,16 +152,20 @@ to the :attr:`~ValidationError.schema_path` of the parent error.
The string representation of an error combines some of these attributes for
easier debugging.
-.. code-block:: python
+.. testcode::
+
+ print(errors[1])
- >>> print(errors[1])
- ValidationError: 3 is not valid under any of the given schemas
- Failed validating 'anyOf' in schema['items']:
- {'anyOf': [{'maxLength': 2, 'type': 'string'},
- {'minimum': 5, 'type': 'integer'}]}
- On instance[1]:
- 3
- <BLANKLINE>
+.. testoutput::
+
+ 3 is not valid under any of the given schemas
+
+ Failed validating 'anyOf' in schema['items']:
+ {'anyOf': [{'maxLength': 2, 'type': 'string'},
+ {'minimum': 5, 'type': 'integer'}]}
+
+ On instance[1]:
+ 3
ErrorTrees
----------
@@ -164,23 +179,25 @@ failed when validating a given instance, you probably will want to do so using
Consider the following example:
-.. code-block:: python
+.. testcode::
- >>> from jsonschema import ErrorTree, Draft3Validator
- >>> schema = {
- ... "type" : "array",
- ... "items" : {"type" : "number", "enum" : [1, 2, 3]},
- ... "minItems" : 3,
- ... }
- >>> instance = ["spam", 2]
+ schema = {
+ "type" : "array",
+ "items" : {"type" : "number", "enum" : [1, 2, 3]},
+ "minItems" : 3,
+ }
+ instance = ["spam", 2]
For clarity's sake, the given instance has three errors under this schema:
-.. code-block:: python
+.. testcode::
+
+ v = Draft3Validator(schema)
+ for error in sorted(v.iter_errors(["spam", 2]), key=str):
+ print(error.message)
+
+.. testoutput::
- >>> v = Draft3Validator(schema)
- >>> for error in sorted(v.iter_errors(["spam", 2]), key=str):
- ... print(error)
'spam' is not of type 'number'
'spam' is not one of [1, 2, 3]
['spam', 2] is too short
@@ -188,9 +205,9 @@ For clarity's sake, the given instance has three errors under this schema:
Let's construct an :class:`ErrorTree` so that we can query the errors a bit
more easily than by just iterating over the error objects.
-.. code-block:: python
+.. testcode::
- >>> tree = ErrorTree(v.iter_errors(instance))
+ tree = ErrorTree(v.iter_errors(instance))
As you can see, :class:`ErrorTree` takes an iterable of
:class:`ValidationError`\s when constructing a tree so you can directly pass it
@@ -200,7 +217,7 @@ the return value of a validator's :attr:`~IValidator.iter_errors` method.
might want to perform is to check whether a given element in our instance
failed validation. We do so using the :keyword:`in` operator:
-.. code-block:: python
+.. doctest::
>>> 0 in tree
True
@@ -215,7 +232,7 @@ it was valid).
If we want to see which errors a child had, we index into the tree and look at
the :attr:`~ErrorTree.errors` attribute.
-.. code-block:: python
+.. doctest::
>>> sorted(tree[0].errors)
['enum', 'type']
@@ -225,7 +242,7 @@ for index ``0``. In fact :attr:`~ErrorTree.errors` is a dict, whose values are
the :class:`ValidationError`\s, so we can get at those directly if we want
them.
-.. code-block:: python
+.. doctest::
>>> print(tree[0].errors["type"].message)
'spam' is not of type 'number'
@@ -233,7 +250,7 @@ them.
Of course this means that if we want to know if a given validator failed for a
given index, we check for its presence in :attr:`~ErrorTree.errors`:
-.. code-block:: python
+.. doctest::
>>> "enum" in tree[0].errors
True
@@ -246,7 +263,7 @@ haven't seen our :validator:`minItems` error appear anywhere yet. This is
because :validator:`minItems` is an error that applies globally to the instance
itself. So it appears in the root node of the tree.
-.. code-block:: python
+.. doctest::
>>> "minItems" in tree.errors
True
diff --git a/tox.ini b/tox.ini
index b0b5a73..ed9e274 100644
--- a/tox.ini
+++ b/tox.ini
@@ -6,7 +6,6 @@ commands =
nosetests -s tests.py
{envpython} -m doctest README.rst
{envpython} -m doctest jsonschema.py
- sphinx-build -b doctest docs {envtmpdir}/html
deps =
{[testenv:notpy33]deps}
{[testenv:py33]deps}
@@ -21,23 +20,18 @@ commands =
sphinx-build -W -b html -d {envtmpdir}/doctrees . {envtmpdir}/html
[testenv:py26]
-commands =
- nosetests -s tests.py
- {envpython} -m doctest README.rst
- {envpython} -m doctest jsonschema.py
deps =
{[testenv:notpy33]deps}
{[testenv:all]deps}
argparse
unittest2
-[testenv:pypy]
+[testenv:py33]
commands =
nosetests -s tests.py
{envpython} -m doctest README.rst
{envpython} -m doctest jsonschema.py
-
-[testenv:py33]
+ sphinx-build -b doctest docs {envtmpdir}/html
deps =
{[testenv:all]deps}
{[testenv:notpy26]deps}