Create unique MongoDB databases.

This commit is contained in:
Sam Rose 2024-04-03 12:55:44 +01:00
parent 0881f34508
commit db877b7802
No known key found for this signature in database
2 changed files with 11 additions and 9 deletions

View File

@ -4,7 +4,7 @@ import {
DatabaseName, DatabaseName,
getDatasource, getDatasource,
} from "../../../../integrations/tests/utils" } 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" import { generator } from "@budibase/backend-core/tests"
const expectValidId = expect.stringMatching(/^\w{24}$/) const expectValidId = expect.stringMatching(/^\w{24}$/)
@ -40,8 +40,7 @@ describe("/queries", () => {
async function withClient<T>( async function withClient<T>(
callback: (client: MongoClient) => Promise<T> callback: (client: MongoClient) => Promise<T>
): Promise<T> { ): Promise<T> {
const ds = await getDatasource(DatabaseName.MONGODB) const client = new MongoClient(datasource.config!.connectionString)
const client = new MongoClient(ds.config!.connectionString)
await client.connect() await client.connect()
try { try {
return await callback(client) return await callback(client)
@ -50,13 +49,16 @@ describe("/queries", () => {
} }
} }
async function withDb<T>(callback: (db: Db) => Promise<T>): Promise<T> {
return await withClient(async client => {
return await callback(client.db(datasource.config!.db))
})
}
async function withCollection<T>( async function withCollection<T>(
callback: (collection: Collection) => Promise<T> callback: (collection: Collection) => Promise<T>
): Promise<T> { ): Promise<T> {
return await withClient(async client => { return await withDb(async db => {
const db = client.db(
(await getDatasource(DatabaseName.MONGODB)).config!.db
)
return await callback(db.collection(collection)) return await callback(db.collection(collection))
}) })
} }

View File

@ -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 { Datasource, SourceName } from "@budibase/types"
import { GenericContainer, Wait } from "testcontainers" import { GenericContainer, Wait } from "testcontainers"
import { startContainer } from "." import { startContainer } from "."
@ -33,7 +33,7 @@ export async function getDatasource(): Promise<Datasource> {
plus: false, plus: false,
config: { config: {
connectionString: `mongodb://mongo:password@127.0.0.1:${port.host}`, connectionString: `mongodb://mongo:password@127.0.0.1:${port.host}`,
db: "mongo", db: generator.guid(),
}, },
} }
} }