Skip to content

Commit e37eaea

Browse files
mrmasterplanandialbrecht
authored andcommitted
test configurable syntax
1 parent 9a1cb5d commit e37eaea

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

tests/test_parse.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import sqlparse
77
from sqlparse import sql, tokens as T
8+
from sqlparse.lexer import Lexer
89

910

1011
def test_parse_tokenize():
@@ -489,3 +490,45 @@ def test_parenthesis():
489490
T.Newline,
490491
T.Newline,
491492
T.Punctuation]
493+
494+
495+
def test_configurable_syntax():
496+
sql = """select * from foo BACON SPAM EGGS;"""
497+
# sql="""select * from mydb.mytable BACON SPAM EGGS;"""
498+
tokens = sqlparse.parse(sql)[0]
499+
500+
assert list(
501+
(t.ttype, t.value) for t in tokens if t.ttype not in sqlparse.tokens.Whitespace
502+
) == [
503+
(sqlparse.tokens.Keyword.DML, "select"),
504+
(sqlparse.tokens.Wildcard, "*"),
505+
(sqlparse.tokens.Keyword, "from"),
506+
(None, "foo BACON"),
507+
(None, "SPAM EGGS"),
508+
(sqlparse.tokens.Punctuation, ";"),
509+
]
510+
511+
Lexer().add_keywords(
512+
{
513+
"BACON": sqlparse.tokens.Name.Builtin,
514+
"SPAM": sqlparse.tokens.Keyword,
515+
"EGGS": sqlparse.tokens.Keyword,
516+
}
517+
)
518+
519+
tokens = sqlparse.parse(sql)[0]
520+
521+
assert list(
522+
(t.ttype, t.value) for t in tokens if t.ttype not in sqlparse.tokens.Whitespace
523+
) == [
524+
(sqlparse.tokens.Keyword.DML, "select"),
525+
(sqlparse.tokens.Wildcard, "*"),
526+
(sqlparse.tokens.Keyword, "from"),
527+
(None, "foo"),
528+
(sqlparse.tokens.Name.Builtin, "BACON"),
529+
(sqlparse.tokens.Keyword, "SPAM"),
530+
(sqlparse.tokens.Keyword, "EGGS"),
531+
(sqlparse.tokens.Punctuation, ";"),
532+
]
533+
# reset the syntax for later tests.
534+
Lexer().default_initialization()

0 commit comments

Comments
 (0)