From db877b7802ded722419b53ecb2d4bca006aadf5f Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Wed, 3 Apr 2024 12:55:44 +0100 Subject: [PATCH] Create unique MongoDB databases. --- .../src/api/routes/tests/queries/mongodb.spec.ts | 16 +++++++++------- .../src/integrations/tests/utils/mongodb.ts | 4 ++-- 2 files changed, 11 insertions(+), 9 deletions(-) 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 148f2c15ec..bdcfd85437 100644 --- a/packages/server/src/api/routes/tests/queries/mongodb.spec.ts +++ b/packages/server/src/api/routes/tests/queries/mongodb.spec.ts @@ -4,7 +4,7 @@ import { DatabaseName, getDatasource, } from "../../../../integrations/tests/utils" -import { MongoClient, type Collection, BSON } from "mongodb" +import { MongoClient, type Collection, BSON, Db } from "mongodb" import { generator } from "@budibase/backend-core/tests" const expectValidId = expect.stringMatching(/^\w{24}$/) @@ -40,8 +40,7 @@ describe("/queries", () => { async function withClient( callback: (client: MongoClient) => Promise ): Promise { - const ds = await getDatasource(DatabaseName.MONGODB) - const client = new MongoClient(ds.config!.connectionString) + const client = new MongoClient(datasource.config!.connectionString) await client.connect() try { return await callback(client) @@ -50,13 +49,16 @@ describe("/queries", () => { } } + async function withDb(callback: (db: Db) => Promise): Promise { + return await withClient(async client => { + return await callback(client.db(datasource.config!.db)) + }) + } + async function withCollection( callback: (collection: Collection) => Promise ): Promise { - return await withClient(async client => { - const db = client.db( - (await getDatasource(DatabaseName.MONGODB)).config!.db - ) + return await withDb(async db => { return await callback(db.collection(collection)) }) } diff --git a/packages/server/src/integrations/tests/utils/mongodb.ts b/packages/server/src/integrations/tests/utils/mongodb.ts index c5c0340dc9..0bdbb2808c 100644 --- a/packages/server/src/integrations/tests/utils/mongodb.ts +++ b/packages/server/src/integrations/tests/utils/mongodb.ts @@ -1,4 +1,4 @@ -import { testContainerUtils } from "@budibase/backend-core/tests" +import { generator, testContainerUtils } from "@budibase/backend-core/tests" import { Datasource, SourceName } from "@budibase/types" import { GenericContainer, Wait } from "testcontainers" import { startContainer } from "." @@ -33,7 +33,7 @@ export async function getDatasource(): Promise { plus: false, config: { connectionString: `mongodb://mongo:password@127.0.0.1:${port.host}`, - db: "mongo", + db: generator.guid(), }, } }