Fix mysql.spec.ts

This commit is contained in:
Sam Rose 2024-03-27 16:31:31 +00:00
parent 385197a5f7
commit 1205cfcbcc
No known key found for this signature in database
2 changed files with 48 additions and 67 deletions

View File

@ -5,9 +5,6 @@ import {
getDatasource, getDatasource,
rawQuery, rawQuery,
} from "../../../../integrations/tests/utils" } from "../../../../integrations/tests/utils"
import pg from "pg"
import mysql from "mysql2/promise"
import mssql from "mssql"
jest.unmock("pg") jest.unmock("pg")

View File

@ -11,8 +11,11 @@ import {
TableRequest, TableRequest,
TableSourceType, TableSourceType,
} from "@budibase/types" } from "@budibase/types"
import { DatabaseName, getDatasource } from "../integrations/tests/utils" import {
import mysql from "mysql2/promise" DatabaseName,
getDatasource,
rawQuery,
} from "../integrations/tests/utils"
import { builderSocket } from "../websockets" import { builderSocket } from "../websockets"
import { generator } from "@budibase/backend-core/tests" import { generator } from "@budibase/backend-core/tests"
// @ts-ignore // @ts-ignore
@ -44,7 +47,8 @@ jest.mock("../websockets", () => ({
describe("mysql integrations", () => { describe("mysql integrations", () => {
let makeRequest: MakeRequestResponse, let makeRequest: MakeRequestResponse,
mysqlDatasource: Datasource, rawDatasource: Datasource,
datasource: Datasource,
primaryMySqlTable: Table primaryMySqlTable: Table
beforeAll(async () => { beforeAll(async () => {
@ -53,9 +57,8 @@ describe("mysql integrations", () => {
makeRequest = generateMakeRequest(apiKey, true) makeRequest = generateMakeRequest(apiKey, true)
mysqlDatasource = await config.api.datasource.create( rawDatasource = await getDatasource(DatabaseName.MYSQL)
await getDatasource(DatabaseName.MYSQL) datasource = await config.api.datasource.create(rawDatasource)
)
}) })
beforeEach(async () => { beforeEach(async () => {
@ -82,7 +85,7 @@ describe("mysql integrations", () => {
type: FieldType.NUMBER, type: FieldType.NUMBER,
}, },
}, },
sourceId: mysqlDatasource._id, sourceId: datasource._id,
sourceType: TableSourceType.EXTERNAL, sourceType: TableSourceType.EXTERNAL,
}) })
}) })
@ -90,18 +93,15 @@ describe("mysql integrations", () => {
afterAll(config.end) afterAll(config.end)
it("validate table schema", async () => { it("validate table schema", async () => {
const res = await makeRequest( const res = await makeRequest("get", `/api/datasources/${datasource._id}`)
"get",
`/api/datasources/${mysqlDatasource._id}`
)
expect(res.status).toBe(200) expect(res.status).toBe(200)
expect(res.body).toEqual({ expect(res.body).toEqual({
config: { config: {
database: "mysql", database: expect.any(String),
host: mysqlDatasource.config!.host, host: datasource.config!.host,
password: "--secret-value--", password: "--secret-value--",
port: mysqlDatasource.config!.port, port: datasource.config!.port,
user: "root", user: "root",
}, },
plus: true, plus: true,
@ -120,7 +120,7 @@ describe("mysql integrations", () => {
it("should be able to verify the connection", async () => { it("should be able to verify the connection", async () => {
await config.api.datasource.verify( await config.api.datasource.verify(
{ {
datasource: await getDatasource(DatabaseName.MYSQL), datasource: rawDatasource,
}, },
{ {
body: { body: {
@ -131,13 +131,12 @@ describe("mysql integrations", () => {
}) })
it("should state an invalid datasource cannot connect", async () => { it("should state an invalid datasource cannot connect", async () => {
const dbConfig = await getDatasource(DatabaseName.MYSQL)
await config.api.datasource.verify( await config.api.datasource.verify(
{ {
datasource: { datasource: {
...dbConfig, ...rawDatasource,
config: { config: {
...dbConfig.config, ...rawDatasource.config,
password: "wrongpassword", password: "wrongpassword",
}, },
}, },
@ -157,7 +156,7 @@ describe("mysql integrations", () => {
it("should fetch information about mysql datasource", async () => { it("should fetch information about mysql datasource", async () => {
const primaryName = primaryMySqlTable.name const primaryName = primaryMySqlTable.name
const response = await makeRequest("post", "/api/datasources/info", { const response = await makeRequest("post", "/api/datasources/info", {
datasource: mysqlDatasource, datasource: datasource,
}) })
expect(response.status).toBe(200) expect(response.status).toBe(200)
expect(response.body.tableNames).toBeDefined() expect(response.body.tableNames).toBeDefined()
@ -166,40 +165,38 @@ describe("mysql integrations", () => {
}) })
describe("Integration compatibility with mysql search_path", () => { describe("Integration compatibility with mysql search_path", () => {
let client: mysql.Connection, pathDatasource: Datasource let datasource: Datasource, rawDatasource: Datasource
const database = "test1" const database = generator.guid()
const database2 = "test-2" const database2 = generator.guid()
beforeAll(async () => { beforeAll(async () => {
const dsConfig = await getDatasource(DatabaseName.MYSQL) rawDatasource = await getDatasource(DatabaseName.MYSQL)
const dbConfig = dsConfig.config!
client = await mysql.createConnection(dbConfig) await rawQuery(rawDatasource, `CREATE DATABASE \`${database}\`;`)
await client.query(`CREATE DATABASE \`${database}\`;`) await rawQuery(rawDatasource, `CREATE DATABASE \`${database2}\`;`)
await client.query(`CREATE DATABASE \`${database2}\`;`)
const pathConfig: any = { const pathConfig: any = {
...dsConfig, ...rawDatasource,
config: { config: {
...dbConfig, ...rawDatasource.config!,
database, database,
}, },
} }
pathDatasource = await config.api.datasource.create(pathConfig) datasource = await config.api.datasource.create(pathConfig)
}) })
afterAll(async () => { afterAll(async () => {
await client.query(`DROP DATABASE \`${database}\`;`) await rawQuery(rawDatasource, `DROP DATABASE \`${database}\`;`)
await client.query(`DROP DATABASE \`${database2}\`;`) await rawQuery(rawDatasource, `DROP DATABASE \`${database2}\`;`)
await client.end()
}) })
it("discovers tables from any schema in search path", async () => { it("discovers tables from any schema in search path", async () => {
await client.query( await rawQuery(
rawDatasource,
`CREATE TABLE \`${database}\`.table1 (id1 SERIAL PRIMARY KEY);` `CREATE TABLE \`${database}\`.table1 (id1 SERIAL PRIMARY KEY);`
) )
const response = await makeRequest("post", "/api/datasources/info", { const response = await makeRequest("post", "/api/datasources/info", {
datasource: pathDatasource, datasource: datasource,
}) })
expect(response.status).toBe(200) expect(response.status).toBe(200)
expect(response.body.tableNames).toBeDefined() expect(response.body.tableNames).toBeDefined()
@ -210,15 +207,17 @@ describe("mysql integrations", () => {
it("does not mix columns from different tables", async () => { it("does not mix columns from different tables", async () => {
const repeated_table_name = "table_same_name" const repeated_table_name = "table_same_name"
await client.query( await rawQuery(
rawDatasource,
`CREATE TABLE \`${database}\`.${repeated_table_name} (id SERIAL PRIMARY KEY, val1 TEXT);` `CREATE TABLE \`${database}\`.${repeated_table_name} (id SERIAL PRIMARY KEY, val1 TEXT);`
) )
await client.query( await rawQuery(
rawDatasource,
`CREATE TABLE \`${database2}\`.${repeated_table_name} (id2 SERIAL PRIMARY KEY, val2 TEXT);` `CREATE TABLE \`${database2}\`.${repeated_table_name} (id2 SERIAL PRIMARY KEY, val2 TEXT);`
) )
const response = await makeRequest( const response = await makeRequest(
"post", "post",
`/api/datasources/${pathDatasource._id}/schema`, `/api/datasources/${datasource._id}/schema`,
{ {
tablesFilter: [repeated_table_name], tablesFilter: [repeated_table_name],
} }
@ -234,30 +233,14 @@ describe("mysql integrations", () => {
}) })
describe("POST /api/tables/", () => { describe("POST /api/tables/", () => {
let client: mysql.Connection
const emitDatasourceUpdateMock = jest.fn() const emitDatasourceUpdateMock = jest.fn()
beforeEach(async () => {
client = await mysql.createConnection(
(
await getDatasource(DatabaseName.MYSQL)
).config!
)
mysqlDatasource = await config.api.datasource.create(
await getDatasource(DatabaseName.MYSQL)
)
})
afterEach(async () => {
await client.end()
})
it("will emit the datasource entity schema with externalType to the front-end when adding a new column", async () => { it("will emit the datasource entity schema with externalType to the front-end when adding a new column", async () => {
const addColumnToTable: TableRequest = { const addColumnToTable: TableRequest = {
type: "table", type: "table",
sourceType: TableSourceType.EXTERNAL, sourceType: TableSourceType.EXTERNAL,
name: uniqueTableName(), name: uniqueTableName(),
sourceId: mysqlDatasource._id!, sourceId: datasource._id!,
primary: ["id"], primary: ["id"],
schema: { schema: {
id: { id: {
@ -304,7 +287,7 @@ describe("mysql integrations", () => {
}, },
}, },
created: true, created: true,
_id: `${mysqlDatasource._id}__${addColumnToTable.name}`, _id: `${datasource._id}__${addColumnToTable.name}`,
} }
delete expectedTable._add delete expectedTable._add
@ -351,17 +334,18 @@ describe("mysql integrations", () => {
"/api/tables/", "/api/tables/",
renameColumnOnTable renameColumnOnTable
) )
mysqlDatasource = (
await makeRequest( const ds = (
"post", await makeRequest("post", `/api/datasources/${datasource._id}/schema`)
`/api/datasources/${mysqlDatasource._id}/schema`
)
).body.datasource ).body.datasource
expect(response.status).toEqual(200) expect(response.status).toEqual(200)
expect( expect(Object.keys(ds.entities![primaryMySqlTable.name].schema)).toEqual([
Object.keys(mysqlDatasource.entities![primaryMySqlTable.name].schema) "id",
).toEqual(["id", "name", "description", "age"]) "name",
"description",
"age",
])
}) })
}) })
}) })