From: Oleg Broytman Date: Sun, 11 Sep 2016 18:37:36 +0000 (+0300) Subject: Use pytest for testing X-Git-Tag: 0.0.6~22 X-Git-Url: https://git.phdru.name/?a=commitdiff_plain;h=d6d2d76d2b29e8384169c5b86e19e494fab1a464;p=sqlconvert.git Use pytest for testing --- diff --git a/ChangeLog b/ChangeLog index ec6b82f..cbf159e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -6,6 +6,8 @@ Version 0.0.6 (2016-09-??) Unescape strings. + Use pytest for testing. + Version 0.0.5 (2016-09-07) Remove /*! directives */; and newlines after them. diff --git a/requirements_dev.txt b/requirements_dev.txt new file mode 100644 index 0000000..73c8ac6 --- /dev/null +++ b/requirements_dev.txt @@ -0,0 +1,3 @@ +-r requirements.txt + +pytest diff --git a/tests/Makefile b/tests/Makefile index b990065..a6a5e1f 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -1,4 +1,4 @@ .PHONY: all all: - ./run_all.py + python `which pytest` diff --git a/tests/__init__.py b/tests/__init__.py deleted file mode 100644 index d26523d..0000000 --- a/tests/__init__.py +++ /dev/null @@ -1,22 +0,0 @@ - -import unittest - -__all__ = ['TestCase', 'main'] - - -class TestCase(unittest.TestCase): - def setUp(self): - pass - - def tearDown(self): - pass - - -def main(): - try: - unittest.main(testRunner=unittest.TextTestRunner()) - except SystemExit as msg: - result = msg.args[0] - else: - result = 0 - raise SystemExit(result) diff --git a/tests/run_all.py b/tests/run_all.py deleted file mode 100755 index 9503411..0000000 --- a/tests/run_all.py +++ /dev/null @@ -1,39 +0,0 @@ -#! /usr/bin/env python -from __future__ import print_function - - -import os -import sys -import subprocess - - -def isexecutable(filename): - infile = open(filename, 'r') - magic = infile.read(2) - infile.close() - return magic == "#!" - - -def collect_tests(): - tests = [] - for dirpath, dirs, files in os.walk("tests"): - tests.extend( - [os.path.join(dirpath, filename) for filename in files - if filename.startswith("test") and filename.endswith(".py") - ]) - return [test[:-3].replace(os.sep, '.') for test in tests - if isexecutable(test)] - - -def main(): - os.chdir(os.path.join(os.path.dirname(sys.argv[0]), os.pardir)) - tests = collect_tests() - - os.environ["PYTHONPATH"] = os.curdir - - for test in sorted(tests): - print(test) - subprocess.call((sys.executable, '-m', test)) - -if __name__ == '__main__': - main() diff --git a/tests/test_stgrouper.py b/tests/test_stgrouper.py old mode 100755 new mode 100644 index 299ea6c..88d0fcf --- a/tests/test_stgrouper.py +++ b/tests/test_stgrouper.py @@ -1,33 +1,26 @@ -#! /usr/bin/env python - - -import unittest +from pytest import raises from sqlconvert.print_tokens import tlist2str from sqlconvert.process_mysql import requote_names from sqlconvert.process_tokens import StatementGrouper -from tests import main -class TestStGrouper(unittest.TestCase): +class TestStGrouper(object): def test_incomplete(self): grouper = StatementGrouper() grouper.process_line("select * from `T`") - self.assertFalse(grouper.statements) - self.assertEqual(len(grouper.statements), 0) - self.assertRaises(ValueError, grouper.close) + assert not grouper.statements + assert len(grouper.statements) == 0 + raises(ValueError, grouper.close) def test_statements(self): grouper = StatementGrouper() grouper.process_line("select * from `T`;") - self.assertTrue(grouper.statements) - self.assertEqual(len(grouper.statements), 1) + assert grouper.statements + assert len(grouper.statements) == 1 for statement in grouper.get_statements(): requote_names(statement) query = tlist2str(statement) - self.assertEqual(query, 'SELECT * FROM "T";') - self.assertEqual(len(grouper.statements), 0) - self.assertEqual(grouper.close(), None) - -if __name__ == "__main__": - main() + assert query == 'SELECT * FROM "T";' + assert len(grouper.statements) == 0 + assert grouper.close() is None diff --git a/tests/test_tokens.py b/tests/test_tokens.py old mode 100755 new mode 100644 index de11485..8201e74 --- a/tests/test_tokens.py +++ b/tests/test_tokens.py @@ -1,58 +1,51 @@ -#! /usr/bin/env python # -*- coding: utf-8 -*- -import unittest 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 tests import main -class TestTokens(unittest.TestCase): +class TestTokens(object): def test_encoding(self): parsed = parse("insert into test (1, 'тест')", 'utf-8')[0] query = tlist2str(parsed).encode('utf-8') - self.assertEqual(query, - u"INSERT INTO test (1, 'тест')".encode('utf-8')) + assert query == \ + u"INSERT INTO test (1, 'тест')".encode('utf-8') def test_unicode(self): parsed = parse(u"insert into test (1, 'тест')")[0] query = tlist2str(parsed) - self.assertEqual(query, u"INSERT INTO test (1, 'тест')") + assert query, u"INSERT INTO test (1 == 'тест')" def test_directive(self): parsed = parse("select /*! test */ * from /* test */ `T`")[0] remove_directive_tokens(parsed) query = tlist2str(parsed) - self.assertEqual(query, u'SELECT * FROM /* test */ `T`') + assert query == u'SELECT * FROM /* test */ `T`' def test_directive_statement(self): parsed = parse("/*! test */ test ;")[0] - self.assertFalse(is_directive_statement(parsed)) + assert not is_directive_statement(parsed) parsed = parse("/*! test */ ;")[0] - self.assertTrue(is_directive_statement(parsed)) + assert is_directive_statement(parsed) def test_requote(self): parsed = parse("select * from `T`")[0] requote_names(parsed) query = tlist2str(parsed) - self.assertEqual(query, u'SELECT * FROM "T"') + assert query == u'SELECT * FROM "T"' def test_string(self): parsed = parse("insert into test values ('\"test\\\"')")[0] unescape_strings(parsed) query = tlist2str(parsed) - self.assertEqual(query, u"INSERT INTO test VALUES ('\"test\"')") + assert query == u"INSERT INTO test VALUES ('\"test\"')" def test_process(self): parsed = parse("select /*! test */ * from /* test */ `T`")[0] process_statement(parsed) query = tlist2str(parsed) - self.assertEqual(query, u'SELECT * FROM /* test */ "T"') - - -if __name__ == "__main__": - main() + assert query == u'SELECT * FROM /* test */ "T"'