Add rest test and fix type

This commit is contained in:
Pedro Silva 2023-04-14 17:35:13 +01:00
parent 695f77fd1d
commit e20838fd42
5 changed files with 136 additions and 10 deletions

View File

@ -56,11 +56,3 @@ export interface RestConfig {
}
]
}
export interface CreateDatasourceResponse {
datasource: Datasource
}
export interface UpdatedDatasourceResponse {
datasource: Datasource
}

View File

@ -45,4 +45,7 @@ export interface PaginationValues {
export interface PreviewQueryRequest extends Omit<Query, "parameters"> {
parameters: {}
flags?: {
urlName?: boolean
}
}

View File

@ -34,8 +34,6 @@ export const postgresSQL = () => {
fetchSchema: true,
}
}
// Add the data source for MariaDB to the this file in the same style as above
export const mariaDB = () => {
return {
datasource: {
@ -55,3 +53,19 @@ export const mariaDB = () => {
fetchSchema: true,
}
}
export const restAPI = () => {
return {
datasource: {
name: "RestAPI",
source: "REST",
type: "datasource",
config: {
defaultHeaders: {},
rejectUnauthorized: true,
url: process.env.REST_API_BASE_URL,
},
},
fetchSchema: false,
}
}

View File

@ -72,4 +72,52 @@ export const expectedSchemaFields = {
postal_code: "string",
region: "string",
},
restAPI: {
abilities: "array",
base_experience: "number",
forms: "array",
game_indices: "array",
height: "number",
held_items: "array",
id: "number",
is_default: "string",
location_area_encounters: "string",
moves: "array",
name: "string",
order: "number",
past_types: "array",
species: "json",
sprites: "json",
stats: "array",
types: "array",
weight: "number",
},
}
const request = (datasourceId: string, fields: any, flags: any): any => {
return {
datasourceId: datasourceId,
fields: fields,
flags: flags,
name: "Query 1",
parameters: {},
queryVerb: "read",
schema: {},
transformer: "return data",
}
}
export const restAPI = (datasourceId: string): PreviewQueryRequest => {
const fields = {
authConfigId: null,
bodyType: "none",
disabledHeaders: {},
headers: {},
pagination: {},
path: `${process.env.REST_API_BASE_URL}/pokemon/ditto`,
queryString: "",
}
const flags = {
urlName: true,
}
return request(datasourceId, fields, flags)
}

View File

@ -0,0 +1,69 @@
import TestConfiguration from "../../config/TestConfiguration"
import * as fixtures from "../../fixtures"
import { Query } from "@budibase/types"
describe("Internal API - Data Sources: REST API", () => {
const config = new TestConfiguration()
beforeAll(async () => {
await config.beforeAll()
})
afterAll(async () => {
await config.afterAll()
})
it("Create an app with a data source - REST API", async () => {
// Create app
await config.createApp()
// Get all integrations
await config.api.integrations.getAll()
// Add data source
const [dataSourceResponse, dataSourceJson] =
await config.api.datasources.add(fixtures.datasources.restAPI())
// Update data source
const newDataSourceInfo = {
...dataSourceJson.datasource,
name: "RestAPI - Updated",
}
const [updatedDataSourceResponse, updatedDataSourceJson] =
await config.api.datasources.update(newDataSourceInfo)
// Query data source
const [queryResponse, queryJson] = await config.api.queries.preview(
fixtures.queries.restAPI(updatedDataSourceJson.datasource._id!)
)
expect(queryJson.rows.length).toEqual(1)
expect(queryJson.schemaFields).toEqual(
fixtures.queries.expectedSchemaFields.restAPI
)
// Save query
const datasourcetoSave: Query = {
...fixtures.queries.postgres(updatedDataSourceJson.datasource._id!),
parameters: [],
}
const [saveQueryResponse, saveQueryJson] = await config.api.queries.save(
datasourcetoSave
)
// Get Query
const [getQueryResponse, getQueryJson] = await config.api.queries.get(
saveQueryJson._id!
)
// Get Query permissions
const [getQueryPermissionsResponse, getQueryPermissionsJson] =
await config.api.permissions.getAll(saveQueryJson._id!)
// Delete data source
const deleteResponse = await config.api.datasources.delete(
updatedDataSourceJson.datasource._id!,
updatedDataSourceJson.datasource._rev!
)
})
})