Skip to main content

Smart comparing data for tests

Project description

SMART DATA

SMART DATA is a package to make your live easier. For example you want to test many JSON (or any other) results from some API in Python. There are plenty of great Python packages to find a differences between two complex structres but sometimes you need omit some their parts. This package help you to write this kind of code easier.

Requirements

No package dependecy.

Installation

pip install smart_data

Functions

list_diff = include(got, expected)

  • got - structure to check
  • expected - structure that is required in 'got' structure. Any difference will be in returned list.
  • list_diff - list with differences, e.g. ['/attributes/temperature/<20.1 vs 22.2>']

Try to check if 'expected' structure includes 'got' structure. Any additional keys from 'got' will be igroned. This situation can be expected if you don't want to check some parts of complex structure (e.g. in tests).

The structures should contain dictionaries, lists, objects, simple types or any comparable structures (__str__ and __eq__ implementation). Additionally 'expected' can be or can contains compiled regular expression (re package) to check e.g. if you don't want mocking datetime objects.

Example of usage

So let's try test some endpoint:

import re
re_datetime = re.compile(r"^\d{4}-\d\d-\d\d \d\d:\d\d:\d\d$")

def test_add_new(self, client):
    with client:
        res = client.post(
            '/air_state',
            json = {
                'data': {
                    'type': 'air_state',
                    'attributes': {
                        'temperature': 20.1,
                        'humidity': 51.2,
                        'location': 'kitchen',
                        'device': 'dev1_esp',
                    },
                },
            },
            content_type = 'application/vnd.api+json',
        )
        assert 201 == res.status_code
        res_json = res.get_json()

        assert res_json['data']['type'] == 'air_state'
        assert res_json['data']['id'] == '1'
        assert res_json['data']['attributes']['temperature'] == '20.1'
        assert res_json['data']['attributes']['humidity'] == '51.2'
        assert res_json['data']['attributes']['location'] == 'kitchen'
        assert res_json['data']['attributes']['device'] == 'dev1_esp'
        assert re_datetime.search(res_json['data']['attributes']['created'])

You need write buch of asserts for many items in result structure. Many lines of code. If bigger structure then more code.

Now you can write it in another way using smart_data package:

from smart_data import include
from re import compile

def test_add_new(self, client):

    payload = {
        'type': 'air_state',
        'attributes': {
            'temperature': 20.1,
            'humidity': 51.2,
            'location': 'kitchen',
            'device': 'dev1_esp',
        },
    }

    with client:
        res = client.post(
            '/air_state',
            json = { 'data': payload },
            content_type = 'application/vnd.api+json',
        )
        assert 201 == res.status_code

        payload['attributes']['id'] = 1
        payload['attributes']['created'] = compile(r"^\d{4}-\d\d-\d\d \d\d:\d\d:\d\d$")
        res_json = res.get_json()
        assert include(got=res_json['data'], expected=payload) == []

This is simple example with really small amount of data to test. For more complex structure the benefit is higher.

Next benefit is readable output from broken assert during tests. For example:

    def test_foo():
        expected = {
            'foo': 1.1,
            'bar': [42, {'baz': 22}],
            'zoo': None,
            'zar': [[1, 3], [5, 8]],
        }
        got = {
            'foo': 1.1,
            'bar': [42, {'baz': 2}],
            'zoo': None,
            'zar': [[1, 3], [5, 8]],
        }
>       assert include(got, expected) == []
E       AssertionError: assert ['/bar/1/baz/<2 vs 22>'] == []
E         Left contains one more item: '/bar/1/baz/<2 vs 22>'
E         Full diff:
E         - []
E         + ['/bar/1/baz/<2 vs 22>']

tests/test_include.py:38: AssertionError

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

smart_data-0.2.0.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

smart_data-0.2.0-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file smart_data-0.2.0.tar.gz.

File metadata

  • Download URL: smart_data-0.2.0.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.1.0 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.6.9

File hashes

Hashes for smart_data-0.2.0.tar.gz
Algorithm Hash digest
SHA256 4c381c19e79c4404e745edaa0da58a119166148743c58cff5ad4e85b8a80ff32
MD5 b502ebf7704d02bb0fcf19b35fbaad77
BLAKE2b-256 33261b74c9f0375c95fc3674f80d7b056f065fc724ed174686f82ab95c2e4312

See more details on using hashes here.

File details

Details for the file smart_data-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: smart_data-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.1.0 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.6.9

File hashes

Hashes for smart_data-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cf92622d8c05f7b69b04adb8ea9c1d9d9fb5f9750fcc82936c775415dc89d8d1
MD5 1d3535e774cf0fbb8c763a1dc87c2dbc
BLAKE2b-256 7b42c7970b324eef0e7eff66bdfcbf3089eaef593fafac062534ef2b216e7419

See more details on using hashes here.

Supported by

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