How to use the pglast.Node function in pglast

To help you get started, we’ve selected a few pglast examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github lelit / pglast / tests / test_dml_printers.py View on Github external
def roundtrip(sql):
    orig_ast = parse_sql(sql)
    _remove_stmt_len_and_location(orig_ast)

    serialized = RawStream()(Node(orig_ast))
    try:
        serialized_ast = parse_sql(serialized)
    except:  # noqa
        raise RuntimeError("Could not reparse %r" % serialized)
    _remove_stmt_len_and_location(serialized_ast)
    assert orig_ast == serialized_ast, "%r != %r" % (sql, serialized)

    indented = IndentedStream()(Node(orig_ast))
    try:
        indented_ast = parse_sql(indented)
    except:  # noqa
        raise RuntimeError("Could not reparse %r" % indented)
    _remove_stmt_len_and_location(indented_ast)
    assert orig_ast == indented_ast, "%r != %r" % (sql, indented)

    # Run ``pytest -s tests/`` to see the following output
github lelit / pglast / tests / test_node.py View on Github external
def test_basic():
    ptree = [{'Foo': {'bar': {'Bar': {'a': 1, 'b': 'b', 'c': None, 'd': 0}}}},
             {'Foo': {'bar': {'Bar': {'a': 0, 'f': False, 't': True, 'c': [
                 {'C': {'x': 0, 'y': 0}},
                 {'C': {'x': 0, 'y': 0}}
             ]}}}}]

    root = Node(ptree)
    assert root.parent_node is None
    assert root.parent_attribute is None
    assert isinstance(root, List)
    assert len(root) == 2
    assert repr(root) == '[2*{Foo}]'
    assert str(root) == 'None=[2*{Foo}]'
    with pytest.raises(AttributeError):
        root.not_there

    foo1 = root[0]
    assert foo1 != root
    assert foo1.node_tag == 'Foo'
    assert foo1.parse_tree == {'bar': {'Bar': {'a': 1, 'b': 'b', 'c': None, 'd': 0}}}
    assert foo1.parent_node is None
    assert foo1.parent_attribute == (None, 0)
    assert repr(foo1) == '{Foo}'
github lelit / pglast / tests / test_node.py View on Github external
def test_nested_lists():
    ptree = [{'Foo': {'bar': {'Bar': {'a': [
        [{'B': {'x': 0, 'y': 0}}, None],
        [{'B': {'x': 1, 'y': 1}}, None],
    ]}}}}]

    root = Node(ptree)
    foo1 = root[0]
    a = foo1.bar.a
    assert isinstance(a, List)
    assert isinstance(a[0], List)
    a00 = a[0][0]
    assert a00.node_tag == 'B'
    assert a00.x.value == a00.y.value == 0
    assert a00.parent_attribute == (('a', 0), 0)
    assert a00.parent_node[a00.parent_attribute] == a00
    a01 = a[0][1]
    assert a01.value is None
    assert a01.parent_attribute == (('a', 0), 1)
    assert a01.parent_node[a01.parent_attribute] == a01
github lelit / pglast / tests / test_node.py View on Github external
root = Node(ptree)
    assert tuple(root.traverse()) == (
        root[0],
        root[0].bar,
        root[0].bar['a'],
        root[0].bar.b,
    )

    ptree = [{'Foo': {'bar': {'Bar': {'a': 1, 'b': 'b'}}}},
             {'Foo': {'bar': {'Bar': {'a': 0, 'c': [
                 {'C': {'x': 0, 'y': 0}},
                 {'C': {'x': 0, 'y': 0}}
             ]}}}}]

    root = Node(ptree)
    assert tuple(root.traverse()) == (
        root[0],
        root[0].bar,
        root[0].bar['a'],
        root[0].bar.b,
        root[1],
        root[1].bar,
        root[1].bar['a'],
        root[1].bar.c[0],
        root[1].bar.c[0].x,
        root[1].bar.c[0].y,
        root[1].bar.c[1],
        root[1].bar.c[1].x,
        root[1].bar.c[1].y,
    )