import * as _ from 'lodash' import TeambitionClient from '../../client/teambition' import TesthubClient from '../../client/testhub' import Base from './base' import {DbCollection, OptionsCollection} from '../../../types' import User = DbCollection.User import Testcase = DbCollection.Testcase import BasicTestcase = DbCollection.BasicTestcase import { ilog } from 'ilog' import { TestcaseChecker } from '../../check/testcase' interface Header { type: string, value: string } export default class TestcaseFactory extends Base { protected headerArray: Header[] protected basic: boolean /** * @param {*} app * @param {*} ctx * @param {boolean} [basic=false] 是否导入基础用例(用例库中) * @memberof TestcaseFactory */ constructor(app, ctx, basic = false) { super(app, ctx) this.basic = basic } async import (testCases: Array) { const client = this.basic ? new TesthubClient(this.app, this.ctx) : new TeambitionClient(this.app, this.ctx) for (let i = 0; i < testCases.length; i++) { try { if (this.basic) { await (client).importBasicTestCases(testCases[i]) } else { await (client).importTestCases(testCases[i]) } } catch (error) { ilog.err(error) } // 限制访问频率 await new Promise((resolve) => { setTimeout(resolve, 100) }) } } buildHeaders() { const header =  this.basic ? [ 'tc.title', 'tc.group', 'tc.maintainer', 'tc.precondition', 'tc.step.desc', 'tc.step.expected', 'tc.caseType', 'tc.priority', ] : [ 'tc.title', 'tc.group', 'tc.executor', 'tc.precondition', 'tc.step.desc', 'tc.step.expected', 'tc.caseType', 'tc.priority', ] this.headerArray = header.map((v) => ({ type: this.NATIVE, value: v, })) this.headers = this.translate(this.headerArray) return this } buildExample () { this.example = [ '(示例勿删)待办列表测试', 'Web端测试用例|首页|我的待办', '王XX', '我的待办中存在待处理的任务', '【1】显示当前未处理的任务\n【2】可以正常进入任务详情', '【1】显示当前未处理的任务\n【2】可以正常进入任务详情', '功能测试', 'P3' ] return this } async transformToTestCase (parsedData: object[], user: User, options: OptionsCollection.TestCase.TransformOtions) { const { project, _testplanId, _scenariofieldconfigId, _testhubId, _organizationId } = options const checker = new TestcaseChecker(this.app, this.ctx) // 验证 _testplanId 合法性 if (!this.basic) { await checker.testPlan(_testplanId, project._id) } else { await checker.testhub(_testhubId, _organizationId) } const testcases: Array = [] let testcase: Testcase | BasicTestcase for (let i = 0; i < parsedData.length; i++) { const item = parsedData[i] if (_.isEmpty(_.pickBy(item))) { continue } testcase = this.basic ? { title: '', _testhubId, steps: [], priority: null, _executorId: null, _commongroupId: null, precondition: null } : { title: '', _projectId: project._id, _testplanId, _scenariofieldconfigId, steps: [], priority: null, _executorId: null, precondition: null } testcase.title = checker.required(item, 'tc.title', i) checker.lengthLimit( { type: 'title', key: 'tc.title', val: testcase.title, row: i}, 500 ) testcase.precondition = item['tc.precondition'] testcase.steps = checker.steps(item['tc.step.desc'], item['tc.step.expected'], i) testcase.caseType = checker.caseType(item['tc.caseType'], i) testcase.priority = checker.priority(item['tc.priority'], i) const appType = this.basic ? 'testhub' : 'testplan' if (this.basic) { (testcase)._commongroupId = await checker.group(item['tc.group'], _testhubId, i) testcase._executorId = await checker.maintainer(item['tc.maintainer'], _organizationId, true, i) } else { (testcase)._commongroupId = await checker.group(item['tc.group'], _testplanId, i, appType) testcase._executorId = await checker.executor(item['tc.executor'], project._id, true, i) } testcases.push(testcase) } return { testcases, errorMsgs: checker.ErrorMsgs } } }