You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
5.5 KiB

#!/usr/bin/python
# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------------------------------------
# Author : Ulrich Carmesin
# Source : gitea.ucarmesin.de
# ---------------------------------------------------------------------------------------------------------
import unittest
import inspect
import basic.program
import utils.path_tool
import basic.toolHandling
import test.constants
import basic.component
import basic.constants as B
import utils.db_abstract
import test.testtools
import utils.config_tool
HOME_PATH = test.constants.HOME_PATH
conf = {}
# here you can select single testfunction for developping the tests
# -> test of components
TEST_FUNCTIONS = ["test_00toolhandling", "test_01getTechnicalIDFields", "test_02parseSql"]
TEST_FUNCTIONS = ["test_02parseSql"]
class MyTestCase(unittest.TestCase):
mymsg = "--------------------------------------------------------------"
def test_01getTechnicalIDFields(self):
global mymsg
actfunction = str(inspect.currentframe().f_code.co_name)
cnttest = 0
if actfunction not in TEST_FUNCTIONS:
return
job = test.testtools.getJob()
comp = basic.component.Component()
comp.name = "testcrmdb"
table = "person"
comp.conf = {}
comp.conf[B.DATA_NODE_DDL] = {}
comp.conf[B.DATA_NODE_DDL][table] = utils.config_tool.getConfig("DATASTRUCTURE", comp.name, table)
res = utils.db_abstract.getTechnicalIDFields(comp.conf[B.DATA_NODE_DDL][table])
self.assertEqual(res, ["persid"])
def test_00toolhandling(self):
global mymsg
actfunction = str(inspect.currentframe().f_code.co_name)
cnttest = 0
if actfunction not in TEST_FUNCTIONS:
return
job = test.testtools.getJob()
#t = basic.toolHandling.ToolManager()
comp = basic.component.Component()
comp.name = "testcrmdb"
table = "person"
comp.conf = {}
comp.conf[B.DATA_NODE_DDL] = {}
comp.conf[B.DATA_NODE_DDL][table] = utils.config_tool.getConfig("DATASTRUCTURE", comp.name, table)
comp.conf[B.SUBJECT_ARTS] = {}
comp.conf[B.SUBJECT_ARTS][B.TOPIC_NODE_DB] = {}
comp.conf[B.SUBJECT_CONN] = {}
comp.conf[B.SUBJECT_CONN][B.TOPIC_NODE_DB] = {}
comp.conf[B.SUBJECT_CONN][B.TOPIC_NODE_DB][B.ATTR_TYPE] = "rel"
comp.conf[B.SUBJECT_CONN][B.TOPIC_NODE_DB][B.ATTR_DB_DATABASE] = "crm-db"
tool = basic.toolHandling.getDbTool(comp)
self.assertRegex(str(type(tool)), 'dbrel_tool.DbFcts')
def test_02parseSql(self):
global mymsg
actfunction = str(inspect.currentframe().f_code.co_name)
cnttest = 0
if actfunction not in TEST_FUNCTIONS:
return
job = test.testtools.getJob()
comp = basic.component.Component()
comp.name = "testcrmdb"
table = "person"
comp.conf = {}
comp.conf[B.DATA_NODE_DDL] = {}
comp.conf[B.DATA_NODE_DDL][table] = utils.config_tool.getConfig("DATASTRUCTURE", comp.name, table)
comp.conf[B.SUBJECT_ARTS] = {}
comp.conf[B.SUBJECT_ARTS][B.TOPIC_NODE_DB] = {}
comp.conf[B.SUBJECT_CONN] = {}
comp.conf[B.SUBJECT_CONN][B.TOPIC_NODE_DB] = {}
comp.conf[B.SUBJECT_CONN][B.TOPIC_NODE_DB][B.ATTR_TYPE] = "rel"
comp.conf[B.SUBJECT_CONN][B.TOPIC_NODE_DB][B.ATTR_DB_DATABASE] = "crm-db"
tool = basic.toolHandling.getDbTool(comp)
attr = tool.getDbAttributes("xx")
self.assertRegex(attr[B.ATTR_DB_PARTITION], 'n')
comp.conf[B.SUBJECT_CONN][B.TOPIC_NODE_DB][B.ATTR_DB_PARTITION] = "y"
attr = tool.getDbAttributes("xx")
self.assertRegex(attr[B.ATTR_DB_PARTITION], 'y')
comp.conf[B.SUBJECT_ARTS][B.TOPIC_NODE_DB][B.ATTR_DB_PARTITION] = "z"
attr = tool.getDbAttributes("xx")
self.assertRegex(attr[B.ATTR_DB_PARTITION], 'z')
#
sqls = comp.composeSqlClauses("SELECT * FROM person")
print(sqls)
self.assertEqual(sqls["ALL"], "SELECT * FROM crm-db.person ORDER BY persid")
setattr(job.par, B.PAR_DB_WHERE, "famname like !%utz%! and state = !+reg+!")
#comp.conf[B.SUBJECT_ARTS][B.TOPIC_NODE_DB][B.ATTR_DB_PARTITION] = "+reg+"
sqls = comp.composeSqlClauses("SELECT * FROM person")
print(sqls)
self.assertNotIn("street", sqls["ALL"]) #assertEqual(("street" in sqls), True)
self.assertIn("famname", sqls["ALL"])
setattr(job.par, "sex", "m")
# TODO replace the formula with job-parameter
setattr(job.par, B.PAR_DB_WHERE, "famname like !%utz%! and sex = {job.par.sex}")
#comp.conf[B.SUBJECT_ARTS][B.TOPIC_NODE_DB][B.ATTR_DB_PARTITION] = "+reg+"
sqls = comp.composeSqlClauses("SELECT * FROM person")
print(sqls)
self.assertIn("famname", sqls["ALL"])
self.assertIn("sex", sqls["ALL"])
setattr(job.par, B.PAR_DB_PARTITION, "m,w")
sqls = comp.composeSqlClauses("SELECT * FROM person")
print(sqls)
MyTestCase.mymsg += "\n----- "+actfunction+" : "+str(cnttest)
def test_formatDbRows(self):
global mymsg
actfunction = str(inspect.currentframe().f_code.co_name)
cnttest = 0
if actfunction not in TEST_FUNCTIONS:
return
job = test.testtools.getJob()
MyTestCase.mymsg += "\n----- "+actfunction+" : "+str(cnttest)
def test_zzz(self):
print(MyTestCase.mymsg)
if __name__ == '__main__':
unittest.main()