Exemplo n.º 1
0
def test_pointless_attributes_remotion():
    sql1 = parse_sql('select a from x; select b from y')
    sql2 = parse_sql('select a from x;\n\nselect b from y')
    assert sql1 != sql2
    _remove_stmt_len_and_location(sql1)
    _remove_stmt_len_and_location(sql2)
    assert sql1 == sql2
Exemplo n.º 2
0
def workhorse(args):
    input = args.infile or sys.stdin
    with input:
        statement = input.read()

    if args.parse_tree or args.plpgsql:
        tree = parse_plpgsql(statement) if args.plpgsql else parse_sql(statement)
        if args.no_location:
            _remove_stmt_len_and_location(tree)
        output = args.outfile or sys.stdout
        with output:
            json.dump(tree, output, sort_keys=True, indent=2)
            output.write('\n')
    else:
        try:
            prettified = prettify(
                statement,
                compact_lists_margin=args.compact_lists_margin,
                split_string_literals_threshold=args.split_string_literals,
                special_functions=args.special_functions,
                comma_at_eoln=args.comma_at_eoln,
                semicolon_after_last_statement=args.semicolon_after_last_statement)
        except Error as e:
            print()
            raise SystemExit(e)

        output = args.outfile or sys.stdout
        with output:
            output.write(prettified)
            output.write('\n')
Exemplo n.º 3
0
def test_multiple_statement_safety_belt():
    sql1 = parse_sql('select a from x; select b from y')
    sql2 = parse_sql('select a from x;\n\nselect b from y')
    assert sql1 != sql2
    _remove_stmt_len_and_location(sql1)
    _remove_stmt_len_and_location(sql2)
    assert sql1 == sql2
Exemplo n.º 4
0
def test_printers_roundtrip(src, lineno, statement):
    try:
        orig_ast = parse_sql(statement)
    except:  # noqa
        raise RuntimeError("%s:%d:Could not parse %r" % (src, lineno, statement))

    _remove_stmt_len_and_location(orig_ast)

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

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

    # Run ``pytest -s tests/`` to see the following output
    print()
    print(indented)
Exemplo n.º 5
0
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
    print()
    print(indented)