2015-08-04 19:01:06 +01:00
|
|
|
JSONPath
|
|
|
|
========
|
|
|
|
|
2016-03-11 21:12:33 +01:00
|
|
|
Gabbi supports JSONPath both for validating JSON response bodies and within
|
|
|
|
:ref:`substitutions <state-substitution>`.
|
|
|
|
|
|
|
|
JSONPath expressions are provided by `jsonpath_rw`_, with
|
|
|
|
`jsonpath_rw_ext`_ custom extensions to address common requirements:
|
2015-08-04 19:01:06 +01:00
|
|
|
|
2016-03-11 21:12:33 +01:00
|
|
|
#. Sorting via ``sorted`` and ``[/property]``.
|
|
|
|
#. Filtering via ``[?property = value]``.
|
|
|
|
#. Returning the respective length via ``len``.
|
2016-03-05 15:17:00 +00:00
|
|
|
|
2016-03-11 21:12:33 +01:00
|
|
|
(These apply both to arrays and key-value pairs.)
|
2015-08-04 19:01:06 +01:00
|
|
|
|
2016-03-11 19:24:36 +00:00
|
|
|
.. highlight:: json
|
2015-08-04 19:01:06 +01:00
|
|
|
|
2016-03-11 19:29:53 +00:00
|
|
|
Here is a JSONPath example demonstrating some of these features. Given
|
2016-03-11 19:24:36 +00:00
|
|
|
JSON data as follows::
|
2015-08-04 19:01:06 +01:00
|
|
|
|
2016-03-11 21:39:38 +01:00
|
|
|
{
|
|
|
|
"pets": [
|
|
|
|
{"type": "cat", "sound": "meow"},
|
|
|
|
{"type": "dog", "sound": "woof"}
|
|
|
|
]
|
|
|
|
}
|
2016-03-11 19:24:36 +00:00
|
|
|
|
|
|
|
.. highlight:: yaml
|
|
|
|
|
2016-03-11 21:12:33 +01:00
|
|
|
If the ordering of the list in ``pets`` is predictable and
|
2016-03-11 19:24:36 +00:00
|
|
|
reliable it is relatively straightforward to test values::
|
|
|
|
|
|
|
|
response_json_paths:
|
|
|
|
# length of list is two
|
|
|
|
$.pets.`len`: 2
|
|
|
|
# sound of second item in list is woof
|
|
|
|
$.pets[1].sound: woof
|
|
|
|
|
|
|
|
If the ordering is *not* predictable additional effort is required::
|
2015-08-04 19:01:06 +01:00
|
|
|
|
|
|
|
response_json_paths:
|
2016-03-11 19:24:36 +00:00
|
|
|
# sort by type
|
|
|
|
$.pets[/type][0].sound: meow
|
|
|
|
# sort by type, reversed
|
|
|
|
$.pets[\type][0].sound: woof
|
|
|
|
# all the sounds
|
|
|
|
$.pets[/type]..sound: ['meow', 'woof']
|
|
|
|
# filter by type = dog
|
|
|
|
$.pets[?type = "dog"].sound: woof
|
2015-08-04 19:01:06 +01:00
|
|
|
|
2016-03-16 17:26:14 +00:00
|
|
|
If it is necessary to validate the entire JSON response use a
|
|
|
|
JSONPath of ``$``::
|
|
|
|
|
|
|
|
response_json_paths:
|
|
|
|
$:
|
|
|
|
pets:
|
|
|
|
- type: cat
|
|
|
|
sound: meow
|
|
|
|
- type: dog
|
|
|
|
sound: woof
|
|
|
|
|
|
|
|
This is not a technique that should be used frequently as it can
|
|
|
|
lead to difficult to read tests and it also indicates that your
|
|
|
|
gabbi tests are being used to test your serializers and data models,
|
|
|
|
not just your API interactions.
|
|
|
|
|
2016-03-05 15:17:00 +00:00
|
|
|
There are more JSONPath examples in :doc:`example` and in the
|
|
|
|
`jsonpath_rw`_ and `jsonpath_rw_ext`_ documentation.
|
2015-08-04 19:01:06 +01:00
|
|
|
|
|
|
|
.. _jsonpath_rw: http://jsonpath-rw.readthedocs.org/en/latest/
|
2016-03-05 15:17:00 +00:00
|
|
|
.. _jsonpath_rw_ext: https://python-jsonpath-rw-ext.readthedocs.org/en/latest/
|