]> git.phdru.name Git - sqlconvert.git/blobdiff - tests/test_mysql2postgres.py
Add a test for unescaped strings for Postgres
[sqlconvert.git] / tests / test_mysql2postgres.py
diff --git a/tests/test_mysql2postgres.py b/tests/test_mysql2postgres.py
new file mode 100644 (file)
index 0000000..37edb57
--- /dev/null
@@ -0,0 +1,29 @@
+from sqlparse import parse
+from sqlobject.tests.dbtest import getConnection
+import py.test
+
+from sqlconvert.print_tokens import tlist2str
+from sqlconvert.process_mysql import unescape_strings
+
+
+create_postgres_test_table = """
+CREATE TABLE test (
+    id serial PRIMARY KEY,
+    test_str VARCHAR(255) NOT NULL
+);
+"""
+
+
+def test_mysql2postgres_string():
+    connection = getConnection()
+    if connection.dbName != "postgres":
+        py.test.skip("This test requires PostgreSQL")
+    connection.query(create_postgres_test_table)
+    parsed = parse("insert into test (id, test_str) "
+                   "values (1, '\"te\\'st\\\"')")[0]
+    unescape_strings(parsed)
+    query = tlist2str(parsed)
+    assert query == u"INSERT INTO test (id, test_str) VALUES (1, '\"te''st\"')"
+    connection.query(query)
+    test_str = connection.queryOne("SELECT test_str FROM test WHERE id=1")[0]
+    assert test_str == u"\"te'st\""