]> git.phdru.name Git - m_librarian.git/commitdiff
Convert tests from unittest to pytest
authorOleg Broytman <phd@phdru.name>
Tue, 20 Sep 2016 20:57:40 +0000 (23:57 +0300)
committerOleg Broytman <phd@phdru.name>
Tue, 20 Sep 2016 20:57:40 +0000 (23:57 +0300)
tests/__init__.py [deleted file]
tests/dbutils.py [new file with mode: 0644]
tests/test_config.py
tests/test_format.py
tests/test_glst.py
tests/test_inp.py
tests/test_search.py

diff --git a/tests/__init__.py b/tests/__init__.py
deleted file mode 100644 (file)
index 598d463..0000000
+++ /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 (file)
index 0000000..c315fff
--- /dev/null
@@ -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))
index 0a6e07b521c1f320b1a2b91b831ebed059384b2a..9ebdfe52c2fcd7ba265df50eb13eb3e53df23458 100644 (file)
@@ -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'
index 2710af97a1bbc236c83df28195c8c8c8a95b3e09..6b3aa363f3d440421b5657d09b9821dbe46a5e75 100644 (file)
@@ -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'
index bf4bd0cf92dcc2438a2ec3f7c3938e1aa25cafec..8211cc1e7dcad602e8fd6ba231b01b34fb13fd2f 100644 (file)
@@ -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
index 9471421cc38392b215da1dafd75fb1048cf9d101..75bcd011b55b4f8a69ccbe35cf3bac1dabae5ef9 100644 (file)
@@ -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
index 7fdf9fe136f2e51af3dab30dc5ab8dc41dbb42cf..dfea04113ce21656bb4e4842b38663fef588bdca 100644 (file)
@@ -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