From 6801ade481879a23b20ce7008f8904f937f2bd9a Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Wed, 6 Nov 2024 16:51:40 +0000 Subject: [PATCH] Convert rowAction.spec.ts to datasourceDescribe --- .../src/api/routes/tests/datasource.spec.ts | 31 ++-- .../routes/tests/queries/generic-sql.spec.ts | 2 +- .../api/routes/tests/queries/mongodb.spec.ts | 2 +- .../server/src/api/routes/tests/row.spec.ts | 2 +- .../src/api/routes/tests/rowAction.spec.ts | 133 +++++++++--------- .../src/api/routes/tests/search.spec.ts | 2 +- .../server/src/api/routes/tests/table.spec.ts | 2 +- .../server/src/integration-test/mysql.spec.ts | 4 +- .../src/integration-test/postgres.spec.ts | 4 +- packages/server/src/integrations/index.ts | 2 +- .../src/integrations/tests/utils/index.ts | 4 +- .../sdk/app/rows/search/tests/search.spec.ts | 2 +- 12 files changed, 96 insertions(+), 94 deletions(-) diff --git a/packages/server/src/api/routes/tests/datasource.spec.ts b/packages/server/src/api/routes/tests/datasource.spec.ts index 7545253181..6b811cc716 100644 --- a/packages/server/src/api/routes/tests/datasource.spec.ts +++ b/packages/server/src/api/routes/tests/datasource.spec.ts @@ -19,8 +19,7 @@ import { } from "@budibase/types" import { DatabaseName, - getDatasource, - knexClient, + datasourceDescribe, } from "../../../integrations/tests/utils" import { tableForDatasource } from "../../../tests/utilities/structures" import nock from "nock" @@ -69,7 +68,7 @@ describe("/datasources", () => { { status: 500, body: { - message: "No datasource implementation found.", + message: 'No datasource implementation found called: "invalid"', }, } ) @@ -163,21 +162,23 @@ describe("/datasources", () => { }) }) }) +}) - describe.each([ - [DatabaseName.POSTGRES, getDatasource(DatabaseName.POSTGRES)], - [DatabaseName.MYSQL, getDatasource(DatabaseName.MYSQL)], - [DatabaseName.SQL_SERVER, getDatasource(DatabaseName.SQL_SERVER)], - [DatabaseName.MARIADB, getDatasource(DatabaseName.MARIADB)], - [DatabaseName.ORACLE, getDatasource(DatabaseName.ORACLE)], - ])("%s", (_, dsProvider) => { +datasourceDescribe( + { name: "%s", exclude: [DatabaseName.MONGODB, DatabaseName.SQS] }, + ({ config, dsProvider }) => { + let datasource: Datasource let rawDatasource: Datasource let client: Knex beforeEach(async () => { - rawDatasource = await dsProvider - datasource = await config.api.datasource.create(rawDatasource) - client = await knexClient(rawDatasource) + const ds = await dsProvider() + rawDatasource = ds.rawDatasource! + datasource = ds.datasource! + client = ds.client! + + jest.clearAllMocks() + nock.cleanAll() }) describe("get", () => { @@ -491,5 +492,5 @@ describe("/datasources", () => { ) }) }) - }) -}) + } +) diff --git a/packages/server/src/api/routes/tests/queries/generic-sql.spec.ts b/packages/server/src/api/routes/tests/queries/generic-sql.spec.ts index e08b662d15..a4939928d5 100644 --- a/packages/server/src/api/routes/tests/queries/generic-sql.spec.ts +++ b/packages/server/src/api/routes/tests/queries/generic-sql.spec.ts @@ -46,7 +46,7 @@ datasourceDescribe( } beforeAll(async () => { - const ds = await dsProvider + const ds = await dsProvider() rawDatasource = ds.rawDatasource! datasource = ds.datasource! client = ds.client! diff --git a/packages/server/src/api/routes/tests/queries/mongodb.spec.ts b/packages/server/src/api/routes/tests/queries/mongodb.spec.ts index 44c4fb7c65..44d1553f9b 100644 --- a/packages/server/src/api/routes/tests/queries/mongodb.spec.ts +++ b/packages/server/src/api/routes/tests/queries/mongodb.spec.ts @@ -64,7 +64,7 @@ datasourceDescribe( } beforeAll(async () => { - const ds = await dsProvider + const ds = await dsProvider() datasource = ds.datasource! }) diff --git a/packages/server/src/api/routes/tests/row.spec.ts b/packages/server/src/api/routes/tests/row.spec.ts index e9a7b26066..d80559285b 100644 --- a/packages/server/src/api/routes/tests/row.spec.ts +++ b/packages/server/src/api/routes/tests/row.spec.ts @@ -92,7 +92,7 @@ datasourceDescribe( let table: Table beforeAll(async () => { - const ds = await dsProvider + const ds = await dsProvider() datasource = ds.datasource client = ds.client }) diff --git a/packages/server/src/api/routes/tests/rowAction.spec.ts b/packages/server/src/api/routes/tests/rowAction.spec.ts index 14a1812195..58d7509798 100644 --- a/packages/server/src/api/routes/tests/rowAction.spec.ts +++ b/packages/server/src/api/routes/tests/rowAction.spec.ts @@ -9,15 +9,20 @@ import { import { automations } from "@budibase/pro" import { CreateRowActionRequest, + Datasource, DocumentType, PermissionLevel, RowActionResponse, + Table, TableRowActions, } from "@budibase/types" import * as setup from "./utilities" import { generator, mocks } from "@budibase/backend-core/tests" import { Expectations } from "../../../tests/utilities/api/base" -import { DatabaseName, getDatasource } from "../../../integrations/tests/utils" +import { + DatabaseName, + datasourceDescribe, +} from "../../../integrations/tests/utils" import { generateRowActionsID } from "../../../db/utils" const expectAutomationId = () => @@ -969,70 +974,66 @@ describe("/rowsActions", () => { status: 200, }) }) - - it.each([ - [ - "internal", - async () => { - await config.newTenant() - await config.api.application.addSampleData(config.getAppId()) - const tables = await config.api.table.fetch() - const table = tables.find( - t => t.sourceId === DEFAULT_BB_DATASOURCE_ID - )! - return table - }, - ], - [ - "external", - async () => { - await config.newTenant() - const ds = await config.createDatasource({ - datasource: await getDatasource(DatabaseName.POSTGRES), - }) - const table = await config.api.table.save( - setup.structures.tableForDatasource(ds) - ) - return table - }, - ], - ])( - "should delete all the row actions (and automations) for its tables when a datasource is deleted", - async (_, getTable) => { - async function getRowActionsFromDb(tableId: string) { - return await context.doInAppContext(config.getAppId(), async () => { - const db = context.getAppDB() - const tableDoc = await db.tryGet( - generateRowActionsID(tableId) - ) - return tableDoc - }) - } - - const table = await getTable() - const tableId = table._id! - - await config.api.rowAction.save(tableId, { - name: generator.guid(), - }) - await config.api.rowAction.save(tableId, { - name: generator.guid(), - }) - - const { actions } = (await getRowActionsFromDb(tableId))! - expect(Object.entries(actions)).toHaveLength(2) - - const { automations } = await config.api.automation.fetch() - expect(automations).toHaveLength(2) - - const datasource = await config.api.datasource.get(table.sourceId) - await config.api.datasource.delete(datasource) - - const automationsResp = await config.api.automation.fetch() - expect(automationsResp.automations).toHaveLength(0) - - expect(await getRowActionsFromDb(tableId)).toBeUndefined() - } - ) }) }) + +datasourceDescribe( + { name: "row actions (%s)", only: [DatabaseName.SQS, DatabaseName.POSTGRES] }, + ({ config, dsProvider, isInternal }) => { + let datasource: Datasource | undefined + + beforeAll(async () => { + const ds = await dsProvider() + datasource = ds.datasource + }) + + async function getTable(): Promise { + if (isInternal) { + await config.api.application.addSampleData(config.getAppId()) + const tables = await config.api.table.fetch() + return tables.find(t => t.sourceId === DEFAULT_BB_DATASOURCE_ID)! + } else { + const table = await config.api.table.save( + setup.structures.tableForDatasource(datasource!) + ) + return table + } + } + + it("should delete all the row actions (and automations) for its tables when a datasource is deleted", async () => { + async function getRowActionsFromDb(tableId: string) { + return await context.doInAppContext(config.getAppId(), async () => { + const db = context.getAppDB() + const tableDoc = await db.tryGet( + generateRowActionsID(tableId) + ) + return tableDoc + }) + } + + const table = await getTable() + const tableId = table._id! + + await config.api.rowAction.save(tableId, { + name: generator.guid(), + }) + await config.api.rowAction.save(tableId, { + name: generator.guid(), + }) + + const { actions } = (await getRowActionsFromDb(tableId))! + expect(Object.entries(actions)).toHaveLength(2) + + const { automations } = await config.api.automation.fetch() + expect(automations).toHaveLength(2) + + const datasource = await config.api.datasource.get(table.sourceId) + await config.api.datasource.delete(datasource) + + const automationsResp = await config.api.automation.fetch() + expect(automationsResp.automations).toHaveLength(0) + + expect(await getRowActionsFromDb(tableId)).toBeUndefined() + }) + } +) diff --git a/packages/server/src/api/routes/tests/search.spec.ts b/packages/server/src/api/routes/tests/search.spec.ts index 9433f0d84b..fe13e5311f 100644 --- a/packages/server/src/api/routes/tests/search.spec.ts +++ b/packages/server/src/api/routes/tests/search.spec.ts @@ -95,7 +95,7 @@ datasourceDescribe( } beforeAll(async () => { - const ds = await dsProvider + const ds = await dsProvider() datasource = ds.datasource client = ds.client diff --git a/packages/server/src/api/routes/tests/table.spec.ts b/packages/server/src/api/routes/tests/table.spec.ts index 550f86cd29..b9d8696714 100644 --- a/packages/server/src/api/routes/tests/table.spec.ts +++ b/packages/server/src/api/routes/tests/table.spec.ts @@ -44,7 +44,7 @@ datasourceDescribe( let datasource: Datasource | undefined beforeAll(async () => { - const ds = await dsProvider + const ds = await dsProvider() datasource = ds.datasource }) diff --git a/packages/server/src/integration-test/mysql.spec.ts b/packages/server/src/integration-test/mysql.spec.ts index 8022b75c83..8edf6a0190 100644 --- a/packages/server/src/integration-test/mysql.spec.ts +++ b/packages/server/src/integration-test/mysql.spec.ts @@ -24,7 +24,7 @@ datasourceDescribe( const database2 = generator.guid() beforeAll(async () => { - const ds = await dsProvider + const ds = await dsProvider() rawDatasource = ds.rawDatasource! datasource = ds.datasource! client = ds.client! @@ -88,7 +88,7 @@ datasourceDescribe( let client: Knex beforeAll(async () => { - const ds = await dsProvider + const ds = await dsProvider() datasource = ds.datasource! client = ds.client! }) diff --git a/packages/server/src/integration-test/postgres.spec.ts b/packages/server/src/integration-test/postgres.spec.ts index f384eb01ba..7ef6b9a968 100644 --- a/packages/server/src/integration-test/postgres.spec.ts +++ b/packages/server/src/integration-test/postgres.spec.ts @@ -15,7 +15,7 @@ datasourceDescribe( let client: Knex beforeAll(async () => { - const ds = await dsProvider + const ds = await dsProvider() datasource = ds.datasource! client = ds.client! }) @@ -218,7 +218,7 @@ datasourceDescribe( let schema2: string beforeEach(async () => { - const ds = await dsProvider + const ds = await dsProvider() datasource = ds.datasource! const rawDatasource = ds.rawDatasource! diff --git a/packages/server/src/integrations/index.ts b/packages/server/src/integrations/index.ts index fb892dcc79..de700d631d 100644 --- a/packages/server/src/integrations/index.ts +++ b/packages/server/src/integrations/index.ts @@ -120,7 +120,7 @@ export async function getIntegration(integration: SourceName) { } } } - throw new Error("No datasource implementation found.") + throw new Error(`No datasource implementation found called: "${integration}"`) } export default { diff --git a/packages/server/src/integrations/tests/utils/index.ts b/packages/server/src/integrations/tests/utils/index.ts index c57f1ef5c9..a3a8ffd965 100644 --- a/packages/server/src/integrations/tests/utils/index.ts +++ b/packages/server/src/integrations/tests/utils/index.ts @@ -49,7 +49,7 @@ export interface DatasourceDescribeReturnPromise { export interface DatasourceDescribeReturn { name: DatabaseName config: TestConfiguration - dsProvider: Promise + dsProvider: () => Promise isInternal: boolean isExternal: boolean isSql: boolean @@ -116,7 +116,7 @@ export function datasourceDescribe( cb({ name, config, - dsProvider: createDatasources(config, name), + dsProvider: () => createDatasources(config, name), isInternal: name === DatabaseName.SQS, isExternal: name !== DatabaseName.SQS, isSql: [ diff --git a/packages/server/src/sdk/app/rows/search/tests/search.spec.ts b/packages/server/src/sdk/app/rows/search/tests/search.spec.ts index 3fe23228f8..78e5ffa30b 100644 --- a/packages/server/src/sdk/app/rows/search/tests/search.spec.ts +++ b/packages/server/src/sdk/app/rows/search/tests/search.spec.ts @@ -26,7 +26,7 @@ datasourceDescribe( let table: Table beforeAll(async () => { - const ds = await dsProvider + const ds = await dsProvider() datasource = ds.datasource })