From 5530d7f4b63b717c7879a6bfaf70387d35844aa8 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Tue, 26 Mar 2024 14:05:58 +0000 Subject: [PATCH] Migrate mongodb.spec.ts to new datasource providers. --- .../routes/tests/queries/generic-sql.spec.ts | 28 ++++++++++--------- .../api/routes/tests/queries/mongodb.spec.ts | 17 ++++++----- 2 files changed, 25 insertions(+), 20 deletions(-) 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 f9a3ac6e03..d393430060 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 @@ -1,6 +1,9 @@ import { Datasource, Query, SourceName } from "@budibase/types" import * as setup from "../utilities" -import { databaseTestProviders } from "../../../../integrations/tests/utils" +import { + DatabaseName, + getDatasource, +} from "../../../../integrations/tests/utils" import pg from "pg" import mysql from "mysql2/promise" import mssql from "mssql" @@ -34,12 +37,14 @@ const createTableSQL: Record = { const insertSQL = `INSERT INTO test_table (name) VALUES ('one'), ('two'), ('three'), ('four'), ('five')` const dropTableSQL = `DROP TABLE test_table;` -describe.each([ - ["postgres", databaseTestProviders.postgres], - ["mysql", databaseTestProviders.mysql], - ["mssql", databaseTestProviders.mssql], - ["mariadb", databaseTestProviders.mariadb], -])("queries (%s)", (dbName, dsProvider) => { +describe.each( + [ + DatabaseName.POSTGRES, + DatabaseName.MYSQL, + DatabaseName.SQL_SERVER, + DatabaseName.MARIADB, + ].map(name => [name, getDatasource(name)]) +)("queries (%s)", (dbName, dsProvider) => { const config = setup.getConfig() let datasource: Datasource @@ -61,7 +66,7 @@ describe.each([ // We re-fetch the datasource here because the one returned by // config.api.datasource.create has the password field blanked out, and we // need the password to connect to the database. - const ds = await dsProvider.datasource() + const ds = await dsProvider switch (ds.source) { case SourceName.POSTGRES: { const client = new pg.Client(ds.config!) @@ -97,9 +102,7 @@ describe.each([ beforeAll(async () => { await config.init() - datasource = await config.api.datasource.create( - await dsProvider.datasource() - ) + datasource = await config.api.datasource.create(await dsProvider) }) beforeEach(async () => { @@ -112,7 +115,6 @@ describe.each([ }) afterAll(async () => { - await dsProvider.stop() setup.afterAll() }) @@ -443,7 +445,7 @@ describe.each([ } catch (err: any) { error = err.message } - if (dbName === "mssql") { + if (dbName === DatabaseName.SQL_SERVER) { expect(error).toBeUndefined() } else { expect(error).toBeDefined() 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 492f24abf9..148f2c15ec 100644 --- a/packages/server/src/api/routes/tests/queries/mongodb.spec.ts +++ b/packages/server/src/api/routes/tests/queries/mongodb.spec.ts @@ -1,14 +1,17 @@ import { Datasource, Query } from "@budibase/types" import * as setup from "../utilities" -import { databaseTestProviders } from "../../../../integrations/tests/utils" +import { + DatabaseName, + getDatasource, +} from "../../../../integrations/tests/utils" import { MongoClient, type Collection, BSON } from "mongodb" - -const collection = "test_collection" +import { generator } from "@budibase/backend-core/tests" const expectValidId = expect.stringMatching(/^\w{24}$/) const expectValidBsonObjectId = expect.any(BSON.ObjectId) describe("/queries", () => { + let collection: string let config = setup.getConfig() let datasource: Datasource @@ -37,7 +40,7 @@ describe("/queries", () => { async function withClient( callback: (client: MongoClient) => Promise ): Promise { - const ds = await databaseTestProviders.mongodb.datasource() + const ds = await getDatasource(DatabaseName.MONGODB) const client = new MongoClient(ds.config!.connectionString) await client.connect() try { @@ -52,25 +55,25 @@ describe("/queries", () => { ): Promise { return await withClient(async client => { const db = client.db( - (await databaseTestProviders.mongodb.datasource()).config!.db + (await getDatasource(DatabaseName.MONGODB)).config!.db ) return await callback(db.collection(collection)) }) } afterAll(async () => { - await databaseTestProviders.mongodb.stop() setup.afterAll() }) beforeAll(async () => { await config.init() datasource = await config.api.datasource.create( - await databaseTestProviders.mongodb.datasource() + await getDatasource(DatabaseName.MONGODB) ) }) beforeEach(async () => { + collection = generator.guid() await withCollection(async collection => { await collection.insertMany([ { name: "one" },