Skip to main content

A tiny library for validating our data if it adheres the contract.

Project description

contractpy

contractpy is a tiny library for validating the data against a contract.

Basic usage

You could simply create a contract object and validate your data if it conforms to the contract.

from pycontract import Contract, Types

my_contract = {
    'name': Types.STRING,
    'id': Types.INTEGER
}
contract = Contract(my_contract)

assert contract.verify({'name': 'Captain America': 'id': 12345}) is True

Yes, it is as simple as this.

Also, it works for the complicated data like nested dict object (Values in a dict object can be another dict object or list of dict objects). In such cases, it will recursively iterate over the inner dicts and validate them against the contract. Please check out the below example,

my_contract = {
    'name': Types.STRING,
    'id': Types.INTEGER,
    'orders': [
        {
            'orderId': Types:INTEGER,
            'price': Types:FLOAT,
        }
    ],
    comments: [Types.String]
}

data = {
    'name': 'Hazel Grace',
    'id: 57331,
    'orders': [
        {
            'orderId': 1,
            'price': 420.45
        },
        {
            'orderId': 2,
            'price': 750.38
        }
    ],
    comments: ["I really liked the product!", "I am completely satisfied."]
}

assert Contract(my_contract).verify(data) is True

If you want to specify a list for any field, use the square bracets ( [ ] ) as specified in the field data. In such cases, it will validate all the values against the contract that are present in the list.

You could also do the contract testing for the REST APIs using this library. This API contract testing would be more useful in the microservices architecture. For Example,

def test_users_api_conforms_the_contract():
    user_api_contract = {
        'page': Types.INTEGER,
        'per_page': Types.INTEGER,
        'total': Types.INTEGER,
        'total_pages': Types.INTEGER,
        'data': [
            {
                'id': Types.INTEGER,
                'name': Types.STRING,
                'year': Types.INTEGER,
                'color': Types.STRING,
                'pantone_value': Types.STRING
            }
        ],
        'ad': {
            'company': Types.STRING,
            'url': Types.STRING,
            'text': Types.STRING
        }
    }

    response = requests.get('https://reqres.in/api/user?page=1')

    assert response.status_code == 200
    assert Contract(user_api_contract).verify(response.json()) is True

Note: Here, the test api is powered by reqres.in

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

contractpy-0.1.9.tar.gz (5.4 kB view details)

Uploaded Source

File details

Details for the file contractpy-0.1.9.tar.gz.

File metadata

  • Download URL: contractpy-0.1.9.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.3

File hashes

Hashes for contractpy-0.1.9.tar.gz
Algorithm Hash digest
SHA256 3ab783846fa8405b6b3be7ded08a779e908a0bf720d0f7a278663720aedecab0
MD5 bb79213f03cc9ba7e752d9ebdf405fa4
BLAKE2b-256 e596df6880b1cd5edf20af7ba9b2651ae0399037b8c556bea59b9a6b6101f652

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