Exemple #1
0
def test_all_public_errors():
    """test that all errors importable from the top-level glom module
    pass a basic set of standards.

    When adding a new public error type, this test will be fail unless
    that type is also tested below.
    """
    import glom
    import copy

    err_types = [
        t for t in [getattr(glom, name) for name in dir(glom)]
        if isinstance(t, type) and issubclass(t, Exception)
    ]
    non_glomerrors = [
        t for t in err_types if not issubclass(t, glom.GlomError)
    ]
    assert not non_glomerrors, "expected all public exception types to subclass GlomError"

    err_types = sorted([t for t in err_types if not t is glom.GlomError],
                       key=lambda t: t.__name__)

    results = []

    def _test_exc(exc_type, target, spec):
        with pytest.raises(exc_type) as exc_info:
            glom.glom(target, spec)
        results.append((target, spec, exc_info.value))
        return exc_info.value

    _test_exc(glom.CheckError, {}, glom.Check(equal_to=[]))

    _test_exc(glom.FoldError, 2, glom.Flatten())

    _test_exc(glom.BadSpec, range(5), glom.grouping.Group([{T: T}]))

    _test_exc(glom.PathAccessError, {}, 'a.b.c')

    _test_exc(glom.UnregisteredTarget, 'kurt', [glom.T])

    _test_exc(glom.CoalesceError, {}, glom.Coalesce('a', 'b'))

    _test_exc(glom.PathAssignError, object(), glom.Assign('a', 'b'))

    _test_exc(glom.PathDeleteError, object(), glom.Delete('a'))

    for (target, spec, exc) in results:
        assert copy.copy(exc) is not exc
        exc_str = str(exc)
        exc_type_name = exc.__class__.__name__
        assert exc_type_name in exc_str
        assert bbrepr(exc).startswith(exc_type_name)

        assert bbrepr(target)[:80] in exc_str
        assert bbrepr(spec)[:80] in exc_str

    tested_types = [type(exc) for _, _, exc in results]
    untested_types = set(err_types) - set(tested_types)

    assert not untested_types, "did not test all public exception types"
Exemple #2
0
def test_bbrepr():
    assert bbrepr({int: dict}) == "{int: dict}"