summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorAndi Albrecht <albrecht.andi@gmail.com>2016-08-13 17:38:21 +0200
committerAndi Albrecht <albrecht.andi@gmail.com>2016-08-13 17:38:21 +0200
commit2893bd1857d685cf892beac3a7429d03cf1a09f1 (patch)
tree1fc1a427841391137820355f33cdaac119c080b6 /tests
parentb7a30d04427e4e4cbc66d08b780ffbb23ab44931 (diff)
downloadsqlparse-2893bd1857d685cf892beac3a7429d03cf1a09f1.tar.gz
Parse double dollars (PostgreSQL) as literal strings (fixes #277).
Diffstat (limited to 'tests')
-rw-r--r--tests/files/function_psql4.sql12
-rw-r--r--tests/test_parse.py19
-rw-r--r--tests/test_split.py3
3 files changed, 33 insertions, 1 deletions
diff --git a/tests/files/function_psql4.sql b/tests/files/function_psql4.sql
new file mode 100644
index 0000000..02900a6
--- /dev/null
+++ b/tests/files/function_psql4.sql
@@ -0,0 +1,12 @@
+CREATE FUNCTION doubledollarinbody(var1 text) RETURNS text
+/* see issue277 */
+LANGUAGE plpgsql
+AS $_$
+DECLARE
+ str text;
+ BEGIN
+ str = $$'foo'$$||var1;
+ execute 'select '||str into str;
+ return str;
+ END
+$_$;
diff --git a/tests/test_parse.py b/tests/test_parse.py
index 2d23425..8dd1150 100644
--- a/tests/test_parse.py
+++ b/tests/test_parse.py
@@ -384,3 +384,22 @@ def test_stmt_tokens_parents():
stmt = sqlparse.parse(s)[0]
for token in stmt.tokens:
assert token.has_ancestor(stmt)
+
+
+@pytest.mark.parametrize('sql, is_literal', [
+ ('$$foo$$', True),
+ ('$_$foo$_$', True),
+ ('$token$ foo $token$', True),
+ # don't parse inner tokens
+ ('$_$ foo $token$bar$token$ baz$_$', True),
+ ('$A$ foo $B$', False) # tokens don't match
+])
+def test_dbldollar_as_literal(sql, is_literal):
+ # see issue 277
+ p = sqlparse.parse(sql)[0]
+ if is_literal:
+ assert len(p.tokens) == 1
+ assert p.tokens[0].ttype == T.Literal
+ else:
+ for token in p.tokens:
+ assert token.ttype != T.Literal
diff --git a/tests/test_split.py b/tests/test_split.py
index af7c9ce..5d846bf 100644
--- a/tests/test_split.py
+++ b/tests/test_split.py
@@ -27,7 +27,8 @@ def test_split_backslash():
@pytest.mark.parametrize('fn', ['function.sql',
'function_psql.sql',
'function_psql2.sql',
- 'function_psql3.sql'])
+ 'function_psql3.sql',
+ 'function_psql4.sql'])
def test_split_create_function(load_file, fn):
sql = load_file(fn)
stmts = sqlparse.parse(sql)