Merge pull request #15073 from Budibase/feat/update-automation-tests

Update automation tests to use the new test builder
This commit is contained in:
Sam Rose 2024-11-27 10:01:05 +00:00 committed by GitHub
commit 79515d42d1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 303 additions and 117 deletions

View File

@ -1,6 +1,7 @@
import * as setup from "./utilities" import * as setup from "./utilities"
import { basicTableWithAttachmentField } from "../../tests/utilities/structures" import { basicTableWithAttachmentField } from "../../tests/utilities/structures"
import { objectStore } from "@budibase/backend-core" import { objectStore } from "@budibase/backend-core"
import { createAutomationBuilder } from "./utilities/AutomationTestBuilder"
async function uploadTestFile(filename: string) { async function uploadTestFile(filename: string) {
let bucket = "testbucket" let bucket = "testbucket"
@ -13,6 +14,7 @@ async function uploadTestFile(filename: string) {
return presignedUrl return presignedUrl
} }
describe("test the create row action", () => { describe("test the create row action", () => {
let table: any let table: any
let row: any let row: any
@ -31,30 +33,78 @@ describe("test the create row action", () => {
afterAll(setup.afterAll) afterAll(setup.afterAll)
it("should be able to run the action", async () => { it("should be able to run the action", async () => {
const res = await setup.runStep(config, setup.actions.CREATE_ROW.stepId, { const result = await createAutomationBuilder({
row, name: "Test Create Row Flow",
appId: config.getAppId(),
config,
}) })
expect(res.id).toBeDefined() .appAction({ fields: { status: "new" } })
expect(res.revision).toBeDefined() .serverLog({ text: "Starting create row flow" }, { stepName: "StartLog" })
expect(res.success).toEqual(true) .createRow({ row }, { stepName: "CreateRow" })
const gottenRow = await config.api.row.get(table._id, res.id) .serverLog(
{ text: "Row created with ID: {{ stepsByName.CreateRow.row._id }}" },
{ stepName: "CreationLog" }
)
.run()
expect(result.steps[1].outputs.success).toBeDefined()
expect(result.steps[1].outputs.id).toBeDefined()
expect(result.steps[1].outputs.revision).toBeDefined()
const gottenRow = await config.api.row.get(
table._id,
result.steps[1].outputs.id
)
expect(gottenRow.name).toEqual("test") expect(gottenRow.name).toEqual("test")
expect(gottenRow.description).toEqual("test") expect(gottenRow.description).toEqual("test")
expect(result.steps[2].outputs.message).toContain(
"Row created with ID: " + result.steps[1].outputs.id
)
}) })
it("should return an error (not throw) when bad info provided", async () => { it("should return an error (not throw) when bad info provided", async () => {
const res = await setup.runStep(config, setup.actions.CREATE_ROW.stepId, { const result = await createAutomationBuilder({
row: { name: "Test Create Row Error Flow",
tableId: "invalid", appId: config.getAppId(),
invalid: "invalid", config,
},
}) })
expect(res.success).toEqual(false) .appAction({ fields: { status: "error" } })
.serverLog({ text: "Starting error test flow" }, { stepName: "StartLog" })
.createRow(
{
row: {
tableId: "invalid",
invalid: "invalid",
},
},
{ stepName: "CreateRow" }
)
.run()
expect(result.steps[1].outputs.success).toEqual(false)
}) })
it("should check invalid inputs return an error", async () => { it("should check invalid inputs return an error", async () => {
const res = await setup.runStep(config, setup.actions.CREATE_ROW.stepId, {}) const result = await createAutomationBuilder({
expect(res.success).toEqual(false) name: "Test Create Row Invalid Flow",
appId: config.getAppId(),
config,
})
.appAction({ fields: { status: "invalid" } })
.serverLog({ text: "Testing invalid input" }, { stepName: "StartLog" })
.createRow({ row: {} }, { stepName: "CreateRow" })
.filter({
field: "{{ stepsByName.CreateRow.success }}",
condition: "equal",
value: true,
})
.serverLog(
{ text: "This log should not appear" },
{ stepName: "SkippedLog" }
)
.run()
expect(result.steps[1].outputs.success).toEqual(false)
expect(result.steps.length).toBeLessThan(4)
}) })
it("should check that an attachment field is sent to storage and parsed", async () => { it("should check that an attachment field is sent to storage and parsed", async () => {
@ -76,13 +126,33 @@ describe("test the create row action", () => {
] ]
attachmentRow.file_attachment = attachmentObject attachmentRow.file_attachment = attachmentObject
const res = await setup.runStep(config, setup.actions.CREATE_ROW.stepId, { const result = await createAutomationBuilder({
row: attachmentRow, name: "Test Create Row Attachment Flow",
appId: config.getAppId(),
config,
}) })
.appAction({ fields: { type: "attachment" } })
.serverLog(
{ text: "Processing attachment upload" },
{ stepName: "StartLog" }
)
.createRow({ row: attachmentRow }, { stepName: "CreateRow" })
.filter({
field: "{{ stepsByName.CreateRow.success }}",
condition: "equal",
value: true,
})
.serverLog(
{
text: "Attachment uploaded with key: {{ stepsByName.CreateRow.row.file_attachment.0.key }}",
},
{ stepName: "UploadLog" }
)
.run()
expect(res.success).toEqual(true) expect(result.steps[1].outputs.success).toEqual(true)
expect(res.row.file_attachment[0]).toHaveProperty("key") expect(result.steps[1].outputs.row.file_attachment[0]).toHaveProperty("key")
let s3Key = res.row.file_attachment[0].key let s3Key = result.steps[1].outputs.row.file_attachment[0].key
const client = objectStore.ObjectStore(objectStore.ObjectStoreBuckets.APPS) const client = objectStore.ObjectStore(objectStore.ObjectStoreBuckets.APPS)
@ -111,13 +181,53 @@ describe("test the create row action", () => {
} }
attachmentRow.single_file_attachment = attachmentObject attachmentRow.single_file_attachment = attachmentObject
const res = await setup.runStep(config, setup.actions.CREATE_ROW.stepId, { const result = await createAutomationBuilder({
row: attachmentRow, name: "Test Create Row Single Attachment Flow",
appId: config.getAppId(),
config,
}) })
.appAction({ fields: { type: "single-attachment" } })
.serverLog(
{ text: "Processing single attachment" },
{ stepName: "StartLog" }
)
.createRow({ row: attachmentRow }, { stepName: "CreateRow" })
.branch({
success: {
steps: stepBuilder =>
stepBuilder
.serverLog(
{ text: "Single attachment processed" },
{ stepName: "ProcessLog" }
)
.serverLog(
{
text: "File key: {{ stepsByName.CreateRow.row.single_file_attachment.key }}",
},
{ stepName: "KeyLog" }
),
condition: {
equal: { "{{ stepsByName.CreateRow.success }}": true },
},
},
error: {
steps: stepBuilder =>
stepBuilder.serverLog(
{ text: "Failed to process attachment" },
{ stepName: "ErrorLog" }
),
condition: {
equal: { "{{ stepsByName.CreateRow.success }}": false },
},
},
})
.run()
expect(res.success).toEqual(true) expect(result.steps[1].outputs.success).toEqual(true)
expect(res.row.single_file_attachment).toHaveProperty("key") expect(result.steps[1].outputs.row.single_file_attachment).toHaveProperty(
let s3Key = res.row.single_file_attachment.key "key"
)
let s3Key = result.steps[1].outputs.row.single_file_attachment.key
const client = objectStore.ObjectStore(objectStore.ObjectStoreBuckets.APPS) const client = objectStore.ObjectStore(objectStore.ObjectStoreBuckets.APPS)
@ -146,13 +256,50 @@ describe("test the create row action", () => {
} }
attachmentRow.single_file_attachment = attachmentObject attachmentRow.single_file_attachment = attachmentObject
const res = await setup.runStep(config, setup.actions.CREATE_ROW.stepId, { const result = await createAutomationBuilder({
row: attachmentRow, name: "Test Create Row Invalid Attachment Flow",
appId: config.getAppId(),
config,
}) })
.appAction({ fields: { type: "invalid-attachment" } })
.serverLog(
{ text: "Testing invalid attachment keys" },
{ stepName: "StartLog" }
)
.createRow({ row: attachmentRow }, { stepName: "CreateRow" })
.branch({
success: {
steps: stepBuilder =>
stepBuilder.serverLog(
{ text: "Unexpected success" },
{ stepName: "UnexpectedLog" }
),
condition: {
equal: { "{{ stepsByName.CreateRow.success }}": true },
},
},
error: {
steps: stepBuilder =>
stepBuilder
.serverLog(
{ text: "Expected error occurred" },
{ stepName: "ErrorLog" }
)
.serverLog(
{ text: "Error: {{ stepsByName.CreateRow.response }}" },
{ stepName: "ErrorDetailsLog" }
),
condition: {
equal: { "{{ stepsByName.CreateRow.success }}": false },
},
},
})
.run()
expect(res.success).toEqual(false) expect(result.steps[1].outputs.success).toEqual(false)
expect(res.response).toEqual( expect(result.steps[1].outputs.response).toEqual(
'Error: Attachments must have both "url" and "filename" keys. You have provided: wrongKey, anotherWrongKey' 'Error: Attachments must have both "url" and "filename" keys. You have provided: wrongKey, anotherWrongKey'
) )
expect(result.steps[2].outputs.status).toEqual("No branch condition met")
}) })
}) })

View File

@ -1,52 +1,65 @@
import { createAutomationBuilder } from "./utilities/AutomationTestBuilder"
import * as setup from "./utilities" import * as setup from "./utilities"
describe("test the delete row action", () => { describe("test the delete row action", () => {
let table: any let table: any,
let row: any row: any,
let inputs: any config = setup.getConfig()
let config = setup.getConfig()
beforeEach(async () => { beforeAll(async () => {
await config.init() await config.init()
table = await config.createTable() table = await config.createTable()
row = await config.createRow() row = await config.createRow()
inputs = {
tableId: table._id,
id: row._id,
revision: row._rev,
}
}) })
afterAll(setup.afterAll) afterAll(setup.afterAll)
it("should be able to run the action", async () => { it("should be able to run the delete row action", async () => {
const res = await setup.runStep( const builder = createAutomationBuilder({
config, name: "Delete Row Automation",
setup.actions.DELETE_ROW.stepId, })
inputs
)
expect(res.success).toEqual(true)
expect(res.response).toBeDefined()
expect(res.row._id).toEqual(row._id)
})
it("check usage quota attempts", async () => { await builder
await setup.runInProd(async () => { .appAction({ fields: {} })
await setup.runStep(config, setup.actions.DELETE_ROW.stepId, inputs) .deleteRow({
tableId: table._id,
id: row._id,
revision: row._rev,
})
.run()
await config.api.row.get(table._id, row._id, {
status: 404,
}) })
}) })
it("should check invalid inputs return an error", async () => { it("should check invalid inputs return an error", async () => {
const res = await setup.runStep(config, setup.actions.DELETE_ROW.stepId, {}) const builder = createAutomationBuilder({
expect(res.success).toEqual(false) name: "Invalid Inputs Automation",
})
const results = await builder
.appAction({ fields: {} })
.deleteRow({ tableId: "", id: "", revision: "" })
.run()
expect(results.steps[0].outputs.success).toEqual(false)
}) })
it("should return an error when table doesn't exist", async () => { it("should return an error when table doesn't exist", async () => {
const res = await setup.runStep(config, setup.actions.DELETE_ROW.stepId, { const builder = createAutomationBuilder({
tableId: "invalid", name: "Nonexistent Table Automation",
id: "invalid",
revision: "invalid",
}) })
expect(res.success).toEqual(false)
const results = await builder
.appAction({ fields: {} })
.deleteRow({
tableId: "invalid",
id: "invalid",
revision: "invalid",
})
.run()
expect(results.steps[0].outputs.success).toEqual(false)
}) })
}) })

View File

@ -8,58 +8,83 @@ import {
Table, Table,
TableSourceType, TableSourceType,
} from "@budibase/types" } from "@budibase/types"
import { createAutomationBuilder } from "./utilities/AutomationTestBuilder"
import * as setup from "./utilities" import * as setup from "./utilities"
import * as uuid from "uuid" import * as uuid from "uuid"
describe("test the update row action", () => { describe("test the update row action", () => {
let table: Table, row: Row, inputs: any let table: Table,
let config = setup.getConfig() row: Row,
config = setup.getConfig()
beforeAll(async () => { beforeAll(async () => {
await config.init() await config.init()
table = await config.createTable() table = await config.createTable()
row = await config.createRow() row = await config.createRow()
inputs = {
rowId: row._id,
row: {
...row,
name: "Updated name",
// put a falsy option in to be removed
description: "",
},
}
}) })
afterAll(setup.afterAll) afterAll(setup.afterAll)
it("should be able to run the action", async () => { it("should be able to run the update row action", async () => {
const res = await setup.runStep( const builder = createAutomationBuilder({
config, name: "Update Row Automation",
setup.actions.UPDATE_ROW.stepId, })
inputs
const results = await builder
.appAction({ fields: {} })
.updateRow({
rowId: row._id!,
row: {
...row,
name: "Updated name",
description: "",
},
meta: {},
})
.run()
expect(results.steps[0].outputs.success).toEqual(true)
const updatedRow = await config.api.row.get(
table._id!,
results.steps[0].outputs.id
) )
expect(res.success).toEqual(true)
const updatedRow = await config.api.row.get(table._id!, res.id)
expect(updatedRow.name).toEqual("Updated name") expect(updatedRow.name).toEqual("Updated name")
expect(updatedRow.description).not.toEqual("") expect(updatedRow.description).not.toEqual("")
}) })
it("should check invalid inputs return an error", async () => { it("should check invalid inputs return an error", async () => {
const res = await setup.runStep(config, setup.actions.UPDATE_ROW.stepId, {}) const builder = createAutomationBuilder({
expect(res.success).toEqual(false) name: "Invalid Inputs Automation",
})
const results = await builder
.appAction({ fields: {} })
.updateRow({ meta: {}, row: {}, rowId: "" })
.run()
expect(results.steps[0].outputs.success).toEqual(false)
}) })
it("should return an error when table doesn't exist", async () => { it("should return an error when table doesn't exist", async () => {
const res = await setup.runStep(config, setup.actions.UPDATE_ROW.stepId, { const builder = createAutomationBuilder({
row: { _id: "invalid" }, name: "Nonexistent Table Automation",
rowId: "invalid",
}) })
expect(res.success).toEqual(false)
const results = await builder
.appAction({ fields: {} })
.updateRow({
row: { _id: "invalid" },
rowId: "invalid",
meta: {},
})
.run()
expect(results.steps[0].outputs.success).toEqual(false)
}) })
it("should not overwrite links if those links are not set", async () => { it("should not overwrite links if those links are not set", async () => {
let linkField: FieldSchema = { const linkField: FieldSchema = {
type: FieldType.LINK, type: FieldType.LINK,
name: "", name: "",
fieldName: "", fieldName: "",
@ -71,7 +96,7 @@ describe("test the update row action", () => {
tableId: InternalTable.USER_METADATA, tableId: InternalTable.USER_METADATA,
} }
let table = await config.api.table.save({ const table = await config.api.table.save({
name: uuid.v4(), name: uuid.v4(),
type: "table", type: "table",
sourceType: TableSourceType.INTERNAL, sourceType: TableSourceType.INTERNAL,
@ -82,23 +107,22 @@ describe("test the update row action", () => {
}, },
}) })
let user1 = await config.createUser() const user1 = await config.createUser()
let user2 = await config.createUser() const user2 = await config.createUser()
let row = await config.api.row.save(table._id!, { const row = await config.api.row.save(table._id!, {
user1: [{ _id: user1._id }], user1: [{ _id: user1._id }],
user2: [{ _id: user2._id }], user2: [{ _id: user2._id }],
}) })
let getResp = await config.api.row.get(table._id!, row._id!) const builder = createAutomationBuilder({
expect(getResp.user1[0]._id).toEqual(user1._id) name: "Link Preservation Automation",
expect(getResp.user2[0]._id).toEqual(user2._id) })
let stepResp = await setup.runStep( const results = await builder
config, .appAction({ fields: {} })
setup.actions.UPDATE_ROW.stepId, .updateRow({
{ rowId: row._id!,
rowId: row._id,
row: { row: {
_id: row._id, _id: row._id,
_rev: row._rev, _rev: row._rev,
@ -106,17 +130,19 @@ describe("test the update row action", () => {
user1: [user2._id], user1: [user2._id],
user2: "", user2: "",
}, },
} meta: {},
) })
expect(stepResp.success).toEqual(true) .run()
getResp = await config.api.row.get(table._id!, row._id!) expect(results.steps[0].outputs.success).toEqual(true)
const getResp = await config.api.row.get(table._id!, row._id!)
expect(getResp.user1[0]._id).toEqual(user2._id) expect(getResp.user1[0]._id).toEqual(user2._id)
expect(getResp.user2[0]._id).toEqual(user2._id) expect(getResp.user2[0]._id).toEqual(user2._id)
}) })
it("should overwrite links if those links are not set and we ask it do", async () => { it("should overwrite links if those links are not set and we ask it to", async () => {
let linkField: FieldSchema = { const linkField: FieldSchema = {
type: FieldType.LINK, type: FieldType.LINK,
name: "", name: "",
fieldName: "", fieldName: "",
@ -128,7 +154,7 @@ describe("test the update row action", () => {
tableId: InternalTable.USER_METADATA, tableId: InternalTable.USER_METADATA,
} }
let table = await config.api.table.save({ const table = await config.api.table.save({
name: uuid.v4(), name: uuid.v4(),
type: "table", type: "table",
sourceType: TableSourceType.INTERNAL, sourceType: TableSourceType.INTERNAL,
@ -139,23 +165,22 @@ describe("test the update row action", () => {
}, },
}) })
let user1 = await config.createUser() const user1 = await config.createUser()
let user2 = await config.createUser() const user2 = await config.createUser()
let row = await config.api.row.save(table._id!, { const row = await config.api.row.save(table._id!, {
user1: [{ _id: user1._id }], user1: [{ _id: user1._id }],
user2: [{ _id: user2._id }], user2: [{ _id: user2._id }],
}) })
let getResp = await config.api.row.get(table._id!, row._id!) const builder = createAutomationBuilder({
expect(getResp.user1[0]._id).toEqual(user1._id) name: "Link Overwrite Automation",
expect(getResp.user2[0]._id).toEqual(user2._id) })
let stepResp = await setup.runStep( const results = await builder
config, .appAction({ fields: {} })
setup.actions.UPDATE_ROW.stepId, .updateRow({
{ rowId: row._id!,
rowId: row._id,
row: { row: {
_id: row._id, _id: row._id,
_rev: row._rev, _rev: row._rev,
@ -170,11 +195,12 @@ describe("test the update row action", () => {
}, },
}, },
}, },
} })
) .run()
expect(stepResp.success).toEqual(true)
getResp = await config.api.row.get(table._id!, row._id!) expect(results.steps[0].outputs.success).toEqual(true)
const getResp = await config.api.row.get(table._id!, row._id!)
expect(getResp.user1[0]._id).toEqual(user2._id) expect(getResp.user1[0]._id).toEqual(user2._id)
expect(getResp.user2).toBeUndefined() expect(getResp.user2).toBeUndefined()
}) })