1
0
Fork 0
mirror of synced 2024-09-19 10:48:30 +12:00
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.unmock("pg")
jest.mock("../websockets") jest.mock("../websockets")
const provider = new PostgresProvider()
describe("postgres integrations", () => { describe("postgres integrations", () => {
let makeRequest: MakeRequestResponse, let makeRequest: MakeRequestResponse,
postgresDatasource: Datasource, postgresDatasource: Datasource,
@ -34,11 +36,7 @@ describe("postgres integrations", () => {
manyToOneRelationshipInfo: ForeignTableInfo, manyToOneRelationshipInfo: ForeignTableInfo,
manyToManyRelationshipInfo: ForeignTableInfo manyToManyRelationshipInfo: ForeignTableInfo
let provider: PostgresProvider
beforeAll(async () => { beforeAll(async () => {
provider = await PostgresProvider.init()
await config.init() await config.init()
const apiKey = await config.generateApiKey() const apiKey = await config.generateApiKey()
@ -47,7 +45,7 @@ describe("postgres integrations", () => {
beforeEach(async () => { beforeEach(async () => {
postgresDatasource = await config.api.datasource.create( postgresDatasource = await config.api.datasource.create(
provider.getDsConfig() await provider.getDsConfig()
) )
async function createAuxTable(prefix: string) { async function createAuxTable(prefix: string) {
@ -1006,14 +1004,14 @@ describe("postgres integrations", () => {
describe("POST /api/datasources/verify", () => { describe("POST /api/datasources/verify", () => {
it("should be able to verify the connection", async () => { it("should be able to verify the connection", async () => {
const response = await config.api.datasource.verify({ const response = await config.api.datasource.verify({
datasource: provider.getDsConfig(), datasource: await provider.getDsConfig(),
}) })
expect(response.status).toBe(200) expect(response.status).toBe(200)
expect(response.body.connected).toBe(true) expect(response.body.connected).toBe(true)
}) })
it("should state an invalid datasource cannot connect", async () => { 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({ const response = await config.api.datasource.verify({
datasource: { datasource: {
...dbConfig, ...dbConfig,

View file

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

View file

@ -1,18 +1,13 @@
import { Datasource, SourceName } from "@budibase/types" import { Datasource, SourceName } from "@budibase/types"
import { GenericContainer, Wait } from "testcontainers" import { GenericContainer, Wait, StartedTestContainer } from "testcontainers"
import { DatabasePlusTestProvider } from "." import { DatabasePlusTestProvider } from "."
export class PostgresProvider implements DatabasePlusTestProvider { export class PostgresProvider implements DatabasePlusTestProvider {
private host: string private container?: StartedTestContainer
private port: number
private constructor(host: string, port: number) { async getDsConfig(): Promise<Datasource> {
this.host = host if (!this.container) {
this.port = port this.container = await new GenericContainer("postgres")
}
static async init() {
const containerPostgres = await new GenericContainer("postgres")
.withExposedPorts(5432) .withExposedPorts(5432)
.withEnv("POSTGRES_PASSWORD", "password") .withEnv("POSTGRES_PASSWORD", "password")
.withWaitStrategy( .withWaitStrategy(
@ -21,20 +16,18 @@ export class PostgresProvider implements DatabasePlusTestProvider {
) )
) )
.start() .start()
const host = containerPostgres.getContainerIpAddress()
const port = containerPostgres.getMappedPort(5432)
return new PostgresProvider(host, port)
} }
getDsConfig(): Datasource { const host = this.container.getContainerIpAddress()
const port = this.container.getMappedPort(5432)
return { return {
type: "datasource_plus", type: "datasource_plus",
source: SourceName.POSTGRES, source: SourceName.POSTGRES,
plus: true, plus: true,
config: { config: {
host: this.host, host,
port: this.port, port,
database: "postgres", database: "postgres",
user: "postgres", user: "postgres",
password: "password", password: "password",