|
| 1 | +"""Dropping the comments that trail a statement. |
| 2 | +
|
| 3 | +sqlparse cannot do this for PostgreSQL: it follows MySQL and reads ``#`` as a |
| 4 | +comment marker, while in PostgreSQL ``#`` is the bitwise XOR operator. That is |
| 5 | +dbcli/pgcli issue #1646 and andialbrecht/sqlparse issue #539: `select 17 # 5` |
| 6 | +silently returns 17 instead of 20. |
| 7 | +""" |
| 8 | + |
| 9 | +from pgcli.packages.parseutils import strip_trailing_comments |
| 10 | + |
| 11 | + |
| 12 | +def test_hash_is_an_operator_not_a_comment(): |
| 13 | + """The reported bug: everything after # was dropped.""" |
| 14 | + assert strip_trailing_comments("select 17 # 5") == "select 17 # 5" |
| 15 | + assert strip_trailing_comments("select 17 # 5;") == "select 17 # 5;" |
| 16 | + |
| 17 | + |
| 18 | +def test_hash_keeps_its_own_trailing_comment_separate(): |
| 19 | + assert strip_trailing_comments("select 17 # 5; -- note") == "select 17 # 5;" |
| 20 | + |
| 21 | + |
| 22 | +def test_trailing_line_comment_is_dropped(): |
| 23 | + """What #1559 fixed: rstrip(';') needs the comment gone first.""" |
| 24 | + assert strip_trailing_comments("vacuum freeze verbose t; -- 82% towards emergency") == "vacuum freeze verbose t;" |
| 25 | + |
| 26 | + |
| 27 | +def test_trailing_block_comment_is_dropped(): |
| 28 | + assert strip_trailing_comments("select 1; /* done */") == "select 1;" |
| 29 | + |
| 30 | + |
| 31 | +def test_nested_block_comments(): |
| 32 | + """PostgreSQL block comments nest, unlike C.""" |
| 33 | + assert strip_trailing_comments("select 1; /* a /* b */ c */") == "select 1;" |
| 34 | + |
| 35 | + |
| 36 | +def test_several_trailing_comments(): |
| 37 | + assert strip_trailing_comments("select 1;\n-- one\n/* two */\n-- three") == "select 1;" |
| 38 | + |
| 39 | + |
| 40 | +def test_comments_in_the_middle_are_kept(): |
| 41 | + """Only trailing comments go: the server should see the rest as written.""" |
| 42 | + for sql in ("select 1, /* keep */ 2;", "select 1\n-- keep\n, 2;"): |
| 43 | + assert strip_trailing_comments(sql) == sql |
| 44 | + |
| 45 | + |
| 46 | +def test_comment_markers_inside_strings_are_text(): |
| 47 | + for sql in ("select '-- not a comment';", "select 'a /* b */ c';", "select 'a # b';"): |
| 48 | + assert strip_trailing_comments(sql) == sql |
| 49 | + |
| 50 | + |
| 51 | +def test_doubled_quotes_inside_strings(): |
| 52 | + sql = "select 'it''s -- fine';" |
| 53 | + assert strip_trailing_comments(sql) == sql |
| 54 | + |
| 55 | + |
| 56 | +def test_quoted_identifiers(): |
| 57 | + sql = 'select 1 as "-- odd name";' |
| 58 | + assert strip_trailing_comments(sql) == sql |
| 59 | + |
| 60 | + |
| 61 | +def test_dollar_quoted_body_is_left_alone(): |
| 62 | + """A function body holds its own comments and must survive intact.""" |
| 63 | + sql = "do $$ begin -- inside\n perform 1; end $$;" |
| 64 | + assert strip_trailing_comments(sql) == sql |
| 65 | + tagged = "create function f() returns int as $body$ select 1; -- inner\n$body$ language sql;" |
| 66 | + assert strip_trailing_comments(tagged) == tagged |
| 67 | + |
| 68 | + |
| 69 | +def test_only_a_comment_leaves_nothing(): |
| 70 | + assert strip_trailing_comments("-- just a note") == "" |
| 71 | + assert strip_trailing_comments("/* just a note */") == "" |
| 72 | + |
| 73 | + |
| 74 | +def test_empty_and_whitespace(): |
| 75 | + assert strip_trailing_comments("") == "" |
| 76 | + assert strip_trailing_comments(" \n ") == "" |
| 77 | + |
| 78 | + |
| 79 | +def test_unterminated_block_comment(): |
| 80 | + assert strip_trailing_comments("select 1; /* never closed") == "select 1;" |
| 81 | + |
| 82 | + |
| 83 | +def test_unterminated_string_does_not_hang(): |
| 84 | + assert strip_trailing_comments("select 'never closed") == "select 'never closed" |
0 commit comments