def test_nested_error(): with pytest.raises(TypeError): validate([{ 'id': positive_int, 'name': title }], [ { 'id': 7, 'name': 'Donald Duck' }, { 'id': -8, 'name': 'Walt Disney' }, ])
def test_dict_nested(): with pytest.raises(TypeError): assert validate([{'a': {'b': [dict]}}], [ {'a': {'b': [{}, SimpleNamespace()]}}, {'a': {'b': [{'any': 'key'}, {'used': 'here'}]}}, ]) is None
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)
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
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)
def test_dict_nested(): assert validate([{ 'a': { 'b': [dict] } }], [ { 'a': { 'b': [{}, {}] } }, { 'a': { 'b': [{ 'any': 'key' }, { 'used': 'here' }] } }, ]) is None
def test_dict_nested(): assert validate(({ 'a': { 'b': (dict, ) } }, ), ( { 'a': { 'b': ({}, {}) } }, { 'a': { 'b': ({ 'any': 'key' }, { 'used': 'here' }) } }, )) is None
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
def test_simple_error(): with pytest.raises(TypeError): validate([str], 'asd')
def test_nested(): assert validate([[str]], [['ab', 'cd'], ['ef', 'g']]) is None
def test_simple(): assert validate([str], ['ab', 'cd']) is None
def test_basic_list_items_missing(): with pytest.raises(IndexError): validate([[int, int, int]], [[1, 2]])
def test_plain(): assert validate([int], [1, 2, 3, 4, 5]) is None
def test_dict_strict(): assert validate(({'a': int}, ), ({'a': 123}, {'a': 456})) is None
def test_multi_basic_dict_fail_list(): struct = {"b": list} data = {"b": "THIS FAILS"} with pytest.raises(LookupError): validate(struct, data)
def test_basic_list_items_missmatch(): with pytest.raises(LookupError): validate({"wat": [1, 2, 3]}, {"wat": [1, 2, '3']})
def test_dict_empty(): assert validate([dict], [{}, {}, {}]) is None
def test_plain_typeerror(): with pytest.raises(TypeError): validate([int], [1, 2, 3, 4.5])
def test_empty(): assert validate((), ()) is None
'''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)
def test_basic_list_items_emptylist(): with pytest.raises(IndexError): validate({"wat": [[]]}, {"wat": []})
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)
def test_plain_typeerror(): with pytest.raises(TypeError): validate((int, ), (1, 2, 3, 4.5))
def test_dict_strict(): assert validate([{'a': int}], [{'a': 123}, {'a': 456}]) is None
def test_dict_empty(): assert validate((dict, ), ({}, {}, {})) is None
def test_empty(): assert validate([], []) is None
def test_list_lengths(): with pytest.raises(ValueError): validate([int, int, int, str], [1, 2, 3, 'a', 'a'])
def test_basic_dict(): assert not validate({"stuff": str}, {"stuff": "string"})
def test_plain(): assert validate((int, ), (1, 2, 3, 4, 5)) is None
def test_list_lengths_same_type_2(): with pytest.raises(ValueError): validate([int, int], [1, 2, 2])