From 00f0aef5739a19921ed06f0397b44ba336184424 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Sat, 24 Sep 2016 23:23:35 +0300 Subject: [PATCH] Add a test for unescaped strings for Postgres Use SQLObject for testing. --- ChangeLog | 4 ++-- TODO | 2 +- requirements_dev.txt | 2 ++ setup.cfg | 2 +- tests/Makefile | 17 +++++++++++++++-- tests/conftest.py | 4 ++++ tests/test_mysql2postgres.py | 29 +++++++++++++++++++++++++++++ tox.ini | 25 ++++++++++++++++++++++++- 8 files changed, 78 insertions(+), 7 deletions(-) create mode 100644 tests/conftest.py create mode 100644 tests/test_mysql2postgres.py diff --git a/ChangeLog b/ChangeLog index be296d6..a15fe36 100644 --- 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 6a5de99..347811b 100644 --- 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. diff --git a/requirements_dev.txt b/requirements_dev.txt index ca2ff5d..18f30f8 100644 --- a/requirements_dev.txt +++ b/requirements_dev.txt @@ -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' diff --git a/setup.cfg b/setup.cfg index f91a9ca..2b26222 100644 --- 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 diff --git a/tests/Makefile b/tests/Makefile index a6a5e1f..6e7d1d8 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -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 index 0000000..b1fda3b --- /dev/null +++ b/tests/conftest.py @@ -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 index 0000000..37edb57 --- /dev/null +++ b/tests/test_mysql2postgres.py @@ -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 37f13e5..8fb179e 100644 --- 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 -- 2.39.2