def update_case_suite(project_id, case_suite_id): try: filtered_data = CaseSuite.filter_field(request.get_json()) for key, value in filtered_data.items(): CaseSuite.update({"_id": ObjectId(case_suite_id)}, {'$set': { key: value }}) update_response = CaseSuite.update( {"_id": ObjectId(case_suite_id)}, {'$set': { 'lastUpdateTime': datetime.datetime.utcnow() }}, ) if update_response["n"] == 0: return jsonify({'status': 'failed', 'data': '未找到相应更新数据!'}) return jsonify({'status': 'ok', 'data': '更新成功'}) except BaseException as e: return jsonify({'status': 'failed', 'data': '更新失败: %s' % e})
def import_test_cases(): file = request.files.get('file') request_data = request.form case_suite_id = request_data.get('caseSuiteId') if request_data else None project_id = request_data.get('projectId') if request_data else None current_user_name = request_data.get('userName') if request_data else None if file is None or project_id is None or current_user_name is None: return jsonify({'status': 'failed', 'data': '参数不合法!值为: None'}) if case_suite_id == 'undefined' or project_id == 'undefined' or current_user_name == 'undefined': return jsonify({'status': 'failed', 'data': '参数不合法!值为: undefined'}) try: file_content = file.read() workbook = xlrd.open_workbook(file_contents=file_content) except BaseException as e: return jsonify({'status': 'failed', 'data': '「Excel」读取失败! %s' % e}) if '测试用例' not in workbook.sheet_names(): return jsonify({'status': 'failed', 'data': '「Excel」缺失「测试用例」Sheet'}) test_case_table = workbook.sheet_by_name('测试用例') rows_num = test_case_table.nrows # 获取该sheet中的有效行数 if rows_num < 2: return jsonify({ 'status': 'failed', 'data': '「测试用例」Sheet 有效行数小于两行: %s 行' % rows_num }) test_case_attributes = test_case_table.row_values(0) non_intersection = list( set(test_case_map.values()) ^ set(test_case_attributes)) if non_intersection: missing_attributes = [ nip for nip in non_intersection if nip in test_case_map.values() ] return jsonify({'status': 'failed', 'data': '「测试用例」Sheet 表头缺失字段: %s' % missing_attributes}) \ if missing_attributes else jsonify({'status': 'failed', 'data': '「测试用例」Sheet 表头存在多余字段: %s' % [nip for nip in non_intersection if nip not in test_case_map.values()]}) attributes_indexes = [ test_case_attributes.index(v) for v in test_case_map.values() ] def get_pre_import_case_info(case_info, test_case_mapping, table_row_index): _is_case_exist, _case_info, _is_case_suite_exist = \ common.validate_and_pre_process_import_test_case(CaseSuite, TestingCase, case_info, test_case_mapping, table_row_index) return _is_case_exist, _case_info, _is_case_suite_exist results = [] import_count = 0 update_count = 0 test_case_info = copy.deepcopy(test_case_map) for i in range(rows_num - 1): for j, v in enumerate(test_case_info.keys()): test_case_info[v] = test_case_table.row_values(i + 1)[ attributes_indexes[j]] try: is_case_exist, pre_import_case_info, is_case_suite_exist\ = get_pre_import_case_info(test_case_info, test_case_mapping=test_case_map, table_row_index=(i+2)) except BaseException as b_e: return jsonify({'status': 'failed', 'data': '导入数据异常: %s' % b_e}) try: # 在接口用例列表中导入 if case_suite_id: if is_case_exist and str( pre_import_case_info.get('caseSuiteId')) == str( case_suite_id): pre_import_case_info = TestingCase.filter_field( pre_import_case_info, use_set_default=False) result = str(TestingCase.update({"_id": ObjectId(pre_import_case_info.get('_id'))}, {'$set': pre_import_case_info})) + \ ' _id: {}'.format(pre_import_case_info.get('_id')) update_count += 1 else: try: pre_import_case_info.pop('_id') except BaseException: pass pre_import_case_info['status'] = True pre_import_case_info['caseSuiteId'] = ObjectId( case_suite_id) pre_import_case_info['projectId'] = ObjectId(project_id) pre_import_case_info['creatorNickName'] = current_user_name pre_import_case_info[ 'lastUpdatorNickName'] = current_user_name pre_import_case_info = TestingCase.filter_field( pre_import_case_info, use_set_default=True) result = TestingCase.insert(pre_import_case_info) import_count += 1 # 在用例列表内导入 else: inserted_case_suite_id = None case_suite_name = pre_import_case_info.get('caseSuiteName')\ if pre_import_case_info.get('caseSuiteName') else '' if is_case_suite_exist: if not case_suite_name == CaseSuite.find_one({ "_id": ObjectId(pre_import_case_info.get('caseSuiteId')) })['name']: CaseSuite.update( { "_id": ObjectId( pre_import_case_info.get('caseSuiteId')) }, {'$set': { 'name': case_suite_name }}) else: pass else: insert_data = dict() insert_data["name"] = case_suite_name insert_data["status"] = True insert_data["projectId"] = ObjectId(project_id) insert_data["lastUpdateTime"] = datetime.datetime.utcnow() insert_data["createAt"] = datetime.datetime.utcnow() insert_data["creatorNickName"] = current_user_name insert_data["lastUpdatorNickName"] = current_user_name inserted_case_suite_id = CaseSuite.insert(insert_data) if inserted_case_suite_id: pre_import_case_info.pop('_id') if is_case_exist else None pre_import_case_info["projectId"] = ObjectId(project_id) pre_import_case_info['caseSuiteId'] = ObjectId( inserted_case_suite_id) pre_import_case_info = TestingCase.filter_field( pre_import_case_info, use_set_default=True) result = TestingCase.insert(pre_import_case_info) import_count += 1 else: if is_case_exist: pre_import_case_info = TestingCase.filter_field( pre_import_case_info, use_set_default=False) result = str(TestingCase.update({"_id": ObjectId(pre_import_case_info.get('_id'))}, {'$set': pre_import_case_info})) + ' _id: {}'\ .format(pre_import_case_info.get('_id')) update_count += 1 else: pre_import_case_info["projectId"] = ObjectId( project_id) pre_import_case_info['caseSuiteId'] = ObjectId( pre_import_case_info.get('caseSuiteId')) pre_import_case_info = TestingCase.filter_field( pre_import_case_info, use_set_default=True) result = TestingCase.insert(pre_import_case_info) import_count += 1 results.append(result) except BaseException as e: return jsonify({'status': 'failed', 'data': '数据导入异常: %s' % e}) def get_returned_data(_results, _import_count, _update_count): _returned_data = dict() _returned_data['status'] = 'ok' if import_count > 0 and update_count == 0: _returned_data['data'] = '操作成功, 共成功导入 %s 条用例' % _import_count # _returned_data['log'] = '导入数据_id: %s' % _results elif update_count > 0 and import_count == 0: _returned_data['data'] = '操作成功, 共成功覆盖 %s 条用例' % _update_count # _returned_data['log'] = '导入数据_id: %s' % _results elif import_count > 0 and update_count > 0: _returned_data['data'] = '操作成功, 共成功导入 %s 条用例、覆盖 %s 条用例' % ( import_count, update_count) # _returned_data['log'] = '导入数据_id: %s' % _results else: _returned_data['data'] = '操作成功,但啥都没导入 / 覆盖' # _returned_data['log'] = None return jsonify(_returned_data) returned_data = get_returned_data(results, import_count, update_count) return returned_data