Rename automation result helper.

This commit is contained in:
Sam Rose 2025-02-10 16:15:53 +00:00
parent 3a956ed309
commit e16c3fc7a9
No known key found for this signature in database
3 changed files with 27 additions and 12 deletions

View File

@ -1,6 +1,6 @@
import { createAutomationBuilder } from "../utilities/AutomationTestBuilder" import { createAutomationBuilder } from "../utilities/AutomationTestBuilder"
import TestConfiguration from "../../../tests/utilities/TestConfiguration" import TestConfiguration from "../../../tests/utilities/TestConfiguration"
import { captureAutomationRuns } from "../utilities" import { captureAutomationResults } from "../utilities"
describe("cron trigger", () => { describe("cron trigger", () => {
const config = new TestConfiguration() const config = new TestConfiguration()
@ -14,14 +14,14 @@ describe("cron trigger", () => {
}) })
it("should queue a Bull cron job", async () => { it("should queue a Bull cron job", async () => {
await createAutomationBuilder(config) const { automation } = await createAutomationBuilder(config)
.onCron({ cron: "* * * * *" }) .onCron({ cron: "* * * * *" })
.serverLog({ .serverLog({
text: "Hello, world!", text: "Hello, world!",
}) })
.save() .save()
const jobs = await captureAutomationRuns(() => const jobs = await captureAutomationResults(automation, () =>
config.api.application.publish() config.api.application.publish()
) )
expect(jobs).toHaveLength(1) expect(jobs).toHaveLength(1)

View File

@ -1,20 +1,22 @@
import { createAutomationBuilder } from "../utilities/AutomationTestBuilder" import { createAutomationBuilder } from "../utilities/AutomationTestBuilder"
import TestConfiguration from "../../../tests/utilities/TestConfiguration" import TestConfiguration from "../../../tests/utilities/TestConfiguration"
import { Table } from "@budibase/types" import { Automation, Table } from "@budibase/types"
import { basicTable } from "../../../tests/utilities/structures" import { basicTable } from "../../../tests/utilities/structures"
import { captureAutomationRuns } from "../utilities" import { captureAutomationResults } from "../utilities"
describe("row saved trigger", () => { describe("row saved trigger", () => {
const config = new TestConfiguration() const config = new TestConfiguration()
let table: Table let table: Table
let automation: Automation
beforeAll(async () => { beforeAll(async () => {
await config.init() await config.init()
table = await config.api.table.save(basicTable()) table = await config.api.table.save(basicTable())
await createAutomationBuilder(config) automation = await createAutomationBuilder(config)
.onRowSaved({ tableId: table._id! }) .onRowSaved({ tableId: table._id! })
.serverLog({ text: "Row created!" }) .serverLog({ text: "Row created!" })
.save() .save()
.then(({ automation }) => automation)
await config.api.application.publish() await config.api.application.publish()
}) })
@ -24,12 +26,12 @@ describe("row saved trigger", () => {
}) })
it("should queue a Bull job when a row is created", async () => { it("should queue a Bull job when a row is created", async () => {
const jobs = await captureAutomationRuns(() => const results = await captureAutomationResults(automation, () =>
config.withProdApp(() => config.api.row.save(table._id!, { name: "foo" })) config.withProdApp(() => config.api.row.save(table._id!, { name: "foo" }))
) )
expect(jobs).toHaveLength(1) expect(results).toHaveLength(1)
expect(jobs[0].data.event).toEqual( expect(results[0].data.event).toEqual(
expect.objectContaining({ expect.objectContaining({
tableId: table._id!, tableId: table._id!,
row: expect.objectContaining({ name: "foo" }), row: expect.objectContaining({ name: "foo" }),
@ -41,12 +43,12 @@ describe("row saved trigger", () => {
const otherTable = await config.api.table.save(basicTable()) const otherTable = await config.api.table.save(basicTable())
await config.api.application.publish() await config.api.application.publish()
const jobs = await captureAutomationRuns(() => const results = await captureAutomationResults(automation, () =>
config.withProdApp(() => config.withProdApp(() =>
config.api.row.save(otherTable._id!, { name: "foo" }) config.api.row.save(otherTable._id!, { name: "foo" })
) )
) )
expect(jobs).toBeEmpty() expect(results).toBeEmpty()
}) })
}) })

View File

@ -4,6 +4,7 @@ import { BUILTIN_ACTION_DEFINITIONS, getAction } from "../../actions"
import emitter from "../../../events/index" import emitter from "../../../events/index"
import env from "../../../environment" import env from "../../../environment"
import { import {
Automation,
AutomationActionStepId, AutomationActionStepId,
AutomationData, AutomationData,
Datasource, Datasource,
@ -74,7 +75,7 @@ export async function runStep(
* Capture all automation runs that occur during the execution of a function. * Capture all automation runs that occur during the execution of a function.
* This function will wait for all messages to be processed before returning. * This function will wait for all messages to be processed before returning.
*/ */
export async function captureAutomationRuns( export async function captureAllAutomationResults(
f: () => Promise<unknown> f: () => Promise<unknown>
): Promise<Job<AutomationData>[]> { ): Promise<Job<AutomationData>[]> {
const runs: Job<AutomationData>[] = [] const runs: Job<AutomationData>[] = []
@ -108,6 +109,18 @@ export async function captureAutomationRuns(
return runs return runs
} }
export async function captureAutomationResults(
automation: Automation | string,
f: () => Promise<unknown>
) {
const results = await captureAllAutomationResults(f)
return results.filter(
r =>
r.data.automation._id ===
(typeof automation === "string" ? automation : automation._id)
)
}
export async function saveTestQuery( export async function saveTestQuery(
config: TestConfiguration, config: TestConfiguration,
client: Knex, client: Knex,