From 6240740a42854bd70ede3859ed676590582526ad Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 5 Sep 2023 11:11:47 +0200 Subject: [PATCH] Simplify --- .../src/integration-test/postgres.spec.ts | 12 +++--- .../src/integrations/tests/utils/index.ts | 2 +- .../src/integrations/tests/utils/postgres.ts | 41 ++++++++----------- 3 files changed, 23 insertions(+), 32 deletions(-) diff --git a/packages/server/src/integration-test/postgres.spec.ts b/packages/server/src/integration-test/postgres.spec.ts index fd85393477..faaa22f2c8 100644 --- a/packages/server/src/integration-test/postgres.spec.ts +++ b/packages/server/src/integration-test/postgres.spec.ts @@ -26,6 +26,8 @@ jest.setTimeout(30000) jest.unmock("pg") jest.mock("../websockets") +const provider = new PostgresProvider() + describe("postgres integrations", () => { let makeRequest: MakeRequestResponse, postgresDatasource: Datasource, @@ -34,11 +36,7 @@ describe("postgres integrations", () => { manyToOneRelationshipInfo: ForeignTableInfo, manyToManyRelationshipInfo: ForeignTableInfo - let provider: PostgresProvider - beforeAll(async () => { - provider = await PostgresProvider.init() - await config.init() const apiKey = await config.generateApiKey() @@ -47,7 +45,7 @@ describe("postgres integrations", () => { beforeEach(async () => { postgresDatasource = await config.api.datasource.create( - provider.getDsConfig() + await provider.getDsConfig() ) async function createAuxTable(prefix: string) { @@ -1006,14 +1004,14 @@ describe("postgres integrations", () => { describe("POST /api/datasources/verify", () => { it("should be able to verify the connection", async () => { const response = await config.api.datasource.verify({ - datasource: provider.getDsConfig(), + datasource: await provider.getDsConfig(), }) expect(response.status).toBe(200) expect(response.body.connected).toBe(true) }) it("should state an invalid datasource cannot connect", async () => { - const dbConfig = provider.getDsConfig() + const dbConfig = await provider.getDsConfig() const response = await config.api.datasource.verify({ datasource: { ...dbConfig, diff --git a/packages/server/src/integrations/tests/utils/index.ts b/packages/server/src/integrations/tests/utils/index.ts index e80dbed83a..20b6f277dd 100644 --- a/packages/server/src/integrations/tests/utils/index.ts +++ b/packages/server/src/integrations/tests/utils/index.ts @@ -2,5 +2,5 @@ import { Datasource } from "@budibase/types" export * from "./postgres" export interface DatabasePlusTestProvider { - getDsConfig(): Datasource + getDsConfig(): Promise } diff --git a/packages/server/src/integrations/tests/utils/postgres.ts b/packages/server/src/integrations/tests/utils/postgres.ts index 514605306e..8d79a7e762 100644 --- a/packages/server/src/integrations/tests/utils/postgres.ts +++ b/packages/server/src/integrations/tests/utils/postgres.ts @@ -1,40 +1,33 @@ import { Datasource, SourceName } from "@budibase/types" -import { GenericContainer, Wait } from "testcontainers" +import { GenericContainer, Wait, StartedTestContainer } from "testcontainers" import { DatabasePlusTestProvider } from "." export class PostgresProvider implements DatabasePlusTestProvider { - private host: string - private port: number + private container?: StartedTestContainer - private constructor(host: string, port: number) { - this.host = host - this.port = port - } - - static async init() { - const containerPostgres = await new GenericContainer("postgres") - .withExposedPorts(5432) - .withEnv("POSTGRES_PASSWORD", "password") - .withWaitStrategy( - Wait.forLogMessage( - "PostgreSQL init process complete; ready for start up." + async getDsConfig(): Promise { + if (!this.container) { + this.container = await new GenericContainer("postgres") + .withExposedPorts(5432) + .withEnv("POSTGRES_PASSWORD", "password") + .withWaitStrategy( + Wait.forLogMessage( + "PostgreSQL init process complete; ready for start up." + ) ) - ) - .start() + .start() + } - const host = containerPostgres.getContainerIpAddress() - const port = containerPostgres.getMappedPort(5432) - return new PostgresProvider(host, port) - } + const host = this.container.getContainerIpAddress() + const port = this.container.getMappedPort(5432) - getDsConfig(): Datasource { return { type: "datasource_plus", source: SourceName.POSTGRES, plus: true, config: { - host: this.host, - port: this.port, + host, + port, database: "postgres", user: "postgres", password: "password",