Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleaning up loop.spec.ts, moved tests from automation.spec.ts to more appropriate places. #15536

Merged
merged 3 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 0 additions & 100 deletions packages/server/src/automations/tests/automation.spec.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,31 @@ describe("automationUtils", () => {
schema,
})
})

it("should be able to clean inputs with the utilities", () => {
// can't clean without a schema
let output = cleanInputValues({ a: "1" })
expect(output.a).toBe("1")
output = cleanInputValues(
{ a: "1", b: "true", c: "false", d: 1, e: "help" },
{
properties: {
a: {
type: "number",
},
b: {
type: "boolean",
},
c: {
type: "boolean",
},
},
}
)
expect(output.a).toBe(1)
expect(output.b).toBe(true)
expect(output.c).toBe(false)
expect(output.d).toBe(1)
})
Comment on lines +123 to +147
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been moved from automation.spec.ts to here.

})
})
67 changes: 34 additions & 33 deletions packages/server/src/automations/tests/steps/loop.spec.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import * as automation from "../../index"
import * as triggers from "../../triggers"
import { basicTable, loopAutomation } from "../../../tests/utilities/structures"
import { context } from "@budibase/backend-core"
import { basicTable } from "../../../tests/utilities/structures"
import {
Table,
LoopStepType,
AutomationResults,
ServerLogStepOutputs,
CreateRowStepOutputs,
FieldType,
} from "@budibase/types"
import * as loopUtils from "../../loopUtils"
import { LoopInput } from "../../../definitions/automations"
import { createAutomationBuilder } from "../utilities/AutomationTestBuilder"
import TestConfiguration from "../../../tests/utilities/TestConfiguration"

Expand All @@ -34,41 +30,46 @@ describe("Attempt to run a basic loop automation", () => {
config.end()
})

async function runLoop(loopOpts?: LoopInput): Promise<AutomationResults> {
const appId = config.getAppId()
return await context.doInAppContext(appId, async () => {
const params = { fields: { appId } }
const result = await triggers.externalTrigger(
loopAutomation(table._id!, loopOpts),
params,
{ getResponses: true }
)
if ("outputs" in result && !result.outputs.success) {
throw new Error("Unable to proceed - failed to return anything.")
}
return result as AutomationResults
})
}

it("attempt to run a basic loop", async () => {
const resp = await runLoop()
expect(resp.steps[2].outputs.iterations).toBe(1)
const result = await createAutomationBuilder(config)
.onAppAction()
.queryRows({
tableId: table._id!,
})
.loop({
option: LoopStepType.ARRAY,
binding: "{{ steps.1.rows }}",
})
.serverLog({ text: "log statement" })
.test({ fields: {} })

expect(result.steps[1].outputs.iterations).toBe(1)
})

it("test a loop with a string", async () => {
const resp = await runLoop({
option: LoopStepType.STRING,
binding: "a,b,c",
})
expect(resp.steps[2].outputs.iterations).toBe(3)
const result = await createAutomationBuilder(config)
.onAppAction()
.loop({
option: LoopStepType.STRING,
binding: "a,b,c",
})
.serverLog({ text: "log statement" })
.test({ fields: {} })

expect(result.steps[0].outputs.iterations).toBe(3)
})

it("test a loop with a binding that returns an integer", async () => {
const resp = await runLoop({
option: LoopStepType.ARRAY,
binding: "{{ 1 }}",
})
expect(resp.steps[2].outputs.iterations).toBe(1)
const result = await createAutomationBuilder(config)
.onAppAction()
.loop({
option: LoopStepType.ARRAY,
binding: "{{ 1 }}",
})
.serverLog({ text: "log statement" })
.test({ fields: {} })

expect(result.steps[0].outputs.iterations).toBe(1)
Comment on lines +34 to +72
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be identical to their runLoop counterparts, but using the new builder instead of runLoop.

})

it("should run an automation with a trigger, loop, and create row step", async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,33 @@ describe("app action trigger", () => {
})
)
})

it("should correct coerce values based on the schema", async () => {
const { automation } = await createAutomationBuilder(config)
.onAppAction({
fields: { text: "string", number: "number", boolean: "boolean" },
})
.serverLog({
text: "{{ fields.text }} {{ fields.number }} {{ fields.boolean }}",
})
.save()

await config.api.application.publish()

const jobs = await captureAutomationResults(automation, async () => {
await config.withProdApp(async () => {
await config.api.automation.trigger(automation._id!, {
fields: { text: "1", number: "2", boolean: "true" },
timeout: 1000,
})
})
})

expect(jobs).toHaveLength(1)
expect(jobs[0].data.event.fields).toEqual({
text: "1",
number: 2,
boolean: true,
})
})
Comment on lines +46 to +73
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is new to cover a test that was happening in automation.spec.ts

})
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,13 @@ class StepBuilder<
TStep extends AutomationTriggerStepId
> extends BranchStepBuilder<TStep> {
private readonly config: TestConfiguration
private readonly trigger: AutomationTrigger
private readonly _trigger: AutomationTrigger
private _name: string | undefined = undefined

constructor(config: TestConfiguration, trigger: AutomationTrigger) {
super()
this.config = config
this.trigger = trigger
this._trigger = trigger
}

name(n: string): this {
Expand All @@ -165,7 +165,7 @@ class StepBuilder<
name,
definition: {
steps: this.steps,
trigger: this.trigger,
trigger: this._trigger,
stepNames: this.stepNames,
},
type: "automation",
Expand All @@ -182,6 +182,13 @@ class StepBuilder<
const runner = await this.save()
return await runner.test(triggerOutput)
}

async trigger(
request: TriggerAutomationRequest
): Promise<TriggerAutomationResponse> {
const runner = await this.save()
return await runner.trigger(request)
}
}

class AutomationRunner<TStep extends AutomationTriggerStepId> {
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/sdk/automations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface AutomationDataEvent {
oldRow?: Row
user?: UserBindings
timestamp?: number
fields?: Record<string, any>
}

export interface AutomationData {
Expand Down
Loading