Simplify
This commit is contained in:
parent
b27899b1e6
commit
6240740a42
|
@ -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,
|
||||||
|
|
|
@ -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>
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,40 +1,33 @@
|
||||||
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")
|
||||||
}
|
.withExposedPorts(5432)
|
||||||
|
.withEnv("POSTGRES_PASSWORD", "password")
|
||||||
static async init() {
|
.withWaitStrategy(
|
||||||
const containerPostgres = await new GenericContainer("postgres")
|
Wait.forLogMessage(
|
||||||
.withExposedPorts(5432)
|
"PostgreSQL init process complete; ready for start up."
|
||||||
.withEnv("POSTGRES_PASSWORD", "password")
|
)
|
||||||
.withWaitStrategy(
|
|
||||||
Wait.forLogMessage(
|
|
||||||
"PostgreSQL init process complete; ready for start up."
|
|
||||||
)
|
)
|
||||||
)
|
.start()
|
||||||
.start()
|
}
|
||||||
|
|
||||||
const host = containerPostgres.getContainerIpAddress()
|
const host = this.container.getContainerIpAddress()
|
||||||
const port = containerPostgres.getMappedPort(5432)
|
const port = this.container.getMappedPort(5432)
|
||||||
return new PostgresProvider(host, port)
|
|
||||||
}
|
|
||||||
|
|
||||||
getDsConfig(): Datasource {
|
|
||||||
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",
|
||||||
|
|
Loading…
Reference in New Issue