def test_errors(): with pytest.raises(Error) as exc: parse_sql('FooBar') assert exc.typename == 'ParseError' assert exc.value.location == 1 assert 'syntax error ' in str(exc.value) with pytest.raises(Error) as exc: parse_sql('SELECT foo FRON bar') assert exc.typename == 'ParseError' assert exc.value.location == 17 errmsg = str(exc.value) assert 'syntax error at or near "bar"' in errmsg assert 'location 17' in errmsg with pytest.raises(Error) as exc: parse_plpgsql('CREATE FUMCTION add (a integer, b integer)' ' RETURNS integer AS $$ BEGIN RETURN a + b; END; $$' ' LANGUAGE plpgsql') assert exc.typename == 'ParseError' assert exc.value.location == 8 errmsg = str(exc.value) assert 'syntax error at or near "FUMCTION"' in errmsg assert 'location 8' in errmsg with pytest.raises(Error) as exc: fingerprint('SELECT foo FRON bar') assert exc.typename == 'ParseError' assert exc.value.location == 17 errmsg = str(exc.value) assert 'syntax error at or near "bar"' in errmsg assert 'location 17' in errmsg
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')
def test_basic(): ptree = parse_sql('SELECT 1') assert isinstance(ptree, list) assert len(ptree) == 1 rawstmt = ptree[0] assert isinstance(rawstmt, dict) assert rawstmt.keys() == {'RawStmt'} ptree = parse_plpgsql('CREATE FUNCTION add (a integer, b integer)' ' RETURNS integer AS $$ BEGIN RETURN a + b; END; $$' ' LANGUAGE plpgsql') assert len(ptree) == 1 function = ptree[0] assert isinstance(function, dict) assert function.keys() == {'PLpgSQL_function'}