From 9ba542525a29756f951399523673fdccc7a329fb Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Sun, 2 Oct 2016 00:05:15 +0300 Subject: [PATCH] Split test_process_tokens.py -> test_process_mysql.py Separate generic tests from MySQL-specific. --- tests/test_process_mysql.py | 60 ++++++++++++++++++++++++++++++++++++ tests/test_process_tokens.py | 56 +-------------------------------- 2 files changed, 61 insertions(+), 55 deletions(-) create mode 100644 tests/test_process_mysql.py diff --git a/tests/test_process_mysql.py b/tests/test_process_mysql.py new file mode 100644 index 0000000..500a35d --- /dev/null +++ b/tests/test_process_mysql.py @@ -0,0 +1,60 @@ +# -*- coding: utf-8 -*- + +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 +from sqlconvert.process_tokens import escape_strings + + +def test_directive(): + parsed = parse("select /*! test */ * from /* test */ `T`")[0] + remove_directive_tokens(parsed) + query = tlist2str(parsed) + assert query == u'SELECT * FROM /* test */ `T`' + + +def test_directive_statement(): + parsed = parse("/*! test */ test ;")[0] + assert not is_directive_statement(parsed) + parsed = parse("/*! test */ ;")[0] + assert is_directive_statement(parsed) + + +def test_requote(): + parsed = parse("select * from `T`")[0] + requote_names(parsed) + query = tlist2str(parsed) + assert query == u'SELECT * FROM "T"' + + +def test_unescape_string(): + parsed = parse("insert into test values ('\"te\\'st\\\"\\n')")[0] + unescape_strings(parsed) + query = tlist2str(parsed) + assert query == u"INSERT INTO test VALUES ('\"te'st\"\n')" + + +def test_escape_string_postgres(): + parsed = parse("insert into test values ('\"te\\'st\\\"\\n')")[0] + unescape_strings(parsed) + escape_strings(parsed, 'postgres') + query = tlist2str(parsed) + assert query == u"INSERT INTO test VALUES (E'\"te''st\"\\n')" + + +def test_escape_string_sqlite(): + parsed = parse("insert into test values ('\"te\\'st\\\"\\n')")[0] + unescape_strings(parsed) + escape_strings(parsed, 'sqlite') + query = tlist2str(parsed) + assert query == u"INSERT INTO test VALUES ('\"te''st\"\n')" + + +def test_process(): + parsed = parse("select /*! test */ * from /* test */ `T`")[0] + statement = next(process_statement(parsed)) + query = tlist2str(statement) + assert query == u'SELECT * FROM /* test */ "T"' diff --git a/tests/test_process_tokens.py b/tests/test_process_tokens.py index 2d30dc9..277805e 100644 --- a/tests/test_process_tokens.py +++ b/tests/test_process_tokens.py @@ -4,10 +4,7 @@ from pytest import raises 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 -from sqlconvert.process_tokens import escape_strings, StatementGrouper +from sqlconvert.process_tokens import StatementGrouper def test_encoding(): @@ -23,57 +20,6 @@ def test_unicode(): assert query, u"INSERT INTO test (1 == 'тест')" -def test_directive(): - parsed = parse("select /*! test */ * from /* test */ `T`")[0] - remove_directive_tokens(parsed) - query = tlist2str(parsed) - assert query == u'SELECT * FROM /* test */ `T`' - - -def test_directive_statement(): - parsed = parse("/*! test */ test ;")[0] - assert not is_directive_statement(parsed) - parsed = parse("/*! test */ ;")[0] - assert is_directive_statement(parsed) - - -def test_requote(): - parsed = parse("select * from `T`")[0] - requote_names(parsed) - query = tlist2str(parsed) - assert query == u'SELECT * FROM "T"' - - -def test_unescape_string(): - parsed = parse("insert into test values ('\"te\\'st\\\"\\n')")[0] - unescape_strings(parsed) - query = tlist2str(parsed) - assert query == u"INSERT INTO test VALUES ('\"te'st\"\n')" - - -def test_escape_string_postgres(): - parsed = parse("insert into test values ('\"te\\'st\\\"\\n')")[0] - unescape_strings(parsed) - escape_strings(parsed, 'postgres') - query = tlist2str(parsed) - assert query == u"INSERT INTO test VALUES (E'\"te''st\"\\n')" - - -def test_escape_string_sqlite(): - parsed = parse("insert into test values ('\"te\\'st\\\"\\n')")[0] - unescape_strings(parsed) - escape_strings(parsed, 'sqlite') - query = tlist2str(parsed) - assert query == u"INSERT INTO test VALUES ('\"te''st\"\n')" - - -def test_process(): - parsed = parse("select /*! test */ * from /* test */ `T`")[0] - statement = next(process_statement(parsed)) - query = tlist2str(statement) - assert query == u'SELECT * FROM /* test */ "T"' - - def test_incomplete(): grouper = StatementGrouper() grouper.process_line("select * from `T`") -- 2.39.5