refactor(plugin-workflow): change strict equal and not equal to unstrict (#2346)

This commit is contained in:
Junyi 2023-07-29 11:25:01 +07:00 committed by GitHub
parent 620d1ff8df
commit 021ca950ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 141 additions and 42 deletions

View File

@ -1,7 +1,7 @@
import { Application } from '@nocobase/server';
import Database from '@nocobase/database'; import Database from '@nocobase/database';
import { Application } from '@nocobase/server';
import { getApp, sleep } from '..'; import { getApp, sleep } from '..';
import { EXECUTION_STATUS, BRANCH_INDEX } from '../../constants'; import { BRANCH_INDEX, EXECUTION_STATUS } from '../../constants';
describe('workflow > instructions > condition', () => { describe('workflow > instructions > condition', () => {
let app: Application; let app: Application;
@ -243,51 +243,150 @@ describe('workflow > instructions > condition', () => {
}); });
describe('engines', () => { describe('engines', () => {
it('default as basic', async () => { describe('basic', () => {
const n1 = await workflow.createNode({ it('default as basic', async () => {
title: 'condition', const n1 = await workflow.createNode({
type: 'condition', title: 'condition',
config: { type: 'condition',
calculation: { config: {
calculator: 'equal', calculation: {
operands: [1, '{{$context.data.read}}'], calculator: 'equal',
operands: [1, '{{$context.data.read}}'],
},
}, },
}, });
const post = await PostRepo.create({ values: { read: 1 } });
await sleep(500);
const [execution] = await workflow.getExecutions();
expect(execution.status).toEqual(EXECUTION_STATUS.RESOLVED);
const [job] = await execution.getJobs();
expect(job.result).toEqual(true);
}); });
const post = await PostRepo.create({ values: { read: 1 } }); it('equal: 0 != null', async () => {
const n1 = await workflow.createNode({
await sleep(500); title: 'condition',
type: 'condition',
const [execution] = await workflow.getExecutions(); config: {
expect(execution.status).toEqual(EXECUTION_STATUS.RESOLVED); engine: 'basic',
calculation: {
const [job] = await execution.getJobs(); calculator: 'equal',
expect(job.result).toEqual(true); operands: [0, '{{$context.data.title}}'],
}); },
rejectOnFalse: false,
it('basic engine', async () => {
const n1 = await workflow.createNode({
title: 'condition',
type: 'condition',
config: {
engine: 'basic',
calculation: {
calculator: 'equal',
operands: [1, '{{$context.data.read}}'],
}, },
}, });
const post = await PostRepo.create({ values: {} });
await sleep(500);
const [execution] = await workflow.getExecutions();
expect(execution.status).toEqual(EXECUTION_STATUS.RESOLVED);
const [job] = await execution.getJobs();
expect(job.result).toEqual(false);
}); });
const post = await PostRepo.create({ values: { read: 1 } }); it('equal: 0 == false', async () => {
const n1 = await workflow.createNode({
title: 'condition',
type: 'condition',
config: {
engine: 'basic',
calculation: {
calculator: 'equal',
operands: [false, '{{$context.data.read}}'],
},
},
});
await sleep(500); const post = await PostRepo.create({ values: {} });
const [execution] = await workflow.getExecutions(); await sleep(500);
expect(execution.status).toEqual(EXECUTION_STATUS.RESOLVED);
const [job] = await execution.getJobs(); const [execution] = await workflow.getExecutions();
expect(job.result).toEqual(true); expect(execution.status).toEqual(EXECUTION_STATUS.RESOLVED);
const [job] = await execution.getJobs();
expect(job.result).toEqual(true);
});
it('equal: number == number', async () => {
const n1 = await workflow.createNode({
title: 'condition',
type: 'condition',
config: {
engine: 'basic',
calculation: {
calculator: 'equal',
operands: [1, '{{$context.data.read}}'],
},
},
});
const post = await PostRepo.create({ values: { read: 1 } });
await sleep(500);
const [execution] = await workflow.getExecutions();
expect(execution.status).toEqual(EXECUTION_STATUS.RESOLVED);
const [job] = await execution.getJobs();
expect(job.result).toEqual(true);
});
it('equal: string == number', async () => {
const n1 = await workflow.createNode({
title: 'condition',
type: 'condition',
config: {
engine: 'basic',
calculation: {
calculator: 'equal',
operands: ['1', '{{$context.data.read}}'],
},
},
});
const post = await PostRepo.create({ values: { read: 1 } });
await sleep(500);
const [execution] = await workflow.getExecutions();
expect(execution.status).toEqual(EXECUTION_STATUS.RESOLVED);
const [job] = await execution.getJobs();
expect(job.result).toEqual(true);
});
it('equal: undefined == null', async () => {
const n1 = await workflow.createNode({
title: 'condition',
type: 'condition',
config: {
engine: 'basic',
calculation: {
calculator: 'equal',
operands: ['{{$context.data.category.id}}', null],
},
},
});
const post = await PostRepo.create({ values: {} });
await sleep(500);
const [execution] = await workflow.getExecutions();
expect(execution.status).toEqual(EXECUTION_STATUS.RESOLVED);
const [job] = await execution.getJobs();
expect(job.result).toEqual(true);
});
}); });
it('math.js', async () => { it('math.js', async () => {

View File

@ -11,11 +11,11 @@ export const calculators = new Registry<Comparer>();
// built-in functions // built-in functions
function equal(a, b) { function equal(a, b) {
return a === b; return a == b;
} }
function notEqual(a, b) { function notEqual(a, b) {
return a !== b; return a != b;
} }
function gt(a, b) { function gt(a, b) {
@ -41,8 +41,8 @@ calculators.register('gte', gte);
calculators.register('lt', lt); calculators.register('lt', lt);
calculators.register('lte', lte); calculators.register('lte', lte);
calculators.register('===', equal); calculators.register('==', equal);
calculators.register('!==', notEqual); calculators.register('!=', notEqual);
calculators.register('>', gt); calculators.register('>', gt);
calculators.register('>=', gte); calculators.register('>=', gte);
calculators.register('<', lt); calculators.register('<', lt);