refactor: Rewrite workflow public-api tests to avoid timeouts (no-changelog) (#5696)

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™ 2023-03-15 14:24:09 +01:00 committed by GitHub
parent 6c74d41f23
commit b4e60c3b47
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 926 additions and 1291 deletions

View file

@ -1,6 +1,6 @@
import type express from 'express';
import { Container } from 'typedi';
import type { FindManyOptions, FindOptionsWhere } from 'typeorm';
import type { FindOptionsWhere } from 'typeorm';
import { In } from 'typeorm';
import { ActiveWorkflowRunner } from '@/ActiveWorkflowRunner';
@ -20,12 +20,11 @@ import {
updateWorkflow,
hasStartNode,
getStartNode,
getWorkflows,
getSharedWorkflows,
getWorkflowsCount,
createWorkflow,
getWorkflowIdsViaTags,
parseTagNames,
getWorkflowsAndCount,
} from './workflows.service';
import { WorkflowsService } from '@/workflows/workflows.services';
import { InternalHooks } from '@/InternalHooks';
@ -98,28 +97,15 @@ export = {
async (req: WorkflowRequest.GetAll, res: express.Response): Promise<express.Response> => {
const { offset = 0, limit = 100, active = undefined, tags = undefined } = req.query;
let workflows: WorkflowEntity[];
let count: number;
const where: FindOptionsWhere<WorkflowEntity> = {
...(active !== undefined && { active }),
};
const query: FindManyOptions<WorkflowEntity> = {
skip: offset,
take: limit,
where,
...(!config.getEnv('workflowTagsDisabled') && { relations: ['tags'] }),
};
if (isInstanceOwner(req.user)) {
if (tags) {
const workflowIds = await getWorkflowIdsViaTags(parseTagNames(tags));
Object.assign(where, { id: In(workflowIds) });
where.id = In(workflowIds);
}
workflows = await getWorkflows(query);
count = await getWorkflowsCount(query);
} else {
const options: { workflowIds?: string[] } = {};
@ -137,14 +123,16 @@ export = {
}
const workflowsIds = sharedWorkflows.map((shareWorkflow) => shareWorkflow.workflowId);
Object.assign(where, { id: In(workflowsIds) });
workflows = await getWorkflows(query);
count = await getWorkflowsCount(query);
where.id = In(workflowsIds);
}
const [workflows, count] = await getWorkflowsAndCount({
skip: offset,
take: limit,
where,
...(!config.getEnv('workflowTagsDisabled') && { relations: ['tags'] }),
});
void Container.get(InternalHooks).onUserRetrievedAllWorkflows({
user_id: req.user.id,
public_api: true,

View file

@ -109,14 +109,10 @@ export async function deleteWorkflow(workflow: WorkflowEntity): Promise<Workflow
return Db.collections.Workflow.remove(workflow);
}
export async function getWorkflows(
export async function getWorkflowsAndCount(
options: FindManyOptions<WorkflowEntity>,
): Promise<WorkflowEntity[]> {
return Db.collections.Workflow.find(options);
}
export async function getWorkflowsCount(options: FindManyOptions<WorkflowEntity>): Promise<number> {
return Db.collections.Workflow.count(options);
): Promise<[WorkflowEntity[], number]> {
return Db.collections.Workflow.findAndCount(options);
}
export async function updateWorkflow(

File diff suppressed because it is too large Load diff

View file

@ -172,7 +172,7 @@ export async function createUser(attributes: Partial<User> = {}): Promise<User>
password: await hashPassword(password ?? randomValidPassword()),
firstName: firstName ?? randomName(),
lastName: lastName ?? randomName(),
globalRole: globalRole ?? (await getGlobalMemberRole()),
globalRoleId: (globalRole ?? (await getGlobalMemberRole())).id,
...rest,
};