def execute_post(self,APIName): dataResult = DataResult() try: tasks = { 'getTableInfoByName': lambda: self.getTableInfoByName(), 'addTable' : lambda : self.addTable(), 'deleteTable':lambda :self.deleteTable(), 'editTable':lambda :self.editTable(), 'editTableByName':lambda :self.editTableByName(), 'discardTableByName':lambda :self.discardTableByName(), 'getColumnListByTableName': lambda: self.getColumnListByTableName(), 'addColumn': lambda: self.addColumn(), 'deleteColumn': lambda: self.deleteColumn(), 'editColumn': lambda: self.editColumn(), 'initSynchronizeDatabase': lambda: self.initSynchronizeDatabase(), 'synchronizeDatabase': lambda: self.synchronizeDatabase(), 'initSynchronizeTable': lambda: self.initSynchronizeTable(), 'initSynchronizeColumn': lambda: self.initSynchronizeColumn(), 'addDataRoute': lambda: self.addDataRoute(), 'deleteDataRoute': lambda: self.deleteDataRoute(), 'editDataRoute': lambda: self.editDataRoute(), 'editColumnRemarkById': lambda: self.editColumnRemarkById(), 'editColumnDiscardById': lambda: self.editColumnDiscardById(), 'getSearchByTable': lambda: self.getSearchByTable(), 'getSearchByTableColumn': lambda: self.getSearchByTableColumn(), 'getSearchByColumn': lambda: self.getSearchByColumn(), } self.write(json.dumps(tasks[APIName]().__dict__,cls=CJsonEncoder)) except: logger.error(traceback.format_exc()) dataResult.setMessage(traceback.format_exc()) dataResult.setSuccess(False) dataResult.setStatusCode(500) self.write(json.dumps(dataResult.__dict__)) finally: try: self.finish() except: pass
def addLinkByMatchRule(self, args): result = DataResult() # 1是自定义匹配,2是驼峰全局匹配,3是_全局匹配 db_id = args["db"] content = args["content"] match_type = args["type"] links = [] link_args = {} link_args.setdefault("relation_type", 1) if match_type == 1: lists = [l.split("-") for l in content.split(",")] for l in lists: link = l[0].lower() src = l[1].lower() table_info = self.getIdColumnInfoByTableName( db_id, link).getMessage() link_args["link_column_id"] = table_info[0]["id"] link_args["link_table_id"] = table_info[0]["table_id"] info_links = self.addLinkByMatchRuleInsert( db_id, src, link_args) links.extend(info_links) elif match_type == 2 or match_type == 3: table_info = self.getIdColumnListByTableName(db_id).getMessage() for t in table_info: link_args["link_column_id"] = t["cl_id"] link_args["link_table_id"] = t["t_id"] link = t["t_name"] src = "" if match_type == 2: src = "{}id".format(link) elif match_type == 3: src = "{}_id".format(link) info_links = self.addLinkByMatchRuleInsert( db_id, src, link_args) links.extend(info_links) return self.DatabaseDaoInterface.addColumnLink(links, is_execute_many=True)
def getCount(self): args = {} args["createUser"] = UserService().getUserCount().getMessage( )[0]["userCount"] args["project"] = ProjectService().getProjectCount().getMessage( )[0]["projectCount"] args["application"] = ApplicationService().getApplicationCount( ).getMessage()[0]["applicationCount"] args["testCase"] = TestCaseService().getTestCaseCount().getMessage( )[0]["testCaseCount"] dataResult = DataResult() dataResult.setMessage(args) dataResult.setSuccess(True) logger.info(dataResult) return dataResult
def execute_get(self,APIName): dataResult = DataResult() try: tasks = { 'getProjectInfoByName' : lambda : self.getProjectInfoByName(), 'getProjectInfoById': lambda: self.getProjectInfoById(), 'getProjectList':lambda :self.getProjectList(), 'getProjectLogListData':lambda :self.getProjectLogListData(), 'getProjectListByApplicationId' :lambda :self.getProjectListByApplicationId(), # lambda alias } self.write(json.dumps(tasks[APIName]().__dict__,cls=CJsonEncoder)) except: logger.error(traceback.format_exc()) dataResult.setMessage(traceback.format_exc()) dataResult.setSuccess(False) dataResult.setStatusCode(500) self.write(json.dumps(dataResult.__dict__)) finally: try: self.finish() except: pass
def synchronizeWebApi(self): swaggerJsonOrUrl = self.get_argument('swaggerJsonOrUrl') projectId = self.get_argument('projectId') applicationId=self.get_argument('applicationId') args = {} args.setdefault("swaggerJsonOrUrl", swaggerJsonOrUrl) args.setdefault("projectId", projectId) args.setdefault("applicationId", applicationId) response = requests.post(url="http://192.168.50.27:83/api/swaggerservice/createwebapis", data=json.dumps(args), headers={"Content-Type": "application/json"}) dataResult = json.loads(response.text) Result = DataResult() Result.setSuccess(True) Result.setMessage(dataResult) logger.info(Result) return Result
def execute_get(self,APIName): dataResult = DataResult() try: tasks = { 'getWebApiForVersion': lambda: self.getWebApiForVersion(), 'getWebApiInfoById':lambda :self.getWebApiInfoById(), 'getWebApiList': lambda : self.getWebApiList(), 'getWebApiInfoByPath':lambda :self.getWebApiInfoByPath(), 'synchronizeWebApi':lambda :self.synchronizeWebApi(), # lambda alias } self.write(json.dumps(tasks[APIName]().__dict__,cls=CJsonEncoder)) except: logger.error(traceback.format_exc()) dataResult.setMessage(traceback.format_exc()) dataResult.setSuccess(False) dataResult.setStatusCode(500) self.write(json.dumps(dataResult.__dict__)) finally: try: self.finish() except: pass
def execute_post(self, APIName): dataResult = DataResult() try: tasks = { 'addInterfaceItem': lambda: self.addInterfaceItem(), 'deleteInterfaceItem': lambda: self.deleteInterfaceItem(), 'updateInterfaceItem': lambda: self.updateInterfaceItem(), 'enableInterfaceItem': lambda: self.enableInterfaceItem(), 'disableInterfaceItem': lambda: self.disableInterfaceItem(), 'setInterfaceGroup': lambda: self.setInterfaceGroup(), } self.write(json.dumps(tasks[APIName]().__dict__, cls=CJsonEncoder)) except: logger.error(traceback.format_exc()) dataResult.setMessage(traceback.format_exc()) dataResult.setSuccess(False) dataResult.setStatusCode(500) self.write(json.dumps(dataResult.__dict__)) finally: try: self.finish() except: pass
def execute_get(self, APIName): dataResult = DataResult() try: tasks = { 'getEnvironmentInfoById': lambda: self.getEnvironmentInfoById(), 'getEnvironmentInfos': lambda: self.getEnvironmentInfos(), 'getEnvironmentInfoByUserId': lambda: self.getEnvironmentInfoByUserId() # lambda alias } self.write(json.dumps(tasks[APIName]().__dict__, cls=CJsonEncoder)) except: logger.error(traceback.format_exc()) dataResult.setMessage(traceback.format_exc()) dataResult.setSuccess(False) dataResult.setStatusCode(500) self.write(json.dumps(dataResult.__dict__)) finally: try: self.finish() except: pass
def execute_post(self,APIName): dataResult = DataResult() try: tasks = { 'addApplication' : lambda : self.addApplication(), 'addApplicationVersion' : lambda : self.addApplicationVersion(), 'deleteApplicationVersion' : lambda :self.deleteApplicationVersion(), 'addApplicationVersionConfig' : lambda :self.addApplicationVersionConfig(), 'editVersionConfig' : lambda :self.editVersionConfig(), 'editApplication': lambda: self.editApplication(), 'deleteApplication' :lambda :self.deleteApplication(), } self.write(json.dumps(tasks[APIName]().__dict__,cls=CJsonEncoder)) except: logger.error(traceback.format_exc()) dataResult.setMessage(traceback.format_exc()) dataResult.setSuccess(False) dataResult.setStatusCode(500) self.write(json.dumps(dataResult.__dict__)) finally: try: self.finish() except: pass
def addEnvironmentItem(self, args): if "url" not in args: args.setdefault("url", None) if "template" not in args: args.setdefault("template", None) else: if not isinstance(args.get("template"), dict): try: #验证data模板是否为json logger.info("template is not dict:{0}".format( args.get("template"))) datatemplate = json.dumps(json.loads(args.get("template"))) # logger.error(datatemplate) # logger.error(type(datatemplate)) args.pop("template") args.setdefault("template", datatemplate) except Exception: logger.error(traceback.format_exc()) dataResult = DataResult() dataResult.setMessage( "template param [{0}]is invalid, must be dict".format( args.get("template"))) dataResult.setSuccess(False) return dataResult else: logger.info("template is dict:{0}".format( args.get("template"))) datatemplateJSONString = json.dumps(args.get("template")) # logger.error(datatemplateJSONString) args.pop("template") args.setdefault("template", datatemplateJSONString) if "dbname" not in args: args.setdefault("dbname", None) if "dbhostname" not in args: args.setdefault("dbhostname", None) if "dbport" not in args: args.setdefault("dbport", None) if "dbusername" not in args: args.setdefault("dbusername", None) if "dbpasswd" not in args: args.setdefault("dbpasswd", None) return self.EnvironmentDaoInterface.addEnvironmentItem(args)
def __execAssertJob(actual, expect, assert_type="0"): #equal if assert_type == "0": result = AssertInstance.get_instance().isEqual(actual, expect) #not equal elif assert_type == "1": result = AssertInstance.get_instance().notEqual(actual, expect) #contain elif assert_type == "2": result = AssertInstance.get_instance().isContain(actual, expect) #not contain elif assert_type == "3": result = AssertInstance.get_instance().notContain(actual, expect) else: result = False assertResult = DataResult() assertResult.setSuccess(result) assertResult.setMessage("Notice:actual={0},expect={1}\n".format( actual, expect)) return assertResult
def editEnvironmentItem(self, args): if "template" not in args: args.setdefault("template", None) else: if not isinstance(args.get("template"), dict): try: #验证data模板是否为json logger.info("template is not dict:{0}".format( args.get("template"))) datatemplate = json.dumps(json.loads(args.get("template"))) args.pop("template") args.setdefault("template", datatemplate) except Exception, err: logger.error(traceback.format_exc()) dataResult = DataResult() dataResult.setMessage( "template param [{0}]is invalid, must be dict".format( args.get("template"))) dataResult.setSuccess(False) return dataResult else:
class RequestBase(object): def __init__(self, url, method="GET", format="application/json", params={}, timeout=1000, object=None, saveSession=False, userId=None): self.url = url self.method = method self.format = format self.params = params self.timeout = timeout self.Object = object self.saveSession = saveSession self.userId = userId self.dataResult = DataResult() self.test = "https://testpay.schoolpal.cn/" @AdminDecoratorServer.execImplDecorator() def getRequestInstance(self): if self.Object is None: return requests.Session() return self.Object @AdminDecoratorServer.execImplDecorator() def parseDomain(self): self.dataResult.setSuccess(True) self.dataResult.setMessage( self.url.replace("http://", "").split("/")[0]) return self.dataResult def route(self): if self.method == "GET": return self.getByCase() elif self.method == "POST": return self.postByCase() else: logger.error("Request method [{0}] not support".format( self.method)) self.dataResult.setSuccess(False) self.dataResult.setMessage( "Request method [{0}] not support".format(self.method)) return self.dataResult @AdminDecoratorServer.execImplDecorator() def get(self): headers = {} if self.format is not None: headers["contentType"] = self.format if self.saveSession and self.userId: #debug dataResult = self.parseDomain() if dataResult.getSuccess(): args = {} args["userId"] = self.userId args["domain"] = dataResult.getMessage() sessionResult = SessionManageDaoInterface().getSessionInfo( args) if sessionResult.getSuccess() and len( sessionResult.getMessage()) > 0: headers["cookie"] = "SessionId={0}".format( sessionResult.getMessage()[0].get("session")) r = self.getRequestInstance().get(self.url, parmas=self.params, headers=headers, verify=False) if self.saveSession and self.userId: if len(r.cookies.values()) > 0: dataResult = self.parseDomain() if dataResult.getSuccess(): args = {} args["userId"] = self.userId args["domain"] = dataResult.getMessage() args["session"] = r.cookies.values()[0] sessionResult = SessionManageDaoInterface().getSessionInfo( args) if sessionResult.getSuccess() and len( sessionResult.getMessage()) <= 0: SessionManageDaoInterface().addSession(args) else: SessionManageDaoInterface().updateSession(args) else: logger.warn("get session fail:Result [{0}]".format(r.text)) return r.text, self.getRequestInstance() @AdminDecoratorServer.execImplDecorator() def post(self): headers = {} if self.format is not None: headers["contentType"] = self.format if self.saveSession and self.userId: #pre debug dataResult = self.parseDomain() if dataResult.getSuccess(): args = {} args["userId"] = self.userId args["domain"] = dataResult.getMessage() sessionResult = SessionManageDaoInterface().getSessionInfo( args) if sessionResult.getSuccess() and len( sessionResult.getMessage()) > 0: headers["cookie"] = "SessionId={0}".format( sessionResult.getMessage()[0].get("session")) r = self.getRequestInstance().post(self.url, parmas=self.params, headers=headers, verify=False) if self.saveSession and self.userId: #post debug if len(r.cookies.values()) > 0: dataResult = self.parseDomain() if dataResult.getSuccess(): args = {} args["userId"] = self.userId args["domain"] = dataResult.getMessage() args["session"] = r.cookies.values()[0] sessionResult = SessionManageDaoInterface().getSessionInfo( args) if sessionResult.getSuccess() and len( sessionResult.getMessage()) <= 0: SessionManageDaoInterface().addSession(args) else: SessionManageDaoInterface().updateSession(args) else: logger.warn("get session fail:Result [{0}]".format(r.text)) return r.text, self.getRequestInstance() def init(self, s): url = self.test + "apiBusiness/MerchantBusiness/InitCurrentUser" data = { "token": "9dd3e3fd4cf042adc0652527d04f297c", "phone": "18201115228" } print(url) r = s.get(url, params=data) print(r.headers) return r.cookies.values()[0] def getSchool(self, s, session=""): url = self.test + "apiBusiness/TransactionBusiness/GetTransactionInfos" data = {"PageIndex": 1, "PageSize": 10} #headers ={"cookie":"SessionId={0}".format(session)} #r = s.get(url,data=data,headers=headers) r = s.get(url, data=data) return r.cookies.values() @AdminDecoratorServer.execImplDecorator() def getByCase(self): dataResult = DataResult() headers = {} if self.format is not None: headers["contentType"] = self.format r = requests.Session().get(self.url, params=self.params, headers=headers, verify=False) dataResult.setMessage(r.text) dataResult.setSuccess(True) return dataResult @AdminDecoratorServer.execImplDecorator() def postByCase(self): dataResult = DataResult() headers = {} if self.format is not None: headers["contentType"] = self.format r = requests.Session().post(self.url, params=self.params, headers=headers, verify=False) dataResult.setMessage(r.text) dataResult.setSuccess(True) return dataResult
def __execTaskJob(taskInfo): taskInstanceId = taskInfo.get("instanceid") logger.info("exec task [{0}]:".format(taskInstanceId)) args = {} args.setdefault("instanceId", taskInstanceId) args.setdefault("build_start", datetime.datetime.now()) result = DataResult() taskGlobalStatus = "Error" try: #set metaq queue status is received metaq_args = {} metaq_args.setdefault("status", "2") #hostname hostname = socket.gethostname() ip = socket.gethostbyname(hostname) metaq_args.setdefault("running_consumer", "{0}|{1}".format(ip, hostname)) metaq_args.setdefault("msg_id", os.getpid()) metaq_args.setdefault("taskId", taskInfo.get("id")) TaskMetaqInfoDaoInterface().updateTaskInfo(metaq_args) #query task instance info by id logger.info("query task instance [{0}] ...".format(taskInstanceId)) dataResult = TestCaseInstanceDaoInterface( ).getTestInstanceInfoById(args) if dataResult.getSuccess() and len(dataResult.getMessage()) > 0: suiteId = dataResult.getMessage()[0].get("suite_id") userId = dataResult.getMessage()[0].get("create_userid") #set instance Running args.setdefault("build_end", None) args.setdefault("status", "Running") TestCaseInstanceDaoInterface().updateTestInstance(args) logger.info("query suite [{0}]by instance [{1}] ...".format( suiteId, taskInstanceId)) dataResult = TestSuiteDaoInterface().getSuiteInfoById( {"suiteId": suiteId}) if dataResult.getSuccess() and len( dataResult.getMessage()) > 0: envId = dataResult.getMessage()[0].get("envid") caseIds = dataResult.getMessage()[0].get("testcaseids") #get global env param envResult = EnvironmentDaoInterface( ).getEnvironmentInfoById({"envId": envId}) if envResult.getSuccess() and len( envResult.getMessage()) <= 0: logger.error( "Test suite [{0}]:No env config".format(suiteId)) taskGlobalStatus = "Failed" return result if caseIds is None or len(caseIds) == 0: logger.info("Test suite [{0}]:No case".format(suiteId)) taskGlobalStatus = "Success" else: # get init case for save session initResult = TaskCenterService.__getInitCaseJob( caseIds) if not initResult.getSuccess(): logger.error( "Test suite [{0}]:No init case".format( suiteId)) taskGlobalStatus = "Failed" return result initCaseInfo = initResult.getMessage()[0] initCaseId = initCaseInfo.get("id") #return request Object initRequest = TaskCenterService.__execTaskCaseJob( initCaseId, userId, envResult[0], None, True, taskInstanceId, "init") for caseId in list(caseIds): if caseId == initCaseId: continue dataResult = TestCaseInstanceDaoInterface( ).getTestInstanceInfoById(args) if dataResult.getMessage()[0].get( "status") not in ["Stopped", "TimeOut"]: execResult = TaskCenterService.__execTaskCaseJob( caseId, None, envResult[0], initRequest.getMessage(), True, taskInstanceId, None) if not execResult.getSuccess(): #set instance db taskGlobalStatus = execResult.getMessage() return result else: logger.error( "Task instance [{0}] is stopped".format( args.get("instanceId"))) taskGlobalStatus = dataResult.getMessage( )[0].get("status") return result taskGlobalStatus = "Success" result.setSuccess(True) return result except Exception as e: logger.error("Exception:{0}".format(traceback.format_exc())) return result finally: args.setdefault("build_end", datetime.datetime.now()) args.setdefault("status", taskGlobalStatus) TestCaseInstanceDaoInterface().updateTestInstance(args)
def startTaskByBatchCase(self, args): logger.info(args) isSuccess = True dataResult = DataResult() try: getCases = TestCaseDaoInterface().getCaseIds(args) if getCases.getSuccess(): if len(getCases.getMessage()) > 0: cases = getCases.getMessage() for case in cases: # caseId.setdefault("envName", args["envName"]) # dataResult = self.startTaskBySingleCase(caseId) contents = CaseContentDaoInterface( ).getContentInfosByCaseId(case) caseResult = {} #caseName = TestCaseDaoInterface().getCaseInfosById(caseId) caseResult.setdefault("caseName", case["name"]) caseResult.setdefault("caseId", case["caseId"]) caseResult.setdefault("instanceId", args.get("instanceId")) message = [] exec_start = time.time() caseResult.setdefault("exec_start", exec_start) if contents.getSuccess(): for content in contents.getMessage(): if int(content["method"]) == 0: method = "GET" else: method = "POST" if int(content["content_type"]) == 0: contentType = "application/json" if content[ "requests_params"] is None or content[ "requests_params"] == "": data = {} else: data = eval(content["requests_params"]) data = json.dumps(data) else: contentType = "application/x-www-form-urlencoded" if content[ "requests_params"] is None or content[ "requests_params"] == "": data = {} else: data = eval(content["requests_params"]) data = data if len(args["envName"]) > 0: environments = EnvironmentDaoInterface( ).getEnvironmentInfoByName(args) environment = environments.getMessage()[0] if environment[ "pre_url"] is None or environment[ "pre_url"] == "": if content[ "webapi_path"] is None or content[ "webapi_path"] == "": logger.info("缺少接口path") continue else: if content[ "ip_url"] is None or content[ "ip_url"] == "": logger.info("缺少请求地址") continue else: url = content[ "ip_url"] + content[ "webapi_path"] else: if content[ "webapi_path"] is None or content[ "webapi_path"] == "": logger.info("缺少接口path") continue else: url = environment[ "pre_url"] + content[ "webapi_path"] if environment[ "headers"] is None or environment[ "headers"] == "": if content[ "headers"] is None or content[ "headers"] == "": headers = {} headers.setdefault( "content-type", contentType) else: header = content["headers"] headers = eval(header) headers.setdefault( "content-type", contentType) else: header = environment["headers"] headers = eval(header) headers.setdefault( "content-type", contentType) else: if content[ "webapi_path"] is None or content[ "webapi_path"] == "": logger.info("缺少接口path") continue else: if content["ip_url"] is None or content[ "ip_url"] == "": logger.info("缺少请求地址") continue else: url = content["ip_url"] + content[ "webapi_path"] if content["headers"] is None or content[ "headers"] == "": headers = {} headers.setdefault( "content-type", contentType) else: header = content["headers"] headers = eval(header) headers.setdefault( "content-type", contentType) requestUtil = RequestBase(url=url, method=method, data=data, headers=headers) response = requestUtil.httpRequest( url=url, method=method, data=data, headers=headers) logger.info(response.text) logger.info(response.status_code) tmpArgs = {} tmpArgs[content["step_name"]] = response.text tmpArgs["status_code"] = response.status_code tmpArgs[ "elapsed_ms"] = response.elapsed.microseconds / 1000.0 message.append(tmpArgs) #本处代码只是为了区别返回值为json还是view try: data = json.loads(response.text) except Exception as e: logger.warn(e) data = response.text if response.status_code == 200: #此处需要验证用户自定义断言 contentJson = {"contentId": content["id"]} assertInfos = AssertDaoInterface( ).getAssertInfosByContentId(contentJson) isFailed = False if assertInfos.getSuccess(): for assertInfo in assertInfos.getMessage( ): actual = CommonTool().render( template=assertInfo["actual"], data=data, status=response.status_code) if not AssertService().routeAssert( actual, assertInfo["expect"], assertInfo["assert_type"]): isFailed = True if not isFailed: caseResult.setdefault( "exe_status", "success") else: caseResult.setdefault( "exe_status", "fail") else: caseResult.setdefault("exe_status", "fail") isSuccess = False caseResult.setdefault("message", str(message)) caseResult.setdefault( "runtime", (time.time() - exec_start) * 1000) logger.info(caseResult) createCaseResult = CaseResultDaoInterface( ).addCaseResult(caseResult) logger.info(createCaseResult.getSuccess()) logger.info(createCaseResult.getMessage()) else: dataResult.setMessage("项目中无可执行的用例") isSuccess = False else: dataResult.setMessage("获取用例失败") isSuccess = False if isSuccess: args["status"] = 2 else: args["status"] = 3 TestCaseInstanceDaoInterface().updateTestInstanceStatus(args) return dataResult except Exception as err: logger.info(err) args["status"] = 4 TestCaseInstanceDaoInterface().updateTestInstanceStatus(args) dataResult.setMessage("程序运行异常") return dataResult
def __execTaskCaseJob(caseId,userId=None,envConfig=False,requestObject=None,init=False, \ instanceId=None,caseName=None): if not envConfig: caseInfo = TestCaseDaoInterface().getCaseInfosById( {"caseId", caseId}) if caseInfo.getSuccess() and len(caseInfo.getMessage()) > 0: envId = caseInfo.getMessage()[0].get("envid") envResult = EnvironmentDaoInterface().getEnvironmentInfoById( {"envId": envId}) if envResult.getSuccess() and len(envResult.getMessage()) <= 0: logger.error( "Test envId [{0}]:No env config".format(envId)) return envConfig = envResult[0] else: return if init: saveSession = False elif requestObject is not None: saveSession = False else: saveSession = True exec_start_flag = time.time() exec_start = datetime.datetime.now() contentResult = CaseContentDaoInterface().getContentInfosByCaseId( caseId) Presult = {} statusFlag = True if contentResult.getSuccess() and len(contentResult.getMessage()) > 0: for content in sorted(contentResult.getMessage(), key=attrgetter('step')): #TODO "DATARESULT" + STEP response = None #request api if content.get("type") == 0: if envConfig.get("datatemplate").endswith("/"): url = envConfig.get("datatemplate")[:-1] + content.get( "url") else: url = envConfig.get("datatemplate") + content.get( "url") params = TaskCenterService.__rendeTemplate( content.get("request_params"), envConfig.get("datatemplate"), Presult) requestUtil = RequestBase(url=url, method=content.get("method"), format=content.get("format"), params=params, object=requestObject, userId=userId, saveSession=saveSession) response, obj = requestUtil.route() try: response = json.loads(response) except Exception as e: logger.warn( "return result is not json:{0} Exception:{1}". format(response, e)) #request sql elif content.get("type") == 1: dbConfig = {} dbConfig.setdefault("db", envConfig.get("dbname")) dbConfig.setdefault("host", envConfig.get("dbhostname")) dbConfig.setdefault("user", envConfig.get("dbusername")) dbConfig.setdefault("passwd", envConfig.get("dbpasswd")) dbConfig.setdefault("port", envConfig.get("dbport")) dbUtil = DbBaseHelper() response = dbUtil.execReadOnlySQL( dbConfig, TaskCenterService.__rendeTemplate( content.get("sqlcontent"), envConfig.get("datatemplate"), Presult, False)) Presult.setdefault("DATAREAULT" + str(content.get("step")), response) #assert assertResult = AssertDaoInterface().getAssertInfosByContentId( content.get("id")) if assertResult.getSuccess(): for assertInfo in assertResult: result = TaskCenterService.__execAssertJob( eval("Presult." + assertInfo.get("actual")), assertInfo.get("expect"), assertInfo.get("assert_type")) if not result.getSuccess(): statusFlag = False break if saveSession: exec_end_flag = time.time() exec_end = datetime.datetime.now() if statusFlag: status = "Success" else: status = "Failed" #caseResult caseResultInfo = {} caseResultInfo.setdefault("instanceid", instanceId) caseResultInfo.setdefault("caseid", caseId) caseResultInfo.setdefault("casename", caseName) caseResultInfo.setdefault("runtime", exec_end_flag - exec_start_flag) caseResultInfo.setdefault("exec_start", exec_start) caseResultInfo.setdefault("exec_end", exec_end) caseResultInfo.setdefault("status", status) caseResultInfo.setdefault("messaga", response) caseResultInfo.setdefault("remarks", None) CaseResultDaoInterface().addCaseResult(caseResultInfo) dataResult = DataResult() dataResult.setSuccess(statusFlag) if init: dataResult.setMessage(obj) else: dataResult.setMessage(status) return dataResult
def editEnvironmentItem(self, args): if "template" not in args: args.setdefault("template", None) else: if not isinstance(args.get("template"), dict): try: #验证data模板是否为json logger.info("template is not dict:{0}".format( args.get("template"))) datatemplate = json.dumps(json.loads(args.get("template"))) args.pop("template") args.setdefault("template", datatemplate) except Exception as e: logger.error(traceback.format_exc()) dataResult = DataResult() dataResult.setMessage( "template param [{0}]is invalid, must be dict".format( args.get("template"))) dataResult.setSuccess(False) return dataResult else: logger.info("template is dict:{0}".format( args.get("template"))) datatemplateJSONString = json.dumps(args.get("template")) args.pop("template") args.setdefault("template", datatemplateJSONString) if "headers" not in args: args.setdefault("headers", None) else: if not isinstance(args.get("headers"), dict): try: #验证authInfo是否为json logger.info("headers is not dict:{0}".format( args.get("headers"))) datatemplate = json.dumps(json.loads(args.get("headers"))) args.pop("headers") args.setdefault("headers", datatemplate) except Exception as e: logger.error(traceback.format_exc()) dataResult = DataResult() dataResult.setMessage( "headers param [{0}]is invalid, must be dict".format( args.get("headers"))) dataResult.setSuccess(False) return dataResult else: logger.info("headers is dict:{0}".format(args.get("headers"))) datatemplateJSONString = json.dumps(args.get("headers")) args.pop("headers") args.setdefault("headers", datatemplateJSONString) if "authInfo" not in args: args.setdefault("authInfo", None) else: if not isinstance(args.get("authInfo"), dict): try: #验证authInfo是否为json logger.info("authInfo is not dict:{0}".format( args.get("authInfo"))) datatemplate = json.dumps(json.loads(args.get("authInfo"))) args.pop("authInfo") args.setdefault("authInfo", datatemplate) except Exception as e: logger.error(traceback.format_exc()) dataResult = DataResult() dataResult.setMessage( "authInfo param [{0}]is invalid, must be dict".format( args.get("authInfo"))) dataResult.setSuccess(False) return dataResult else: logger.info("authInfo is dict:{0}".format( args.get("authInfo"))) datatemplateJSONString = json.dumps(args.get("authInfo")) args.pop("authInfo") args.setdefault("authInfo", datatemplateJSONString) dataResult = self.EnvironmentDaoInterface.getEnvironmentInfoById(args) if dataResult.getSuccess() and len(dataResult.getMessage()) > 0: for key, value in dataResult.getMessage()[0].items(): if key not in args: args.setdefault(key, value) return self.EnvironmentDaoInterface.editEnvironmentItem(args) dataResult.setMessage("apiId [{0}] is invalid".format( args.get("envId"))) return dataResult
def __deco(*args, **kwargs): dataResult = DataResult() try: isAdmin = False ips = adminIps.split(",") #get remote ip of client remote_ip = args[0].__dict__['request'].remote_ip for ip in ips: if remote_ip == ip: isAdmin = True if not isAdmin: dataResult.setSuccess(False) dataResult.setMessage( "Request IP [{0}]without a white list,to be intercepted" .format(remote_ip)) return dataResult return func(*args, **kwargs) except Exception, err: logger.error(traceback.format_exc()) dataResult.setMessage(traceback.format_exc()) dataResult.setSuccess(False) dataResult.setStatusCode(500) return dataResult
def __init__(self,sql=None,args=None,is_execute_many=False): self.data = DataResult() self.sql=sql self.args =args self.is_execute_many=is_execute_many
class DbBaseHelper(object): def __init__(self,sql=None,args=None,is_execute_many=False): self.data = DataResult() self.sql=sql self.args =args self.is_execute_many=is_execute_many #针对select操作 def read(self, **kwargs): db = Connection(autocommit=False, **kwargs) try: if self.args is not None and not isinstance(self.args,dict): logger.error("sql params [{0}] type is error,must be dict".format(self.args)) self.data.setMessage("sql params type is error,must be dict") self.data.setSuccess(False) return self.data ret = db.read(self.sql,self.args) self.data.setMessage(list(ret)) self.data.setSuccess(True) return self.data except Exception, e: logger.error("select sql:{0} args:{1} Exception:{2}".format(self.sql,self.args,traceback.format_exc())) self.data.setSuccess(False) self.data.setMessage(traceback.format_exc()) self.data.setStatusCode(500) return self.data finally:
class DbBaseHelper(object): def __init__(self, sql=None, args=None, is_execute_many=False): self.data = DataResult() self.sql = sql self.args = args self.is_execute_many = is_execute_many #针对select操作 def read(self, **kwargs): db = Connection(autocommit=False, **kwargs) try: if self.args is not None and not isinstance(self.args, dict): logger.error( "sql params [{0}] type is error,must be dict".format( self.args)) self.data.setMessage("sql params type is error,must be dict") self.data.setSuccess(False) return self.data ret = db.read(self.sql, self.args) self.data.setMessage(list(ret)) self.data.setSuccess(True) return self.data except Exception as e: logger.error("select sql:{0} args:{1} Exception:{2}".format( self.sql, self.args, traceback.format_exc())) self.data.setSuccess(False) self.data.setMessage(traceback.format_exc()) self.data.setStatusCode(500) return self.data finally: db.close() # 针对update、insert、delete操作,is_execute_many=True 支持批量插入和更新 def write(self): db = Connection(autocommit=False) try: if self.is_execute_many: if not isinstance(self.args, list): logger.error( "sql params [{0}] type is error,must be list".format( self.args)) self.data.setMessage( "sql params type is error,must be list") self.data.setSuccess(False) return self.data elif self.args is not None and not isinstance(self.args, dict): logger.error( "sql params [{0}] type is error,must be dict".format( self.args)) self.data.setMessage("sql params type is error,must be dict") self.data.setSuccess(False) return self.data ret = db.write(self.sql, self.args, self.is_execute_many) db.commit() self.data.setMessage(ret) self.data.setSuccess(True) return self.data except Exception as e: logger.error("write sql:{0} args:{1} Exception:{2}".format( self.sql, self.args, traceback.format_exc())) self.data.setSuccess(False) self.data.setMessage(traceback.format_exc()) self.data.setStatusCode(500) return self.data finally: db.close() def execReadOnlySQL(self, dbConfig, sql): db = Connection(autocommit=False, **dbConfig) try: ret = db.read(sql) return list(ret) except Exception as e: logger.error("execReadOnlySQL Exception:sql{0} reason:{1}".format( sql, traceback.format_exc())) return [] finally: db.close()
def updateComment(self, args): result = DataResult() result.setSuccess(True) user_id = 0 table_id = args["id"] db_id = args["db_id"] # 获取表信息 table_info = self.getTableInfoById(table_id).getMessage() table_e_name = table_info[0]["e_name"] table_c_name = table_info[0]["c_name"] # 获取原库的表信息 table_info_src = self.getTableComment(db_id, table_e_name).getMessage() table_c_name_src = table_info_src[0]["TABLE_COMMENT"] flag_table = False table_content = [] # 对比表信息 if table_c_name != table_c_name_src: flag_table = True content = "表{}的【中文名】从 [{}] 变成 [{}]".format(table_e_name, table_c_name, table_c_name_src) table_content.append(content) if flag_table: self.editTableCNameById(table_id, table_c_name_src) self.addDBLog(db_id, table_content, user_id, 1) # 对比字段信息 c_args = {} c_args.setdefault("e_name", table_e_name) c_args.setdefault("db_id", db_id) # 获取表字段信息 column_info = self.getColumnListByTableName(c_args).getMessage() # 获取原库的表字段信息 db_src = self.getDBSrcInfo(db_id) cs_args = {} cs_args.setdefault("schema_name", db_src["db"]) cs_args.setdefault("table_name", table_e_name) column_info_src = self.DatabaseDaoInterface.getSynchronizeTable( cs_args, **db_src).getMessage() column_info = [{ "name": i["e_name"], "type": i["type"], "remark": i["remark"], "id": i["id"], "pre_name": i["e_name"][2:-2] if i["e_name"].startswith("~~") and i["e_name"].endswith("~~") else i["e_name"] } for i in column_info] column_info_src = [{ "name": i["COLUMN_NAME"], "type": i["COLUMN_TYPE"], "remark": i["COLUMN_COMMENT"], "pre_name": i["COLUMN_NAME"][2:-2] if i["COLUMN_NAME"].startswith("~~") and i["COLUMN_NAME"].endswith("~~") else i["COLUMN_NAME"] } for i in column_info_src] column_info = sorted(column_info, key=lambda x: x["pre_name"]) column_info_src = sorted(column_info_src, key=lambda x: x["pre_name"]) flag_column = False column_content = [] update_column_type = [] update_column_remark = [] update_column_ename = [] if len(column_info) == len(column_info_src): flag_column = True else: result.setSuccess(False) result.setMessage("当前表的字段数和源库中不一致,请先同步表信息") if flag_column: for x, y in zip(column_info, column_info_src): # one_flag = False # if x["name"] == y["name"]: # one_flag = True if x["pre_name"] == y["pre_name"]: if x["type"] != y["type"]: args_type = {} args_type.setdefault("id", x["id"]) args_type.setdefault("val", y["type"]) update_column_type.append(args_type) content = "字段{}.{}的【类型】从 [{}] 变成 [{}]".format( table_e_name, x["name"], x["type"], y["type"]) column_content.append(content) if x["remark"] != y["remark"]: args_remark = {} args_remark.setdefault("id", x["id"]) args_remark.setdefault("val", y["remark"]) update_column_remark.append(args_remark) content = "字段{}.{}的【备注】从 [{}] 变成 [{}]".format( table_e_name, x["name"], x["remark"], y["remark"]) column_content.append(content) # 处理废弃字段的字段变更 if x["name"] != y["name"]: args_ename = {} args_ename.setdefault("id", x["id"]) args_ename.setdefault("val", y["name"]) update_column_ename.append(args_ename) content = "字段{}.{}的【英文名】从 [{}] 变成 [{}]".format( table_e_name, x["name"], x["name"], y["name"]) column_content.append(content) if flag_column: if update_column_type != []: self.editColumnTypeById(update_column_type, True) if update_column_remark != []: self.editColumnRemarkById(update_column_remark, True) if update_column_ename != []: self.editColumnEnameById(update_column_ename, True) if len(column_content) > 0: self.addDBLog(db_id, column_content, user_id, 1) return result
def auth_logout(self): data = json.loads(self.request.body) self.clear_cookie(data["userName"]) return DataResult()
def __init__(self): self.DatabaseDaoInterface = DatabaseDaoInterface() self.result = DataResult() self.TableService = TableService()
class DatabaseService(object): def __init__(self): self.DatabaseDaoInterface = DatabaseDaoInterface() self.result = DataResult() self.TableService = TableService() @AdminDecoratorServer.execImplDecorator() def addDatabase(self, args): result = self.DatabaseDaoInterface.addDatabase(args) # 添加默认表分组 DBId = result.getMessage() groupDict = {} groupDict["DBId"] = DBId groupDict["name"] = "未分组" groupDict["isDefault"] = 1 self.addTableGroup(groupDict) return result @AdminDecoratorServer.execImplDecorator() def deleteDatabase(self, args): # todo 删除关联关系 # self.deleteTableGroup() # self.deleteTableGroupRelation() # self.TableService.deleteTable() # self.TableService.deleteColumn() return self.DatabaseDaoInterface.deleteDatabase(args) @AdminDecoratorServer.execImplDecorator() def getDatabaseInfoById(self, databaseId): args = {} args.setdefault("id", databaseId) return self.DatabaseDaoInterface.getDatabaseInfoById(args) @AdminDecoratorServer.execImplDecorator() def getDatabaseList(self): # todo businessUnit=2 args = {} args.setdefault("businessUnit", 2) return self.DatabaseDaoInterface.getDatabaseList(args) @AdminDecoratorServer.execImplDecorator() def editDatabase(self, args): return self.DatabaseDaoInterface.editDatabase(args) @AdminDecoratorServer.execImplDecorator() def addTableGroup(self, args): return self.DatabaseDaoInterface.addTableGroup(args) @AdminDecoratorServer.execImplDecorator() def deleteTableGroup(self, args): if args["isDefault"] == 1: self.result.setMessage("默认分组不能删除") return self.result else: #todo 创建事务 groupId = args["id"] DBId = args["DBId"] name = "未分组" defaultId = (self.getTableGroupInfoByName( DBId, name).getMessage())[0]["id"] self.updateTableGroupRelationByGroupId(groupId, defaultId) return self.DatabaseDaoInterface.deleteTableGroup(args) @AdminDecoratorServer.execImplDecorator() def getTableGroupInfoById(self, groupId): args = {} args.setdefault("id", groupId) return self.DatabaseDaoInterface.getTableGroupInfoById(args) @AdminDecoratorServer.execImplDecorator() def getTableGroupInfoByName(self, DBId, name): args = {} args.setdefault("DBId", DBId) args.setdefault("name", name) return self.DatabaseDaoInterface.getTableGroupInfoByName(args) @AdminDecoratorServer.execImplDecorator() def getTableGroupList(self, DBId): args = {} args.setdefault("DBId", DBId) return self.DatabaseDaoInterface.getTableGroupList(args) @AdminDecoratorServer.execImplDecorator() def editTableGroup(self, args): return self.DatabaseDaoInterface.editTableGroup(args) @AdminDecoratorServer.execImplDecorator() def addTableGroupRelation(self, args): return self.DatabaseDaoInterface.addTableGroupRelation(args) @AdminDecoratorServer.execImplDecorator() def deleteTableGroupRelation(self, relationId): args = {} args.setdefault("id", relationId) return self.DatabaseDaoInterface.deleteTableGroupRelation(args) @AdminDecoratorServer.execImplDecorator() def getTableGroupRelationList(self, DBId): from itertools import groupby args = {} args.setdefault("DBId", DBId) self.result = self.DatabaseDaoInterface.getTableGroupRelationList(args) dbName = (self.getDatabaseInfoById(DBId).getMessage())[0]["name"] # 先排序再分组,不然会被隔开 handleRet = sorted(self.result.getMessage(), key=lambda x: x["groupId"]) handleRet = groupby(handleRet, key=lambda x: x["groupId"]) endRet = [] index = 0 for key, group in handleRet: groupDict = {} # 先转为list group = list(group) groupDict.setdefault("title", group[0]["name"]) logger.info(group) groupDict.setdefault("children", [{ "title": l["eName"], "id": l["id"], "tableId": l["tableId"] } for l in group]) # 用于设置默认展开 if index == 0: groupDict.setdefault("expand", True) itmeDict = groupDict["children"][0] itmeDict.setdefault("selected", True) logger.info(itmeDict) index += 1 endRet.append(groupDict) message = {} message.setdefault("dbName", dbName) message.setdefault("groupInfo", endRet) self.result.setMessage(message) return self.result @AdminDecoratorServer.execImplDecorator() def updateTableGroupRelation(self, infos): args = {} ids = ",".join(infos["tables"]) args.setdefault("DBId", int(infos["DBId"])) args.setdefault("groupId", infos["groupId"]) args.setdefault("ids", ids) logger.info(args) return self.DatabaseDaoInterface.updateTableGroupRelation(args) @AdminDecoratorServer.execImplDecorator() def updateTableGroupRelationByGroupId(self, groupId, defaultId): args = {} args.setdefault("groupId", groupId) args.setdefault("defaultId", defaultId) logger.info(args) return self.DatabaseDaoInterface.updateTableGroupRelationByGroupId( args)
else: status = "Failed" #caseResult caseResultInfo = {} caseResultInfo.setdefault("instanceid", instanceId) caseResultInfo.setdefault("caseid", caseId) caseResultInfo.setdefault("casename", caseName) caseResultInfo.setdefault("runtime", exec_end_flag - exec_start_flag) caseResultInfo.setdefault("exec_start", exec_start) caseResultInfo.setdefault("exec_end", exec_end) caseResultInfo.setdefault("status", status) caseResultInfo.setdefault("messaga", response) caseResultInfo.setdefault("remarks", None) CaseResultDaoInterface().addCaseResult(caseResultInfo) dataResult = DataResult() dataResult.setSuccess(statusFlag) if init: dataResult.setMessage(obj) else: dataResult.setMessage(status) return dataResult @staticmethod @AdminDecoratorServer.execImplDecorator() def __execAssertJob(actual, expect, assert_type="0"): #equal if assert_type == "0": result = AssertInstance.get_instance().isEqual(actual, expect) #not equal elif assert_type == "1":
def synchronizeDatabase(self, args): # 后续同步,比较表差和字段差 dao_db = self.DatabaseDaoInterface db_id = args["id"] db_info = dao_db.getDatabaseAllInfoById({"id": db_id}).getMessage() db_src = { 'host': db_info[0]["host"], 'user': db_info[0]["username"], 'passwd': db_info[0]["password"], 'db': db_info[0]["schema_name"], 'port': db_info[0]["port"] } db_schema = db_info[0]["schema_name"] args = {} args.setdefault("schema_name", db_schema) tables_src = dao_db.getSynchronizeDatabase(args, **db_src).getMessage() tables_src_list = [i["TABLE_NAME"] for i in tables_src] tables_dest = self.getTableList(db_id).getMessage() tables_dest_list = [i["e_name"] for i in tables_dest] # 获取源数据库和目标数据库的差集,用于增加整表 tables_diff_add = set(tables_src_list) - set(tables_dest_list) tables_diff_list = list(tables_diff_add) tables_diff_dict = [ i for i in tables_src if tables_diff_list.count(i["TABLE_NAME"]) > 0 ] # 获取需要比较表字段的数据表,排除上面差集的数据表 tables_compare = set(tables_src_list) - tables_diff_add # 增加表 logTableList = [] for i in tables_diff_dict: db_table = i["TABLE_NAME"] tableDict = {} tableDict["db_id"] = db_id tableDict["c_name"] = i["TABLE_COMMENT"] tableDict["e_name"] = db_table tableDict["remark"] = "" tableDict["discarded"] = 0 logTableList.append(db_table) result_table = dao_db.addTable(tableDict).getSuccess() insertColumnList = [] args = {} args["db_id"] = db_id args["name"] = "未分组" groupinfo = dao_db.getTableGroupInfoByName(args) group_id = ( dao_db.getTableGroupInfoByName(args).getMessage())[0]["id"] args = {} args.setdefault("db_id", db_id) args.setdefault("e_name", db_table) tables = dao_db.getTableInfoByName(args).getMessage() table_id = tables[0]["id"] # 添加默认表关系 relationDict = {} relationDict["table_id"] = table_id relationDict["group_id"] = group_id dao_db.addTableGroupRelation(relationDict) if result_table: args = {} args.setdefault("db_id", db_id) args.setdefault("e_name", db_table) tables = dao_db.getTableInfoByName(args).getMessage() table_id = tables[0]["id"] db_table = tables[0]["e_name"] # 获取源数据库表字段 args = {} args.setdefault("schema_name", db_schema) args.setdefault("table_name", db_table) columns = dao_db.getSynchronizeTable(args, **db_src).getMessage() for j in columns: columnDict = {} columnDict["table_id"] = table_id e_name = j["COLUMN_NAME"] columnDict["e_name"] = e_name columnDict["type"] = j["COLUMN_TYPE"] remark = j["COLUMN_COMMENT"] columnDict["remark"] = remark if e_name[0:2] == "~~": columnDict["discarded"] = 1 else: columnDict["discarded"] = 0 insertColumnList.append(columnDict) dao_db.addColumn(insertColumnList, True) # 删除表 tables_diff_minus = set(tables_dest_list) - set(tables_src_list) table_del = [] log_del_table_list = [] for l in tables_diff_minus: table_id = [i["id"] for i in tables_dest if i["e_name"] == l] table_del.extend(table_id) log_del_table_list.append(l) if table_del != []: self.deleteColumnByTableId(table_del) self.deleteTableById(table_del) # 比较表,根据缺少的字段,增加字段 logColumnList = [] for i in tables_compare: args = {} args.setdefault("db_id", db_id) args.setdefault("e_name", i) table_info = dao_db.getTableInfoByName(args).getMessage() table_id = table_info[0]["id"] db_table = table_info[0]["e_name"] args = {} args.setdefault("schema_name", db_schema) args.setdefault("table_name", db_table) columns_src = dao_db.getSynchronizeTable(args, **db_src).getMessage() columns_src_list = [i["COLUMN_NAME"] for i in columns_src] columns_dest = self.getColumnListByTableId(table_id).getMessage() columns_dest_list = [i["e_name"] for i in columns_dest] columns_diff_add = set(columns_src_list) - set(columns_dest_list) # 获取废弃字段列表 column_discarded = [ i[2:-2] for i in columns_src_list if i.startswith("~~") and i.endswith("~~") ] for j in columns_diff_add: args = {} args.setdefault("schema_name", db_schema) args.setdefault("table_name", db_table) args.setdefault("columnName", j) column_info = dao_db.getSynchronizeColumn( args, **db_src).getMessage() columnDict = {} columnDict["table_id"] = table_id e_name = column_info[0]["COLUMN_NAME"] columnDict["e_name"] = e_name # 处理~~xxx~~的字段 pre_name = e_name[2:-2] if e_name.startswith( "~~") and e_name.endswith("~~") else e_name columnDict["type"] = column_info[0]["COLUMN_TYPE"] remark = column_info[0]["COLUMN_COMMENT"] columnDict["remark"] = remark if e_name[0:2] == "~~": columnDict["discarded"] = 1 else: columnDict["discarded"] = 0 pre_col_info = self.getColumnIdByTwoName(pre_name, i, db_id).getMessage() if pre_col_info == []: logColumnName = "{}.{}".format(db_table, columnDict["e_name"]) logColumnList.append(logColumnName) dao_db.addColumn(columnDict) # 删除字段 columns_diff_minus = set(columns_dest_list) - set(columns_src_list) column_del = [] log_del_col_list = [] for k in columns_diff_minus: if k not in column_discarded: col_id = [ i["id"] for i in columns_dest if i["e_name"] == k ] column_del.extend(col_id) log_del_col_list.append("{}.{}".format(i, k)) if column_del != []: self.deleteColumnById(column_del) dao_db.getDatabaseAllInfoById({"id": db_id}) # 增加log信息 if log_del_table_list != [] or log_del_col_list != []: self.addDBLog(db_id, log_del_table_list + log_del_col_list, type=2) if logTableList != [] or logColumnList != []: self.addDBLog(db_id, logTableList + logColumnList) d = DataResult() d.setSuccess(True) return d
def syncCreateTestCase(self, args): #这里需要用到数据库事务,保证数据一致性和幂等性 dataResult = DataResult() errMessageList = [] db = Connection(autocommit=False) try: logger.info(args) # 获取指定应用某个项目中的接口列表 sql = """ select Path from webapi where ApplicationId = %(applicationId)s and ProjectId=%(projectId)s """ paths = list(db.read(sql, args)) logger.info("paths={0}".format(paths)) if len(paths) == 0: dataResult.setSuccess(True) dataResult.setMessage("该版本中还没有接口") return dataResult # 遍历webapilist,自动创建用例 for apiPath in paths: errMessage = {} # 查看该版本的接口是否存在该用例用例 用例已经存在时,记录一条日志,并继续,若用例不存在,则创建用例 searchApiInfoData = {} searchApiInfoData.setdefault("projectId", args["projectId"]) searchApiInfoData.setdefault("applicationId", args["applicationId"]) searchApiInfoData.setdefault("Path", apiPath["Path"]) # 查询指定path的用例 sql = """ select test_case.id from test_case left join case_content on test_case.id=case_content.case_id where test_case.project_id = %(projectId)s and test_case.application_id =%(applicationId)s and case_content.webapi_path=%(Path)s """ caseInfos = list(db.read(sql, searchApiInfoData)) if len(caseInfos) > 0: logger.info("用例[{0}]已存在,用例ID:{1}".format( apiPath["Path"], caseInfos[0]["id"])) continue else: # 构建查询接口参数的数据,参数详情 sql = """ select * from webapi left join webapi_parameter on webapi.Id= webapi_parameter.webApiId where webapi.ApplicationId = %(applicationId)s and webapi.ProjectId=%(projectId)s and webapi.Path=%(Path)s """ apiContents = list(db.read(sql, searchApiInfoData)) # 创建test_case testCaseData = {} testCaseData.setdefault("projectId", args["projectId"]) testCaseData.setdefault("applicationId", args["applicationId"]) testCaseData.setdefault("name", "autoTest" + apiPath["Path"]) testCaseData.setdefault("desc", "为" + apiPath["Path"] + "自动生成冒烟用例") sql = """ insert into test_case (name,case_describe,case_status,project_id,application_id,gmt_create) values(%(name)s,%(desc)s,0,%(projectId)s,%(applicationId)s,now()) """ caseId = db.write(sql, testCaseData) # 创建case_content content = {} logger.info("contents={0}".format(content)) content.setdefault("caseId", caseId) content.setdefault("path", apiPath["Path"]) content.setdefault("execute_step", "1") content.setdefault("step_name", "step1") num = 0 params = {} for apiContent in apiContents: if num == 0: if apiContent[ "Produces"] == "application/json" or apiContent[ "Consumes"] == "application/json": content.setdefault("content_type", 0) else: # formdata格式置为1,其他格式暂不处理 content.setdefault("content_type", 1) if apiContent["Method"].lower() == "get": content.setdefault("method", 0) elif apiContent["Method"].lower() == "post": content.setdefault("method", 1) else: errMessage.setdefault("path", apiPath["Path"]) errMessage.setdefault("errMessage", "请求类型只支持get、post") errMessageList.append(errMessage) break #content.setdefault("outPut", apiContent["Schema"]) # 遍历参数,根据参数类型不同,设置入参 注意入参和出参 if apiContent["ParameterType"] == 1: if apiContent["In"] == "query": params.setdefault(apiContent["Name"], "") elif apiContent["In"] == "body": params = apiContent["Schema"] break if isinstance(params, dict): content.setdefault("params", json.dumps(params)) else: content.setdefault("params", params) sql = """ insert into case_content (case_id,webapi_path,method,content_type,requests_params,execute_step,step_name) values (%(caseId)s,%(path)s,%(method)s,%(content_type)s,%(params)s,%(execute_step)s,%(step_name)s) """ contentId = db.write(sql, content) # 创建断言 assertData = {} assertData.setdefault("contentId", contentId) assertData.setdefault("actual", "STATUS") assertData.setdefault("expect", 200) assertData.setdefault("type", 0) sql = """ insert into assert (casecontentid,actual,expect,assert_type) values (%(contentId)s, %(actual)s,%(expect)s,%(type)s) """ db.write(sql, assertData) db.commit() dataResult.setMessage(errMessageList) dataResult.setSuccess(True) return dataResult except Exception as err: logger.error(err) dataResult.setSuccess(False) dataResult.setMessage(errMessageList) return dataResult finally: db.close()
def getViewLinks(self, id, link_type): result = DataResult() if int(link_type) == 1: table_id = id args = {} args.setdefault("table_id", table_id) tb_info = self.getTableInfoById(table_id).getMessage() type_name = tb_info[0]["e_name"] link_info = self.DatabaseDaoInterface.getViewLinks(args) infos = link_info.getMessage() links = [] for i in infos: link = {} link.setdefault("source", i["src_tb_name"]) link.setdefault("target", i["link_tb_name"]) link.setdefault( "value", "{} > {}".format(i["src_col_name"], i["link_col_name"])) links.append(link) datus = [] table_info = self.DatabaseDaoInterface.getViewTableInfo(args) table_group = itertools.groupby(table_info.getMessage(), key=lambda x: x["name"]) for key, group in table_group: data = {} group = list(group) db = group[0]["db_name"] link_type = group[0]["link_type"] data.setdefault("t_name", key) data.setdefault("db_name", db) data.setdefault("link_type", link_type) cols = [g['e_name'] for g in group] data.setdefault('info', cols) datus.append(data) elif int(link_type) == 2: group_id = id group_info = self.getTableGroupInfoById(group_id).getMessage() type_name = group_info[0]["name"] table_list = [] tables = self.getViewTableByGroup(group_id).getMessage() tables = [t["table_id"] for t in tables] table_list.extend(tables) datus = [] level = 0 while tables != []: table_info = self.getViewTableInfoByGroup(tables, level).getMessage() table_group = itertools.groupby(table_info, key=lambda x: x["name"]) for key, group in table_group: exist = [t["t_name"] for t in datus] data = {} group = list(group) db = group[0]["db_name"] link_type = group[0]["link_type"] data.setdefault("t_name", key) data.setdefault("db_name", db) data.setdefault("link_type", link_type) cols = [g['e_name'] for g in group] data.setdefault('info', cols) if key not in exist: datus.append(data) tables = self.getLinkTable(tables) tables = [t["link_table_id"] for t in tables] table_list.extend(tables) level += 1 link_info = self.getViewLinksByGroup(set(table_list)) infos = link_info.getMessage() links = [] for i in infos: link = {} link.setdefault("source", i["src_tb_name"]) link.setdefault("target", i["link_tb_name"]) link.setdefault( "value", "{} > {}".format(i["src_col_name"], i["link_col_name"])) links.append(link) message = {} message.setdefault("links", links) message.setdefault("data", datus) message.setdefault("type_name", type_name) result.setMessage(message) result.setSuccess(True) return result
def addTestCase(self, args, userId): logger.info("args={0}".format(args)) dataResult = DataResult() #这里需要事务保证一致性 if "status" not in args: args.setdefault("status", None) if "groupId" not in args: args.setdefault("groupId", None) args.setdefault("userId", userId) caseArgs = copy.deepcopy(args) projectId = int(caseArgs["projectId"]) caseArgs.pop("projectId") caseArgs.setdefault("projectId", projectId) applicationId = int(caseArgs["applicationId"]) caseArgs.pop("applicationId") caseArgs.setdefault("applicationId", applicationId) caseArgs.pop("itemsSteps") data_1 = self.testCaseDaoInterface.addTestCase(caseArgs) if data_1.getSuccess(): caseId = data_1.getMessage() logger.info(caseId) else: logger.info(data_1.getMessage()) data_1.setMessage("添加test_case失败") return data_1 for stepItem in args["itemsSteps"]: if stepItem["statusStep"] == 0: continue stepJson = {} stepJson.setdefault("step_name", stepItem["value"]) stepJson.setdefault("caseId", caseId) stepJson.setdefault("execute_step", stepItem["indexStep"]) stepJson.setdefault("host", stepItem["host"]) stepJson.setdefault("path", stepItem["path"]) stepJson.setdefault("method", stepItem["method"]) stepJson.setdefault("content_type", stepItem["content_type"]) stepJson.setdefault("headers", stepItem["header"]) if stepItem["params"] == "": stepJson.setdefault("params", None) else: stepJson.setdefault("params", stepItem["params"]) data_2 = self.testCaseDaoInterface.addTestCaseContent(stepJson) if data_2.getSuccess(): contentId = data_2.getMessage() logger.error(contentId) else: logger.info(data_2.getMessage()) data_2.setMessage("添加case_content失败") return data_2 assertDatas = [] for assertItem in stepItem["itemsAsserts"]: if assertItem["statusAssert"] == 0: continue assertJSON = {} assertJSON.setdefault("contentId", contentId) assertJSON.setdefault("actual", assertItem["actual"]) assertJSON.setdefault("expect", assertItem["expect"]) assertJSON.setdefault("type", assertItem["rules"]) assertDatas.append(assertJSON) logger.info(assertDatas) data_3 = self.testCaseDaoInterface.addTestCaseAssert( assertJSON) logger.info(data_3.getMessage()) if data_3.getSuccess(): dataResult.setSuccess(True) dataResult.setMessage(caseId) else: logger.info(data_3.getMessage()) dataResult.setSuccess(False) dataResult.setMessage("添加用例失败") return dataResult