Typing improvements around automation loop tests.

This commit is contained in:
Sam Rose 2024-01-30 10:00:44 +00:00
parent 2172fabb16
commit 669b0743ac
No known key found for this signature in database
5 changed files with 21 additions and 19 deletions

View File

@ -5,7 +5,7 @@ import {
} from "@budibase/string-templates"
import sdk from "../sdk"
import { Row } from "@budibase/types"
import { LoopStep, LoopStepType, LoopInput } from "../definitions/automations"
import { LoopStep, LoopStepType } from "../definitions/automations"
/**
* When values are input to the system generally they will be of type string as this is required for template strings.
@ -139,7 +139,8 @@ export function stringSplit(value: string | string[]) {
return value
}
export function typecastForLooping(loopStep: LoopStep, input: LoopInput) {
export function typecastForLooping(loopStep: LoopStep) {
const input = loopStep.inputs
if (!input || !input.binding) {
return null
}

View File

@ -3,11 +3,13 @@ import * as triggers from "../triggers"
import { loopAutomation } from "../../tests/utilities/structures"
import { context } from "@budibase/backend-core"
import * as setup from "./utilities"
import { Row, Table } from "@budibase/types"
import { LoopInput, LoopStepType } from "../../definitions/automations"
describe("Attempt to run a basic loop automation", () => {
let config = setup.getConfig(),
table: any,
row: any
table: Table,
row: Row
beforeEach(async () => {
await automation.init()
@ -18,12 +20,12 @@ describe("Attempt to run a basic loop automation", () => {
afterAll(setup.afterAll)
async function runLoop(loopOpts?: any) {
async function runLoop(loopOpts?: LoopInput) {
const appId = config.getAppId()
return await context.doInAppContext(appId, async () => {
const params = { fields: { appId } }
return await triggers.externalTrigger(
loopAutomation(table._id, loopOpts),
loopAutomation(table._id!, loopOpts),
params,
{ getResponses: true }
)
@ -37,7 +39,7 @@ describe("Attempt to run a basic loop automation", () => {
it("test a loop with a string", async () => {
const resp = await runLoop({
type: "String",
option: LoopStepType.STRING,
binding: "a,b,c",
})
expect(resp.steps[2].outputs.iterations).toBe(3)

View File

@ -6,13 +6,11 @@ export enum LoopStepType {
}
export interface LoopStep extends AutomationStep {
inputs: {
option: LoopStepType
[key: string]: any
}
inputs: LoopInput
}
export interface LoopInput {
option: LoopStepType
binding: string[] | string
}

View File

@ -23,6 +23,7 @@ import {
TableSourceType,
Query,
} from "@budibase/types"
import { LoopInput, LoopStepType } from "../../definitions/automations"
const { BUILTIN_ROLE_IDS } = roles
@ -204,10 +205,13 @@ export function serverLogAutomation(appId?: string): Automation {
}
}
export function loopAutomation(tableId: string, loopOpts?: any): Automation {
export function loopAutomation(
tableId: string,
loopOpts?: LoopInput
): Automation {
if (!loopOpts) {
loopOpts = {
option: "Array",
option: LoopStepType.ARRAY,
binding: "{{ steps.1.rows }}",
}
}

View File

@ -23,7 +23,6 @@ import {
} from "@budibase/types"
import {
AutomationContext,
LoopInput,
LoopStep,
TriggerOutput,
} from "../definitions/automations"
@ -47,9 +46,8 @@ function getLoopIterations(loopStep: LoopStep) {
if (!binding) {
return 0
}
const isString = typeof binding === "string"
try {
if (isString) {
if (typeof binding === "string") {
binding = JSON.parse(binding)
}
} catch (err) {
@ -58,7 +56,7 @@ function getLoopIterations(loopStep: LoopStep) {
if (Array.isArray(binding)) {
return binding.length
}
if (isString) {
if (typeof binding === "string") {
return automationUtils.stringSplit(binding).length
}
return 0
@ -331,8 +329,7 @@ class Orchestrator {
}
try {
loopStep.inputs.binding = automationUtils.typecastForLooping(
loopStep as LoopStep,
loopStep.inputs as LoopInput
loopStep as LoopStep
)
} catch (err) {
this.updateContextAndOutput(