From 6994de0544ffbef8abf8c6bcbaae9a918aab32b8 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Fri, 9 Mar 2018 15:23:17 +0100 Subject: [PATCH] Adding test/general.py and test/test_lock.py --- test/general.py | 110 ++++++++++++++++++++++++++++++++++++++++++++++ test/test_lock.py | 104 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 214 insertions(+) create mode 100644 test/general.py create mode 100755 test/test_lock.py diff --git a/test/general.py b/test/general.py new file mode 100644 index 0000000..e0bd1f4 --- /dev/null +++ b/test/general.py @@ -0,0 +1,110 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@author: Frank Brehm +@contact: frank.brehm@pixlpark.com +@license: GPL3 +@summary: general used functions an objects used for unit tests on + the some python modules +""" + +import os +import sys +import logging +import argparse + +try: + import unittest2 as unittest +except ImportError: + import unittest + +# Own modules + +# ============================================================================= + +log = logging.getLogger(__name__) + + +# ============================================================================= +def get_arg_verbose(): + + arg_parser = argparse.ArgumentParser() + + arg_parser = argparse.ArgumentParser() + arg_parser.add_argument( + "-v", "--verbose", action="count", + dest='verbose', help='Increase the verbosity level') + args = arg_parser.parse_args() + + return args.verbose + + +# ============================================================================= +def init_root_logger(verbose=0): + + root_log = logging.getLogger() + root_log.setLevel(logging.WARNING) + if verbose > 1: + root_log.setLevel(logging.DEBUG) + elif verbose: + root_log.setLevel(logging.INFO) + + appname = os.path.basename(sys.argv[0]) + format_str = appname + ': ' + if verbose: + if verbose > 1: + format_str += '%(name)s(%(lineno)d) %(funcName)s() ' + else: + format_str += '%(name)s ' + format_str += '%(levelname)s - %(message)s' + formatter = None + formatter = logging.Formatter(format_str) + + # create log handler for console output + lh_console = logging.StreamHandler(sys.stderr) + if verbose: + lh_console.setLevel(logging.DEBUG) + else: + lh_console.setLevel(logging.INFO) + lh_console.setFormatter(formatter) + + root_log.addHandler(lh_console) + + +# ============================================================================= +class WebHooksTestcase(unittest.TestCase): + + # ------------------------------------------------------------------------- + def __init__(self, methodName='runTest', verbose=0): + + self._verbose = int(verbose) + + super(WebHooksTestcase, self).__init__(methodName) + + self.test_dir = os.path.abspath(os.path.dirname(__file__)) + + # ------------------------------------------------------------------------- + @property + def verbose(self): + """The verbosity level.""" + return getattr(self, '_verbose', 0) + + # ------------------------------------------------------------------------- + def setUp(self): + + pass + + # ------------------------------------------------------------------------- + def tearDown(self): + + pass + +# ============================================================================= + +if __name__ == '__main__': + + pass + +# ============================================================================= + +# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 diff --git a/test/test_lock.py b/test/test_lock.py new file mode 100755 index 0000000..9b0b19e --- /dev/null +++ b/test/test_lock.py @@ -0,0 +1,104 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@author: Frank Brehm +@contact: frank.brehm@pixlpark.com +@license: GPL3 +@summary: test script (and module) for unit tests on locking handler object +""" + +import os +import sys +import logging +import tempfile +import time + +try: + import unittest2 as unittest +except ImportError: + import unittest + +libdir = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), '..', 'lib')) +sys.path.insert(0, libdir) + +from webhooks.common import to_utf8 + +from general import WebHooksTestcase, get_arg_verbose, init_root_logger + + +APPNAME = 'test_lock' + +LOG = logging.getLogger(APPNAME) + +# ============================================================================= +class TestLockHandler(WebHooksTestcase): + + # ------------------------------------------------------------------------- + def setUp(self): + self.lock_dir = '/tmp' + self.lock_basename = 'test-%d.lock' % (os.getpid()) + self.lock_file = os.path.join(self.lock_dir, self.lock_basename) + + # ------------------------------------------------------------------------- + def tearDown(self): + + if os.path.exists(self.lock_file): + log.debug("Removing %r ...", self.lock_file) + os.remove(self.lock_file) + + # ------------------------------------------------------------------------- + def test_import(self): + + LOG.info("Testing import of webhooks.lock_handler ...") + import webhooks.lock_handler # noqa + LOG.debug("Module webhooks.lock_handler imported.") + + from webhooks.lock_handler import LockHandlerError # noqa + LOG.debug( + "Exception class %r from %r imported.", + 'LockHandlerError', 'webhooks.lock_handler') + + from webhooks.lock_handler import LockdirNotExistsError # noqa + LOG.debug( + "Exception class %r from %r imported.", + 'LockdirNotExistsError', 'webhooks.lock_handler') + + from webhooks.lock_handler import LockdirNotWriteableError # noqa + LOG.debug( + "Exception class %r from %r imported.", + 'LockdirNotWriteableError', 'webhooks.lock_handler') + + from webhooks.lock_handler import LockHandler # noqa + LOG.debug( + "Class %r from %r imported.", + 'LockHandler', 'webhooks.lock_handler') + + +# ============================================================================= + + +if __name__ == '__main__': + + verbose = get_arg_verbose() + if verbose is None: + verbose = 0 + init_root_logger(verbose) + + LOG.info("Starting tests ...") + + loader = unittest.TestLoader() + suite = unittest.TestSuite() + + suite.addTest(TestLockHandler('test_import', verbose)) + + runner = unittest.TextTestRunner(verbosity=verbose) + + result = runner.run(suite) + + +# ============================================================================= + +# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 LIST + + + -- 2.39.5