Fix scienarios.spec.ts

This commit is contained in:
Sam Rose 2024-11-06 17:13:29 +00:00
parent 6801ade481
commit 50ce97135c
No known key found for this signature in database
6 changed files with 4289 additions and 4294 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,65 +1,64 @@
import { Datasource, Query } from "@budibase/types" import { Datasource, Query } from "@budibase/types"
import * as setup from "./utilities" import * as setup from "./utilities"
import { DatabaseName } from "../../integrations/tests/utils" import {
DatabaseName,
datasourceDescribe,
} from "../../integrations/tests/utils"
import { Knex } from "knex" import { Knex } from "knex"
import { generator } from "@budibase/backend-core/tests"
describe.each([ datasourceDescribe(
DatabaseName.POSTGRES, { name: "execute query action", exclude: [DatabaseName.MONGODB] },
DatabaseName.MYSQL, ({ config, dsProvider }) => {
DatabaseName.SQL_SERVER, let tableName: string
DatabaseName.MARIADB, let client: Knex
DatabaseName.ORACLE, let datasource: Datasource
])("execute query action (%s)", name => { let query: Query
let tableName: string
let client: Knex
let datasource: Datasource
let query: Query
const config = setup.getConfig()
beforeAll(async () => { beforeAll(async () => {
await config.init() const ds = await dsProvider()
datasource = ds.datasource!
const testSetup = await setup.setupTestDatasource(config, name) client = ds.client!
datasource = testSetup.datasource
client = testSetup.client
})
beforeEach(async () => {
tableName = await setup.createTestTable(client, {
a: { type: "string" },
b: { type: "number" },
}) })
await setup.insertTestData(client, tableName, [{ a: "string", b: 1 }])
query = await setup.saveTestQuery(config, client, tableName, datasource)
})
afterEach(async () => { beforeEach(async () => {
await client.schema.dropTable(tableName) tableName = generator.guid()
}) await client.schema.createTable(tableName, table => {
table.string("a")
afterAll(setup.afterAll) table.integer("b")
})
it("should be able to execute a query", async () => { await client(tableName).insert({ a: "string", b: 1 })
let res = await setup.runStep(setup.actions.EXECUTE_QUERY.stepId, { query = await setup.saveTestQuery(config, client, tableName, datasource)
query: { queryId: query._id },
}) })
expect(res.response).toEqual([{ a: "string", b: 1 }])
expect(res.success).toEqual(true)
})
it("should handle a null query value", async () => { afterEach(async () => {
let res = await setup.runStep(setup.actions.EXECUTE_QUERY.stepId, { await client.schema.dropTable(tableName)
query: null,
}) })
expect(res.response.message).toEqual("Invalid inputs")
expect(res.success).toEqual(false)
})
it("should handle an error executing a query", async () => { afterAll(setup.afterAll)
let res = await setup.runStep(setup.actions.EXECUTE_QUERY.stepId, {
query: { queryId: "wrong_id" }, it("should be able to execute a query", async () => {
let res = await setup.runStep(setup.actions.EXECUTE_QUERY.stepId, {
query: { queryId: query._id },
})
expect(res.response).toEqual([{ a: "string", b: 1 }])
expect(res.success).toEqual(true)
}) })
expect(res.response).toBeDefined()
expect(res.success).toEqual(false) it("should handle a null query value", async () => {
}) let res = await setup.runStep(setup.actions.EXECUTE_QUERY.stepId, {
}) query: null,
})
expect(res.response.message).toEqual("Invalid inputs")
expect(res.success).toEqual(false)
})
it("should handle an error executing a query", async () => {
let res = await setup.runStep(setup.actions.EXECUTE_QUERY.stepId, {
query: { queryId: "wrong_id" },
})
expect(res.response).toBeDefined()
expect(res.success).toEqual(false)
})
}
)

View File

@ -1,9 +1,14 @@
import * as automation from "../../index" import * as automation from "../../index"
import * as setup from "../utilities" import * as setup from "../utilities"
import { LoopStepType, FieldType, Table } from "@budibase/types" import { LoopStepType, FieldType, Table, Datasource } from "@budibase/types"
import { createAutomationBuilder } from "../utilities/AutomationTestBuilder" import { createAutomationBuilder } from "../utilities/AutomationTestBuilder"
import { DatabaseName } from "../../../integrations/tests/utils" import {
DatabaseName,
datasourceDescribe,
} from "../../../integrations/tests/utils"
import { FilterConditions } from "../../../automations/steps/filter" import { FilterConditions } from "../../../automations/steps/filter"
import { Knex } from "knex"
import { generator } from "@budibase/backend-core/tests"
describe("Automation Scenarios", () => { describe("Automation Scenarios", () => {
let config = setup.getConfig() let config = setup.getConfig()
@ -107,96 +112,6 @@ describe("Automation Scenarios", () => {
expect(results.steps[2].outputs.rows).toHaveLength(1) expect(results.steps[2].outputs.rows).toHaveLength(1)
}) })
it("should query an external database for some data then insert than into an internal table", async () => {
const { datasource, client } = await setup.setupTestDatasource(
config,
DatabaseName.MYSQL
)
const newTable = await config.createTable({
name: "table",
type: "table",
schema: {
name: {
name: "name",
type: FieldType.STRING,
constraints: {
presence: true,
},
},
age: {
name: "age",
type: FieldType.NUMBER,
constraints: {
presence: true,
},
},
},
})
const tableName = await setup.createTestTable(client, {
name: { type: "string" },
age: { type: "number" },
})
const rows = [
{ name: "Joe", age: 20 },
{ name: "Bob", age: 25 },
{ name: "Paul", age: 30 },
]
await setup.insertTestData(client, tableName, rows)
const query = await setup.saveTestQuery(
config,
client,
tableName,
datasource
)
const builder = createAutomationBuilder({
name: "Test external query and save",
})
const results = await builder
.appAction({
fields: {},
})
.executeQuery({
query: {
queryId: query._id!,
},
})
.loop({
option: LoopStepType.ARRAY,
binding: "{{ steps.1.response }}",
})
.createRow({
row: {
name: "{{ loop.currentItem.name }}",
age: "{{ loop.currentItem.age }}",
tableId: newTable._id!,
},
})
.queryRows({
tableId: newTable._id!,
})
.run()
expect(results.steps).toHaveLength(3)
expect(results.steps[1].outputs.iterations).toBe(3)
expect(results.steps[1].outputs.items).toHaveLength(3)
expect(results.steps[2].outputs.rows).toHaveLength(3)
rows.forEach(expectedRow => {
expect(results.steps[2].outputs.rows).toEqual(
expect.arrayContaining([expect.objectContaining(expectedRow)])
)
})
})
it("should trigger an automation which creates and then updates a row", async () => { it("should trigger an automation which creates and then updates a row", async () => {
const table = await config.createTable({ const table = await config.createTable({
name: "TestTable", name: "TestTable",
@ -517,3 +432,104 @@ describe("Automation Scenarios", () => {
expect(results.steps[0].outputs.message).toContain("example.com") expect(results.steps[0].outputs.message).toContain("example.com")
}) })
}) })
datasourceDescribe(
{ name: "", only: [DatabaseName.MYSQL] },
({ config, dsProvider }) => {
let datasource: Datasource
let client: Knex
beforeAll(async () => {
const ds = await dsProvider()
datasource = ds.datasource!
client = ds.client!
})
it("should query an external database for some data then insert than into an internal table", async () => {
const newTable = await config.createTable({
name: "table",
type: "table",
schema: {
name: {
name: "name",
type: FieldType.STRING,
constraints: {
presence: true,
},
},
age: {
name: "age",
type: FieldType.NUMBER,
constraints: {
presence: true,
},
},
},
})
const tableName = generator.guid()
await client.schema.createTable(tableName, table => {
table.string("name")
table.integer("age")
})
const rows = [
{ name: "Joe", age: 20 },
{ name: "Bob", age: 25 },
{ name: "Paul", age: 30 },
]
await client(tableName).insert(rows)
const query = await setup.saveTestQuery(
config,
client,
tableName,
datasource
)
const builder = createAutomationBuilder({
name: "Test external query and save",
config,
})
const results = await builder
.appAction({
fields: {},
})
.executeQuery({
query: {
queryId: query._id!,
},
})
.loop({
option: LoopStepType.ARRAY,
binding: "{{ steps.1.response }}",
})
.createRow({
row: {
name: "{{ loop.currentItem.name }}",
age: "{{ loop.currentItem.age }}",
tableId: newTable._id!,
},
})
.queryRows({
tableId: newTable._id!,
})
.run()
expect(results.steps).toHaveLength(3)
expect(results.steps[1].outputs.iterations).toBe(3)
expect(results.steps[1].outputs.items).toHaveLength(3)
expect(results.steps[2].outputs.rows).toHaveLength(3)
rows.forEach(expectedRow => {
expect(results.steps[2].outputs.rows).toEqual(
expect.arrayContaining([expect.objectContaining(expectedRow)])
)
})
})
}
)

View File

@ -5,12 +5,6 @@ import emitter from "../../../events/index"
import env from "../../../environment" import env from "../../../environment"
import { AutomationActionStepId, Datasource } from "@budibase/types" import { AutomationActionStepId, Datasource } from "@budibase/types"
import { Knex } from "knex" import { Knex } from "knex"
import { generator } from "@budibase/backend-core/tests"
import {
getDatasource,
knexClient,
DatabaseName,
} from "../../../integrations/tests/utils"
let config: TestConfig let config: TestConfig
@ -64,29 +58,6 @@ export async function runStep(stepId: string, inputs: any, stepContext?: any) {
} }
} }
export async function createTestTable(client: Knex, schema: any) {
const tableName = generator.guid()
await client.schema.createTable(tableName, table => {
for (const fieldName in schema) {
const field = schema[fieldName]
if (field.type === "string") {
table.string(fieldName)
} else if (field.type === "number") {
table.integer(fieldName)
}
}
})
return tableName
}
export async function insertTestData(
client: Knex,
tableName: string,
rows: any[]
) {
await client(tableName).insert(rows)
}
export async function saveTestQuery( export async function saveTestQuery(
config: TestConfig, config: TestConfig,
client: Knex, client: Knex,
@ -107,15 +78,5 @@ export async function saveTestQuery(
}) })
} }
export async function setupTestDatasource(
config: TestConfig,
dbName: DatabaseName
) {
const db = await getDatasource(dbName)
const datasource = await config.api.datasource.create(db)
const client = await knexClient(db)
return { datasource, client }
}
export const apiKey = "test" export const apiKey = "test"
export const actions = BUILTIN_ACTION_DEFINITIONS export const actions = BUILTIN_ACTION_DEFINITIONS

View File

@ -135,7 +135,7 @@ export function datasourceDescribe(
}) })
} }
export function getDatasource( function getDatasource(
sourceName: DatabaseName sourceName: DatabaseName
): Promise<Datasource | undefined> { ): Promise<Datasource | undefined> {
return providers[sourceName]() return providers[sourceName]()

View File

@ -1,13 +1,18 @@
// Generated with:
// cd packages/server
// rg -l "datasourceDescribe" --glob "*.spec.ts"
export const DATASOURCE_TEST_FILES = [ export const DATASOURCE_TEST_FILES = [
"src/automations/tests/executeQuery.spec.ts",
"src/integration-test/mysql.spec.ts", "src/integration-test/mysql.spec.ts",
"src/integration-test/postgres.spec.ts", "src/integration-test/postgres.spec.ts",
"src/api/routes/tests/rowAction.spec.ts",
"src/automations/tests/scenarios/scenarios.spec.ts",
"src/api/routes/tests/queries/generic-sql.spec.ts", "src/api/routes/tests/queries/generic-sql.spec.ts",
"src/sdk/app/rows/search/tests/search.spec.ts", "src/api/routes/tests/table.spec.ts",
"src/api/routes/tests/queries/mongodb.spec.ts", "src/api/routes/tests/queries/mongodb.spec.ts",
"src/api/routes/tests/search.spec.ts",
"src/api/routes/tests/datasource.spec.ts", "src/api/routes/tests/datasource.spec.ts",
"src/api/routes/tests/viewV2.spec.ts", "src/api/routes/tests/viewV2.spec.ts",
"src/api/routes/tests/row.spec.ts", "src/api/routes/tests/row.spec.ts",
"src/api/routes/tests/rowAction.spec.ts", "src/api/routes/tests/search.spec.ts",
"src/api/routes/tests/table.spec.ts", "src/sdk/app/rows/search/tests/search.spec.ts",
] ]