import unittest import inspect import basic.program import utils.path_tool import basic.componentHandling import test.constants import basic.component import basic.constants as B import utils.db_abstract import test.testtools import utils.config_tool import utils.conn_tool HOME_PATH = test.constants.HOME_PATH DATA_PATH = test.constants.DATA_PATH conf = {} # here you can select single testfunction for developping the tests TEST_FUNCTIONS = ["test_10actHandler", "test_21createInstance", "test_22createComponent", "test_23getComponents", "test_24getComponent"] TEST_FUNCTIONS = ["test_24getComponent"] class MyTestCase(unittest.TestCase): mymsg = "--------------------------------------------------------------" def test_22createComponent(self): global mymsg actfunction = str(inspect.currentframe().f_code.co_name) cnttest = 0 if actfunction not in TEST_FUNCTIONS: return job = test.testtools.getJob() cm = basic.componentHandling.ComponentManager() cm.createComponent("testpoldb", 0, "") self.assertIn("testpoldb", cm.comps, "component must be stored") self.assertEqual(len(cm.comps), 1, "component without subcomponents") cnttest += 2 cm.createComponent("testprd", 0, "") self.assertIn("testprd", cm.comps, "component must be stored") self.assertIn("testprddb", cm.comps, "subcomponent must be stored") self.assertEqual(len(cm.comps), 3, "2 component with 1 subcomponents") cnttest += 2 cm.createComponent("testcm", 0, "") self.assertIn("testcm_01", cm.comps, "multiple component must be stored") self.assertIn("testcm_02", cm.comps, "multiple component must be stored") self.assertEqual(len(cm.comps), 6, "2 component with 2 subcomponents and 2 sub-subcomponents") cnttest += 2 MyTestCase.mymsg += "\n----- "+actfunction+" : "+str(cnttest) def test_21createInstance(self): global mymsg actfunction = str(inspect.currentframe().f_code.co_name) cnttest = 0 if actfunction not in TEST_FUNCTIONS: return job = test.testtools.getJob() cm = basic.componentHandling.ComponentManager.getInstance("J") componentName = "testcm" confs = utils.config_tool.getConfig("comp", componentName) conns = utils.conn_tool.getConnections(componentName) c = cm.createInstance(componentName, None, confs, conns, 1) self.assertEqual(hasattr(c, "conf"), True, "cinfig-dict must be") self.assertEqual(hasattr(c, "m"), True, "message-object must be") self.assertEqual(c.name, "testcm_01", "classname with number") # classname with number cnttest += 3 self.assertEqual(c.conf[B.SUBJECT_INST][B.ATTR_INST_CNT], 2, "conn-attribute overwrites config-attribute") cnttest += 1 # it overwrites self.assertEqual(c.conf[B.SUBJECT_INST][B.ATTR_INST_SGL], "n", "without conn-attribute the config-attribute keeps") cnttest += 1 # it keep componentName = "testprddb" confs = utils.config_tool.getConfig("comp", componentName) conns = utils.conn_tool.getConnections(componentName) c = cm.createInstance(componentName, None, confs, conns, 0) self.assertEqual(c.name, "testprddb") self.assertIn(B.ATTR_DB_TYPE, c.conf[B.SUBJECT_ARTS][B.TOPIC_NODE_DB], "conn-attribute creates missing config-attribute") cnttest += 2 # new attributes MyTestCase.mymsg += "\n----- "+actfunction+" : "+str(cnttest) def test_10actHandler(self): global mymsg actfunction = str(inspect.currentframe().f_code.co_name) cnttest = 0 if actfunction not in TEST_FUNCTIONS: return job = test.testtools.getJob() cm = basic.componentHandling.ComponentManager.getInstance("J") self.assertIsNotNone(cm) self.assertEqual(len(cm.components), 0) cnttest += 2 MyTestCase.mymsg += "\n----- "+actfunction+" : "+str(cnttest) def test_23getComponents(self): global mymsg actfunction = str(inspect.currentframe().f_code.co_name) cnttest = 0 if actfunction not in TEST_FUNCTIONS: return job = test.testtools.getJob() cm = basic.componentHandling.ComponentManager() cm.createComponent("testcm", 0, "") cm.createComponent("testcrm", 0, "") prog = "init_testcase" comps = cm.getComponents(prog) self.assertEqual(len(comps), 7, "not all created comps for program "+prog) prog = "check_environment" comps = cm.getComponents(prog) self.assertEqual(len(comps), 8, "all created comps for program "+prog) cnttest += 2 MyTestCase.mymsg += "\n----- "+actfunction+" : "+str(cnttest) def test_24getComponent(self): global mymsg actfunction = str(inspect.currentframe().f_code.co_name) cnttest = 0 if actfunction not in TEST_FUNCTIONS: return job = test.testtools.getJob() cm = basic.componentHandling.ComponentManager() cm.createComponent("testcm", 0, "") self.assertEqual(len(cm.comps), 6, "6 components inside") comp = cm.getComponent("testprd") self.assertEqual(len(cm.comps), 6, "6 components inside") self.assertEqual(comp.name, "testprd", "component found") cnttest += 2 comp = cm.getComponent("testcrm") self.assertEqual(len(cm.comps), 6, "6 components inside and nothing added") self.assertIsNotNone("terstcrm") cnttest += 2 MyTestCase.mymsg += "\n----- "+actfunction+" : "+str(cnttest) def test_zzz(self): print(MyTestCase.mymsg) if __name__ == '__main__': unittest.main()