]> git.phdru.name Git - m_librarian.git/commitdiff
Add tests; test importing list of genres
authorOleg Broytman <phd@phdru.name>
Tue, 5 Apr 2016 18:24:14 +0000 (21:24 +0300)
committerOleg Broytman <phd@phdru.name>
Tue, 5 Apr 2016 18:24:14 +0000 (21:24 +0300)
Makefile
tests/Makefile
tests/__init__.py [new file with mode: 0644]
tests/run_all.py [new file with mode: 0755]
tests/test_glst.py [new file with mode: 0755]

index 5ef35dc7a11f404a12e5780fe722cc30745229c4..20168df489e943d0ae5c365f167d90d852f5f1c6 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -11,3 +11,6 @@ htmldoc:
 .PHONY: test
 test:
        $(MAKE) -C tests
+
+.PHONY: tests
+tests: test
index ceb35bd1e1afe9f206bb647555fe91ba521b3bd0..b990065cfd8bab40e800e4f3987644c570fca99e 100644 (file)
@@ -1,4 +1,4 @@
 
 .PHONY: all
 all:
-       @echo "Nothing to be done for 'all'"
+       ./run_all.py
diff --git a/tests/__init__.py b/tests/__init__.py
new file mode 100644 (file)
index 0000000..c624381
--- /dev/null
@@ -0,0 +1,33 @@
+
+__all__ = ['TestCase', 'main']
+
+
+import os
+import unittest
+from m_librarian.db import open_db, init_db
+
+
+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 main():
+    try:
+        unittest.main(testRunner=unittest.TextTestRunner())
+    except SystemExit, msg:
+        result = msg.args[0]
+    else:
+        result = 0
+    raise SystemExit(result)
diff --git a/tests/run_all.py b/tests/run_all.py
new file mode 100755 (executable)
index 0000000..51d3d6a
--- /dev/null
@@ -0,0 +1,44 @@
+#! /usr/bin/env python
+
+
+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):
+        sys.stdout.write("Test: %s... " % test)
+        sys.stdout.flush()
+        rc = subprocess.call((sys.executable, '-m', test))
+        if rc:
+            sys.stdout.write("ERROR\n")
+        else:
+            sys.stdout.write("Ok\n")
+        sys.stdout.flush()
+
+if __name__ == '__main__':
+    main()
diff --git a/tests/test_glst.py b/tests/test_glst.py
new file mode 100755 (executable)
index 0000000..5349bb3
--- /dev/null
@@ -0,0 +1,16 @@
+#! /usr/bin/env python
+
+
+from tests import TestCase, main
+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)
+
+
+if __name__ == "__main__":
+    main()