-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_cflexer.py
397 lines (328 loc) · 11.7 KB
/
test_cflexer.py
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
import pytest
import cflexer
from cftoken import CFToken, CFTokenKind
def test_none():
text = None
expected = []
actual = cflexer.lex(text)
assert expected == actual
def test_empty():
text = ""
expected = []
actual = cflexer.lex(text)
assert expected == actual
def test_newline():
text = "\n"
expected = [CFToken(CFTokenKind.NEWLINE, "\n")]
actual = cflexer.lex(text)
assert expected == actual
def test_spaces():
for i in range(1, 10): # arbitrary
i_spaces = " " * i
expected = [CFToken(CFTokenKind.SPACES, i_spaces)]
actual = cflexer.lex(i_spaces)
assert expected == actual
def test_integer_literal():
text = "1"
expected = [CFToken(CFTokenKind.WORD, "1")]
actual = cflexer.lex(text)
assert expected == actual
def test_decimal_literal():
text = "3.14"
expected = [CFToken(CFTokenKind.WORD, "3.14")]
actual = cflexer.lex(text)
assert expected == actual
def test_scientific_literal():
text = "1.23e4"
expected = [CFToken(CFTokenKind.WORD, "1.23e4")]
actual = cflexer.lex(text)
assert expected == actual
def test_word():
text = "foo"
expected = [CFToken(CFTokenKind.WORD, "foo")]
actual = cflexer.lex(text)
assert expected == actual
def test_words_with_spaces():
text = "foo bar baz"
expected = [
CFToken(CFTokenKind.WORD, "foo"),
CFToken(CFTokenKind.SPACES, " "),
CFToken(CFTokenKind.WORD, "bar"),
CFToken(CFTokenKind.SPACES, " "),
CFToken(CFTokenKind.WORD, "baz"),
]
actual = cflexer.lex(text)
assert expected == actual
def test_words_with_newlines():
text = "foo\nbar\nbaz"
expected = [
CFToken(CFTokenKind.WORD, "foo"),
CFToken(CFTokenKind.NEWLINE, "\n"),
CFToken(CFTokenKind.WORD, "bar"),
CFToken(CFTokenKind.NEWLINE, "\n"),
CFToken(CFTokenKind.WORD, "baz"),
]
actual = cflexer.lex(text)
assert expected == actual
def test_single_quoted_literal():
text = "'literal'"
expected = [CFToken(CFTokenKind.LITERAL, "'literal'")]
actual = cflexer.lex(text)
assert expected == actual
def test_single_quoted_literal_with_escaped_quote1():
text = "'bob''s'"
expected = [CFToken(CFTokenKind.LITERAL, "'bob''s'")]
actual = cflexer.lex(text)
assert expected == actual
def test_single_quoted_literal_with_escaped_quote2():
text = r"'bob\'s'"
expected = [CFToken(CFTokenKind.LITERAL, r"'bob\'s'")]
actual = cflexer.lex(text)
assert expected == actual
def test_single_quoted_literal_multiline():
text = "'line one\nline two'"
expected = [CFToken(CFTokenKind.LITERAL, "'line one\nline two'")]
actual = cflexer.lex(text)
assert expected == actual
def test_double_quoted_identifier():
text = '"identifier"'
expected = [CFToken(CFTokenKind.LITERAL, '"identifier"')]
actual = cflexer.lex(text)
assert expected == actual
def test_double_quoted_identifier_with_escaped_quote1():
text = '"this is a ""bad"" identifier"'
expected = [CFToken(CFTokenKind.LITERAL, '"this is a ""bad"" identifier"')]
actual = cflexer.lex(text)
assert expected == actual
def test_double_quoted_identifier_with_escaped_quote2():
text = r'"this is a \"bad\" identifier"'
expected = [CFToken(CFTokenKind.LITERAL, r'"this is a \"bad\" identifier"')]
actual = cflexer.lex(text)
assert expected == actual
def test_backtick_quoted_identifier():
text = "`identifier`"
expected = [CFToken(CFTokenKind.LITERAL, "`identifier`")]
actual = cflexer.lex(text)
assert expected == actual
def test_backtick_quoted_identifier_with_escaped_quote1():
text = "`one ``two`` three`"
expected = [CFToken(CFTokenKind.LITERAL, "`one ``two`` three`")]
actual = cflexer.lex(text)
assert expected == actual
def test_backtick_quoted_identifier_with_escaped_quote2():
text = r"`one \`two\` three`"
expected = [CFToken(CFTokenKind.LITERAL, r"`one \`two\` three`")]
actual = cflexer.lex(text)
assert expected == actual
def test_dotted_identifier():
text = "foo.bar"
expected = [CFToken(CFTokenKind.WORD, "foo"), CFToken(CFTokenKind.SYMBOL, "."), CFToken(CFTokenKind.WORD, "bar")]
actual = cflexer.lex(text)
assert expected == actual
def test_line_comment(): # this one case is probably not sufficient
text = "foo\n--hey\nbar"
expected = [
CFToken(CFTokenKind.WORD, "foo"),
CFToken(CFTokenKind.NEWLINE, "\n"),
CFToken(CFTokenKind.LINE_COMMENT, "--hey\n"),
CFToken(CFTokenKind.WORD, "bar"),
]
actual = cflexer.lex(text)
assert expected == actual
def test_block_comment(): # this one case is probably not sufficient
text = "foo/* hey */bar"
expected = [
CFToken(CFTokenKind.WORD, "foo"),
CFToken(CFTokenKind.BLOCK_COMMENT, "/* hey */"),
CFToken(CFTokenKind.WORD, "bar"),
]
actual = cflexer.lex(text)
assert expected == actual
def test_symbols():
symbols = [
"~",
"!",
"@",
"#",
"$",
"%",
"^",
"&",
"*",
"(",
")",
"-",
"+",
"=",
"[",
"]",
"{",
"}",
"|",
",",
"<",
">",
"?",
"/",
":",
";",
",",
".",
]
for s in symbols:
expected = [CFToken(CFTokenKind.SYMBOL, s)]
actual = cflexer.lex(s)
assert expected == actual
def test_two_word_key_phrases():
phrases = [
'cross join',
'distinct on',
'inner join',
'left join',
'right join',
'group by',
'order by',
'partition by',
'within group',
'is false',
'is null',
'is true',
'is unknown',
'not between',
'between symmetric',
'union all',
'union distinct',
'except all',
'except distinct',
'intersect all',
'intersect distinct',
]
for phrase in phrases:
expected = [CFToken(CFTokenKind.WORD, phrase)]
actual = cflexer.lex(phrase)
assert expected == actual
for phrase in [p.upper() for p in phrases]:
expected = [CFToken(CFTokenKind.WORD, phrase)]
actual = cflexer.lex(phrase)
assert expected == actual
def test_three_word_key_phrases():
phrases = [
'left outer join',
'right outer join',
'full outer join',
'is not false',
'is not null',
'is not true',
'is not unknown',
'is distinct from',
'not between symmetric',
'at time zone',
]
for phrase in phrases:
expected = [CFToken(CFTokenKind.WORD, phrase)]
actual = cflexer.lex(phrase)
assert expected == actual
for phrase in [p.upper() for p in phrases]:
expected = [CFToken(CFTokenKind.WORD, phrase)]
actual = cflexer.lex(phrase)
assert expected == actual
def test_four_word_key_phrases():
phrases = [
'is not distinct from',
]
for phrase in phrases:
expected = [CFToken(CFTokenKind.WORD, phrase)]
actual = cflexer.lex(phrase)
assert expected == actual
for phrase in [p.upper() for p in phrases]:
expected = [CFToken(CFTokenKind.WORD, phrase)]
actual = cflexer.lex(phrase)
assert expected == actual
def test_qualified_identifier_one_dot():
tokens = cflexer.lex("foo.bar") # [(WORD, 'foo'), (SYMBOL, '.'), (WORD, 'bar')]
actual_token, actual_consumed = cflexer.get_qualified_identifier(tokens)
expected_token = CFToken(CFTokenKind.WORD, "foo.bar")
expected_consumed = 3
assert expected_token == actual_token
assert expected_consumed == actual_consumed
def test_qualified_identifier_two_dots():
tokens = cflexer.lex("foo.bar.baz") # [(WORD, 'foo'), (SYMBOL, '.'), (WORD, 'bar'), (SYMBOL, '.'), (WORD, 'baz')]
actual_token, actual_consumed = cflexer.get_qualified_identifier(tokens)
expected_token = CFToken(CFTokenKind.WORD, "foo.bar.baz")
expected_consumed = 5
assert expected_token == actual_token
assert expected_consumed == actual_consumed
def test_qualified_identifier_three_dots():
tokens = cflexer.lex("foo.bar.baz.idk") # [(WORD, 'foo'), (SYMBOL, '.'), (WORD, 'bar'), (SYMBOL, '.'), (WORD, 'baz'), (SYMBOL, '.'), (WORD, 'idk')]
actual_token, actual_consumed = cflexer.get_qualified_identifier(tokens)
expected_token = CFToken(CFTokenKind.WORD, "foo.bar.baz.idk")
expected_consumed = 7
assert expected_token == actual_token
assert expected_consumed == actual_consumed
def test_qualified_identifier_with_quotes_one_dot():
tokens = cflexer.lex('"Alice"."Bob"') # [(WORD, '"Alice"'), (SYMBOL, '.'), (WORD, '"Bob"')]
actual_token, actual_consumed = cflexer.get_qualified_identifier(tokens)
expected_token = CFToken(CFTokenKind.LITERAL, '"Alice"."Bob"')
expected_consumed = 3
assert expected_token == actual_token
assert expected_consumed == actual_consumed
def test_qualified_identifier_with_quotes_two_dots():
tokens = cflexer.lex('"Alice"."Bob"."Cindy"') # [(WORD, '"Alice"'), (SYMBOL, '.'), (WORD, '"Bob"'), (SYMBOL, '.'), (WORD, '"Cindy"')]
actual_token, actual_consumed = cflexer.get_qualified_identifier(tokens)
expected_token = CFToken(CFTokenKind.LITERAL, '"Alice"."Bob"."Cindy"')
expected_consumed = 5
assert expected_token == actual_token
assert expected_consumed == actual_consumed
def test_qualified_identifier_with_mixed_quotes():
tokens = cflexer.lex('alice."Bob"') # [(Token.Name, 'alice'), (Token.Literal.Number.Float, '.'), (Token.Name, '"Bob"')]
actual_token, actual_consumed = cflexer.get_qualified_identifier(tokens)
expected_token = CFToken(CFTokenKind.LITERAL, 'alice."Bob"')
expected_consumed = 3
assert expected_token == actual_token
assert expected_consumed == actual_consumed
def test_collapse_identifiers():
sql = (
"select table_name.column_name\n"
" , schema_name.table_name.column_name\n"
" , db_name.schema_name.table_name.column_name\n"
" , \"Quoted\".\"Name\"\n"
" , \"Longer\".\"Quoted\".\"Name\"\n"
" from foo"
)
tokens = cflexer.lex(sql)
actual_tokens = cflexer.collapse_identifiers(tokens)
expected_tokens = [
CFToken(CFTokenKind.WORD, 'select'),
CFToken(CFTokenKind.SPACES, ' '),
CFToken(CFTokenKind.WORD, 'table_name.column_name'),
CFToken(CFTokenKind.NEWLINE, '\n'),
CFToken(CFTokenKind.SPACES, ' '),
CFToken(CFTokenKind.SYMBOL, ','),
CFToken(CFTokenKind.SPACES, ' '),
CFToken(CFTokenKind.WORD, 'schema_name.table_name.column_name'),
CFToken(CFTokenKind.NEWLINE, '\n'),
CFToken(CFTokenKind.SPACES, ' '),
CFToken(CFTokenKind.SYMBOL, ','),
CFToken(CFTokenKind.SPACES, ' '),
CFToken(CFTokenKind.WORD, 'db_name.schema_name.table_name'),
CFToken(CFTokenKind.SYMBOL, '.'),
CFToken(CFTokenKind.WORD, 'column_name'),
CFToken(CFTokenKind.NEWLINE, '\n'),
CFToken(CFTokenKind.SPACES, ' '),
CFToken(CFTokenKind.SYMBOL, ','),
CFToken(CFTokenKind.SPACES, ' '),
CFToken(CFTokenKind.LITERAL, '"Quoted"."Name"'),
CFToken(CFTokenKind.NEWLINE, '\n'),
CFToken(CFTokenKind.SPACES, ' '),
CFToken(CFTokenKind.SYMBOL, ','),
CFToken(CFTokenKind.SPACES, ' '),
CFToken(CFTokenKind.LITERAL, '"Longer"."Quoted"."Name"'),
CFToken(CFTokenKind.NEWLINE, '\n'),
CFToken(CFTokenKind.SPACES, ' '),
CFToken(CFTokenKind.WORD, 'from'),
CFToken(CFTokenKind.SPACES, ' '),
CFToken(CFTokenKind.WORD, 'foo'),
]
for t in actual_tokens:
print(t)
assert expected_tokens == actual_tokens