This commit is contained in:
Adria Navarro 2023-09-05 11:11:47 +02:00
parent b27899b1e6
commit 6240740a42
3 changed files with 23 additions and 32 deletions

View File

@ -26,6 +26,8 @@ jest.setTimeout(30000)
jest.unmock("pg")
jest.mock("../websockets")
const provider = new PostgresProvider()
describe("postgres integrations", () => {
let makeRequest: MakeRequestResponse,
postgresDatasource: Datasource,
@ -34,11 +36,7 @@ describe("postgres integrations", () => {
manyToOneRelationshipInfo: ForeignTableInfo,
manyToManyRelationshipInfo: ForeignTableInfo
let provider: PostgresProvider
beforeAll(async () => {
provider = await PostgresProvider.init()
await config.init()
const apiKey = await config.generateApiKey()
@ -47,7 +45,7 @@ describe("postgres integrations", () => {
beforeEach(async () => {
postgresDatasource = await config.api.datasource.create(
provider.getDsConfig()
await provider.getDsConfig()
)
async function createAuxTable(prefix: string) {
@ -1006,14 +1004,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: provider.getDsConfig(),
datasource: await 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 dbConfig = await provider.getDsConfig()
const response = await config.api.datasource.verify({
datasource: {
...dbConfig,

View File

@ -2,5 +2,5 @@ import { Datasource } from "@budibase/types"
export * from "./postgres"
export interface DatabasePlusTestProvider {
getDsConfig(): Datasource
getDsConfig(): Promise<Datasource>
}

View File

@ -1,40 +1,33 @@
import { Datasource, SourceName } from "@budibase/types"
import { GenericContainer, Wait } from "testcontainers"
import { GenericContainer, Wait, StartedTestContainer } from "testcontainers"
import { DatabasePlusTestProvider } from "."
export class PostgresProvider implements DatabasePlusTestProvider {
private host: string
private port: number
private container?: StartedTestContainer
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."
async getDsConfig(): Promise<Datasource> {
if (!this.container) {
this.container = await new GenericContainer("postgres")
.withExposedPorts(5432)
.withEnv("POSTGRES_PASSWORD", "password")
.withWaitStrategy(
Wait.forLogMessage(
"PostgreSQL init process complete; ready for start up."
)
)
)
.start()
.start()
}
const host = containerPostgres.getContainerIpAddress()
const port = containerPostgres.getMappedPort(5432)
return new PostgresProvider(host, port)
}
const host = this.container.getContainerIpAddress()
const port = this.container.getMappedPort(5432)
getDsConfig(): Datasource {
return {
type: "datasource_plus",
source: SourceName.POSTGRES,
plus: true,
config: {
host: this.host,
port: this.port,
host,
port,
database: "postgres",
user: "postgres",
password: "password",