Browse Source

unit-test in components

master
Ulrich Carmesin 2 years ago
parent
commit
830589fead
  1. 13
      basic/compexec.py
  2. 8
      test/constants.py
  3. 14
      test/test_31db.py
  4. 10
      utils/db_abstract.py
  5. 102
      utils/dbrel_tool.py

13
basic/compexec.py

@ -1,3 +1,9 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------------------------------------
# Author : Ulrich Carmesin
# Source : gitea.ucarmesin.de
# ---------------------------------------------------------------------------------------------------------
# abstract class for testrunning-functions # abstract class for testrunning-functions
# --------------------------------------------- # ---------------------------------------------
""" """
@ -32,6 +38,7 @@ import basic.message
import basic.program import basic.program
import inspect import inspect
import os import os
import re
import utils.db_abstract import utils.db_abstract
import basic.toolHandling import basic.toolHandling
import basic.component import basic.component
@ -196,12 +203,14 @@ class Testexecuter():
sql_new += " "+attr[B.ATTR_DB_DATABASE]+"."+table sql_new += " "+attr[B.ATTR_DB_DATABASE]+"."+table
print("sql_new "+sql_new) print("sql_new "+sql_new)
if (hasattr(job.par, B.PAR_DB_WHERE)): if (hasattr(job.par, B.PAR_DB_WHERE)):
# actual it parses only conjunct or disjunct normalform without parentesis
parts = utils.db_abstract.parseSQLwhere(getattr(job.par, B.PAR_DB_WHERE), self.conf[B.DATA_NODE_DDL][table]) parts = utils.db_abstract.parseSQLwhere(getattr(job.par, B.PAR_DB_WHERE), self.conf[B.DATA_NODE_DDL][table])
# Felder und Operationen # Felder und Operationen
# print(dbwhere) # print(dbwhere)
sql_new += " WHERE "+getattr(job.par, B.PAR_DB_WHERE) sql_new += " WHERE "+parts
if sql_new[0:6] == "SELECT": if sql_new[0:6] == "SELECT":
sql_new += " ORDER BY id" ids = utils.db_abstract.getTechnicalIDFields(self.conf["ddl"][table])
sql_new += " ORDER BY "+",".join(ids)
print("sql_new "+sql_new) print("sql_new "+sql_new)
sql_new = sql_new.replace('!', "\'") sql_new = sql_new.replace('!', "\'")
if (hasattr(job.par, B.PAR_DB_PARTITION)) and (attr[B.ATTR_DB_PARTITION] != "n"): if (hasattr(job.par, B.PAR_DB_PARTITION)) and (attr[B.ATTR_DB_PARTITION] != "n"):

8
test/constants.py

@ -1,3 +1,9 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------------------------------------
# Author : Ulrich Carmesin
# Source : gitea.ucarmesin.de
# ---------------------------------------------------------------------------------------------------------
""" """
constants constants
""" """
@ -6,6 +12,8 @@ home = os.getcwd()
prgdir = "" prgdir = ""
if home[-4:] == "test" and home[-6:] != "datest": if home[-4:] == "test" and home[-6:] != "datest":
home = home[0:-5] home = home[0:-5]
if home[-10:] == "components":
home = home[0:-11]
if home[-6:] == "datest": if home[-6:] == "datest":
prgdir = home[-6:] prgdir = home[-6:]
home = home[0:-7] home = home[0:-7]

14
test/test_31db.py

@ -1,3 +1,9 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------------------------------------
# Author : Ulrich Carmesin
# Source : gitea.ucarmesin.de
# ---------------------------------------------------------------------------------------------------------
import unittest import unittest
import inspect import inspect
import basic.program import basic.program
@ -15,8 +21,8 @@ HOME_PATH = test.constants.HOME_PATH
conf = {} conf = {}
# here you can select single testfunction for developping the tests # here you can select single testfunction for developping the tests
# "test_toolhandling", "test_parseSql" -> test of components # "test_toolhandling", "test_parseSql" -> test of components
TEST_FUNCTIONS = ["test_formatDbRows"] TEST_FUNCTIONS = ["test_formatDbRows", "test_getTechnicalIDFields"]
#TEST_FUNCTIONS = ["test_formatDbRows"] TEST_FUNCTIONS = ["test_getTechnicalIDFields"]
class MyTestCase(unittest.TestCase): class MyTestCase(unittest.TestCase):
mymsg = "--------------------------------------------------------------" mymsg = "--------------------------------------------------------------"
@ -29,10 +35,6 @@ class MyTestCase(unittest.TestCase):
if actfunction not in TEST_FUNCTIONS: if actfunction not in TEST_FUNCTIONS:
return return
job = test.testtools.getJob() job = test.testtools.getJob()
args = {"application": "TEST", "environment": "ENV01", "modus": "unit", "loglevel": "debug", "tool": "config_tool",
"modus": "unit"}
job.par.setParameterArgs(args)
# "SELECT * FROM lofts"
obj = utils.db_abstract.parseConditions("family like !%utz%! and state = !+reg+!") obj = utils.db_abstract.parseConditions("family like !%utz%! and state = !+reg+!")
self.assertEqual(obj[0][3], "and") self.assertEqual(obj[0][3], "and")
self.assertEqual(obj[0][1], "family") self.assertEqual(obj[0][1], "family")

10
utils/db_abstract.py

@ -65,7 +65,6 @@ def getDbAttributes(comp, table):
B.ATTR_DB_CONN_JAR: D.DEFAULT_DB_CONN_JAR B.ATTR_DB_CONN_JAR: D.DEFAULT_DB_CONN_JAR
} }
for attr in out.keys(): for attr in out.keys():
print(attr)
if (table in comp.conf[B.SUBJECT_ARTS][B.TOPIC_NODE_DB]) \ if (table in comp.conf[B.SUBJECT_ARTS][B.TOPIC_NODE_DB]) \
and (attr in comp.conf[B.SUBJECT_ARTS][B.TOPIC_NODE_DB][table]): and (attr in comp.conf[B.SUBJECT_ARTS][B.TOPIC_NODE_DB][table]):
out[attr] = comp.conf[B.SUBJECT_ARTS][B.TOPIC_NODE_DB][table][attr] out[attr] = comp.conf[B.SUBJECT_ARTS][B.TOPIC_NODE_DB][table][attr]
@ -180,10 +179,17 @@ def getTechnicalIDFields(ddl):
if "T" in ddl[f][D.DDL_KEY]: if "T" in ddl[f][D.DDL_KEY]:
keys[ddl[f][D.DDL_KEY]] = f keys[ddl[f][D.DDL_KEY]] = f
for k in keys: for k in keys:
ids.append(k) ids.append(keys[k])
return ids return ids
def isConditionInFields(cond, ddl):
a = cond.split(" ")
for x in a:
if x in ddl:
return True
return False
def formatDbRows(table, comp, rows): def formatDbRows(table, comp, rows):
out = [] out = []
fields = comp.conf[B.DATA_NODE_DDL][table] fields = comp.conf[B.DATA_NODE_DDL][table]

102
utils/dbrel_tool.py

@ -0,0 +1,102 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------------------------------------
# Author : Ulrich Carmesin
# Source : gitea.ucarmesin.de
# ---------------------------------------------------------------------------------------------------------
import basic.program
import utils.config_tool
import utils.db_abstract
import mysql.connector
import basic.constants as B
class DbFcts(utils.db_abstract.DbFcts):
"""
This interface defines each necessary connection to any kind of database.
The specific technique how to connect to the concrete DBMS has to be implemented in the specific tool.
"""
def __init__(self):
pass
def selectRows(self, table):
""" method to select rows from a database
statement written in sql """
tdata = {}
job = basic.program.Job.getInstance()
verify = -1+job.getDebugLevel("db_tool")
cmd = "SELECT * FROM "+table+";"
#mycursor = self.getConnector()
#mycursor.execute(cmd)
#myresult = mycursor.fetchall()
tdata[B.DATA_NODE_HEADER] = []
for f in self.comp.conf[B.DATA_NODE_DDL][table][B.DATA_NODE_HEADER]:
tdata[B.DATA_NODE_HEADER].append(f)
myresult = []
for x in myresult:
print(x)
self.comp.m.logInfo(cmd)
return tdata
def deleteRows(self, table):
""" method to delete rows from a database
statement written in sql """
job = basic.program.Job.getInstance()
verify = -1+job.getDebugLevel("db_tool")
cmd = "DELETE FROM "+table+";"
self.comp.m.logInfo(cmd)
def updateRows(self, statement):
""" method to delete rows from a database
statement written in sql """
raise Exception(B.EXCEPT_NOT_IMPLEMENT)
def insertRows(self, table, rows):
""" method to insert rows into a database
the rows will be interpreted by the ddl of the component
"""
job = basic.program.Job.getInstance()
verify = -1+job.getDebugLevel("db_tool")
cmd = "INSERT INTO "+table+";"
header = ""
for h in self.comp.conf[B.DATA_NODE_DDL][table][B.DATA_NODE_HEADER]:
print(h)
header += ", "+h
cmd += " (" + header[1:]+" ) "
rowvalues = ""
for r in rows:
print("r-----------------")
print(r)
rowvalues = ""
cmd += "\n ( "
for h in self.comp.conf[B.DATA_NODE_DDL][table][B.DATA_NODE_HEADER]:
print("h "+h)
if (h in r):
rowvalues += ", "+self.getDbValue(self.comp.conf[B.DATA_NODE_DDL][table][B.DATA_NODE_DATA][h], r[h])
else:
rowvalues += ", "+self.getDbValue(self.comp.conf[B.DATA_NODE_DDL][table][B.DATA_NODE_DATA][h], "")
print("rv " + rowvalues)
cmd += rowvalues[1:]+" ),"
cmd = cmd[0:-1]+";"
self.comp.m.logInfo(cmd)
def getConnector(self):
""" add-on-method to get the connector
this method should only called by the class itself """
job = basic.program.Job.getInstance()
mydb = mysql.connector.connect(
host = "localhost",
user = "datest",
password = "Advent!2021",
database = "datest"
)
return mysql
@staticmethod
def execStatement(self, comp, conn, statement):
""" add-on-method to execute the statement
this method should only called by the class itself """
raise Exception(B.EXCEPT_NOT_IMPLEMENT)
Loading…
Cancel
Save