From d35819aa616d64b200f8d6967e4e9e54cf3dbba9 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Wed, 6 Nov 2024 17:16:58 +0100 Subject: [PATCH] Adding test/test_03_common.py --- test/test_03_common.py | 144 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100755 test/test_03_common.py diff --git a/test/test_03_common.py b/test/test_03_common.py new file mode 100755 index 0000000..8c49ee0 --- /dev/null +++ b/test/test_03_common.py @@ -0,0 +1,144 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@summary: Test script (and module) for unit tests on module pp_admintools.common. + +@author: Frank Brehm +@contact: frank.brehm@pixelpark.com +@copyright: © 2024 by Frank Brehm, Berlin +@license: GPL3 +""" + +import logging +import os +import sys + +try: + import unittest2 as unittest +except ImportError: + import unittest + +# from babel.dates import LOCALTZ + +libdir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'lib')) +sys.path.insert(0, libdir) + +# from fb_tools.common import pp, to_str, is_sequence +from fb_tools.common import pp + +from general import PpAdminToolsTestcase, get_arg_verbose, init_root_logger + +APPNAME = 'test-common' +LOG = logging.getLogger(APPNAME) + + +# ============================================================================= +class TestCommon(PpAdminToolsTestcase): + """Testcase for unit tests on module pp_admintools.common.""" + + # ------------------------------------------------------------------------- + def setUp(self): + """Execute this on setting up before calling each particular test method.""" + if self.verbose >= 1: + print() + + self._appname = APPNAME + + # ------------------------------------------------------------------------- + def tearDown(self): + """Execute this after calling each particular test method.""" + pass + + # ------------------------------------------------------------------------- + def test_import(self): + """Test importing module pp_admintools.common.""" + LOG.info(self.get_method_doc()) + + import pp_admintools.common + LOG.debug( + 'Version of pp_admintools.common: ' + pp_admintools.common.__version__) + + # ------------------------------------------------------------------------- + def test_split_parts(self): + """Test splitting strings in chunks by function split_parts().""" + LOG.info(self.get_method_doc()) + + from pp_admintools.errors import BrokenStringSplit + from pp_admintools.common import split_parts + + test_data = ( + { + 'src': 'Hallo', + 'quotes': False, + 'tgt': ['Hallo'], + }, { + 'src': 'Hallo Ballo!', + 'quotes': False, + 'tgt': ['Hallo', 'Ballo!'], + }, { + 'src': ' Hallo Ballo! \n ', + 'quotes': False, + 'tgt': ['Hallo', 'Ballo!'], + }, { + 'src': 'Uhu "Banane"', + 'quotes': False, + 'tgt': ['Uhu', 'Banane'], + }, { + 'src': 'Uhu " Banane "', + 'quotes': False, + 'tgt': ['Uhu', ' Banane '], + }, { + 'src': "'Uhu' \"Banane\"", + 'quotes': False, + 'tgt': ['Uhu', 'Banane'], + }, { + 'src': "' Uhu ' \"Banane\"", + 'quotes': True, + 'tgt': ["' Uhu '", '"Banane"'], + }, { + 'src': 'Frank\\"s Test', + 'quotes': False, + 'tgt': ['Frank"s', 'Test'], + }, { + 'src': 'Frank\\\'s Test', + 'quotes': False, + 'tgt': ["Frank's", 'Test'], + }, { + 'src': 'Frank\\\'s Test Frank\\\'s', + 'quotes': False, + 'tgt': ["Frank's", 'Test', "Frank's"], + }, + ) + + for data in test_data: + if self.verbose >= 1: + print() + LOG.debug('Splitting: {src!r} (Quotes: {quotes}), Expected: {tgt!r}.'.format(**data)) + res = split_parts(data['src'], keep_quotes=data['quotes']) + LOG.debug('Result: {!r}'.format(res)) + self.assertEqual(data['tgt'], res) + + +# ============================================================================= +if __name__ == '__main__': + + verbose = get_arg_verbose() + if verbose is None: + verbose = 0 + init_root_logger(verbose) + + LOG.info('Starting tests ...') + + suite = unittest.TestSuite() + + suite.addTest(TestCommon('test_import', verbose)) + suite.addTest(TestCommon('test_split_parts', verbose)) + + runner = unittest.TextTestRunner(verbosity=verbose) + + result = runner.run(suite) + + +# ============================================================================= + +# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 list -- 2.39.5