From 26ca2f53d8fe333bb2448ea412e82be415cb3fd2 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Sun, 25 Sep 2016 18:25:49 +0300 Subject: [PATCH] Add tests for SQLite --- ChangeLog | 2 ++ TODO | 2 -- tests/Makefile | 7 ++++++- tests/test_mysql2sqlite.py | 32 ++++++++++++++++++++++++++++++++ tox.ini | 19 ++++++++++++++++++- 5 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 tests/test_mysql2sqlite.py diff --git a/ChangeLog b/ChangeLog index cd39888..5193324 100644 --- a/ChangeLog +++ b/ChangeLog @@ -10,6 +10,8 @@ Version 0.0.6 (2016-09-??) Use pytest, coverage and tox for testing. + Add tests for Postgres and SQLite. + Version 0.0.5 (2016-09-07) Remove /*! directives */; and newlines after them. diff --git a/TODO b/TODO index 347811b..26baf84 100644 --- a/TODO +++ b/TODO @@ -1,6 +1,4 @@ Convert string escapes to generic SQL, Postgres- or SQLite-specific. -Add tests for SQLite. - Convert extended INSERTs to series of plain INSERTs. diff --git a/tests/Makefile b/tests/Makefile index 6e7d1d8..444794b 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -1,6 +1,6 @@ .PHONY: all -all: nodb postgres clean +all: nodb postgres sqlite clean .PHONY: nodb nodb: @@ -12,6 +12,11 @@ postgres: PYTHONPATH=.. python -m pytest -D postgres:///test dropdb test +.PHONY: sqlite +sqlite: + PYTHONPATH=.. python -m pytest -D sqlite:///tmp/test.sqdb + rm -f /tmp/test.sqdb + .PHONY: clean clean: rm -f *.tmp diff --git a/tests/test_mysql2sqlite.py b/tests/test_mysql2sqlite.py new file mode 100644 index 0000000..b5ae10f --- /dev/null +++ b/tests/test_mysql2sqlite.py @@ -0,0 +1,32 @@ +from sqlparse import parse +from sqlobject.tests.dbtest import getConnection +import pytest + +from sqlconvert.print_tokens import tlist2str +from sqlconvert.process_mysql import unescape_strings +from sqlconvert.process_tokens import escape_strings + +connection = getConnection() +pytestmark = pytest.mark.skipif(connection.dbName != "sqlite", + reason="This test requires SQLite") + +create_sqlite_test_table = """ +CREATE TABLE test ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + test_str VARCHAR(255) NOT NULL +); +""" + + +def test_mysql2sqlite_string(): + connection.query(create_sqlite_test_table) + parsed = parse("insert into test (id, test_str) values " + "(1, '\"te\\'st\\\"\\n')")[0] + unescape_strings(parsed) + escape_strings(parsed, 'sqlite') + query = tlist2str(parsed) + assert query == u"INSERT INTO test (id, test_str) VALUES " \ + u"(1, '\"te''st\"\n')" + connection.query(query) + test_str = connection.queryOne("SELECT test_str FROM test WHERE id=1")[0] + assert test_str == u"\"te'st\"\n" diff --git a/tox.ini b/tox.ini index 5941ef8..e634cdf 100644 --- a/tox.ini +++ b/tox.ini @@ -1,6 +1,6 @@ [tox] minversion = 1.8 -envlist = {py26,py27,py34},{py26,py27,py34}-postgres,{py27,py34}-flake8 +envlist = {py26,py27,py34},{py26,py27,py34}-{postgres,sqlite},{py27,py34}-flake8 # Base test environment settings [testenv] @@ -20,6 +20,7 @@ whitelist_externals = flake8 createdb dropdb + rm [general] commands = @@ -50,6 +51,22 @@ commands = {[postgresql]commands} [testenv:py34-postgres] commands = {[postgresql]commands} +# SQLite test environments +[sqlite] +commands = + python -m pytest -D sqlite:///tmp/test.sqdb + rm -f /tmp/test.sqdb + +[testenv:py26-sqlite] +commands = {[sqlite]commands} + +[testenv:py27-sqlite] +commands = {[sqlite]commands} + +[testenv:py34-sqlite] +commands = {[sqlite]commands} + +# flake8 [testenv:py27-flake8] deps = flake8 -- 2.39.5