Fix scienarios.spec.ts
This commit is contained in:
parent
6801ade481
commit
50ce97135c
File diff suppressed because it is too large
Load Diff
|
@ -1,65 +1,64 @@
|
|||
import { Datasource, Query } from "@budibase/types"
|
||||
import * as setup from "./utilities"
|
||||
import { DatabaseName } from "../../integrations/tests/utils"
|
||||
import {
|
||||
DatabaseName,
|
||||
datasourceDescribe,
|
||||
} from "../../integrations/tests/utils"
|
||||
import { Knex } from "knex"
|
||||
import { generator } from "@budibase/backend-core/tests"
|
||||
|
||||
describe.each([
|
||||
DatabaseName.POSTGRES,
|
||||
DatabaseName.MYSQL,
|
||||
DatabaseName.SQL_SERVER,
|
||||
DatabaseName.MARIADB,
|
||||
DatabaseName.ORACLE,
|
||||
])("execute query action (%s)", name => {
|
||||
let tableName: string
|
||||
let client: Knex
|
||||
let datasource: Datasource
|
||||
let query: Query
|
||||
const config = setup.getConfig()
|
||||
datasourceDescribe(
|
||||
{ name: "execute query action", exclude: [DatabaseName.MONGODB] },
|
||||
({ config, dsProvider }) => {
|
||||
let tableName: string
|
||||
let client: Knex
|
||||
let datasource: Datasource
|
||||
let query: Query
|
||||
|
||||
beforeAll(async () => {
|
||||
await config.init()
|
||||
|
||||
const testSetup = await setup.setupTestDatasource(config, name)
|
||||
datasource = testSetup.datasource
|
||||
client = testSetup.client
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
tableName = await setup.createTestTable(client, {
|
||||
a: { type: "string" },
|
||||
b: { type: "number" },
|
||||
beforeAll(async () => {
|
||||
const ds = await dsProvider()
|
||||
datasource = ds.datasource!
|
||||
client = ds.client!
|
||||
})
|
||||
await setup.insertTestData(client, tableName, [{ a: "string", b: 1 }])
|
||||
query = await setup.saveTestQuery(config, client, tableName, datasource)
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await client.schema.dropTable(tableName)
|
||||
})
|
||||
|
||||
afterAll(setup.afterAll)
|
||||
|
||||
it("should be able to execute a query", async () => {
|
||||
let res = await setup.runStep(setup.actions.EXECUTE_QUERY.stepId, {
|
||||
query: { queryId: query._id },
|
||||
beforeEach(async () => {
|
||||
tableName = generator.guid()
|
||||
await client.schema.createTable(tableName, table => {
|
||||
table.string("a")
|
||||
table.integer("b")
|
||||
})
|
||||
await client(tableName).insert({ a: "string", b: 1 })
|
||||
query = await setup.saveTestQuery(config, client, tableName, datasource)
|
||||
})
|
||||
expect(res.response).toEqual([{ a: "string", b: 1 }])
|
||||
expect(res.success).toEqual(true)
|
||||
})
|
||||
|
||||
it("should handle a null query value", async () => {
|
||||
let res = await setup.runStep(setup.actions.EXECUTE_QUERY.stepId, {
|
||||
query: null,
|
||||
afterEach(async () => {
|
||||
await client.schema.dropTable(tableName)
|
||||
})
|
||||
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" },
|
||||
afterAll(setup.afterAll)
|
||||
|
||||
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)
|
||||
})
|
||||
}
|
||||
)
|
||||
|
|
|
@ -1,9 +1,14 @@
|
|||
import * as automation from "../../index"
|
||||
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 { DatabaseName } from "../../../integrations/tests/utils"
|
||||
import {
|
||||
DatabaseName,
|
||||
datasourceDescribe,
|
||||
} from "../../../integrations/tests/utils"
|
||||
import { FilterConditions } from "../../../automations/steps/filter"
|
||||
import { Knex } from "knex"
|
||||
import { generator } from "@budibase/backend-core/tests"
|
||||
|
||||
describe("Automation Scenarios", () => {
|
||||
let config = setup.getConfig()
|
||||
|
@ -107,96 +112,6 @@ describe("Automation Scenarios", () => {
|
|||
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 () => {
|
||||
const table = await config.createTable({
|
||||
name: "TestTable",
|
||||
|
@ -517,3 +432,104 @@ describe("Automation Scenarios", () => {
|
|||
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)])
|
||||
)
|
||||
})
|
||||
})
|
||||
}
|
||||
)
|
||||
|
|
|
@ -5,12 +5,6 @@ import emitter from "../../../events/index"
|
|||
import env from "../../../environment"
|
||||
import { AutomationActionStepId, Datasource } from "@budibase/types"
|
||||
import { Knex } from "knex"
|
||||
import { generator } from "@budibase/backend-core/tests"
|
||||
import {
|
||||
getDatasource,
|
||||
knexClient,
|
||||
DatabaseName,
|
||||
} from "../../../integrations/tests/utils"
|
||||
|
||||
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(
|
||||
config: TestConfig,
|
||||
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 actions = BUILTIN_ACTION_DEFINITIONS
|
||||
|
|
|
@ -135,7 +135,7 @@ export function datasourceDescribe(
|
|||
})
|
||||
}
|
||||
|
||||
export function getDatasource(
|
||||
function getDatasource(
|
||||
sourceName: DatabaseName
|
||||
): Promise<Datasource | undefined> {
|
||||
return providers[sourceName]()
|
||||
|
|
|
@ -1,13 +1,18 @@
|
|||
// Generated with:
|
||||
// cd packages/server
|
||||
// rg -l "datasourceDescribe" --glob "*.spec.ts"
|
||||
export const DATASOURCE_TEST_FILES = [
|
||||
"src/automations/tests/executeQuery.spec.ts",
|
||||
"src/integration-test/mysql.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/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/search.spec.ts",
|
||||
"src/api/routes/tests/datasource.spec.ts",
|
||||
"src/api/routes/tests/viewV2.spec.ts",
|
||||
"src/api/routes/tests/row.spec.ts",
|
||||
"src/api/routes/tests/rowAction.spec.ts",
|
||||
"src/api/routes/tests/table.spec.ts",
|
||||
"src/api/routes/tests/search.spec.ts",
|
||||
"src/sdk/app/rows/search/tests/search.spec.ts",
|
||||
]
|
||||
|
|
Loading…
Reference in New Issue