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,
rawQuery,
} from "../../../../integrations/tests/utils"
import pg from "pg"
import mysql from "mysql2/promise"
import mssql from "mssql"
jest.unmock("pg")

View File

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