import unittest import os import inspect import utils.path_tool import basic.program import basic.constants as B import test.constants import test.testtools import utils.path_const as P HOME_PATH = test.constants.HOME_PATH DATA_PATH = test.constants.DATA_PATH OS_SYSTEM = test.constants.OS_SYSTEM # here you can select single testfunction for developping the tests TEST_FUNCTIONS = ["test_key", "test_rejoinPath", "test_rejoinPath", "test_composePath", "test_composePattern", "test_extractPath", "test_extractPattern"] #TEST_FUNCTIONS = [ "test_extractPath"] verbose = True class MyTestCase(unittest.TestCase): mymsg = "--------------------------------------------------------------" def setTestPaths(self, job): # here you can overwrite your workspace-configuration in order to get stable unittests job.conf.confs[B.SUBJECT_PATH][B.ATTR_PATH_HOME] = "home/unittest" job.conf.confs[B.SUBJECT_PATH][B.ATTR_PATH_TDATA] = "home/unittest/tdata" job.conf.confs[B.SUBJECT_PATH][B.ATTR_PATH_ENV] = "home/unittest/env" job.conf.confs[B.SUBJECT_PATH][B.ATTR_PATH_ARCHIV] = "home/unittest/archiv" pass def setPathConfig(self, pt): # here you can overwrite your workspace-configuration in order to get stable unittests paths = pt.getInstance() paths.pattern[P.P_ENVBASE] = "{job.conf.environment}/{job.par.environment}" def test_key(self): global mymsg actfunction = str(inspect.currentframe().f_code.co_name) cnttest = 0 if actfunction not in TEST_FUNCTIONS: return job = test.testtools.getJob() res = utils.path_tool.getKeyValue("job.par."+B.PAR_ENV, None) self.assertEqual(res, test.testtools.DEFAULT_ENV) cnttest += 1 for par in [B.ATTR_PATH_ARCHIV, B.ATTR_PATH_ENV]: res = utils.path_tool.getKeyValue("job.conf."+par, None) if verbose: print("assertIn "+par+": "+res+" -- "+DATA_PATH) self.assertIn(DATA_PATH, res) cnttest += 1 MyTestCase.mymsg += "\n----- "+actfunction+" : "+str(cnttest) def test_rejoinPath(self): global mymsg actfunction = str(inspect.currentframe().f_code.co_name) cnttest = 0 if actfunction not in TEST_FUNCTIONS: return if OS_SYSTEM == "linux": res = utils.path_tool.rejoinPath("home", "ulrich") self.assertEqual(res, "/home/ulrich") cnttest += 1 res = utils.path_tool.rejoinPath("opt") self.assertEqual(res, "/opt") cnttest += 1 res = utils.path_tool.rejoinPath("http://domain.com", "application", "function") self.assertEqual(res, "http://domain.com/application/function") cnttest += 1 MyTestCase.mymsg += "\n----- "+actfunction+" : "+str(cnttest) def test_composePath(self): global mymsg actfunction = str(inspect.currentframe().f_code.co_name) cnttest = 0 if actfunction not in TEST_FUNCTIONS: return # set the relevant datastrutures job = test.testtools.getJob() comp = test.testtools.getComp() pt = utils.path_tool.PathConf() self.setTestPaths(job) self.setPathConfig(pt) # tests path = utils.path_tool.composePath(P.P_ENVBASE, None) self.assertIn(job.conf.confs[B.SUBJECT_PATH][B.ATTR_PATH_ENV], path) self.assertIn(getattr(job.par, B.PAR_ENV), path) cnttest += 2 path = utils.path_tool.composePath(P.P_TCLOG, None) self.assertIn(job.conf.confs[B.SUBJECT_PATH][B.ATTR_PATH_ARCHIV], path) self.assertIn(getattr(job.par, B.PAR_TESTCASE), path) cnttest += 2 MyTestCase.mymsg += "\n----- "+actfunction+" : "+str(cnttest) def test_composePattern(self): global mymsg actfunction = str(inspect.currentframe().f_code.co_name) cnttest = 0 if actfunction not in TEST_FUNCTIONS: return # set the relevant datastrutures job = test.testtools.getJob() comp = test.testtools.getComp() pt = utils.path_tool.PathConf() self.setTestPaths(job) self.setPathConfig(pt) # tests path = utils.path_tool.composePattern("{"+P.P_ENVBASE+"}", None) self.assertIn(job.conf.confs[B.SUBJECT_PATH][B.ATTR_PATH_ENV], path) self.assertIn(getattr(job.par, B.PAR_ENV), path) cnttest += 2 MyTestCase.mymsg += "\n----- "+actfunction+" : "+str(cnttest) def test_extractPattern(self): global mymsg actfunction = str(inspect.currentframe().f_code.co_name) cnttest = 0 if actfunction not in TEST_FUNCTIONS: return job = test.testtools.getJob() pt = utils.path_tool.PathConf() job = test.testtools.getJob("ts", test.testtools.DEFAULT_APP, test.testtools.DEFAULT_ENV, "2021-08-21_18-ß2-01") r = utils.path_tool.extractPattern("tsbase" ) self.assertEqual(r[0][1], "job.conf.archiv") self.assertEqual(r[1][1], P.KEY_TESTSUITE) self.assertEqual(r[1][2], "testlauf") cnttest += 3 MyTestCase.mymsg += "\n----- "+actfunction+" : "+str(cnttest) def test_extractPath(self): global mymsg actfunction = str(inspect.currentframe().f_code.co_name) cnttest = 0 if actfunction not in TEST_FUNCTIONS: return job = test.testtools.getJob() pt = utils.path_tool.PathConf() r = utils.path_tool.extractPath("tsbase" , os.path.join(DATA_PATH, "lauf", "testlauf", "startjob_2021-08-21_10-02-01")) if verbose: print("r " + str(r)) self.assertEqual(job.par.usecase, "startjob") self.assertEqual(job.par.tstime, "2021-08-21_10-02-01") cnttest += 2 MyTestCase.mymsg += "\n----- "+actfunction+" : "+str(cnttest) def test_zzz(self): if verbose: print(MyTestCase.mymsg) if __name__ == '__main__': unittest.main()