Migrate DatasourceAPI.

This commit is contained in:
Sam Rose 2024-02-29 15:50:18 +00:00
parent 5163434b08
commit 4fbe03bbda
No known key found for this signature in database
5 changed files with 77 additions and 75 deletions

View File

@ -397,15 +397,16 @@ describe("/queries", () => {
})
it("should fail with invalid integration type", async () => {
const response = await config.api.datasource.create(
{
const datasource: Datasource = {
...basicDatasource().datasource,
source: "INVALID_INTEGRATION" as SourceName,
}
await config.api.datasource.create(datasource, {
status: 500,
body: {
message: "No datasource implementation found.",
},
{ expectStatus: 500, rawResponse: true }
)
expect(response.body.message).toBe("No datasource implementation found.")
})
})
})

View File

@ -1040,16 +1040,22 @@ describe("postgres integrations", () => {
describe("POST /api/datasources/verify", () => {
it("should be able to verify the connection", async () => {
const response = await config.api.datasource.verify({
await config.api.datasource.verify(
{
datasource: await databaseTestProviders.postgres.datasource(),
})
expect(response.status).toBe(200)
expect(response.body.connected).toBe(true)
},
{
body: {
connected: true,
},
}
)
})
it("should state an invalid datasource cannot connect", async () => {
const dbConfig = await databaseTestProviders.postgres.datasource()
const response = await config.api.datasource.verify({
const response = await config.api.datasource.verify(
{
datasource: {
...dbConfig,
config: {
@ -1057,11 +1063,14 @@ describe("postgres integrations", () => {
password: "wrongpassword",
},
},
})
expect(response.status).toBe(200)
expect(response.body.connected).toBe(false)
expect(response.body.error).toBeDefined()
},
{
body: {
connected: false,
error: 'password authentication failed for user "postgres"',
},
}
)
})
})

View File

@ -3,6 +3,10 @@ import { SuperTest, Test, Response } from "supertest"
import { ReadStream } from "fs"
type Headers = Record<string, string | string[] | undefined>
type SuccessStatus = 200 | 201 | 204
type ErrorStatus = 400 | 401 | 403 | 404 | 500 | 502 | 503 | 504
type Status = SuccessStatus | ErrorStatus
type Method = "get" | "post" | "put" | "patch" | "delete"
export interface AttachedFile {
name: string
@ -21,9 +25,10 @@ function isAttachedFile(file: any): file is AttachedFile {
}
export interface Expectations {
status?: number
status?: Status
headers?: Record<string, string | RegExp>
headersNotPresent?: string[]
body?: Record<string, any>
}
export interface RequestOpts {
@ -137,7 +142,7 @@ export abstract class TestAPI {
}
protected _request = async <T>(
method: "get" | "post" | "put" | "patch" | "delete",
method: Method,
url: string,
opts?: RequestOpts
): Promise<T> => {
@ -180,6 +185,10 @@ export abstract class TestAPI {
}
}
return response.body as T
if (expectations?.body) {
expect(response.body).toMatchObject(expectations.body)
}
return response.body
}
}

View File

@ -1,63 +1,48 @@
import {
CreateDatasourceRequest,
Datasource,
VerifyDatasourceRequest,
CreateDatasourceResponse,
UpdateDatasourceResponse,
UpdateDatasourceRequest,
} from "@budibase/types"
import TestConfiguration from "../TestConfiguration"
import { TestAPI } from "./base"
import supertest from "supertest"
import { Expectations, TestAPI } from "./base"
export class DatasourceAPI extends TestAPI {
constructor(config: TestConfiguration) {
super(config)
}
create = async <B extends boolean = false>(
create = async (
config: Datasource,
expectations?: Expectations
): Promise<Datasource> => {
const response = await this._post<CreateDatasourceResponse>(
`/api/datasources`,
{
expectStatus,
rawResponse,
}: { expectStatus?: number; rawResponse?: B } = {}
): Promise<B extends false ? Datasource : supertest.Response> => {
const body: CreateDatasourceRequest = {
body: {
datasource: config,
tablesFilter: [],
},
expectations,
}
const result = await this.request
.post(`/api/datasources`)
.send(body)
.set(this.config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(expectStatus || 200)
if (rawResponse) {
return result as any
}
return result.body.datasource
)
return response.datasource
}
update = async (
datasource: Datasource,
{ expectStatus } = { expectStatus: 200 }
datasource: UpdateDatasourceRequest,
expectations?: Expectations
): Promise<Datasource> => {
const result = await this.request
.put(`/api/datasources/${datasource._id}`)
.send(datasource)
.set(this.config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(expectStatus)
return result.body.datasource as Datasource
const response = await this._put<UpdateDatasourceResponse>(
`/api/datasources/${datasource._id}`,
{ body: datasource, expectations }
)
return response.datasource
}
verify = async (
data: VerifyDatasourceRequest,
{ expectStatus } = { expectStatus: 200 }
expectations?: Expectations
) => {
const result = await this.request
.post(`/api/datasources/verify`)
.send(data)
.set(this.config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(expectStatus)
return result
return await this._post(`/api/datasources/verify`, {
body: data,
expectations,
})
}
}

View File

@ -32,9 +32,7 @@ export interface FetchDatasourceInfoResponse {
tableNames: string[]
}
export interface UpdateDatasourceRequest extends Datasource {
datasource: Datasource
}
export type UpdateDatasourceRequest = Datasource
export interface BuildSchemaFromSourceRequest {
tablesFilter?: string[]