Migrate mongodb.spec.ts to new datasource providers.
This commit is contained in:
parent
158964c4d2
commit
5530d7f4b6
2 changed files with 25 additions and 20 deletions
|
@ -1,6 +1,9 @@
|
||||||
import { Datasource, Query, SourceName } from "@budibase/types"
|
import { Datasource, Query, SourceName } from "@budibase/types"
|
||||||
import * as setup from "../utilities"
|
import * as setup from "../utilities"
|
||||||
import { databaseTestProviders } from "../../../../integrations/tests/utils"
|
import {
|
||||||
|
DatabaseName,
|
||||||
|
getDatasource,
|
||||||
|
} from "../../../../integrations/tests/utils"
|
||||||
import pg from "pg"
|
import pg from "pg"
|
||||||
import mysql from "mysql2/promise"
|
import mysql from "mysql2/promise"
|
||||||
import mssql from "mssql"
|
import mssql from "mssql"
|
||||||
|
@ -34,12 +37,14 @@ const createTableSQL: Record<string, string> = {
|
||||||
const insertSQL = `INSERT INTO test_table (name) VALUES ('one'), ('two'), ('three'), ('four'), ('five')`
|
const insertSQL = `INSERT INTO test_table (name) VALUES ('one'), ('two'), ('three'), ('four'), ('five')`
|
||||||
const dropTableSQL = `DROP TABLE test_table;`
|
const dropTableSQL = `DROP TABLE test_table;`
|
||||||
|
|
||||||
describe.each([
|
describe.each(
|
||||||
["postgres", databaseTestProviders.postgres],
|
[
|
||||||
["mysql", databaseTestProviders.mysql],
|
DatabaseName.POSTGRES,
|
||||||
["mssql", databaseTestProviders.mssql],
|
DatabaseName.MYSQL,
|
||||||
["mariadb", databaseTestProviders.mariadb],
|
DatabaseName.SQL_SERVER,
|
||||||
])("queries (%s)", (dbName, dsProvider) => {
|
DatabaseName.MARIADB,
|
||||||
|
].map(name => [name, getDatasource(name)])
|
||||||
|
)("queries (%s)", (dbName, dsProvider) => {
|
||||||
const config = setup.getConfig()
|
const config = setup.getConfig()
|
||||||
let datasource: Datasource
|
let datasource: Datasource
|
||||||
|
|
||||||
|
@ -61,7 +66,7 @@ describe.each([
|
||||||
// We re-fetch the datasource here because the one returned by
|
// We re-fetch the datasource here because the one returned by
|
||||||
// config.api.datasource.create has the password field blanked out, and we
|
// config.api.datasource.create has the password field blanked out, and we
|
||||||
// need the password to connect to the database.
|
// need the password to connect to the database.
|
||||||
const ds = await dsProvider.datasource()
|
const ds = await dsProvider
|
||||||
switch (ds.source) {
|
switch (ds.source) {
|
||||||
case SourceName.POSTGRES: {
|
case SourceName.POSTGRES: {
|
||||||
const client = new pg.Client(ds.config!)
|
const client = new pg.Client(ds.config!)
|
||||||
|
@ -97,9 +102,7 @@ describe.each([
|
||||||
|
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
await config.init()
|
await config.init()
|
||||||
datasource = await config.api.datasource.create(
|
datasource = await config.api.datasource.create(await dsProvider)
|
||||||
await dsProvider.datasource()
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
|
@ -112,7 +115,6 @@ describe.each([
|
||||||
})
|
})
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
await dsProvider.stop()
|
|
||||||
setup.afterAll()
|
setup.afterAll()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -443,7 +445,7 @@ describe.each([
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
error = err.message
|
error = err.message
|
||||||
}
|
}
|
||||||
if (dbName === "mssql") {
|
if (dbName === DatabaseName.SQL_SERVER) {
|
||||||
expect(error).toBeUndefined()
|
expect(error).toBeUndefined()
|
||||||
} else {
|
} else {
|
||||||
expect(error).toBeDefined()
|
expect(error).toBeDefined()
|
||||||
|
|
|
@ -1,14 +1,17 @@
|
||||||
import { Datasource, Query } from "@budibase/types"
|
import { Datasource, Query } from "@budibase/types"
|
||||||
import * as setup from "../utilities"
|
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"
|
import { MongoClient, type Collection, BSON } from "mongodb"
|
||||||
|
import { generator } from "@budibase/backend-core/tests"
|
||||||
const collection = "test_collection"
|
|
||||||
|
|
||||||
const expectValidId = expect.stringMatching(/^\w{24}$/)
|
const expectValidId = expect.stringMatching(/^\w{24}$/)
|
||||||
const expectValidBsonObjectId = expect.any(BSON.ObjectId)
|
const expectValidBsonObjectId = expect.any(BSON.ObjectId)
|
||||||
|
|
||||||
describe("/queries", () => {
|
describe("/queries", () => {
|
||||||
|
let collection: string
|
||||||
let config = setup.getConfig()
|
let config = setup.getConfig()
|
||||||
let datasource: Datasource
|
let datasource: Datasource
|
||||||
|
|
||||||
|
@ -37,7 +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 databaseTestProviders.mongodb.datasource()
|
const ds = await getDatasource(DatabaseName.MONGODB)
|
||||||
const client = new MongoClient(ds.config!.connectionString)
|
const client = new MongoClient(ds.config!.connectionString)
|
||||||
await client.connect()
|
await client.connect()
|
||||||
try {
|
try {
|
||||||
|
@ -52,25 +55,25 @@ describe("/queries", () => {
|
||||||
): Promise<T> {
|
): Promise<T> {
|
||||||
return await withClient(async client => {
|
return await withClient(async client => {
|
||||||
const db = client.db(
|
const db = client.db(
|
||||||
(await databaseTestProviders.mongodb.datasource()).config!.db
|
(await getDatasource(DatabaseName.MONGODB)).config!.db
|
||||||
)
|
)
|
||||||
return await callback(db.collection(collection))
|
return await callback(db.collection(collection))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
await databaseTestProviders.mongodb.stop()
|
|
||||||
setup.afterAll()
|
setup.afterAll()
|
||||||
})
|
})
|
||||||
|
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
await config.init()
|
await config.init()
|
||||||
datasource = await config.api.datasource.create(
|
datasource = await config.api.datasource.create(
|
||||||
await databaseTestProviders.mongodb.datasource()
|
await getDatasource(DatabaseName.MONGODB)
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
|
collection = generator.guid()
|
||||||
await withCollection(async collection => {
|
await withCollection(async collection => {
|
||||||
await collection.insertMany([
|
await collection.insertMany([
|
||||||
{ name: "one" },
|
{ name: "one" },
|
||||||
|
|
Loading…
Reference in a new issue