forked from andialbrecht/sqlparse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_split.py
More file actions
286 lines (223 loc) · 8.4 KB
/
test_split.py
File metadata and controls
286 lines (223 loc) · 8.4 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# Tests splitting functions.
import types
from io import StringIO
import pytest
import sqlparse
def test_split_semicolon():
sql1 = 'select * from foo;'
sql2 = "select * from foo where bar = 'foo;bar';"
stmts = sqlparse.parse(''.join([sql1, sql2]))
assert len(stmts) == 2
assert str(stmts[0]) == sql1
assert str(stmts[1]) == sql2
def test_split_backslash():
stmts = sqlparse.parse("select '\'; select '\'';")
assert len(stmts) == 2
@pytest.mark.parametrize('fn', ['function.sql',
'function_psql.sql',
'function_psql2.sql',
'function_psql3.sql',
'function_psql4.sql'])
def test_split_create_function(load_file, fn):
sql = load_file(fn)
stmts = sqlparse.parse(sql)
assert len(stmts) == 1
assert str(stmts[0]) == sql
def test_split_dashcomments(load_file):
sql = load_file('dashcomment.sql')
stmts = sqlparse.parse(sql)
assert len(stmts) == 3
assert ''.join(str(q) for q in stmts) == sql
@pytest.mark.parametrize('s', ['select foo; -- comment\n',
'select foo; -- comment\r',
'select foo; -- comment\r\n',
'select foo; -- comment'])
def test_split_dashcomments_eol(s):
stmts = sqlparse.parse(s)
assert len(stmts) == 1
def test_split_begintag(load_file):
sql = load_file('begintag.sql')
stmts = sqlparse.parse(sql)
assert len(stmts) == 3
assert ''.join(str(q) for q in stmts) == sql
def test_split_begintag_2(load_file):
sql = load_file('begintag_2.sql')
stmts = sqlparse.parse(sql)
assert len(stmts) == 1
assert ''.join(str(q) for q in stmts) == sql
def test_split_dropif():
sql = 'DROP TABLE IF EXISTS FOO;\n\nSELECT * FROM BAR;'
stmts = sqlparse.parse(sql)
assert len(stmts) == 2
assert ''.join(str(q) for q in stmts) == sql
def test_split_comment_with_umlaut():
sql = ('select * from foo;\n'
'-- Testing an umlaut: ä\n'
'select * from bar;')
stmts = sqlparse.parse(sql)
assert len(stmts) == 2
assert ''.join(str(q) for q in stmts) == sql
def test_split_comment_end_of_line():
sql = ('select * from foo; -- foo\n'
'select * from bar;')
stmts = sqlparse.parse(sql)
assert len(stmts) == 2
assert ''.join(str(q) for q in stmts) == sql
# make sure the comment belongs to first query
assert str(stmts[0]) == 'select * from foo; -- foo\n'
def test_split_casewhen():
sql = ("SELECT case when val = 1 then 2 else null end as foo;\n"
"comment on table actor is 'The actor table.';")
stmts = sqlparse.split(sql)
assert len(stmts) == 2
def test_split_casewhen_procedure(load_file):
# see issue580
stmts = sqlparse.split(load_file('casewhen_procedure.sql'))
assert len(stmts) == 2
def test_split_cursor_declare():
sql = ('DECLARE CURSOR "foo" AS SELECT 1;\n'
'SELECT 2;')
stmts = sqlparse.split(sql)
assert len(stmts) == 2
def test_split_if_function(): # see issue 33
# don't let IF as a function confuse the splitter
sql = ('CREATE TEMPORARY TABLE tmp '
'SELECT IF(a=1, a, b) AS o FROM one; '
'SELECT t FROM two')
stmts = sqlparse.split(sql)
assert len(stmts) == 2
def test_split_stream():
stream = StringIO("SELECT 1; SELECT 2;")
stmts = sqlparse.parsestream(stream)
assert isinstance(stmts, types.GeneratorType)
assert len(list(stmts)) == 2
def test_split_encoding_parsestream():
stream = StringIO("SELECT 1; SELECT 2;")
stmts = list(sqlparse.parsestream(stream))
assert isinstance(stmts[0].tokens[0].value, str)
def test_split_unicode_parsestream():
stream = StringIO('SELECT ö')
stmts = list(sqlparse.parsestream(stream))
assert str(stmts[0]) == 'SELECT ö'
def test_split_simple():
stmts = sqlparse.split('select * from foo; select * from bar;')
assert len(stmts) == 2
assert stmts[0] == 'select * from foo;'
assert stmts[1] == 'select * from bar;'
def test_split_ignores_empty_newlines():
stmts = sqlparse.split('select foo;\nselect bar;\n')
assert len(stmts) == 2
assert stmts[0] == 'select foo;'
assert stmts[1] == 'select bar;'
def test_split_quotes_with_new_line():
stmts = sqlparse.split('select "foo\nbar"')
assert len(stmts) == 1
assert stmts[0] == 'select "foo\nbar"'
stmts = sqlparse.split("select 'foo\n\bar'")
assert len(stmts) == 1
assert stmts[0] == "select 'foo\n\bar'"
def test_split_mysql_handler_for(load_file):
# see issue581
stmts = sqlparse.split(load_file('mysql_handler.sql'))
assert len(stmts) == 2
@pytest.mark.parametrize('sql, expected', [
('select * from foo;', ['select * from foo']),
('select * from foo', ['select * from foo']),
('select * from foo; select * from bar;', [
'select * from foo',
'select * from bar',
]),
(' select * from foo;\n\nselect * from bar;\n\n\n\n', [
'select * from foo',
'select * from bar',
]),
('select * from foo\n\n; bar', ['select * from foo', 'bar']),
])
def test_split_strip_semicolon(sql, expected):
stmts = sqlparse.split(sql, strip_semicolon=True)
assert len(stmts) == len(expected)
for idx, expectation in enumerate(expected):
assert stmts[idx] == expectation
def test_split_strip_semicolon_procedure(load_file):
stmts = sqlparse.split(load_file('mysql_handler.sql'),
strip_semicolon=True)
assert len(stmts) == 2
assert stmts[0].endswith('end')
assert stmts[1].endswith('end')
@pytest.mark.parametrize('sql, num', [
('USE foo;\nGO\nSELECT 1;\nGO', 4),
('SELECT * FROM foo;\nGO', 2),
('USE foo;\nGO 2\nSELECT 1;', 3)
])
def test_split_go(sql, num): # issue762
stmts = sqlparse.split(sql)
assert len(stmts) == num
def test_split_multiple_case_in_begin(load_file): # issue784
stmts = sqlparse.split(load_file('multiple_case_in_begin.sql'))
assert len(stmts) == 1
def test_split_if_exists_in_begin_end(): # issue812
# IF EXISTS should not be confused with control flow IF
sql = """CREATE TASK t1 AS
BEGIN
CREATE OR REPLACE TABLE temp1;
DROP TABLE IF EXISTS temp1;
END;
EXECUTE TASK t1;"""
stmts = sqlparse.split(sql)
assert len(stmts) == 2
assert 'CREATE TASK' in stmts[0]
assert 'EXECUTE TASK' in stmts[1]
def test_split_begin_end_semicolons(): # issue809
# Semicolons inside BEGIN...END blocks should not split statements
sql = """WITH
FUNCTION meaning_of_life()
RETURNS tinyint
BEGIN
DECLARE a tinyint DEFAULT CAST(6 as tinyint);
DECLARE b tinyint DEFAULT CAST(7 as tinyint);
RETURN a * b;
END
SELECT meaning_of_life();"""
stmts = sqlparse.split(sql)
assert len(stmts) == 1
assert 'WITH' in stmts[0]
assert 'SELECT meaning_of_life()' in stmts[0]
def test_split_begin_end_procedure(): # issue809
# Test with CREATE PROCEDURE (BigQuery style)
sql = """CREATE OR REPLACE PROCEDURE mydataset.create_customer()
BEGIN
DECLARE id STRING;
SET id = GENERATE_UUID();
INSERT INTO mydataset.customers (customer_id)
VALUES(id);
SELECT FORMAT("Created customer %s", id);
END;"""
stmts = sqlparse.split(sql)
assert len(stmts) == 1
assert 'CREATE OR REPLACE PROCEDURE' in stmts[0]
def test_split_begin_transaction(): # issue826
# BEGIN TRANSACTION should not be treated as a block start
sql = """BEGIN TRANSACTION;
DELETE FROM "schema"."table_a" USING "table_a_temp" WHERE "schema"."table_a"."id" = "table_a_temp"."id";
INSERT INTO "schema"."table_a" SELECT * FROM "table_a_temp";
END TRANSACTION;"""
stmts = sqlparse.split(sql)
assert len(stmts) == 4
assert stmts[0] == 'BEGIN TRANSACTION;'
assert stmts[1].startswith('DELETE')
assert stmts[2].startswith('INSERT')
assert stmts[3] == 'END TRANSACTION;'
def test_split_begin_transaction_formatted(): # issue826
# Test with formatted SQL (newlines between BEGIN and TRANSACTION)
sql = """BEGIN
TRANSACTION;
DELETE FROM "schema"."table_a" USING "table_a_temp" WHERE "schema"."table_a"."id" = "table_a_temp"."id";
INSERT INTO "schema"."table_a" SELECT * FROM "table_a_temp";
END
TRANSACTION;"""
stmts = sqlparse.split(sql)
assert len(stmts) == 4
assert stmts[0] == 'BEGIN\nTRANSACTION;'
assert stmts[1].startswith('DELETE')
assert stmts[2].startswith('INSERT')
assert stmts[3] == 'END\nTRANSACTION;'