json-verifier


Namejson-verifier JSON
Version 0.6 PyPI version JSON
download
home_pageNone
SummaryAllows a test to spot-check various values of a JSON object
upload_time2024-09-12 06:16:35
maintainerNone
docs_urlNone
authorNone
requires_python>=3.9
licenseNone
keywords json pytest
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # json-verifier

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

## Installation

```console
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:

```json
{
    "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:

```python
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:

```python
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:

```none
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](docs/usage.md)

## License

`json-verifier` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "json-verifier",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": "json, pytest",
    "author": null,
    "author_email": "Hai Vu <haivu2004@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/2c/6f/9294c9608a4e65efdfe4a0c51d7cd1cb87e6d22972d280ac24b48146ffbc/json_verifier-0.6.tar.gz",
    "platform": null,
    "description": "# json-verifier\n\n`json-verifier` is a package which allows a test to spot-check various values of a JSON object. \n\n## Installation\n\n```console\npip install json-verifier\n```\n\n## Why do We Need It?\n\nLet's pretend that we are testing an API, which returns a JSON object such as:\n\n```json\n{\n    \"metadata\": {\n        \"name\": \"sandbox\",\n        \"tags\": [\"scratch\", \"pre-production\", \"experimental\"]\n    }\n}\n```\n\nLet say we need to check a few things:\n\n1. The name must be \"staging\"\n2. The description must be \"Team sandbox\"\n3. The first tag must be \"testing\"\n4. The fourth tag must be \"non-production\"\n\nA typical test then would look like this:\n\n```python\ndef test_api():\n    actual = call_api()\n    assert actual[\"metadata\"][\"name\"] == \"staging\"\n    assert actual[\"metadata\"][\"description\"] == \"Team sandbox\"\n    assert actual[\"metadata\"][\"tags\"][0] == \"testing\"\n    assert actual[\"metadata\"][\"tags\"][3] == \"non-production\"\n```\n\nWhile this test works, it contains a couple of issues:\n\n1. We want to check for all issues, but the above will stop at the first assertion failure and will not test the rest\n2. Notice that the actual value does not contain a \"description\" value. That means the expression `actual[\"metadata\"][\"description\"]` will generate a `KeyError` exception\n3. Likewise, the expression `actual[\"metadata\"][\"tags\"][3]` will generate an `IndexError` exception\n4. It would be nice to be able to shorthand the keys, especially when dealing with a deeply nested JSON object\n\n`json-verifier` is looking to address these issues. Consider the following test:\n\n```python\nfrom json_verifier import JsonVerifier\n\ndef test_api(verifier):\n    actual = call_api()\n    with JsonVerifier(actual) as verifier:\n        verifier.verify_value(\"metadata.name\", \"staging\")\n        verifier.verify_value(\"metadata.description\", \"Team sandbox\")\n        verifier.verify_value(\"metadata.tags.0\", \"testing\")\n        verifier.verify_value(\"metadata.tags.3\", \"non-production\")\n```\n\nHere is a sample `pytest` output:\n\n```none\nAssertionError: Verify JSON failed\nObject:\n{\n    \"metadata\": {\n        \"name\": \"sandbox\",\n        \"tags\": [\n            \"scratch\",\n            \"pre-production\",\n            \"experimental\"\n        ]\n    }\n}\nErrors:\n- path='metadata.name', expected='staging', actual='sandbox'\n  /home/user/workspace/test_api.py(11) in test_api()\n          verifier.verify_value(\"metadata.name\", \"staging\")\n\n- path='metadata.description', expected='Team sandbox', key error: 'description'\n  /home/user/workspace/test_api.py(12) in test_api()\n          verifier.verify_value(\"metadata.description\", \"Team sandbox\")\n\n- path='metadata.tags.0', expected='testing', actual='scratch'\n  /home/user/workspace/test_api.py(13) in test_api()\n          verifier.verify_value(\"metadata.tags.0\", \"testing\")\n\n- path='metadata.tags.3', expected='non-production', index error: 3\n  /home/user/workspace/test_api.py(14) in test_api()\n          verifier.verify_value(\"metadata.tags.3\", \"non-production\")\n\nThis output shows some important information:\n\n1. A formatted dump of the object under test\n2. A list of errors. Each complete with filename, line number, the function, the offending line, and why test failed\n```\n\n## Usage\n\nFor additional usage tips, see [usage.md](docs/usage.md)\n\n## License\n\n`json-verifier` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license.\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Allows a test to spot-check various values of a JSON object",
    "version": "0.6",
    "project_urls": {
        "Homepage": "https://github.com/htv2012/json-verifier"
    },
    "split_keywords": [
        "json",
        " pytest"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "79f72545a1deca354db9efba634de059d3f9f71f59c2a01299bfb07cacc076f1",
                "md5": "f9f8988fff7bd1e577fdfe96e1a2b2ba",
                "sha256": "f717de9c73e0d8b681e6c349e667ea9eee468c995b3ce95ea14114f0a535a0c4"
            },
            "downloads": -1,
            "filename": "json_verifier-0.6-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "f9f8988fff7bd1e577fdfe96e1a2b2ba",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 5016,
            "upload_time": "2024-09-12T06:16:35",
            "upload_time_iso_8601": "2024-09-12T06:16:35.051694Z",
            "url": "https://files.pythonhosted.org/packages/79/f7/2545a1deca354db9efba634de059d3f9f71f59c2a01299bfb07cacc076f1/json_verifier-0.6-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2c6f9294c9608a4e65efdfe4a0c51d7cd1cb87e6d22972d280ac24b48146ffbc",
                "md5": "6bfecadc0df59cd26849b87fe51cb18b",
                "sha256": "c024daa379663900f91a8474c69a9768aedf8aeefad34f267d6b6a969f89f5a8"
            },
            "downloads": -1,
            "filename": "json_verifier-0.6.tar.gz",
            "has_sig": false,
            "md5_digest": "6bfecadc0df59cd26849b87fe51cb18b",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 7652,
            "upload_time": "2024-09-12T06:16:35",
            "upload_time_iso_8601": "2024-09-12T06:16:35.931937Z",
            "url": "https://files.pythonhosted.org/packages/2c/6f/9294c9608a4e65efdfe4a0c51d7cd1cb87e6d22972d280ac24b48146ffbc/json_verifier-0.6.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-09-12 06:16:35",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "htv2012",
    "github_project": "json-verifier",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "json-verifier"
}
        
Elapsed time: 0.55571s