Simplify
This commit is contained in:
parent
6240740a42
commit
887ebb2eeb
|
@ -17,7 +17,7 @@ import {
|
||||||
import _ from "lodash"
|
import _ from "lodash"
|
||||||
import { generator } from "@budibase/backend-core/tests"
|
import { generator } from "@budibase/backend-core/tests"
|
||||||
import { utils } from "@budibase/backend-core"
|
import { utils } from "@budibase/backend-core"
|
||||||
import { PostgresProvider } from "../integrations/tests/utils"
|
import { databaseTestProviders } from "../integrations/tests/utils"
|
||||||
|
|
||||||
const config = setup.getConfig()!
|
const config = setup.getConfig()!
|
||||||
|
|
||||||
|
@ -26,8 +26,6 @@ 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,
|
||||||
|
@ -45,7 +43,7 @@ describe("postgres integrations", () => {
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
postgresDatasource = await config.api.datasource.create(
|
postgresDatasource = await config.api.datasource.create(
|
||||||
await provider.getDsConfig()
|
await databaseTestProviders.postgres.getDsConfig()
|
||||||
)
|
)
|
||||||
|
|
||||||
async function createAuxTable(prefix: string) {
|
async function createAuxTable(prefix: string) {
|
||||||
|
@ -1004,14 +1002,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: await provider.getDsConfig(),
|
datasource: await databaseTestProviders.postgres.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 = await provider.getDsConfig()
|
const dbConfig = await databaseTestProviders.postgres.getDsConfig()
|
||||||
const response = await config.api.datasource.verify({
|
const response = await config.api.datasource.verify({
|
||||||
datasource: {
|
datasource: {
|
||||||
...dbConfig,
|
...dbConfig,
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
import { Datasource } from "@budibase/types"
|
import { Datasource } from "@budibase/types"
|
||||||
export * from "./postgres"
|
import * as pg from "./postgres"
|
||||||
|
|
||||||
export interface DatabasePlusTestProvider {
|
export interface DatabasePlusTestProvider {
|
||||||
getDsConfig(): Promise<Datasource>
|
getDsConfig(): Promise<Datasource>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const databaseTestProviders = {
|
||||||
|
postgres: pg,
|
||||||
|
}
|
||||||
|
|
|
@ -1,58 +1,23 @@
|
||||||
import { Datasource, SourceName } from "@budibase/types"
|
import { Datasource, SourceName } from "@budibase/types"
|
||||||
import { GenericContainer, Wait, StartedTestContainer } from "testcontainers"
|
import { GenericContainer, Wait, StartedTestContainer } from "testcontainers"
|
||||||
import { DatabasePlusTestProvider } from "."
|
|
||||||
|
|
||||||
export class PostgresProvider implements DatabasePlusTestProvider {
|
let container: StartedTestContainer | undefined
|
||||||
private container?: StartedTestContainer
|
|
||||||
|
|
||||||
async getDsConfig(): Promise<Datasource> {
|
export async function getDsConfig(): Promise<Datasource> {
|
||||||
if (!this.container) {
|
if (!container) {
|
||||||
this.container = await new GenericContainer("postgres")
|
container = await new GenericContainer("postgres")
|
||||||
.withExposedPorts(5432)
|
.withExposedPorts(5432)
|
||||||
.withEnv("POSTGRES_PASSWORD", "password")
|
.withEnv("POSTGRES_PASSWORD", "password")
|
||||||
.withWaitStrategy(
|
.withWaitStrategy(
|
||||||
Wait.forLogMessage(
|
Wait.forLogMessage(
|
||||||
"PostgreSQL init process complete; ready for start up."
|
"PostgreSQL init process complete; ready for start up."
|
||||||
)
|
|
||||||
)
|
)
|
||||||
.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()
|
||||||
.start()
|
}
|
||||||
|
|
||||||
const host = containerPostgres.getContainerIpAddress()
|
const host = container.getContainerIpAddress()
|
||||||
const port = containerPostgres.getMappedPort(5432)
|
const port = container.getMappedPort(5432)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
type: "datasource_plus",
|
type: "datasource_plus",
|
||||||
|
|
Loading…
Reference in New Issue