Skip to main content

Allows a test to spot-check various values of a JSON object

Project description

json-verifier

json-verifier is a package which allows a test to spot-check various values of a JSON object.

Installation

pip install json-verifier

Why do We Need It?

Let's pretend that we are testing an API, which returns a JSON object such as:

{
    "metadata": {
        "name": "sandbox",
        "tags": ["scratch", "pre-production", "experimental"]
    }
}

Let say we need to check a few things:

  1. The name must be "staging"
  2. The description must be "Team sandbox"
  3. The first tag must be "testing"
  4. The fourth tag must be "non-production"

A typical test then would look like this:

def test_api():
    actual = call_api()
    assert actual["metadata"]["name"] == "staging"
    assert actual["metadata"]["description"] == "Team sandbox"
    assert actual["metadata"]["tags"][0] == "testing"
    assert actual["metadata"]["tags"][3] == "non-production"

While this test works, it contains a couple of issues:

  1. We want to check for all issues, but the above will stop at the first assertion failure and will not test the rest
  2. Notice that the actual value does not contain a "description" value. That means the expression actual["metadata"]["description"] will generate a KeyError exception
  3. Likewise, the expression actual["metadata"]["tags"][3] will generate an IndexError exception
  4. It would be nice to be able to shorthand the keys, especially when dealing with a deeply nested JSON object

json-verifier is looking to address these issues. Consider the following test:

from json_verifier import JsonVerifier

def test_api(verifier):
    actual = call_api()
    with JsonVerifier(actual) as verifier:
        verifier.verify_value("metadata.name", "staging")
        verifier.verify_value("metadata.description", "Team sandbox")
        verifier.verify_value("metadata.tags.0", "testing")
        verifier.verify_value("metadata.tags.3", "non-production")

Here is a sample pytest output:

AssertionError: Verify JSON failed
Object:
{
    "metadata": {
        "name": "sandbox",
        "tags": [
            "scratch",
            "pre-production",
            "experimental"
        ]
    }
}
Errors:
- path='metadata.name', expected='staging', actual='sandbox'
  /home/user/workspace/test_api.py(11) in test_api()
          verifier.verify_value("metadata.name", "staging")

- path='metadata.description', expected='Team sandbox', key error: 'description'
  /home/user/workspace/test_api.py(12) in test_api()
          verifier.verify_value("metadata.description", "Team sandbox")

- path='metadata.tags.0', expected='testing', actual='scratch'
  /home/user/workspace/test_api.py(13) in test_api()
          verifier.verify_value("metadata.tags.0", "testing")

- path='metadata.tags.3', expected='non-production', index error: 3
  /home/user/workspace/test_api.py(14) in test_api()
          verifier.verify_value("metadata.tags.3", "non-production")

This output shows some important information:

1. A formatted dump of the object under test
2. A list of errors. Each complete with filename, line number, the function, the offending line, and why test failed

Usage

For additional usage tips, see usage.md

License

json-verifier is distributed under the terms of the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

json_verifier-0.6.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

json_verifier-0.6-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file json_verifier-0.6.tar.gz.

File metadata

  • Download URL: json_verifier-0.6.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.20

File hashes

Hashes for json_verifier-0.6.tar.gz
Algorithm Hash digest
SHA256 c024daa379663900f91a8474c69a9768aedf8aeefad34f267d6b6a969f89f5a8
MD5 6bfecadc0df59cd26849b87fe51cb18b
BLAKE2b-256 2c6f9294c9608a4e65efdfe4a0c51d7cd1cb87e6d22972d280ac24b48146ffbc

See more details on using hashes here.

File details

Details for the file json_verifier-0.6-py3-none-any.whl.

File metadata

  • Download URL: json_verifier-0.6-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.20

File hashes

Hashes for json_verifier-0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 f717de9c73e0d8b681e6c349e667ea9eee468c995b3ce95ea14114f0a535a0c4
MD5 f9f8988fff7bd1e577fdfe96e1a2b2ba
BLAKE2b-256 79f72545a1deca354db9efba634de059d3f9f71f59c2a01299bfb07cacc076f1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page