]> git.phdru.name Git - sqlconvert.git/commitdiff
Add a test for unescaped strings for Postgres
authorOleg Broytman <phd@phdru.name>
Sat, 24 Sep 2016 20:23:35 +0000 (23:23 +0300)
committerOleg Broytman <phd@phdru.name>
Sat, 24 Sep 2016 20:23:35 +0000 (23:23 +0300)
Use SQLObject for testing.

ChangeLog
TODO
requirements_dev.txt
setup.cfg
tests/Makefile
tests/conftest.py [new file with mode: 0644]
tests/test_mysql2postgres.py [new file with mode: 0644]
tox.ini

index be296d601c7ada01744f97352d374867ac705b2b..a15fe3699b373df4af470258439aac018a0a015d 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -4,9 +4,9 @@ Version 0.0.6 (2016-09-??)
 
    Rename remove_directives -> remove_directive_tokens.
 
-   Unescape strings.
+   Unescape strings. Add a test for Postgres.
 
-   Use pytest, coverage and tox for testing.
+   Use pytest, coverage, tox and SQLObject for testing.
 
 Version 0.0.5 (2016-09-07)
 
diff --git a/TODO b/TODO
index 6a5de99b7eb57d50838ef53718f02ecbb580ef9e..347811bcb2d5d001920f1488bdb1800087863ddc 100644 (file)
--- a/TODO
+++ b/TODO
@@ -1,6 +1,6 @@
 Convert string escapes to generic SQL, Postgres- or SQLite-specific.
 
-Add tests for Postgres and SQLite.
+Add tests for SQLite.
 
 
 Convert extended INSERTs to series of plain INSERTs.
index ca2ff5d62c0481b2a56e73b65df745bfdd065c5b..18f30f86432ac4d5f45f3dd24ff504890170b931 100644 (file)
@@ -3,3 +3,5 @@
 pytest
 pytest-cov
 tox >= 1.8
+SQLObject>=2.2.1; python_version >= '2.6' and python_version < '3.0'
+SQLObject>=3.0.0; python_version >= '3.4'
index f91a9caca5762fb9aadad0d39fcb6baf82a660ca..2b26222f157bced59e080dd896be1bb89f5889be 100644 (file)
--- a/setup.cfg
+++ b/setup.cfg
@@ -11,5 +11,5 @@ tag_date = 0
 tag_svn_revision = 0
 
 [flake8]
-exclude = .git,docs/conf.py,ez_setup.py
+exclude = .git,docs/conf.py,tests/conftest.py,ez_setup.py
 
index a6a5e1f64fcca64b44fc4d8a7e2de2345b4999b9..6e7d1d837dfb435d3bc8b287a5c577d5b11cd48a 100644 (file)
@@ -1,4 +1,17 @@
 
 .PHONY: all
-all:
-       python `which pytest`
+all: nodb postgres clean
+
+.PHONY: nodb
+nodb:
+       PYTHONPATH=.. python -m  pytest
+
+.PHONY: postgres
+postgres:
+       createdb test
+       PYTHONPATH=.. python -m  pytest -D postgres:///test
+       dropdb test
+
+.PHONY: clean
+clean:
+       rm -f *.tmp
diff --git a/tests/conftest.py b/tests/conftest.py
new file mode 100644 (file)
index 0000000..b1fda3b
--- /dev/null
@@ -0,0 +1,4 @@
+# noqa: imported for pytest
+
+from sqlobject.conftest import \
+    pytest_addoption, pytest_configure, setup_tests
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\""
diff --git a/tox.ini b/tox.ini
index 37f13e51f696aa73972dafe18bc7a43dee1504c9..8fb179e6389b6135145abc3241764f07fcb27197 100644 (file)
--- a/tox.ini
+++ b/tox.ini
@@ -1,6 +1,6 @@
 [tox]
 minversion = 1.8
-envlist = {py26,py27,py34},{py27,py34}-flake8
+envlist = {py26,py27,py34},{py26,py27,py34}-postgres,{py27,py34}-flake8
 
 # Base test environment settings
 [testenv]
@@ -9,16 +9,22 @@ deps =
     pytest
     pytest-cov
     py26: argparse
+    py26,py27: SQLObject>=2.2.1
+    py34: SQLObject>=3.0.0
     py26,py27: m_lib>=2.0
     py34: m_lib>=3.0
+    postgres: psycopg2
 sitepackages = True
 # Don't fail or warn on uninstalled commands
 whitelist_externals =
     flake8
+    createdb
+    dropdb
 
 [general]
 commands =
     python -m pytest --cov=sqlconvert
+    rm -f *.tmp
 
 [testenv:py26]
 commands = {[general]commands}
@@ -29,6 +35,23 @@ commands = {[general]commands}
 [testenv:py34]
 commands = {[general]commands}
 
+# PostgreSQL test environments
+[postgresql]
+commands =
+    createdb test
+    python -m pytest -D postgres:///test
+    dropdb test
+    rm -f *.tmp
+
+[testenv:py26-postgres]
+commands = {[postgresql]commands}
+
+[testenv:py27-postgres]
+commands = {[postgresql]commands}
+
+[testenv:py34-postgres]
+commands = {[postgresql]commands}
+
 [testenv:py27-flake8]
 deps =
     flake8