This commit is contained in:
Adria Navarro 2023-09-05 11:16:37 +02:00
parent 6240740a42
commit 887ebb2eeb
3 changed files with 22 additions and 55 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 { PostgresProvider } from "../integrations/tests/utils"
import { databaseTestProviders } from "../integrations/tests/utils"
const config = setup.getConfig()!
@ -26,8 +26,6 @@ jest.setTimeout(30000)
jest.unmock("pg")
jest.mock("../websockets")
const provider = new PostgresProvider()
describe("postgres integrations", () => {
let makeRequest: MakeRequestResponse,
postgresDatasource: Datasource,
@ -45,7 +43,7 @@ describe("postgres integrations", () => {
beforeEach(async () => {
postgresDatasource = await config.api.datasource.create(
await provider.getDsConfig()
await databaseTestProviders.postgres.getDsConfig()
)
async function createAuxTable(prefix: string) {
@ -1004,14 +1002,14 @@ 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: await provider.getDsConfig(),
datasource: await databaseTestProviders.postgres.getDsConfig(),
})
expect(response.status).toBe(200)
expect(response.body.connected).toBe(true)
})
it("should state an invalid datasource cannot connect", async () => {
const dbConfig = await provider.getDsConfig()
const dbConfig = await databaseTestProviders.postgres.getDsConfig()
const response = await config.api.datasource.verify({
datasource: {
...dbConfig,

View File

@ -1,6 +1,10 @@
import { Datasource } from "@budibase/types"
export * from "./postgres"
import * as pg from "./postgres"
export interface DatabasePlusTestProvider {
getDsConfig(): Promise<Datasource>
}
export const databaseTestProviders = {
postgres: pg,
}

View File

@ -1,13 +1,11 @@
import { Datasource, SourceName } from "@budibase/types"
import { GenericContainer, Wait, StartedTestContainer } from "testcontainers"
import { DatabasePlusTestProvider } from "."
export class PostgresProvider implements DatabasePlusTestProvider {
private container?: StartedTestContainer
let container: StartedTestContainer | undefined
async getDsConfig(): Promise<Datasource> {
if (!this.container) {
this.container = await new GenericContainer("postgres")
export async function getDsConfig(): Promise<Datasource> {
if (!container) {
container = await new GenericContainer("postgres")
.withExposedPorts(5432)
.withEnv("POSTGRES_PASSWORD", "password")
.withWaitStrategy(
@ -18,41 +16,8 @@ export class PostgresProvider implements DatabasePlusTestProvider {
.start()
}
const host = this.container.getContainerIpAddress()
const port = this.container.getMappedPort(5432)
return {
type: "datasource_plus",
source: SourceName.POSTGRES,
plus: true,
config: {
host,
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")
.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)
const host = container.getContainerIpAddress()
const port = container.getMappedPort(5432)
return {
type: "datasource_plus",