Пример #1
0
def test_nested_error():
    with pytest.raises(TypeError):
        validate([{
            'id': positive_int,
            'name': title
        }], [
            {
                'id': 7,
                'name': 'Donald Duck'
            },
            {
                'id': -8,
                'name': 'Walt Disney'
            },
        ])
Пример #2
0
def test_dict_nested():
    with pytest.raises(TypeError):
        assert validate([{'a': {'b': [dict]}}],
            [
                {'a': {'b': [{}, SimpleNamespace()]}},
                {'a': {'b': [{'any': 'key'}, {'used': 'here'}]}},
            ]) is None
Пример #3
0
def test_multi_basic_dict_partial():
    struct = {"d": {"nested": int, "stuff": int}}
    data = {
        "d": {
            "nested": 1,
            "stuff": 2
        },
        "undescribed":
        ["Can't", "Parse", "This", "Dooo", "DoDoDo", "BaDo", "Badum"]
    }
    assert not validate(struct, data)
Пример #4
0
def test_nested():
    struct = [{'id': positive_int, 'name': title}]
    data = [
        {
            'id': 5,
            'name': 'Donald Duck'
        },
        {
            'id': 6,
            'name': 'Walt Disney'
        },
    ]
    assert validate(struct, data) is None
Пример #5
0
def test_multi_basic_dict():
    struct = {
        "a": int,
        "b": list,
        "c": [int, int, str],
        "d": {
            "nested": int,
            "stuff": int
        }
    }
    data = {
        "a": 1,
        "b": [5, 4, 3, 2, 1],
        "c": [1, 1, "x"],
        "d": {
            "nested": 1,
            "stuff": 2
        }
    }
    assert not validate(struct, data)
Пример #6
0
def test_dict_nested():
    assert validate([{
        'a': {
            'b': [dict]
        }
    }], [
        {
            'a': {
                'b': [{}, {}]
            }
        },
        {
            'a': {
                'b': [{
                    'any': 'key'
                }, {
                    'used': 'here'
                }]
            }
        },
    ]) is None
Пример #7
0
def test_dict_nested():
    assert validate(({
        'a': {
            'b': (dict, )
        }
    }, ), (
        {
            'a': {
                'b': ({}, {})
            }
        },
        {
            'a': {
                'b': ({
                    'any': 'key'
                }, {
                    'used': 'here'
                })
            }
        },
    )) is None
Пример #8
0
def test_advanced():
    structure = {
        'total_count': int,
        'active': bool,
        'people': [{
            'id': int,
            'title': str,
            'salary': float,
        }],
        'timestamps': [int],
    }
    data = {
        'total_count':
        52,
        'active':
        False,
        'people': [
            {
                'id': 23974291847,
                'title': 'big guy',
                'salary': 256.12
            },
            {
                'id': 1932,
                'title': 'python developer',
                'salary': 4.5
            },
            {
                'id': 9828974,
                'title': 'president',
                'salary': 122352.23
            },
        ],
        'timestamps': [12351346, 12345134, 7684545, 1267457, 0],
    }
    assert validate(structure, data) is None
Пример #9
0
def test_simple_error():
    with pytest.raises(TypeError):
        validate([str], 'asd')
Пример #10
0
def test_nested():
    assert validate([[str]], [['ab', 'cd'], ['ef', 'g']]) is None
Пример #11
0
def test_simple():
    assert validate([str], ['ab', 'cd']) is None
Пример #12
0
def test_basic_list_items_missing():
    with pytest.raises(IndexError):
        validate([[int, int, int]], [[1, 2]])
Пример #13
0
def test_plain():
    assert validate([int], [1, 2, 3, 4, 5]) is None
Пример #14
0
def test_dict_strict():
    assert validate(({'a': int}, ), ({'a': 123}, {'a': 456})) is None
Пример #15
0
def test_multi_basic_dict_fail_list():
    struct = {"b": list}
    data = {"b": "THIS FAILS"}
    with pytest.raises(LookupError):
        validate(struct, data)
Пример #16
0
def test_basic_list_items_missmatch():
    with pytest.raises(LookupError):
        validate({"wat": [1, 2, 3]}, {"wat": [1, 2, '3']})
Пример #17
0
def test_dict_empty():
    assert validate([dict], [{}, {}, {}]) is None
Пример #18
0
def test_plain_typeerror():
    with pytest.raises(TypeError):
        validate([int], [1, 2, 3, 4.5])
Пример #19
0
def test_empty():
    assert validate((), ()) is None
Пример #20
0
'''Some lil tests.
This is a messy testing ground, please avert your eyes, it
is bad times ahead my friend.'''

from numbers import Number
import json

from datatyping import validate, create_structure

data_structure_1 = {
    "name": str,
    "age": int,
    "height": float,
    "favourite_things": {
        "cat": str,
        "flavour": str,
        "number": Number,
    },
    "list": [[]]
}


api_results = json.loads('{"name": "frank", "age": 999, "height":1.89, "favourite_things":'
                         ' {"cat": "ket", "flavour": "YUM", "number": 8}, "list": []}')

if __name__ == '__main__':
    validate(data_structure_1, api_results)
Пример #21
0
def test_basic_list_items_emptylist():
    with pytest.raises(IndexError):
        validate({"wat": [[]]}, {"wat": []})
Пример #22
0
def test_multi_basic_dict_fail_dict():
    struct = {"c": [int, int, str], "d": {"nested": int, "stuff": int}}
    data = {"c": [1, 1, "x"], "d": {"nested": 1.23, "stuff": 2}}
    with pytest.raises(LookupError):
        validate(struct, data)
Пример #23
0
def test_plain_typeerror():
    with pytest.raises(TypeError):
        validate((int, ), (1, 2, 3, 4.5))
Пример #24
0
def test_dict_strict():
    assert validate([{'a': int}], [{'a': 123}, {'a': 456}]) is None
Пример #25
0
def test_dict_empty():
    assert validate((dict, ), ({}, {}, {})) is None
Пример #26
0
def test_empty():
    assert validate([], []) is None
Пример #27
0
def test_list_lengths():
    with pytest.raises(ValueError):
        validate([int, int, int, str], [1, 2, 3, 'a', 'a'])
Пример #28
0
def test_basic_dict():
    assert not validate({"stuff": str}, {"stuff": "string"})
Пример #29
0
def test_plain():
    assert validate((int, ), (1, 2, 3, 4, 5)) is None
Пример #30
0
def test_list_lengths_same_type_2():
    with pytest.raises(ValueError):
        validate([int, int], [1, 2, 2])