def key_case_delete(): # 初始化返回内容 response_json = {"code": 200, "msg": "数据删除成功", "data": None} # 取出必传入参 request_user_id = flask.request.headers['UserId'] case_id = flask.request.args['id'] # 查用例是否存在,构造基础信息 try: mysql_case_info = model_mysql_case.query.filter( model_mysql_case.id == case_id, model_mysql_case.type == 2).first() except Exception as e: api_logger.error("测试计划类型读取失败,失败原因:" + repr(e)) return route.error_msgs[500]['msg_db_error'] else: if mysql_case_info is None: return route.error_msgs[201]['msg_no_case'] else: indexchang(mysql_case_info.index, mysql_case_info.columnId) mysql_case_info.status = -1 mysql_case_info.updateUserId = request_user_id mysql_case_info.index = 0 mysqlpool.session.commit() # 添加日志 case_logs = model_mysql_caseEditLog(caseId=mysql_case_info.id, type=7) mysqlpool.session.add(case_logs) mysqlpool.session.commit() return response_json
def key_case_delete(): # 初始化返回内容 response_json = {"code": 200, "msg": "数据删除成功", "data": None} # 取出必传入参 request_user_id = flask.request.headers['UserId'] case_id = int(flask.request.args['id']) project_id = int(flask.request.args['projectId']) # 获取用例的全部信息 try: mysql_case_info = model_mysql_case.query.filter( model_mysql_case.id == case_id, model_mysql_case.type == 2).first() except Exception as e: api_logger.error("用例数据读取失败,失败原因:" + repr(e)) return route.error_msgs[500]['msg_db_error'] else: if mysql_case_info is None: return route.error_msgs[201]['msg_no_case'] # 待删除用例无法再次删除 if mysql_case_info.status == 3 or mysql_case_info.veri == 1: return route.error_msgs[201]['msg_case_already_to_be_deleted'] # 判断是否是仓库的用例 # 若是本项目的测试用例 if mysql_case_info.projectId == project_id: # 若为项目新增用例 if mysql_case_info.originalCaseId == 0: # 需要判断用例是否通过评审,已通过评审的用例要重新评审才可以删除 if mysql_case_info.veri != 3: # 当前项目 && originalCaseId == 0 && veri in (0,1,2) # 删除用例 mysql_case_info.status = -1 mysql_case_info.updateUserId = request_user_id mysql_case_info.updateTime = str( datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")) # 添加日志 mysqlpool.session.add( model_mysql_caseEditLog(caseId=mysql_case_info.id, type=7)) try: mysqlpool.session.commit() api_logger.error("用例删除成功") except Exception as e: api_logger.error("用例删除失败,失败原因:" + repr(e)) return route.error_msgs[500]['msg_db_error'] else: mysql_case_info.status = 3 mysql_case_info.updateUserId = request_user_id mysql_case_info.updateTime = str( datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")) mysql_case_info.veri = 0 # 添加日志 mysqlpool.session.add( model_mysql_caseEditLog(caseId=mysql_case_info.id, type=7)) try: mysqlpool.session.commit() api_logger.error("用例删除成功") except Exception as e: api_logger.error("用例删除失败,失败原因:" + repr(e)) return route.error_msgs[500]['msg_db_error'] # 若非项目新增用例 else: mysql_case_info.status = 3 mysql_case_info.updateUserId = request_user_id mysql_case_info.updateTime = str( datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")) mysql_case_info.veri = 0 # 添加日志 mysqlpool.session.add( model_mysql_caseEditLog(caseId=mysql_case_info.id, type=7)) try: mysqlpool.session.commit() api_logger.error("用例删除成功") except Exception as e: api_logger.error("用例删除失败,失败原因:" + repr(e)) return route.error_msgs[500]['msg_db_error'] # 若非本项目的测试用例 else: # 后台新增一条用例caseId自增、且内容与被删除用例数据一致,其originalCaseId为被删除的用例的caseId # 复制case mysql_new_case = model_mysql_case( title=mysql_case_info.title, depositoryId=mysql_case_info.depositoryId, projectId=mysql_case_info.projectId, columnId=mysql_case_info.columnId, index=mysql_case_info.index, columnLevel=mysql_case_info.columnLevel, level=mysql_case_info.level, type=mysql_case_info.type, status=3, userId=mysql_case_info.userId, createTime=mysql_case_info.createTime, updateUserId=mysql_case_info.updateUserId, updateTime=mysql_case_info.updateTime, veri=0, arch=mysql_case_info.arch, originalCaseId=mysql_case_info.id, ) mysqlpool.session.add(mysql_new_case) try: mysqlpool.session.commit() except Exception as e: api_logger.error("用例复制失败,失败原因:" + repr(e)) return route.error_msgs[500]['msg_db_error'] # 复制precondition try: mysql_case_precondition_info = model_mysql_casePrecondition.query.filter( model_mysql_casePrecondition.caseId == case_id).first() except Exception as e: api_logger.error("用例期望结果读取失败,失败原因:" + repr(e)) return route.error_msgs[500]['msg_db_error'] else: if mysql_case_precondition_info is not None: mysqlpool.session.add( model_mysql_casePrecondition( content=mysql_case_precondition_info.content, caseId=mysql_new_case.id, createTime=mysql_case_precondition_info.createTime, updateTime=mysql_case_precondition_info.updateTime, )) # 复制steps try: mysql_case_steps_info = model_mysql_caseStep.query.filter( model_mysql_caseStep.caseId == case_id).order_by( model_mysql_caseStep.index).all() except Exception as e: api_logger.error("用例步骤数据读取失败,失败原因:" + repr(e)) return route.error_msgs[500]['msg_db_error'] else: for mcsi in mysql_case_steps_info: mysqlpool.session.add( model_mysql_caseStep( caseId=mysql_new_case.id, index=mcsi.index, content=mcsi.content, expectation=mcsi.expectation, status=mcsi.status, userId=mcsi.userId, createTime=mcsi.createTime, updateUserId=mcsi.updateUserId, updateTime=mcsi.updateTime, )) # 复制files try: mysql_case_files_info = model_mysql_caseFile.query.filter( model_mysql_caseFile.caseId == case_id).all() except Exception as e: api_logger.error("用例附件数据读取失败,失败原因:" + repr(e)) return route.error_msgs[500]['msg_db_error'] else: for mcfi in mysql_case_files_info: mysqlpool.session.add( model_mysql_caseFile( caseId=mysql_new_case.id, ossPath=mcfi.ossPath, fileAlias=mcfi.fileAlias, status=mcfi.status, userId=mcfi.userId, createTime=mcfi.createTime, )) # 复制logs try: mysql_case_logs_info = model_mysql_caseEditLog.query.filter( model_mysql_caseEditLog.caseId == case_id).all() except Exception as e: api_logger.error("用例编辑日志数据读取失败,失败原因:" + repr(e)) return route.error_msgs[500]['msg_db_error'] else: for mcli in mysql_case_logs_info: mysqlpool.session.add( model_mysql_caseEditLog( caseId=mysql_new_case.id, type=mcli.type, before=mcli.before, after=mcli.after, createTime=mcli.createTime, )) # 添加日志 mysqlpool.session.add( model_mysql_caseEditLog(caseId=mysql_new_case.id, type=1)) try: mysqlpool.session.commit() api_logger.error("用例克隆并预删除成功") except Exception as e: api_logger.error("用例克隆并预删除失败,失败原因:" + repr(e)) return route.error_msgs[500]['msg_db_error'] return response_json
def key_case_put(): index = 0 # 初始化返回内容 response_json = { "code": 200, "msg": "数据修改成功", "data": None } # 取出必传入参 request_user_id = flask.request.headers['UserId'] case_id = flask.request.json['caseId'] case_columnId = flask.request.json['columnId'] case_title = flask.request.json['title'] case_level = flask.request.json['level'] front_case = flask.request.json['frontCaseId'] case_precondition = flask.request.json['casePrecondition'] case_step = flask.request.json['caseStep'] files = flask.request.json['files'] project_id = flask.request.json['projectId'] # 查用例是否存在 try: mysql_caseinfo = model_mysql_case.query.filter( model_mysql_case.id == case_id, model_mysql_case.type == 2, model_mysql_case.status == 1,model_mysql_case.veri!=1 ).first() except Exception as e: api_logger.error("测试计划类型读取失败,失败原因:" + repr(e)) return route.error_msgs[500]['msg_db_error'] else: if mysql_caseinfo is None: return route.error_msgs[201]['msg_no_case'] else: before_title = mysql_caseinfo.title # 查目录是否存在,更新目录 try: mysql_column = model_mysql_case.query.filter( model_mysql_case.id == case_columnId, model_mysql_case.type == 1, model_mysql_case.status == 1 ).first() except Exception as e: api_logger.error("测试计划类型读取失败,失败原因:" + repr(e)) return route.error_msgs[500]['msg_db_error'] else: if mysql_column is None: return route.error_msgs[201]['msg_no_catalogue'] # 判断必输项title、front_case和level必传 if len(case_title) == 0: return route.error_msgs[201]['msg_data_error'] elif case_level is None: return route.error_msgs[201]['msg_data_error'] elif front_case is None: return route.error_msgs[201]['msg_data_error'] # 判断是否需要拷贝一个新的case # 相同不需要拷贝 if int(mysql_caseinfo.projectId) == int(project_id): # 更新用例主数据 if front_case == 0: index = 1 # 判断目录是否改变 if mysql_caseinfo.columnId == case_columnId: indexchang2(mysql_caseinfo.index, case_columnId) mysql_caseinfo.index = 0 mysqlpool.session.commit() indexchang(index, case_columnId) else: indexchang(index, case_columnId) mysql_caseinfo.index = 1 mysql_caseinfo.title = case_title mysql_caseinfo.level = case_level mysql_caseinfo.columnId = case_columnId mysql_caseinfo.veri = 0 mysql_caseinfo.status = 1 mysqlpool.session.commit() else: try: mysql_front = model_mysql_case.query.filter( model_mysql_case.id == front_case, model_mysql_case.type == 2, model_mysql_case.status == 1 ).first() except Exception as e: api_logger.error("测试计划类型读取失败,失败原因:" + repr(e)) return route.error_msgs[500]['msg_db_error'] else: if mysql_front is None: return route.error_msgs[201]['msg_no_case'] else: # *************需要解决往下拖的问题,往上托目前没问题 index = mysql_front.index + 1 if mysql_caseinfo.columnId == case_columnId: indexchang2(mysql_caseinfo.index, case_columnId) mysql_caseinfo.index = 0 mysqlpool.session.commit() # 重新获取前一个case的排序 try: mysql_front = model_mysql_case.query.filter( model_mysql_case.id == front_case, model_mysql_case.type == 2, model_mysql_case.status == 1 ).first() except Exception as e: api_logger.error("测试计划类型读取失败,失败原因:" + repr(e)) return route.error_msgs[500]['msg_db_error'] index = mysql_front.index + 1 indexchang(index, case_columnId) else: indexchang(index, case_columnId) mysql_caseinfo.index = index mysql_caseinfo.title = case_title mysql_caseinfo.level = case_level mysql_caseinfo.columnId = case_columnId mysql_caseinfo.veri = 0 mysql_caseinfo.status = 1 mysqlpool.session.commit() precondition(mysql_caseinfo.id, case_precondition) ossPath(mysql_caseinfo.id, request_user_id, files) casestep(mysql_caseinfo.id, case_step, request_user_id) # 添加日志 if before_title == case_title: pass else: case_logs = model_mysql_caseEditLog( caseId=mysql_caseinfo.id, type=2, before=before_title, after=case_title ) mysqlpool.session.add(case_logs) mysqlpool.session.commit() else: # 获取原用例信息 case_title = case_title case_depositoryId = mysql_caseinfo.depositoryId case_projectId = project_id case_columnId = case_columnId case_index = mysql_caseinfo.index case_level = case_level case_type = 2 case_userId = request_user_id case_status = 1 case_veri = 0 case_arch = 0 # 获取原用例的前置条件 case_precondition = case_precondition # # 查询是否存在附件 oss_path = files # 查询是否存在测试步骤 case_step = case_step # 备份新的用例 new_case_info = model_mysql_case( title=case_title, columnId=case_columnId, projectId=case_projectId, index=case_index, level=case_level, type=case_type, status=case_status, veri=case_veri, arch=case_arch, userId=case_userId, depositoryId=case_depositoryId, originalCaseId=case_id ) mysqlpool.session.add(new_case_info) mysqlpool.session.commit() # 获得主用例编号 try: mysql_id = model_mysql_case.query.filter( model_mysql_case.projectId == case_projectId, model_mysql_case.type == 2, model_mysql_case.status == 1, model_mysql_case.columnId == case_columnId, model_mysql_case.originalCaseId == case_id ).first() except Exception as e: api_logger.error("测试计划类型读取失败,失败原因:" + repr(e)) return route.error_msgs[500]['msg_db_error'] else: if mysql_id is None: return route.error_msgs[201]['msg_no_case'] if case_precondition is None: pass else: new_caseprecondition_info = model_mysql_casePrecondition( content=case_precondition, caseId=mysql_id.id ) mysqlpool.session.add(new_caseprecondition_info) if len(oss_path) == 0: pass else: for x in oss_path: new_casefile_info = model_mysql_caseFile( ossPath=x['ossPath'], caseId=mysql_id.id, status=1, userId=case_userId, fileAlias=x['fileAlias'] ) mysqlpool.session.add(new_casefile_info) if len(case_step) == 0: pass else: for x in case_step: new_casestep_info = model_mysql_caseStep( index=x['index'], caseId=mysql_id.id, content=x['content'], expectation=x['expectation'], status=1, userId=case_userId, updateUserId=case_userId ) mysqlpool.session.add(new_casestep_info) mysqlpool.session.commit() # 添加日志 case_logs = model_mysql_caseEditLog( caseId=mysql_id.id, type=1 ) mysqlpool.session.add(case_logs) mysqlpool.session.commit() # 最后返回内容 return response_json
def casestep(caseid, case_step, userID): lists = [] # 插入测试步骤 # 这里一定要注意传入数组的参数中千万不能有空格,否则会死都查不出为啥会错,python的json方法无法解析,postman居然可以识别。 try: case_caseStep = model_mysql_caseStep.query.filter( model_mysql_caseStep.caseId == caseid, model_mysql_caseStep.status == 1 ).all() except Exception as e: api_logger.error("测试计划类型读取失败,失败原因:" + repr(e)) return route.error_msgs[500]['msg_db_error'] else: # 原来为空 if len(case_caseStep) == 0: # 现在也为空 if len(case_step) == 0: pass # 现在不为空 else: try: for x in case_step: new_caseStep_info = model_mysql_caseStep( index=x['index'], caseId=caseid, content=x['content'], expectation=x['expectation'], status=1, userId=userID ) mysqlpool.session.add(new_caseStep_info) mysqlpool.session.commit() except Exception as e: api_logger.error("测试数据读取失败,失败原因:" + repr(e)) return route.error_msgs[500]['msg_db_error'] # 原来数据不为空 else: # 现在数据为空 if len(case_step) == 0: for x in case_caseStep: x.status = -1 x.updateUserId = userID mysqlpool.session.commit() # 现在不为空 else: # 修改用例需要穿step-id try: for x in case_step: lists.append(x['id']) if (x['id']) == 0: new_caseStep_info = model_mysql_caseStep( index=x['index'], caseId=caseid, content=x['content'], expectation=x['expectation'], status=1, userId=userID ) mysqlpool.session.add(new_caseStep_info) mysqlpool.session.commit() else: try: case_caseStep_info = model_mysql_caseStep.query.filter( model_mysql_caseStep.id == x['id'], model_mysql_caseStep.caseId == caseid ).first() except Exception as e: api_logger.error("测试计划类型读取失败,失败原因:" + repr(e)) return route.error_msgs[500]['msg_db_error'] else: if case_caseStep_info is None: return route.error_msgs[201]['msg_no_casestep'] else: before_content = case_caseStep_info.content before_expectation = case_caseStep_info.expectation case_caseStep_info.index = x['index'] case_caseStep_info.content = x['content'] case_caseStep_info.expectation = x['expectation'] case_caseStep_info.status = 1 case_caseStep_info.updateUserId = userID mysqlpool.session.commit() # 添加日志 if before_content == x['content']: pass else: case_logs = model_mysql_caseEditLog( caseId=caseid, type=3, before=before_content, after=x['content'] ) mysqlpool.session.add(case_logs) mysqlpool.session.commit() if before_expectation == x['expectation']: pass else: case_logs = model_mysql_caseEditLog( caseId=caseid, type=4, before=before_expectation, after=x['expectation'] ) mysqlpool.session.add(case_logs) mysqlpool.session.commit() except Exception as e: api_logger.error("读取失败,失败原因:" + repr(e)) return route.error_msgs[301]['msg_request_params_illegal'] # 踢出本次删除的步骤 try: for x in case_caseStep: if x.id not in lists: x.status = -1 x.updateUserId = userID mysqlpool.session.commit() else: pass except Exception as e: api_logger.error("测试计划类型读取失败,失败原因:" + repr(e)) return route.error_msgs[500]['msg_db_error']
def ossPath(caseid, userID, files): if len(files) == 0: pass else: # 需要前端返回附件的id和状态,无id的附件当做新增处理 for x in files: if x['id'] == 0: new_caseFile_info = model_mysql_caseFile( ossPath=x['ossPath'], caseId=caseid, status=1, userId=userID, fileAlias=x['fileAlias'] ) mysqlpool.session.add(new_caseFile_info) mysqlpool.session.commit() # 获取id try: case_file = model_mysql_caseFile.query.filter( model_mysql_caseFile.caseId == caseid, model_mysql_caseFile.ossPath == x['ossPath'] ).first() except Exception as e: api_logger.error("读取失败,失败原因:" + repr(e)) return route.error_msgs[500]['msg_db_error'] else: if case_file is None: pass else: # 添加日志 case_logs = model_mysql_caseEditLog( caseId=caseid, type=5, before=case_file.id ) mysqlpool.session.add(case_logs) mysqlpool.session.commit() else: try: case_file_info = model_mysql_caseFile.query.filter( model_mysql_caseFile.id == x['id'] ).first() except Exception as e: api_logger.error("读取失败,失败原因:" + repr(e)) return route.error_msgs[500]['msg_db_error'] else: if case_file_info is None: pass else: case_file_info.status = x['status'] case_file_info.userId = userID mysqlpool.session.commit() if x['status'] == -1: # 添加日志 case_logs = model_mysql_caseEditLog( caseId=caseid, type=6, before=case_file_info.id ) mysqlpool.session.add(case_logs) mysqlpool.session.commit()
def key_case_post(): # 初始化返回内容 response_json = {"code": 200, "msg": "新增用例成功", "data": {}} # 取出必传入参 request_user_id = flask.request.headers['UserId'] request_column_id = flask.request.json['columnId'] request_project_id = flask.request.json['projectId'] request_title = flask.request.json['title'] request_level = flask.request.json['level'] request_precondition = flask.request.json['precondition'] request_steps = flask.request.json['steps'] request_files = flask.request.json['files'] # 查目录是否存在 try: mysql_column = model_mysql_case.query.filter( model_mysql_case.id == request_column_id, model_mysql_case.type == 1, model_mysql_case.status == 1).first() except Exception as e: api_logger.error("项目数据读取失败,失败原因:" + repr(e)) return route.error_msgs[500]['msg_db_error'] else: if mysql_column is None: return route.error_msgs[201]['msg_no_catalogue'] # 插入用例主数据 # 获取目录下用例的最大index try: mysql_cases_max_index = mysqlpool.session.query( func.max(model_mysql_case.index)).filter( model_mysql_case.columnId == mysql_column.id, model_mysql_case.depositoryId == mysql_column.depositoryId, model_mysql_case.status != -1, model_mysql_case.type == 2).first()[0] api_logger.debug("用例信息读取成功") except Exception as e: api_logger.error("用例信息读取失败,失败原因:" + repr(e)) return route.error_msgs[500]['msg_db_error'] new_case_info = model_mysql_case( title=request_title, depositoryId=mysql_column.depositoryId, projectId=request_project_id, columnId=request_column_id, index=mysql_cases_max_index + 1 if mysql_cases_max_index is not None else 1, columnLevel=0, level=request_level, type=2, status=1, userId=request_user_id, veri=0, arch=0, originalCaseId=0) mysqlpool.session.add(new_case_info) try: mysqlpool.session.commit() api_logger.error("用例基础信息新增成功") except Exception as e: api_logger.error("用例基础信息新增失败,失败原因:" + repr(e)) return route.error_msgs[500]['msg_db_error'] # 一次性检查前置条件+步骤+附件 # 前置条件 # 此项可为空字符串 mysqlpool.session.add( model_mysql_casePrecondition(content=request_precondition, caseId=new_case_info.id)) # 步骤 for rs in request_steps: if 'content' not in rs or 'expectation' not in rs: return route.error_msgs[302]['msg_request_params_incomplete'] elif type(rs['content']) is not str or type( rs['expectation']) is not str: return route.error_msgs[301]['msg_request_params_illegal'] elif rs['content'] == '' or rs['expectation'] == '': return route.error_msgs[301]['msg_request_params_illegal'] else: mysqlpool.session.add( model_mysql_caseStep(caseId=new_case_info.id, content=rs['content'], expectation=rs['expectation'], status=1, userId=request_user_id)) # 附件 for rf in request_files: if 'ossPath' not in rf or 'fileAlias' not in rf: return route.error_msgs[302]['msg_request_params_incomplete'] elif type(rf['ossPath']) is not str or type( rf['fileAlias']) is not str: return route.error_msgs[301]['msg_request_params_illegal'] elif rf['ossPath'] == '' or rf['fileAlias'] == '': return route.error_msgs[301]['msg_request_params_illegal'] else: mysqlpool.session.add( model_mysql_caseFile(ossPath=rf['ossPath'], caseId=new_case_info.id, status=1, userId=request_user_id, fileAlias=rf['fileAlias'])) # 添加日志 mysqlpool.session.add( model_mysql_caseEditLog(caseId=new_case_info.id, type=1)) # 入库 try: mysqlpool.session.commit() api_logger.error("用例前置条件信息新增成功") except Exception as e: api_logger.error("用例前置条件信息新增失败,失败原因:" + repr(e)) return route.error_msgs[500]['msg_db_error'] response_json['data'] = { 'id': new_case_info.id, 'title': new_case_info.title, 'depositoryId': new_case_info.depositoryId, 'projectId': new_case_info.projectId, 'columnId': new_case_info.columnId, 'index': new_case_info.index, 'columnLevel': new_case_info.columnLevel, 'level': new_case_info.level, 'type': new_case_info.type, 'status': new_case_info.status, 'userId': new_case_info.userId, 'createTime': str(new_case_info.createTime), 'updateUserId': new_case_info.updateUserId, 'updateTime': str(new_case_info.updateTime), 'veri': new_case_info.veri, 'arch': new_case_info.arch, 'originalCaseId': new_case_info.originalCaseId } # 最后返回内容 return response_json