Migrate mysql.spec.ts to new datasource providers.

This commit is contained in:
Sam Rose 2024-03-26 14:36:18 +00:00
parent 5530d7f4b6
commit 2304aeaa71
No known key found for this signature in database
2 changed files with 32 additions and 37 deletions

View File

@ -1,4 +1,4 @@
import { databaseTestProviders } from "../../../integrations/tests/utils" import { DatabaseName, getDatasource } from "../../../integrations/tests/utils"
import tk from "timekeeper" import tk from "timekeeper"
import { outputProcessing } from "../../../utilities/rowProcessor" import { outputProcessing } from "../../../utilities/rowProcessor"
@ -34,10 +34,10 @@ jest.unmock("pg")
describe.each([ describe.each([
["internal", undefined], ["internal", undefined],
["postgres", databaseTestProviders.postgres], [DatabaseName.POSTGRES, getDatasource(DatabaseName.POSTGRES)],
["mysql", databaseTestProviders.mysql], [DatabaseName.MYSQL, getDatasource(DatabaseName.MYSQL)],
["mssql", databaseTestProviders.mssql], [DatabaseName.SQL_SERVER, getDatasource(DatabaseName.SQL_SERVER)],
["mariadb", databaseTestProviders.mariadb], [DatabaseName.MARIADB, getDatasource(DatabaseName.MARIADB)],
])("/rows (%s)", (__, dsProvider) => { ])("/rows (%s)", (__, dsProvider) => {
const isInternal = dsProvider === undefined const isInternal = dsProvider === undefined
const config = setup.getConfig() const config = setup.getConfig()
@ -49,23 +49,23 @@ describe.each([
await config.init() await config.init()
if (dsProvider) { if (dsProvider) {
datasource = await config.createDatasource({ datasource = await config.createDatasource({
datasource: await dsProvider.datasource(), datasource: await dsProvider,
}) })
} }
}) })
afterAll(async () => { afterAll(async () => {
if (dsProvider) {
await dsProvider.stop()
}
setup.afterAll() setup.afterAll()
}) })
function saveTableRequest( function saveTableRequest(
...overrides: Partial<SaveTableRequest>[] // We omit the name field here because it's generated in the function with a
// high likelihood to be unique. Tests should not have any reason to control
// the table name they're writing to.
...overrides: Partial<Omit<SaveTableRequest, "name">>[]
): SaveTableRequest { ): SaveTableRequest {
const req: SaveTableRequest = { const req: SaveTableRequest = {
name: uuid.v4().substring(0, 16), name: uuid.v4().substring(0, 10),
type: "table", type: "table",
sourceType: datasource sourceType: datasource
? TableSourceType.EXTERNAL ? TableSourceType.EXTERNAL
@ -87,7 +87,10 @@ describe.each([
} }
function defaultTable( function defaultTable(
...overrides: Partial<SaveTableRequest>[] // We omit the name field here because it's generated in the function with a
// high likelihood to be unique. Tests should not have any reason to control
// the table name they're writing to.
...overrides: Partial<Omit<SaveTableRequest, "name">>[]
): SaveTableRequest { ): SaveTableRequest {
return saveTableRequest( return saveTableRequest(
{ {
@ -194,7 +197,6 @@ describe.each([
const newTable = await config.api.table.save( const newTable = await config.api.table.save(
saveTableRequest({ saveTableRequest({
name: "TestTableAuto",
schema: { schema: {
"Row ID": { "Row ID": {
name: "Row ID", name: "Row ID",
@ -383,11 +385,9 @@ describe.each([
isInternal && isInternal &&
it("doesn't allow creating in user table", async () => { it("doesn't allow creating in user table", async () => {
const userTableId = InternalTable.USER_METADATA
const response = await config.api.row.save( const response = await config.api.row.save(
userTableId, InternalTable.USER_METADATA,
{ {
tableId: userTableId,
firstName: "Joe", firstName: "Joe",
lastName: "Joe", lastName: "Joe",
email: "joe@joe.com", email: "joe@joe.com",
@ -462,7 +462,6 @@ describe.each([
table = await config.api.table.save(defaultTable()) table = await config.api.table.save(defaultTable())
otherTable = await config.api.table.save( otherTable = await config.api.table.save(
defaultTable({ defaultTable({
name: "a",
schema: { schema: {
relationship: { relationship: {
name: "relationship", name: "relationship",
@ -898,8 +897,8 @@ describe.each([
let o2mTable: Table let o2mTable: Table
let m2mTable: Table let m2mTable: Table
beforeAll(async () => { beforeAll(async () => {
o2mTable = await config.api.table.save(defaultTable({ name: "o2m" })) o2mTable = await config.api.table.save(defaultTable())
m2mTable = await config.api.table.save(defaultTable({ name: "m2m" })) m2mTable = await config.api.table.save(defaultTable())
}) })
describe.each([ describe.each([
@ -1256,7 +1255,6 @@ describe.each([
otherTable = await config.api.table.save(defaultTable()) otherTable = await config.api.table.save(defaultTable())
table = await config.api.table.save( table = await config.api.table.save(
saveTableRequest({ saveTableRequest({
name: "b",
schema: { schema: {
links: { links: {
name: "links", name: "links",
@ -1354,7 +1352,6 @@ describe.each([
const table = await config.api.table.save( const table = await config.api.table.save(
saveTableRequest({ saveTableRequest({
name: "table",
schema: { schema: {
text: { text: {
name: "text", name: "text",

View File

@ -3,7 +3,6 @@ import {
generateMakeRequest, generateMakeRequest,
MakeRequestResponse, MakeRequestResponse,
} from "../api/routes/public/tests/utils" } from "../api/routes/public/tests/utils"
import { v4 as uuidv4 } from "uuid"
import * as setup from "../api/routes/tests/utilities" import * as setup from "../api/routes/tests/utilities"
import { import {
Datasource, Datasource,
@ -12,9 +11,10 @@ import {
TableRequest, TableRequest,
TableSourceType, TableSourceType,
} from "@budibase/types" } from "@budibase/types"
import { databaseTestProviders } from "../integrations/tests/utils" import { DatabaseName, getDatasource } from "../integrations/tests/utils"
import mysql from "mysql2/promise" import mysql from "mysql2/promise"
import { builderSocket } from "../websockets" import { builderSocket } from "../websockets"
import { generator } from "@budibase/backend-core/tests"
// @ts-ignore // @ts-ignore
fetch.mockSearch() fetch.mockSearch()
@ -47,17 +47,13 @@ describe("mysql integrations", () => {
makeRequest = generateMakeRequest(apiKey, true) makeRequest = generateMakeRequest(apiKey, true)
mysqlDatasource = await config.api.datasource.create( mysqlDatasource = await config.api.datasource.create(
await databaseTestProviders.mysql.datasource() await getDatasource(DatabaseName.MYSQL)
) )
}) })
afterAll(async () => {
await databaseTestProviders.mysql.stop()
})
beforeEach(async () => { beforeEach(async () => {
primaryMySqlTable = await config.createTable({ primaryMySqlTable = await config.createTable({
name: uuidv4(), name: generator.guid().replaceAll("-", "_").substring(0, 10),
type: "table", type: "table",
primary: ["id"], primary: ["id"],
schema: { schema: {
@ -117,7 +113,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 databaseTestProviders.mysql.datasource(), datasource: await getDatasource(DatabaseName.MYSQL),
}, },
{ {
body: { body: {
@ -128,7 +124,7 @@ describe("mysql integrations", () => {
}) })
it("should state an invalid datasource cannot connect", async () => { it("should state an invalid datasource cannot connect", async () => {
const dbConfig = await databaseTestProviders.mysql.datasource() const dbConfig = await getDatasource(DatabaseName.MYSQL)
await config.api.datasource.verify( await config.api.datasource.verify(
{ {
datasource: { datasource: {
@ -168,7 +164,7 @@ describe("mysql integrations", () => {
const database2 = "test-2" const database2 = "test-2"
beforeAll(async () => { beforeAll(async () => {
const dsConfig = await databaseTestProviders.mysql.datasource() const dsConfig = await getDatasource(DatabaseName.MYSQL)
const dbConfig = dsConfig.config! const dbConfig = dsConfig.config!
client = await mysql.createConnection(dbConfig) client = await mysql.createConnection(dbConfig)
@ -237,11 +233,11 @@ describe("mysql integrations", () => {
beforeEach(async () => { beforeEach(async () => {
client = await mysql.createConnection( client = await mysql.createConnection(
( (
await databaseTestProviders.mysql.datasource() await getDatasource(DatabaseName.MYSQL)
).config! ).config!
) )
mysqlDatasource = await config.api.datasource.create( mysqlDatasource = await config.api.datasource.create(
await databaseTestProviders.mysql.datasource() await getDatasource(DatabaseName.MYSQL)
) )
}) })
@ -253,7 +249,7 @@ describe("mysql integrations", () => {
const addColumnToTable: TableRequest = { const addColumnToTable: TableRequest = {
type: "table", type: "table",
sourceType: TableSourceType.EXTERNAL, sourceType: TableSourceType.EXTERNAL,
name: "table", name: generator.guid().replaceAll("-", "_").substring(0, 10),
sourceId: mysqlDatasource._id!, sourceId: mysqlDatasource._id!,
primary: ["id"], primary: ["id"],
schema: { schema: {
@ -301,14 +297,16 @@ describe("mysql integrations", () => {
}, },
}, },
created: true, created: true,
_id: `${mysqlDatasource._id}__table`, _id: `${mysqlDatasource._id}__${addColumnToTable.name}`,
} }
delete expectedTable._add delete expectedTable._add
expect(emitDatasourceUpdateMock).toHaveBeenCalledTimes(1) expect(emitDatasourceUpdateMock).toHaveBeenCalledTimes(1)
const emittedDatasource: Datasource = const emittedDatasource: Datasource =
emitDatasourceUpdateMock.mock.calls[0][1] emitDatasourceUpdateMock.mock.calls[0][1]
expect(emittedDatasource.entities!["table"]).toEqual(expectedTable) expect(emittedDatasource.entities![expectedTable.name]).toEqual(
expectedTable
)
}) })
it("will rename a column", async () => { it("will rename a column", async () => {