-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathtest_edge_cases.py
More file actions
67 lines (49 loc) · 2.5 KB
/
Copy pathtest_edge_cases.py
File metadata and controls
67 lines (49 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""Edge-case tests exercised through the public :class:`Parser` API.
These tests cover degenerate inputs (empty SQL after paren stripping,
unterminated comments, deeply nested parentheses) by feeding them into
``Parser`` and asserting on its public properties — no internal helpers
are imported.
"""
from sql_metadata import Parser
from sql_metadata.sql_cleaner import SqlCleaner
from sql_metadata.utils import UniqueList
def test_unique_list_subtraction():
"""UniqueList.__sub__ returns elements not present in the other list."""
ul = UniqueList(["a", "b", "c", "d"])
result = ul - ["b", "d"]
assert result == ["a", "c"]
def test_unique_list_deduplicates_on_init():
"""UniqueList removes duplicates when constructed from an iterable."""
ul = UniqueList(["x", "y", "x", "z", "y"])
assert list(ul) == ["x", "y", "z"]
def test_extract_comments_unterminated_block_comment():
"""Unterminated block comment causes tokenizer failure — returns []."""
parser = Parser("/*")
assert parser.comments == []
def test_strip_comments_unterminated_block_comment():
"""Unterminated block comment in strip_comments returns input stripped."""
parser = Parser("/*")
assert parser.without_comments == "/*"
def test_preprocess_query_unterminated_block_comment():
"""Tokenizer failure on Parser.query falls back to whitespace collapse."""
# Exercises the TokenError branch in SqlCleaner.preprocess_query.
assert Parser("/*").query == "/*"
assert Parser(" /*\n ").query == "/*"
def test_clean_empty_after_paren_strip():
"""SQL that becomes empty after outer-paren stripping."""
result = SqlCleaner.clean("(())")
assert result.sql is None
def test_strip_outer_parens_depth_guard():
"""Deeply nested parentheses don't stack-overflow the cleaner's recursion."""
# 150 levels exceeds the 100-deep recursion guard in _strip_outer_parens;
# parsing through Parser must return gracefully rather than raise
# RecursionError.
parser = Parser("(" * 150 + "SELECT 1" + ")" * 150)
assert parser.columns == []
def test_strip_outer_parens_unbalanced_middle():
"""Queries that look paren-wrapped but aren't (UNION of parenthesised SELECTs)."""
# "(SELECT ...) UNION (SELECT ...)" starts with "(" and ends with ")" but the
# inner parens go negative — _is_wrapped must short-circuit and leave the SQL
# intact so both SELECT branches parse.
parser = Parser("(SELECT a FROM t1) UNION (SELECT b FROM t2)")
assert parser.tables == ["t1", "t2"]