#!/usr/bin/python # -*- coding: utf-8 -*- # --------------------------------------------------------------------------------------------------------- # Author : Ulrich Carmesin # Source : gitea.ucarmesin.de # --------------------------------------------------------------------------------------------------------- import os import basic.toolHandling import tools.job_const as J import utils.data_const as D import basic.constants as B import model.entity import tools.config_tool import tools.job_tool import tools.path_tool import tools.path_const as P import model.entity import model.story import model.document import model.step import model.table class Testcase(model.entity.Entity): """ Generally this object can be stored as a file with data or in a database. references: application -> story -> story variant -> comp.step subtables steps -> comp.step tables -> comp.table """ tcid = "" name = "" description = "" project = "" application = "" reference = "" attributes = "" story = [] docs = [] tables = {} steps = [] def __init__(self, job, project="", name=""): """ to be initialized by readSpec :param job: """ self.job = job if len(project) > 1: self.project = project if len(name) > 1: self.name = name self.getEntity(job, name) def read_entity(self, job, name): """ reads the entity from the file-system :param job: :param name: :return: """ pathname = tools.config_tool.select_config_path(job, P.KEY_TESTCASE, name, "") print(pathname) (fname, ext) = os.path.splitext(pathname) fi = basic.toolHandling.getFileTool(job, None, ext[1:]) tdata = fi.load_file(pathname, ttype=D.CSV_SPECTYPE_DATA) stories = [] docs = [] attributes = {} pass if B.DATA_NODE_HEAD in tdata: for h in D.LIST_HEAD_ATTR: if h in tdata[B.DATA_NODE_HEAD]: setattr(self, h, tdata[B.DATA_NODE_HEAD][h]) else: job.m.logWarn("Keine Head-Daten in Testcase "+name) if B.DATA_NODE_OPTION in tdata: for o in tdata[B.DATA_NODE_OPTION]: if o in [D.OPT_ATTR_UCID, D.OPT_ATTR_USECASE]: pass elif o in [D.OPT_ATTR_STORYID, D.OPT_ATTR_STORY]: if len(stories) < 1: story = model.story.Story(job) stories.append(story) else: story = stories[0] setattr(story, model.story.FIELDS[o], tdata[B.DATA_NODE_OPTION][o]) elif o in D.LIST_OPT_ATTR: setattr(self, o, tdata[B.DATA_NODE_OPTION][o]) else: attributes[o] = tdata[B.DATA_NODE_OPTION][o] else: job.m.logWarn("Keine Option-Daten in Testcase "+name) if len(attributes) > 0: self.attributes = attributes if len(stories) > 0: self.story = stories pass if B.DATA_NODE_STEPS in tdata: for s in tdata[B.DATA_NODE_STEPS]: step = model.step.Step(job, project=job.par.project, name=s) pass if B.DATA_NODE_TABLES in tdata: for t in tdata[B.DATA_NODE_TABLES]: table = model.table.Table(job, project=job.par.project, name=t) pass # for o in tdata[B.option # header = tdata[B.DATA_NODE_STEPS][] LIST # reference # # for t in tdata[B.tables #raise Exception(B.EXCEPT_NOT_IMPLEMENT) def select_entity(self, job, name): """ reads the entity from the database it should get the same result like read_entity :param job: :param name: :return: """ raise Exception(B.EXCEPT_NOT_IMPLEMENT) def get_schema(self, tableName="", tableObject=None): dbtype = self.job.conf[B.TOPIC_NODE_DB][B.ATTR_TYPE] dbi = basic.toolHandling.getDbTool(self.job, None, dbtype) sql = dbi.getCreateTable("testcase") sql += dbi.getSchemaAttribut("tcid", "id")+"," sql += dbi.getSchemaAttribut("name", D.TYPE_STR)+"," sql += dbi.getSchemaAttribut("description", D.TYPE_TEXT)+"," sql += dbi.getSchemaAttribut("project", D.TYPE_STR)+"," sql += dbi.getSchemaAttribut("usecase", D.TYPE_STR)+"," sql += dbi.getSchemaAttribut("attributes", D.TYPE_TEXT)+"," sql += self.getHistoryFields() sql += ");\n" sql += self.getHistoryIndex("testcase") for attr in ["application", "story"]: sql += dbi.getSchemaSubtable("tc", [{"attr":attr, "atype": D.TYPE_STR}])+"\n" sql += dbi.getSchemaIndex(dbi.getIndexName("tc", attr), dbi.getSubTableId(dbi.getSubTableName("tc", attr), attr))+"\n" for attr in ["dtable", "step"]: sql += dbi.getSchemaSubtable("tc", [{"attr":attr, "atype": D.TYPE_STR}, {"attr":"attributes", "atype": D.TYPE_TEXT}])+"\n" sql += dbi.getSchemaIndex(dbi.getSubTableName("tc", attr), dbi.getSubTableId(dbi.getSubTableName("tc", attr), attr))+"\n" return sql def select_testcase(job, project, testcase): """ to select a concrete testcase :param job: :param project: :param testcase: :return: """ jobProj = None if hasattr(job.par, B.PAR_PROJ): jobProj = getattr(job.par, B.PAR_PROJ) setattr(job.par, B.PAR_PROJ, project) path = tools.path_tool.compose_path(job, P.P_TDROOT, None) specpath = os.path.join(path, testcase, D.DFILE_TESTCASE_NAME + ".csv") spec = model.entity.read_spec(job, testcase, J.GRAN_TS, specpath) if jobProj is None: delattr(job.par, B.PAR_PROJ) else: setattr(job.par, B.PAR_PROJ, jobProj) print("select_testcase "+str(spec)) return spec def select_testcases(job, projList, appList): out = {} jobProj = None if hasattr(job.par, B.PAR_PROJ): jobProj = getattr(job.par, B.PAR_PROJ) for proj in projList: setattr(job.par, B.PAR_PROJ, proj) path = tools.path_tool.compose_path(job, P.P_TDROOT, None) if os.path.exists(path): for d in os.listdir(path): if not os.path.isdir(os.path.join(path, d)): continue if d[0:1] == "_": continue specpath = os.path.join(path, d, D.DFILE_TESTCASE_NAME + ".csv") spec = model.entity.read_spec(job, d, J.GRAN_TS, specpath) if spec is None: continue out[d] = spec out[d][B.SUBJECT_PROJECTS] = [proj] if jobProj is None: delattr(job.par, B.PAR_PROJ) else: setattr(job.par, B.PAR_PROJ, jobProj) return out