From 6a5ab6e9ddddda1e97b5fb64c2342a76cc807a3f Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Tue, 20 Sep 2016 23:57:40 +0300 Subject: [PATCH] Convert tests from unittest to pytest --- tests/__init__.py | 26 -------------------------- tests/dbutils.py | 26 ++++++++++++++++++++++++++ tests/test_config.py | 14 ++++++-------- tests/test_format.py | 20 +++++++++----------- tests/test_glst.py | 9 ++++----- tests/test_inp.py | 18 ++++++++++-------- tests/test_search.py | 38 ++++++++++++++++---------------------- 7 files changed, 71 insertions(+), 80 deletions(-) delete mode 100644 tests/__init__.py create mode 100644 tests/dbutils.py diff --git a/tests/__init__.py b/tests/__init__.py deleted file mode 100644 index 598d463..0000000 --- a/tests/__init__.py +++ /dev/null @@ -1,26 +0,0 @@ - -import os -import unittest -from m_librarian.db import open_db, init_db -from m_librarian.inp import import_inpx - -__all__ = ['TestCase', 'main'] - - -class TestCase(unittest.TestCase): - def setUp(self): - try: - os.remove('/tmp/m_librarian-test.sqlite') - except OSError: - pass - open_db('sqlite:///tmp/m_librarian-test.sqlite') - init_db() - - def tearDown(self): - try: - os.remove('/tmp/m_librarian-test.sqlite') - except OSError: - pass - - def import_inpx(self, inpx): - import_inpx(os.path.join(os.path.dirname(__file__), inpx)) diff --git a/tests/dbutils.py b/tests/dbutils.py new file mode 100644 index 0000000..c315fff --- /dev/null +++ b/tests/dbutils.py @@ -0,0 +1,26 @@ + +import os +from m_librarian.db import open_db, init_db +from m_librarian.inp import import_inpx + +__all__ = ['setup_module', 'teardown_module', 'load_inpx'] + + +def setup_module(): + try: + os.remove('/tmp/m_librarian-test.sqlite') + except OSError: + pass + open_db('sqlite:///tmp/m_librarian-test.sqlite') + init_db() + + +def teardown_module(): + try: + os.remove('/tmp/m_librarian-test.sqlite') + except OSError: + pass + + +def load_inpx(inpx): + import_inpx(os.path.join(os.path.dirname(__file__), inpx)) diff --git a/tests/test_config.py b/tests/test_config.py index 0a6e07b..9ebdfe5 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -1,13 +1,11 @@ import os -import unittest from m_librarian.config import get_config -class TestFormat(unittest.TestCase): - def test_config(self): - config_path = os.path.join( - os.path.dirname(__file__), 'test_config.conf') - get_config(config_path) - ml_conf = get_config() - self.assertEqual(ml_conf.get('library', 'path'), '/home/test-config') +def test_config(): + config_path = os.path.join( + os.path.dirname(__file__), 'test_config.conf') + get_config(config_path) + ml_conf = get_config() + assert ml_conf.get('library', 'path') == '/home/test-config' diff --git a/tests/test_format.py b/tests/test_format.py index 2710af9..6b3aa36 100644 --- a/tests/test_format.py +++ b/tests/test_format.py @@ -1,17 +1,15 @@ import os -import unittest from m_librarian import download from m_librarian.config import get_config -class TestFormat(unittest.TestCase): - def test_compile_format(self): - config_path = os.path.join( - os.path.dirname(__file__), 'test_config.conf') - get_config(config_path) - ml_conf = get_config() - ml_conf.set('download', 'format', '%a/%s/%n %t') - download._compile_format() - self.assertEqual(download.compiled_format, - u'%(author)s/%(series)s/%(ser_no)d %(title)s') +def test_compile_format(): + config_path = os.path.join( + os.path.dirname(__file__), 'test_config.conf') + get_config(config_path) + ml_conf = get_config() + ml_conf.set('download', 'format', '%a/%s/%n %t') + download._compile_format() + assert download.compiled_format == \ + u'%(author)s/%(series)s/%(ser_no)d %(title)s' diff --git a/tests/test_glst.py b/tests/test_glst.py index bf4bd0c..8211cc1 100644 --- a/tests/test_glst.py +++ b/tests/test_glst.py @@ -1,10 +1,9 @@ -from tests import TestCase +from dbutils import setup_module, teardown_module # noqa from m_librarian.db import Genre from m_librarian.glst import import_glst -class TestGlst(TestCase): - def test_import_glst(self): - import_glst() - self.assertEqual(Genre.select().count(), 340) +def test_import_glst(): + import_glst() + assert Genre.select().count() == 340 diff --git a/tests/test_inp.py b/tests/test_inp.py index 9471421..75bcd01 100644 --- a/tests/test_inp.py +++ b/tests/test_inp.py @@ -1,13 +1,15 @@ -from tests import TestCase +from pytest import raises +from dbutils import setup_module, teardown_module # noqa +from dbutils import load_inpx from m_librarian.db import Author, Book -class TestInp(TestCase): - def test_import_bad_inpx(self): - self.assertRaises(ValueError, self.import_inpx, 'bad.inpx') +def test_import_bad_inpx(): + raises(ValueError, load_inpx, 'bad.inpx') - def test_import_inpx(self): - self.import_inpx('test.inpx') - self.assertEqual(Author.select().count(), 4) - self.assertEqual(Book.select().count(), 4) + +def test_import_inpx(): + load_inpx('test.inpx') + assert Author.select().count() == 4 + assert Book.select().count() == 4 diff --git a/tests/test_search.py b/tests/test_search.py index 7fdf9fe..dfea041 100644 --- a/tests/test_search.py +++ b/tests/test_search.py @@ -1,30 +1,24 @@ # coding: utf-8 -from tests import TestCase +from dbutils import setup_module, teardown_module # noqa +from dbutils import load_inpx from m_librarian.db import Author, Book from m_librarian.search import mk_search_conditions, \ search_authors, search_books -class TestSearch(TestCase): - def test_search_authors(self): - self.import_inpx('test.inpx') - self.assertEqual( - search_authors('full', True, {'surname': u'Друг'}).count(), 1) - self.assertEqual( - search_authors('start', True, {'surname': u'Друг'}).count(), 2) - self.assertEqual( - search_authors('substring', True, {'surname': u'Друг'}).count(), 2) - self.assertEqual( - search_authors('substring', False, {'surname': u'друг'}).count(), - 3) +def test_search_authors(): + load_inpx('test.inpx') + assert search_authors('full', True, {'surname': u'Друг'}).count() == 1 + assert search_authors('start', True, {'surname': u'Друг'}).count() == 2 + assert search_authors('substring', True, {'surname': u'Друг'}).count() == 2 + assert search_authors( + 'substring', False, {'surname': u'друг'}).count() == 3 - join_expressions = [] - join_expressions.append(Book.j.authors) - conditions = mk_search_conditions( - Author, 'start', False, {'surname': u'друг'}) - join_expressions.extend(conditions) - self.assertEqual( - search_books('start', False, - {'title': u'тест'}, join_expressions).count(), - 2) + join_expressions = [] + join_expressions.append(Book.j.authors) + conditions = mk_search_conditions( + Author, 'start', False, {'surname': u'друг'}) + join_expressions.extend(conditions) + assert search_books( + 'start', False, {'title': u'тест'}, join_expressions).count() == 2 -- 2.39.5