Adding test cases as per PR comments.

This commit is contained in:
mike12345567 2023-01-25 18:27:58 +00:00 committed by adrinr
parent 27b472b662
commit b38399cb74
4 changed files with 117 additions and 3 deletions

View File

@ -0,0 +1,45 @@
import * as automation from "../index"
import * as triggers from "../triggers"
import { loopAutomation } from "../../tests/utilities/structures"
import { context } from "@budibase/backend-core"
import * as setup from "./utilities"
describe("Attempt to run a basic loop automation", () => {
let config = setup.getConfig(),
table: any,
row: any
beforeEach(async () => {
await automation.init()
await config.init()
table = await config.createTable()
row = await config.createRow()
})
afterAll(setup.afterAll)
async function runLoop(loopOpts?: any) {
const appId = config.getAppId()
return await context.doInAppContext(appId, async () => {
const params = { fields: { appId } }
return await triggers.externalTrigger(
loopAutomation(table._id, loopOpts),
params,
{ getResponses: true }
)
})
}
it("attempt to run a basic loop", async () => {
const resp = await runLoop()
expect(resp.steps[2].outputs.iterations).toBe(1)
})
it("test a loop with a string", async () => {
const resp = await runLoop({
type: "String",
binding: "a,b,c",
})
expect(resp.steps[2].outputs.iterations).toBe(3)
})
})

View File

@ -109,8 +109,13 @@ export async function externalTrigger(
} }
params.fields = coercedFields params.fields = coercedFields
} }
const data = { automation, event: params } const data: Record<string, any> = { automation, event: params }
if (getResponses) { if (getResponses) {
data.event = {
...data.event,
appId: context.getAppId(),
automation,
}
return utils.processEvent({ data }) return utils.processEvent({ data })
} else { } else {
return automationQueue.add(data, JOB_OPTS) return automationQueue.add(data, JOB_OPTS)

View File

@ -1,8 +1,14 @@
import { roles, permissions } from "@budibase/backend-core" import { permissions, roles } from "@budibase/backend-core"
import { createHomeScreen } from "../../constants/screens" import { createHomeScreen } from "../../constants/screens"
import { EMPTY_LAYOUT } from "../../constants/layouts" import { EMPTY_LAYOUT } from "../../constants/layouts"
import { cloneDeep } from "lodash/fp" import { cloneDeep } from "lodash/fp"
import { TRIGGER_DEFINITIONS, ACTION_DEFINITIONS } from "../../automations" import { ACTION_DEFINITIONS, TRIGGER_DEFINITIONS } from "../../automations"
import {
Automation,
AutomationActionStepId,
AutomationTriggerStepId,
} from "@budibase/types"
const { v4: uuidv4 } = require("uuid") const { v4: uuidv4 } = require("uuid")
export const TENANT_ID = "default" export const TENANT_ID = "default"
@ -116,6 +122,63 @@ export function basicAutomation() {
} }
} }
export function loopAutomation(tableId: string, loopOpts?: any): Automation {
if (!loopOpts) {
loopOpts = {
option: "Array",
binding: "{{ steps.1.rows }}",
}
}
const automation: any = {
name: "looping",
type: "automation",
definition: {
steps: [
{
id: "b",
type: "ACTION",
stepId: AutomationActionStepId.QUERY_ROWS,
internal: true,
inputs: {
tableId,
},
schema: ACTION_DEFINITIONS.QUERY_ROWS.schema,
},
{
id: "c",
type: "ACTION",
stepId: AutomationActionStepId.LOOP,
internal: true,
inputs: loopOpts,
blockToLoop: "d",
schema: ACTION_DEFINITIONS.LOOP.schema,
},
{
id: "d",
type: "ACTION",
internal: true,
stepId: AutomationActionStepId.SERVER_LOG,
inputs: {
text: "log statement",
},
schema: ACTION_DEFINITIONS.SERVER_LOG.schema,
},
],
trigger: {
id: "a",
type: "TRIGGER",
event: "row:save",
stepId: AutomationTriggerStepId.ROW_SAVED,
inputs: {
tableId,
},
schema: TRIGGER_DEFINITIONS.ROW_SAVED.schema,
},
},
}
return automation as Automation
}
export function basicRow(tableId: string) { export function basicRow(tableId: string) {
return { return {
name: "Test Contact", name: "Test Contact",

View File

@ -50,6 +50,7 @@ export interface AutomationStepSchema {
internal?: boolean internal?: boolean
deprecated?: boolean deprecated?: boolean
stepId: AutomationTriggerStepId | AutomationActionStepId stepId: AutomationTriggerStepId | AutomationActionStepId
blockToLoop?: string
inputs: { inputs: {
[key: string]: any [key: string]: any
} }