diff --git a/basic/componentHandling.py b/basic/componentHandling.py index 3b831e1..9830611 100644 --- a/basic/componentHandling.py +++ b/basic/componentHandling.py @@ -19,7 +19,7 @@ import utils.config_tool import utils.conn_tool import basic.program import basic.message -import components.component +import basic.component import importlib import copy import basic.constants as B @@ -70,9 +70,11 @@ class ComponentManager: job = basic.program.Job.getInstance() job.m.logDebug("applicationscomponente -- " + str(type(job.par))) self.components = {} + self.comps = {} ComponentManager.__instance = self print ("init ComponentHandling "+str(self)) + def initComponents(self): # sets components the first time # afterwards set components from parameter-file @@ -86,29 +88,35 @@ class ComponentManager: job.m.logDebug("applicationscomponente -- " + k + ":") self.createComponent(k, 0, "") + def setComponents(self): # set components from parameter-file job = basic.program.Job.getInstance() job.m.logDebug("applicationscomponente -- " + str(type(job.par))) + def getComponent(self, compobjname): job = basic.program.Job.getInstance() verify = -2 + job.getDebugLevel("job_tool") job.debug(verify, "getComponents " + compobjname) - return comps[compobjname] + if compobjname in self.comps: + return self.comps[compobjname] + return None + def getComponents(self, mainfct): job = basic.program.Job.getInstance() verify = -2 + job.getDebugLevel("job_tool") job.debug(verify, "getComponents " + mainfct) out = [] - for c in comps: - job.debug(verify, "getComponents " + c + ": " + str(comps[c].conf)) - print("getComponents " + c + ": " + str(comps[c].conf)) - if mainfct in comps[c].conf["function"]: + for c in self.comps: + job.debug(verify, "getComponents " + c + ": " + str(self.comps[c].conf)) + print("getComponents " + c + ": " + str(self.comps[c].conf)) + if mainfct in self.comps[c].conf["function"]: out.append(c) return out + @staticmethod def getInstance(init="N"): if (ComponentManager.__instance is not None): @@ -118,6 +126,7 @@ class ComponentManager: else: raise Exception(B.EXCEPT_NOT_INITIALIZED) + def createComponent(self, componentName, nr, suffix): """ in order to create a component it must be loaded @@ -155,7 +164,8 @@ class ComponentManager: self.createSubComponents(c, i, suffix) i = i + 1 print("createComponent 9 " + componentName) - print(comps) + print(self.comps) + def createInstance(self, compName, parContent, confs, conns, nr): """ @@ -177,9 +187,9 @@ class ComponentManager: name = compName i = 0 c.name = name - c.conf = confs["conf"] - c.conf[B.SUBJECT_CONN] = conns[i] c.m = basic.message.Message(basic.message.LIMIT_DEBUG, "logTime", name) + c.conf = utils.config_tool.mergeConn(c.m, confs["conf"], conns[i]) + c.conf[B.SUBJECT_CONN] = conns[i] c.init() if parContent is not None: print("createComponent 5 a " + name + " : " + str(parContent)) @@ -193,13 +203,15 @@ class ComponentManager: if table in ["type"]: continue conf = utils.config_tool.getConfig("DATASTRUCTURE", c.name, table) - if "tabelle" in conf and table in conf["tabelle"]: - c.conf[B.DATA_NODE_DDL][table] = conf["tabelle"][table] + if B.DATA_NODE_TABLES in conf and table in conf[B.DATA_NODE_TABLES]: + c.conf[B.DATA_NODE_DDL][table] = conf[B.DATA_NODE_TABLES][table] else: c.conf[B.DATA_NODE_DDL][table] = conf comps[name] = c + self.comps[name] = c return c + def createSubComponents(self, comp, nr, suffix): job = basic.program.Job.getInstance() verify = -2 + job.getDebugLevel("job_tool") diff --git a/test/test_component.py b/test/test_component.py index e69de29..8f330bf 100644 --- a/test/test_component.py +++ b/test/test_component.py @@ -0,0 +1,141 @@ +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_formatDbRows"] + +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("testa21", 0, "") + self.assertIn("testa21", cm.comps, "component must be stored") + self.assertEqual(len(cm.comps), 1, "component without subcomponents") + cnttest += 2 + cm.createComponent("testa1", 0, "") + self.assertIn("testa1", cm.comps, "component must be stored") + self.assertIn("testa11", cm.comps, "subcomponent must be stored") + self.assertEqual(len(cm.comps), 3, "2 component with 1 subcomponents") + cnttest += 2 + cm.createComponent("testa", 0, "") + self.assertIn("testa_01", cm.comps, "multiple component must be stored") + self.assertIn("testa_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 = "testa" + 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, "testa_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 = "testa1" + 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, "testa1") + 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("testa", 0, "") + prog = "init_testcase" + comps = cm.getComponents(prog) + self.assertEqual(len(comps), 5, "not all created comps for program "+prog) + prog = "finish_testcase" + comps = cm.getComponents(prog) + self.assertEqual(len(comps), 6, "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("testa", 0, "") + self.assertEqual(len(cm.comps), 6, "6 components inside") + comp = cm.getComponent("testa1") + self.assertEqual(len(cm.comps), 6, "6 components inside") + self.assertEqual(comp.name, "testa1", "component found") + cnttest += 2 + comp = cm.getComponent("testb1") + self.assertEqual(len(cm.comps), 6, "6 components inside and nothing added") + self.assertIsNotNone("terstb1") + cnttest += 2 + MyTestCase.mymsg += "\n----- "+actfunction+" : "+str(cnttest) + + + def test_zzz(self): + print(MyTestCase.mymsg) + + +if __name__ == '__main__': + unittest.main() diff --git a/utils/config_tool.py b/utils/config_tool.py index 9b4d2c4..29640a2 100644 --- a/utils/config_tool.py +++ b/utils/config_tool.py @@ -222,6 +222,8 @@ def mergeConn(msg, conf, conn): if b in conn[topic][a]: conf[B.SUBJECT_ARTS][topic][a][b] = conn[topic][a][b] for a in list: + if topic not in conn: + break if a in conn[topic]: conf[B.SUBJECT_ARTS][topic][a] = conn[topic][a] return conf \ No newline at end of file