コード例 #1
0
def test_missed_required(all_errors):
    class Foo(Structure):
        r: str

    with raises(Exception) as ex:
        deserialize_structure(Foo, {})
    errs = standard_readable_error_for_typedpy_exception(ex.value)
    assert errs[0].problem.startswith("missing a required argument: 'r'")
コード例 #2
0
def test_multiple_errors_not_fail_fast(all_errors):
    with raises(Exception) as ex:
        Foo(a=1, b=1000, c=-5, arr=[1])
    errs = standard_readable_error_for_typedpy_exception(ex.value)
    assert ErrorInfo(field='b',
                     problem='Expected a maximum of 100',
                     value='1000') in errs
    assert ErrorInfo(field='arr_0', problem='Expected a string',
                     value='1') in errs
    assert ErrorInfo(field='c',
                     problem='Expected a positive number',
                     value='-5') in errs
コード例 #3
0
def test_real_world_usage():
    try:
        Foo(a=1, b=10, c=1.1, arr=['abc', 1])
    except Exception as ex:
        assert standard_readable_error_for_typedpy_exception(ex) == \
           ErrorInfo(field='arr_1', problem='Expected a string', value='1')
コード例 #4
0
def test_error_7():
    with raises(Exception) as ex:
        Foo(a=1, b=100, c=1.1, arr=["a"], e=5)
    assert standard_readable_error_for_typedpy_exception(ex.value) == \
           ErrorInfo(problem="got an unexpected keyword argument 'e'")
コード例 #5
0
def test_error_6():
    with raises(Exception) as ex:
        Foo(a=1, b=100, c=1.1, arr=["a"], d="xyz")
    print(standard_readable_error_for_typedpy_exception(ex.value))
    assert standard_readable_error_for_typedpy_exception(ex.value) == \
           ErrorInfo(field="d", problem="time data 'xyz' does not match format '%Y-%m-%d'", value="'xyz'")
コード例 #6
0
def test_error_5():
    with raises(Exception) as ex:
        Foo(a=1, b=1000, c=1.1, arr=["a"])
    assert standard_readable_error_for_typedpy_exception(ex.value) == \
           ErrorInfo(field='b', problem='Expected a maximum of 100', value='1000')
コード例 #7
0
def test_error_4():
    with raises(Exception) as ex:
        Foo(a=1, b=10, c=1.1, arr=[])
    assert standard_readable_error_for_typedpy_exception(ex.value) == \
           ErrorInfo(field='arr', problem='Expected length of at least 1', value='[]')
コード例 #8
0
def test_error_3():
    with raises(Exception) as ex:
        Foo(a=1, b=10, c=1.1)
    assert standard_readable_error_for_typedpy_exception(ex.value) == \
           ErrorInfo(problem="missing a required argument: 'arr'")
コード例 #9
0
def test_error_2():
    with raises(Exception) as ex:
        Foo(a=1, b=10, c=1.1, arr=2)
    assert standard_readable_error_for_typedpy_exception(ex.value) == \
           ErrorInfo(field='arr', problem='Expected an array', value='2')
コード例 #10
0
def test_error_1():
    with raises(Exception) as ex:
        Foo(a=1, b=10, c=1.1, arr=['abc', 1])
    assert standard_readable_error_for_typedpy_exception(ex.value) == \
           ErrorInfo(field='arr_1', problem='Expected a string', value='1')
コード例 #11
0
def test_unsuccessful_deserialization_with_many_types(all_errors):
    data = {
        'anything': {'a', 'b', 'c'},
        'i':
        50,  # Error: > 10
        's': [],  # Error: should be string
        'complex_allof': {
            'name': 'john',
            'ssid': '123'
        },
        'array': [10, 7, "aaa"],  # Error: array_2
        'any': [{
            'name': 'john',
            'ssid': '123'
        }, 'xxx'],  # any_1
        'embedded': {
            'a1': 8,
            #         'a2': 0.5         # Error: missing 'a2'
        },
        'people': [{
            'name': 'john',
            'ssid': '13'
        }],
        'simplestruct': {
            'name': 'danny'
        },
        'array_of_one_of': [{
            'a1': 8,
            'a2': 0.5
        }, 0.5, 4, {
            'name': 'john',
            'ssid': '123'
        }],
        'all':
        5,
        'enum':
        4  # Error
    }

    with raises(Exception) as ex:
        deserialize_structure(Example, data)
    errs = standard_readable_error_for_typedpy_exception(ex.value)
    expected_errors = [
        ErrorInfo(field='i', problem='Expected a maximum of 10', value='50'),
        ErrorInfo(field='s', problem='Expected a string', value='[]'),
        ErrorInfo(
            field='any',
            problem="Does not match any field option:"
            " (1) Does not match <Array. Properties: items = <ClassReference: Person>>. reason: any_1: Expected "
            "a dictionary; Got 'xxx'. (2) Does not match <ClassReference: Person>."
            " reason: any: Expected a dictionary; Got"
            " [{'name': 'john', 'ssid': '123'}, 'xxx']",
            value="[{'name': 'john', 'ssid': '123'}, 'xxx']"),
        ErrorInfo(field='embedded',
                  problem="missing a required argument: 'a2'",
                  value="{'a1': 8}"),
        ErrorInfo(field='enum', problem='Expected one of 1, 2, 3', value='4'),
        ErrorInfo(field='people_0',
                  problem=[
                      ErrorInfo(field='ssid',
                                problem='Expected a minimum length of 3',
                                value="'13'")
                  ]),
    ]
    for e in expected_errors:
        assert e in errs