]> git.phdru.name Git - sqlconvert.git/blobdiff - tests/test_process_mysql.py
Fix: Adapt to newer `sqlparse`
[sqlconvert.git] / tests / test_process_mysql.py
index 500a35db746f146e3eed543cc6c10b633a6e4700..420a219d22efb26004a657df56f48f15c71aa181 100644 (file)
@@ -1,11 +1,12 @@
 # -*- coding: utf-8 -*-
 
+import pytest
 from sqlparse import parse
 
 from sqlconvert.print_tokens import tlist2str
 from sqlconvert.process_mysql import remove_directive_tokens, \
         is_directive_statement, requote_names, unescape_strings, \
-        process_statement
+        get_DML_type, process_statement
 from sqlconvert.process_tokens import escape_strings
 
 
@@ -53,6 +54,51 @@ def test_escape_string_sqlite():
     assert query == u"INSERT INTO test VALUES ('\"te''st\"\n')"
 
 
+def test_DML_type():
+    parsed = parse("create table test ();")[0]
+    statement = next(process_statement(parsed))
+    with pytest.raises(ValueError):
+        get_DML_type(statement)
+
+    parsed = parse("select /*! test */ * from /* test */ `T`")[0]
+    statement = next(process_statement(parsed))
+    assert get_DML_type(statement) == "SELECT"
+
+    parsed = parse("insert into test values ('\"te\\'st\\\"\\n')")[0]
+    statement = next(process_statement(parsed))
+    assert get_DML_type(statement) == "INSERT"
+
+
+def test_split_ext_insert():
+    parsed = parse("insert into test values (1, 2)")[0]
+    statement = next(process_statement(parsed))
+    query = tlist2str(statement)
+    assert query == u"INSERT INTO test VALUES (1, 2)"
+
+    parsed = parse("insert into test (age, salary) values (1, 2);")[0]
+    statement = next(process_statement(parsed))
+    query = tlist2str(statement)
+    assert query == u"INSERT INTO test (age, salary) VALUES (1, 2);"
+
+    parsed = parse("insert into test values (1, 2), (3, 4);")[0]
+    stiter = process_statement(parsed)
+    statement = next(stiter)
+    query = tlist2str(statement)
+    assert query == u"INSERT INTO test VALUES (1, 2);\n"
+    statement = next(stiter)
+    query = tlist2str(statement)
+    assert query == u"INSERT INTO test VALUES (3, 4);"
+
+    parsed = parse("insert into test (age, salary) values (1, 2), (3, 4)")[0]
+    stiter = process_statement(parsed)
+    statement = next(stiter)
+    query = tlist2str(statement)
+    assert query == u"INSERT INTO test (age, salary) VALUES (1, 2)\n"
+    statement = next(stiter)
+    query = tlist2str(statement)
+    assert query == u"INSERT INTO test (age, salary) VALUES (3, 4)"
+
+
 def test_process():
     parsed = parse("select /*! test */ * from /* test */ `T`")[0]
     statement = next(process_statement(parsed))