From 19eaafd9465eb886b718de7c5139e1f7f19e0d82 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Thu, 26 Oct 2023 16:32:34 +0100 Subject: [PATCH] Fixing some test cases. --- .../server/src/api/controllers/table/index.ts | 9 ++- .../server/src/api/routes/tests/table.spec.ts | 3 +- packages/server/src/environment.ts | 1 - .../src/integrations/tests/utils/postgres.ts | 64 +++++++++++-------- .../src/tests/utilities/TestConfiguration.ts | 16 ++--- .../src/utilities/rowProcessor/index.ts | 2 +- 6 files changed, 53 insertions(+), 42 deletions(-) diff --git a/packages/server/src/api/controllers/table/index.ts b/packages/server/src/api/controllers/table/index.ts index 99b1cd73c6..c68b6e9923 100644 --- a/packages/server/src/api/controllers/table/index.ts +++ b/packages/server/src/api/controllers/table/index.ts @@ -10,6 +10,7 @@ import { events } from "@budibase/backend-core" import { BulkImportRequest, BulkImportResponse, + DocumentType, FetchTablesResponse, MigrateRequest, MigrateResponse, @@ -20,6 +21,7 @@ import { TableResponse, TableSourceType, UserCtx, + SEPARATOR, } from "@budibase/types" import sdk from "../../../sdk" import { jsonFromCsvString } from "../../../utilities/csv" @@ -30,7 +32,12 @@ function pickApi({ tableId, table }: { tableId?: string; table?: Table }) { if (table && !tableId) { tableId = table._id } - if (table && table.sourceType === TableSourceType.EXTERNAL) { + if ( + table?.sourceId && + table.sourceId.includes(DocumentType.DATASOURCE + SEPARATOR) + ) { + return external + } else if (table?.sourceType === TableSourceType.EXTERNAL) { return external } else if (tableId && isExternalTable(tableId)) { return external diff --git a/packages/server/src/api/routes/tests/table.spec.ts b/packages/server/src/api/routes/tests/table.spec.ts index 5392954353..3767e5e912 100644 --- a/packages/server/src/api/routes/tests/table.spec.ts +++ b/packages/server/src/api/routes/tests/table.spec.ts @@ -245,7 +245,8 @@ describe("/tables", () => { .expect(200) const fetchedTable = res.body[0] expect(fetchedTable.name).toEqual(testTable.name) - expect(fetchedTable.type).toEqual("internal") + expect(fetchedTable.type).toEqual("table") + expect(fetchedTable.sourceType).toEqual("internal") }) it("should apply authorization to endpoint", async () => { diff --git a/packages/server/src/environment.ts b/packages/server/src/environment.ts index a1701535ce..91424113ac 100644 --- a/packages/server/src/environment.ts +++ b/packages/server/src/environment.ts @@ -75,7 +75,6 @@ const environment = { }, isTest: coreEnv.isTest, isJest: coreEnv.isJest, - isDev: coreEnv.isDev, isProd: () => { return !coreEnv.isDev() diff --git a/packages/server/src/integrations/tests/utils/postgres.ts b/packages/server/src/integrations/tests/utils/postgres.ts index b749551721..f65d33e3e0 100644 --- a/packages/server/src/integrations/tests/utils/postgres.ts +++ b/packages/server/src/integrations/tests/utils/postgres.ts @@ -1,39 +1,47 @@ import { Datasource, SourceName } from "@budibase/types" import { GenericContainer, Wait, StartedTestContainer } from "testcontainers" +import env from "../../../environment" let container: StartedTestContainer | undefined +const isMac = process.platform === "darwin" + export async function getDsConfig(): Promise { - if (!container) { - container = await new GenericContainer("postgres") - .withExposedPorts(5432) - .withEnv("POSTGRES_PASSWORD", "password") - .withWaitStrategy( - Wait.forLogMessage( - "PostgreSQL init process complete; ready for start up." + try { + if (!container) { + // postgres 15-bullseye safer bet on Linux + const version = isMac ? undefined : "15-bullseye" + container = await new GenericContainer("postgres", version) + .withExposedPorts(5432) + .withEnv("POSTGRES_PASSWORD", "password") + .withWaitStrategy( + Wait.forLogMessage( + "PostgreSQL init process complete; ready for start up." + ) ) - ) - .start() - } + .start() + } + const host = container.getContainerIpAddress() + const port = container.getMappedPort(5432) - const host = container.getContainerIpAddress() - const port = container.getMappedPort(5432) - - return { - type: "datasource_plus", - source: SourceName.POSTGRES, - plus: true, - config: { - host, - port, - database: "postgres", - user: "postgres", - password: "password", - schema: "public", - ssl: false, - rejectUnauthorized: false, - ca: false, - }, + return { + type: "datasource_plus", + source: SourceName.POSTGRES, + plus: true, + config: { + host, + port, + database: "postgres", + user: "postgres", + password: "password", + schema: "public", + ssl: false, + rejectUnauthorized: false, + ca: false, + }, + } + } catch (err) { + throw new Error("**UNABLE TO CREATE TO POSTGRES CONTAINER**") } } diff --git a/packages/server/src/tests/utilities/TestConfiguration.ts b/packages/server/src/tests/utilities/TestConfiguration.ts index a397b00441..0c81ba4123 100644 --- a/packages/server/src/tests/utilities/TestConfiguration.ts +++ b/packages/server/src/tests/utilities/TestConfiguration.ts @@ -570,11 +570,9 @@ class TestConfiguration { if (!config.sourceId) { config.sourceId = INTERNAL_TABLE_SOURCE_ID } - if (this.datasource && !config.sourceId) { - config.sourceId = this.datasource._id || INTERNAL_TABLE_SOURCE_ID - if (this.datasource.plus) { - config.sourceType = TableSourceType.EXTERNAL - } + if (this.datasource?._id) { + config.sourceId = this.datasource._id + config.sourceType = TableSourceType.EXTERNAL } return this.updateTable(config, options) @@ -608,11 +606,9 @@ class TestConfiguration { } as RelationshipFieldMetadata } - if (this.datasource && !tableConfig.sourceId) { - tableConfig.sourceId = this.datasource._id || INTERNAL_TABLE_SOURCE_ID - if (this.datasource.plus) { - tableConfig.sourceType = TableSourceType.EXTERNAL - } + if (this.datasource?._id) { + tableConfig.sourceId = this.datasource._id + tableConfig.sourceType = TableSourceType.EXTERNAL } return await this.createTable(tableConfig) diff --git a/packages/server/src/utilities/rowProcessor/index.ts b/packages/server/src/utilities/rowProcessor/index.ts index cf3875b2ea..0024e96d50 100644 --- a/packages/server/src/utilities/rowProcessor/index.ts +++ b/packages/server/src/utilities/rowProcessor/index.ts @@ -51,7 +51,7 @@ function getRemovedAttachmentKeys( /** * This will update any auto columns that are found on the row/table with the correct information based on * time now and the current logged in user making the request. - * @param user The user to be used for an appId as well as the createdBy and createdAt fields. + * @param userId The user to be used for an appId as well as the createdBy and createdAt fields. * @param table The table which is to be used for the schema, as well as handling auto IDs incrementing. * @param row The row which is to be updated with information for the auto columns. * @param opts specific options for function to carry out optional features.