Redo helpers

This commit is contained in:
Adria Navarro 2023-09-05 10:47:59 +02:00
parent 57801b5bcc
commit b27899b1e6
3 changed files with 63 additions and 10 deletions

View File

@ -17,7 +17,7 @@ import {
import _ from "lodash"
import { generator } from "@budibase/backend-core/tests"
import { utils } from "@budibase/backend-core"
import { testDatasourceConfig } from "../integrations/tests/utils"
import { PostgresProvider } from "../integrations/tests/utils"
const config = setup.getConfig()!
@ -34,10 +34,10 @@ describe("postgres integrations", () => {
manyToOneRelationshipInfo: ForeignTableInfo,
manyToManyRelationshipInfo: ForeignTableInfo
let pgDatasourceConfig: Datasource
let provider: PostgresProvider
beforeAll(async () => {
pgDatasourceConfig = await testDatasourceConfig.postgres()
provider = await PostgresProvider.init()
await config.init()
const apiKey = await config.generateApiKey()
@ -46,7 +46,9 @@ describe("postgres integrations", () => {
})
beforeEach(async () => {
postgresDatasource = await config.api.datasource.create(pgDatasourceConfig)
postgresDatasource = await config.api.datasource.create(
provider.getDsConfig()
)
async function createAuxTable(prefix: string) {
return await config.createTable({
@ -1004,17 +1006,21 @@ 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: pgDatasourceConfig,
datasource: 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 response = await config.api.datasource.verify({
datasource: {
...pgDatasourceConfig,
config: { ...pgDatasourceConfig.config, password: "wrongpassword" },
...dbConfig,
config: {
...dbConfig.config,
password: "wrongpassword",
},
},
})

View File

@ -1,5 +1,6 @@
import * as postgres from "./postgres"
import { Datasource } from "@budibase/types"
export * from "./postgres"
export const testDatasourceConfig = {
postgres: postgres.getDatasourceConfig,
export interface DatabasePlusTestProvider {
getDsConfig(): Datasource
}

View File

@ -1,5 +1,51 @@
import { Datasource, SourceName } from "@budibase/types"
import { GenericContainer, Wait } from "testcontainers"
import { DatabasePlusTestProvider } from "."
export class PostgresProvider implements DatabasePlusTestProvider {
private host: string
private port: number
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."
)
)
.start()
const host = containerPostgres.getContainerIpAddress()
const port = containerPostgres.getMappedPort(5432)
return new PostgresProvider(host, port)
}
getDsConfig(): Datasource {
return {
type: "datasource_plus",
source: SourceName.POSTGRES,
plus: true,
config: {
host: this.host,
port: this.port,
database: "postgres",
user: "postgres",
password: "password",
schema: "public",
ssl: false,
rejectUnauthorized: false,
ca: false,
},
}
}
}
export async function getDatasourceConfig(): Promise<Datasource> {
const containerPostgres = await new GenericContainer("postgres")