Merge pull request #13883 from Budibase/mysql-test-cleanup-2

Move some mysql.spec.ts and postgres.spec.ts tests into datasource.sp…
This commit is contained in:
Sam Rose 2024-06-07 17:59:48 +01:00 committed by GitHub
commit 5e38ee03f1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 73 additions and 129 deletions

View File

@ -156,8 +156,10 @@ describe("/datasources", () => {
[DatabaseName.SQL_SERVER, getDatasource(DatabaseName.SQL_SERVER)], [DatabaseName.SQL_SERVER, getDatasource(DatabaseName.SQL_SERVER)],
[DatabaseName.MARIADB, getDatasource(DatabaseName.MARIADB)], [DatabaseName.MARIADB, getDatasource(DatabaseName.MARIADB)],
])("%s", (_, dsProvider) => { ])("%s", (_, dsProvider) => {
let rawDatasource: Datasource
beforeEach(async () => { beforeEach(async () => {
datasource = await config.api.datasource.create(await dsProvider) rawDatasource = await dsProvider
datasource = await config.api.datasource.create(rawDatasource)
}) })
describe("get", () => { describe("get", () => {
@ -372,5 +374,58 @@ describe("/datasources", () => {
expect(updated).toEqual(expected) expect(updated).toEqual(expected)
}) })
}) })
describe("verify", () => {
it("should be able to verify the connection", async () => {
await config.api.datasource.verify(
{
datasource: rawDatasource,
},
{
body: {
connected: true,
},
}
)
})
it("should state an invalid datasource cannot connect", async () => {
await config.api.datasource.verify(
{
datasource: {
...rawDatasource,
config: {
...rawDatasource.config,
password: "wrongpassword",
},
},
},
{
body: {
connected: false,
error: /.*/, // error message differs between databases
},
}
)
})
})
describe("info", () => {
it("should fetch information about postgres datasource", async () => {
const table = await config.api.table.save(
tableForDatasource(datasource, {
schema: {
name: {
name: "name",
type: FieldType.STRING,
},
},
})
)
const info = await config.api.datasource.info(datasource)
expect(info.tableNames).toContain(table.name)
})
})
}) })
}) })

View File

@ -4,13 +4,14 @@ import {
MakeRequestResponse, MakeRequestResponse,
} from "../api/routes/public/tests/utils" } from "../api/routes/public/tests/utils"
import * as setup from "../api/routes/tests/utilities" import * as setup from "../api/routes/tests/utilities"
import { Datasource, FieldType, Table, TableSourceType } from "@budibase/types" import { Datasource, FieldType } from "@budibase/types"
import { import {
DatabaseName, DatabaseName,
getDatasource, getDatasource,
rawQuery, rawQuery,
} from "../integrations/tests/utils" } from "../integrations/tests/utils"
import { generator } from "@budibase/backend-core/tests" import { generator } from "@budibase/backend-core/tests"
import { tableForDatasource } from "../../src/tests/utilities/structures"
// @ts-ignore // @ts-ignore
fetch.mockSearch() fetch.mockSearch()
@ -41,8 +42,7 @@ jest.mock("../websockets", () => ({
describe("mysql integrations", () => { describe("mysql integrations", () => {
let makeRequest: MakeRequestResponse, let makeRequest: MakeRequestResponse,
rawDatasource: Datasource, rawDatasource: Datasource,
datasource: Datasource, datasource: Datasource
primaryMySqlTable: Table
beforeAll(async () => { beforeAll(async () => {
await config.init() await config.init()
@ -54,38 +54,12 @@ describe("mysql integrations", () => {
datasource = await config.api.datasource.create(rawDatasource) datasource = await config.api.datasource.create(rawDatasource)
}) })
beforeEach(async () => {
primaryMySqlTable = await config.createTable({
name: uniqueTableName(),
type: "table",
primary: ["id"],
schema: {
id: {
name: "id",
type: FieldType.AUTO,
autocolumn: true,
},
name: {
name: "name",
type: FieldType.STRING,
},
description: {
name: "description",
type: FieldType.STRING,
},
value: {
name: "value",
type: FieldType.NUMBER,
},
},
sourceId: datasource._id,
sourceType: TableSourceType.EXTERNAL,
})
})
afterAll(config.end) afterAll(config.end)
it("validate table schema", async () => { it("validate table schema", async () => {
// Creating a table so that `entities` is populated.
await config.api.table.save(tableForDatasource(datasource))
const res = await makeRequest("get", `/api/datasources/${datasource._id}`) const res = await makeRequest("get", `/api/datasources/${datasource._id}`)
expect(res.status).toBe(200) expect(res.status).toBe(200)
@ -109,54 +83,6 @@ describe("mysql integrations", () => {
}) })
}) })
describe("POST /api/datasources/verify", () => {
it("should be able to verify the connection", async () => {
await config.api.datasource.verify(
{
datasource: rawDatasource,
},
{
body: {
connected: true,
},
}
)
})
it("should state an invalid datasource cannot connect", async () => {
await config.api.datasource.verify(
{
datasource: {
...rawDatasource,
config: {
...rawDatasource.config,
password: "wrongpassword",
},
},
},
{
body: {
connected: false,
error:
"Access denied for the specified user. User does not have the necessary privileges or the provided credentials are incorrect. Please verify the credentials, and ensure that the user has appropriate permissions.",
},
}
)
})
})
describe("POST /api/datasources/info", () => {
it("should fetch information about mysql datasource", async () => {
const primaryName = primaryMySqlTable.name
const response = await makeRequest("post", "/api/datasources/info", {
datasource: datasource,
})
expect(response.status).toBe(200)
expect(response.body.tableNames).toBeDefined()
expect(response.body.tableNames.indexOf(primaryName)).not.toBe(-1)
})
})
describe("Integration compatibility with mysql search_path", () => { describe("Integration compatibility with mysql search_path", () => {
let datasource: Datasource, rawDatasource: Datasource let datasource: Datasource, rawDatasource: Datasource
const database = generator.guid() const database = generator.guid()

View File

@ -1035,54 +1035,6 @@ describe("postgres integrations", () => {
}) })
}) })
describe("POST /api/datasources/verify", () => {
it("should be able to verify the connection", async () => {
await config.api.datasource.verify(
{
datasource: await getDatasource(DatabaseName.POSTGRES),
},
{
body: {
connected: true,
},
}
)
})
it("should state an invalid datasource cannot connect", async () => {
const dbConfig = await getDatasource(DatabaseName.POSTGRES)
await config.api.datasource.verify(
{
datasource: {
...dbConfig,
config: {
...dbConfig.config,
password: "wrongpassword",
},
},
},
{
body: {
connected: false,
error: 'password authentication failed for user "postgres"',
},
}
)
})
})
describe("POST /api/datasources/info", () => {
it("should fetch information about postgres datasource", async () => {
const primaryName = primaryPostgresTable.name
const response = await makeRequest("post", "/api/datasources/info", {
datasource: datasource,
})
expect(response.status).toBe(200)
expect(response.body.tableNames).toBeDefined()
expect(response.body.tableNames.indexOf(primaryName)).not.toBe(-1)
})
})
describe("POST /api/datasources/:datasourceId/schema", () => { describe("POST /api/datasources/:datasourceId/schema", () => {
let tableName: string let tableName: string

View File

@ -6,6 +6,7 @@ import {
UpdateDatasourceRequest, UpdateDatasourceRequest,
QueryJson, QueryJson,
BuildSchemaFromSourceResponse, BuildSchemaFromSourceResponse,
FetchDatasourceInfoResponse,
} from "@budibase/types" } from "@budibase/types"
import { Expectations, TestAPI } from "./base" import { Expectations, TestAPI } from "./base"
@ -83,4 +84,14 @@ export class DatasourceAPI extends TestAPI {
} }
) )
} }
info = async (datasource: Datasource, expectations?: Expectations) => {
return await this._post<FetchDatasourceInfoResponse>(
`/api/datasources/info`,
{
body: { datasource },
expectations,
}
)
}
} }