()
用例添加、编辑
用例添加、编辑
用例添加、编辑
def add_case():
    """ 用例添加、编辑 """
    data = request.json
    name = data.get('name')
    if not name:
        return jsonify({'msg': '用例名称不能为空', 'status': 0})
    desc = data.get('desc')
    ids = data.get('ids')
    times = data.get('times')
    case_set_id = data.get('caseSetId')
    func_address = json.dumps(data.get('funcAddress'))
    project_id = data.get('projectId')
    project_data = Project.query.filter_by(id=project_id).first()
    variable = data.get('variable')
    api_cases = data.get('apiCases')
    environment = data.get('environment')

    merge_variable = json.dumps(json.loads(variable) + json.loads(project_data.variables))
    _temp_check = extract_variables(convert(json.loads(merge_variable)))
    if not case_set_id:
        return jsonify({'msg': '请选择用例集', 'status': 0})
    if re.search('\${(.*?)}', '{}{}'.format(variable, json.dumps(api_cases)), flags=0) and not func_address:
        return jsonify({'msg': '参数引用函数后，必须引用函数文件', 'status': 0})
    if _temp_check:
        return jsonify({'msg': '参数引用${}在业务变量和项目公用变量均没找到'.format(',$'.join(_temp_check)), 'status': 0})

    # cases_check = check_case(api_cases, func_address)
    # if cases_check:
    #     return jsonify({'msg': cases_check, 'status': 0})

    # variable_check = check_case(variable, func_address)
    # if variable_check:
    #     return jsonify({'msg': variable_check, 'status': 0})

    num = auto_num(data.get('num'), Case, project_id=project_id, case_set_id=case_set_id)
    if ids:
        old_data = Case.query.filter_by(id=ids).first()
        old_num = old_data.num
        if Case.query.filter_by(name=name, project_id=project_id,
                                case_set_id=case_set_id).first() and name != old_data.name:
            return jsonify({'msg': '用例名字重复', 'status': 0})
        else:
            list_data = Case.query.filter_by(case_set_id=case_set_id).all()
            # list_data = CaseSet.query.filter_by(id=case_set_id).first().cases.all()
            num_sort(num, old_num, list_data, old_data)
            old_data.name = name
            old_data.times = times
            old_data.project_id = project_id
            old_data.desc = desc
            old_data.environment = environment
            old_data.case_set_id = case_set_id
            old_data.func_address = func_address
            old_data.variable = variable
            db.session.commit()
        for _num, c in enumerate(api_cases):
            if c.get('id'):
                old_api_case = CaseData.query.filter_by(id=c.get('id')).first()
                old_api_case.num = _num
                old_api_case.extract = json.dumps(c['extract'])
                old_api_case.validate = json.dumps(c['validate'])
                old_api_case.variable = json.dumps(c['variable'])
                old_api_case.header = json.dumps(c['header'])
                old_api_case.json_variable = c['json_variable']
                old_api_case.param = json.dumps(c['param'])
                old_api_case.time = c['time']
                old_api_case.status_variables = json.dumps(c['statusCase']['variable'])
                old_api_case.status_extract = json.dumps(c['statusCase']['extract'])
                old_api_case.status_validate = json.dumps(c['statusCase']['validate'])
                old_api_case.status_param = json.dumps(c['statusCase']['param'])
                old_api_case.status_header = json.dumps(c['statusCase']['header'])
                old_api_case.name = c['case_name']
                old_api_case.status = json.dumps(c['status'])
                old_api_case.up_func = c['up_func']
                old_api_case.down_func = c['down_func']
                old_api_case.skip = c['skip']
                db.session.commit()
            else:
                new_api_case = CaseData(num=_num,
                                        json_variable=c['json_variable'],
                                        variable=json.dumps(c['variable']),
                                        extract=json.dumps(c['extract']),
                                        param=json.dumps(c['param']),
                                        validate=json.dumps(c['validate']),
                                        case_id=ids,
                                        time=c['time'],
                                        api_msg_id=c['apiMsgId'],
                                        status_variables=json.dumps(c['statusCase']['variable']),
                                        status_extract=json.dumps(c['statusCase']['extract']),
                                        status_validate=json.dumps(c['statusCase']['validate']),
                                        status_param=json.dumps(c['statusCase']['param']),
                                        header=json.dumps(c['header']),
                                        status_header=json.dumps(c['statusCase']['header']),
                                        status=json.dumps(c['status']),
                                        name=c['case_name'], up_func=c['up_func'], down_func=c['down_func'])
                db.session.add(new_api_case)
                db.session.commit()
        return jsonify({'msg': '修改成功', 'status': 1, 'case_id': ids})
    else:
        if Case.query.filter_by(name=name, project_id=project_id, case_set_id=case_set_id).first():
            return jsonify({'msg': '用例名字重复', 'status': 0})
        elif Case.query.filter_by(num=num, project_id=project_id, case_set_id=case_set_id).first():
            return jsonify({'msg': '编号重复', 'status': 0})
        else:

            new_case = Case(num=num, name=name, desc=desc, project_id=project_id, variable=variable,
                            func_address=func_address, case_set_id=case_set_id, times=times, environment=environment)
            db.session.add(new_case)
            db.session.commit()
            case_id = new_case.id
            for _num, c in enumerate(api_cases):
                new_api_case = CaseData(num=_num,
                                        variable=json.dumps(c['variable']),
                                        json_variable=c['json_variable'],
                                        extract=json.dumps(c['extract']),
                                        param=json.dumps(c['param']),
                                        time=c['time'],
                                        validate=json.dumps(c['validate']), case_id=case_id, api_msg_id=c['apiMsgId'],
                                        status_variables=json.dumps(c['statusCase']['variable']),
                                        status_extract=json.dumps(c['statusCase']['extract']),
                                        status_validate=json.dumps(c['statusCase']['validate']),
                                        status_param=json.dumps(c['statusCase']['param']),
                                        header=json.dumps(c['header']),
                                        status_header=json.dumps(c['statusCase']['header']),
                                        status=json.dumps(c['status']), skip=c['skip'],
                                        name=c['case_name'], up_func=c['up_func'], down_func=c['down_func'])
                db.session.add(new_api_case)
                db.session.commit()
            return jsonify({'msg': '新建成功', 'status': 1, 'case_id': case_id, 'num': new_case.num})
def add_case ( ) : data = request . json name = data . get ( 'name' ) if not name : return jsonify ( { 'msg' : '用例名称不能为空', 'status': 0}) desc = data . get ( 'desc' ) ids = data . get ( 'ids' ) times = data . get ( 'times' ) case_set_id = data . get ( 'caseSetId' ) func_address = json . dumps ( data . get ( 'funcAddress' ) ) project_id = data . get ( 'projectId' ) project_data = Project . query . filter_by ( id = project_id ) . first ( ) variable = data . get ( 'variable' ) api_cases = data . get ( 'apiCases' ) environment = data . get ( 'environment' ) merge_variable = json . dumps ( json . loads ( variable ) + json . loads ( project_data . variables ) ) _temp_check = extract_variables ( convert ( json . loads ( merge_variable ) ) ) if not case_set_id : return jsonify ( { 'msg' : '请选择用例集', 'status':  0 ) if re . search ( '\${(.*?)}' , '{}{}' . format ( variable , json . dumps ( api_cases ) ) , flags = 0 ) and not func_address : return jsonify ( { 'msg' : '参数引用函数后，必须引用函数文件', 'status': 0}) if _temp_check : return jsonify ( { 'msg' : '参数引用${}在业务变量和项目公用变量均没找到'.format(',$'.join(_temp_check)), 'status ' : 0}) # cases_check = check_case(api_cases, func_address) # if cases_check: #     return jsonify({'msg': cases_check, 'status': 0}) # variable_check = check_case(variable, func_address) # if variable_check: #     return jsonify({'msg': variable_check, 'status': 0}) num = auto_num ( data . get ( 'num' ) , Case , project_id = project_id , case_set_id = case_set_id ) if ids : old_data = Case . query . filter_by ( id = ids ) . first ( ) old_num = old_data . num if Case . query . filter_by ( name = name , project_id = project_id , case_set_id = case_set_id ) . first ( ) and name != old_data . name : return jsonify ( { 'msg' : '用例名字重复', 'status':  0 ) else : list_data = Case . query . filter_by ( case_set_id = case_set_id ) . all ( ) # list_data = CaseSet.query.filter_by(id=case_set_id).first().cases.all() num_sort ( num , old_num , list_data , old_data ) old_data . name = name old_data . times = times old_data . project_id = project_id old_data . desc = desc old_data . environment = environment old_data . case_set_id = case_set_id old_data . func_address = func_address old_data . variable = variable db . session . commit ( ) for _num , c in enumerate ( api_cases ) : if c . get ( 'id' ) : old_api_case = CaseData . query . filter_by ( id = c . get ( 'id' ) ) . first ( ) old_api_case . num = _num old_api_case . extract = json . dumps ( c [ 'extract' ] ) old_api_case . validate = json . dumps ( c [ 'validate' ] ) old_api_case . variable = json . dumps ( c [ 'variable' ] ) old_api_case . header = json . dumps ( c [ 'header' ] ) old_api_case . json_variable = c [ 'json_variable' ] old_api_case . param = json . dumps ( c [ 'param' ] ) old_api_case . time = c [ 'time' ] old_api_case . status_variables = json . dumps ( c [ 'statusCase' ] [ 'variable' ] ) old_api_case . status_extract = json . dumps ( c [ 'statusCase' ] [ 'extract' ] ) old_api_case . status_validate = json . dumps ( c [ 'statusCase' ] [ 'validate' ] ) old_api_case . status_param = json . dumps ( c [ 'statusCase' ] [ 'param' ] ) old_api_case . status_header = json . dumps ( c [ 'statusCase' ] [ 'header' ] ) old_api_case . name = c [ 'case_name' ] old_api_case . status = json . dumps ( c [ 'status' ] ) old_api_case . up_func = c [ 'up_func' ] old_api_case . down_func = c [ 'down_func' ] old_api_case . skip = c [ 'skip' ] db . session . commit ( ) else : new_api_case = CaseData ( num = _num , json_variable = c [ 'json_variable' ] , variable = json . dumps ( c [ 'variable' ] ) , extract = json . dumps ( c [ 'extract' ] ) , param = json . dumps ( c [ 'param' ] ) , validate = json . dumps ( c [ 'validate' ] ) , case_id = ids , time = c [ 'time' ] , api_msg_id = c [ 'apiMsgId' ] , status_variables = json . dumps ( c [ 'statusCase' ] [ 'variable' ] ) , status_extract = json . dumps ( c [ 'statusCase' ] [ 'extract' ] ) , status_validate = json . dumps ( c [ 'statusCase' ] [ 'validate' ] ) , status_param = json . dumps ( c [ 'statusCase' ] [ 'param' ] ) , header = json . dumps ( c [ 'header' ] ) , status_header = json . dumps ( c [ 'statusCase' ] [ 'header' ] ) , status = json . dumps ( c [ 'status' ] ) , name = c [ 'case_name' ] , up_func = c [ 'up_func' ] , down_func = c [ 'down_func' ] ) db . session . add ( new_api_case ) db . session . commit ( ) return jsonify ( { 'msg' : '修改成功', 'statu s : 1, 'ca s _ i ': ids}) else : if Case . query . filter_by ( name = name , project_id = project_id , case_set_id = case_set_id ) . first ( ) : return jsonify ( { 'msg' : '用例名字重复', 'status':  0 ) elif Case . query . filter_by ( num = num , project_id = project_id , case_set_id = case_set_id ) . first ( ) : return jsonify ( { 'msg' : '编号重复', 'statu s : 0}) else : new_case = Case ( num = num , name = name , desc = desc , project_id = project_id , variable = variable , func_address = func_address , case_set_id = case_set_id , times = times , environment = environment ) db . session . add ( new_case ) db . session . commit ( ) case_id = new_case . id for _num , c in enumerate ( api_cases ) : new_api_case = CaseData ( num = _num , variable = json . dumps ( c [ 'variable' ] ) , json_variable = c [ 'json_variable' ] , extract = json . dumps ( c [ 'extract' ] ) , param = json . dumps ( c [ 'param' ] ) , time = c [ 'time' ] , validate = json . dumps ( c [ 'validate' ] ) , case_id = case_id , api_msg_id = c [ 'apiMsgId' ] , status_variables = json . dumps ( c [ 'statusCase' ] [ 'variable' ] ) , status_extract = json . dumps ( c [ 'statusCase' ] [ 'extract' ] ) , status_validate = json . dumps ( c [ 'statusCase' ] [ 'validate' ] ) , status_param = json . dumps ( c [ 'statusCase' ] [ 'param' ] ) , header = json . dumps ( c [ 'header' ] ) , status_header = json . dumps ( c [ 'statusCase' ] [ 'header' ] ) , status = json . dumps ( c [ 'status' ] ) , skip = c [ 'skip' ] , name = c [ 'case_name' ] , up_func = c [ 'up_func' ] , down_func = c [ 'down_func' ] ) db . session . add ( new_api_case ) db . session . commit ( ) return jsonify ( { 'msg' : '新建成功', 'statu s : 1, 'ca s _ i ': case_i d  'num': ew_ca s .num})