Use api for testing

This commit is contained in:
Adria Navarro 2023-09-12 20:17:21 +02:00
parent f0872c1fa3
commit 458de1282e
6 changed files with 36 additions and 28 deletions

View File

@ -6,13 +6,11 @@ import * as setup from "./utilities"
import { context, roles, tenancy } from "@budibase/backend-core" import { context, roles, tenancy } from "@budibase/backend-core"
import { quotas } from "@budibase/pro" import { quotas } from "@budibase/pro"
import { import {
Datasource,
FieldType, FieldType,
MonthlyQuotaName, MonthlyQuotaName,
PermissionLevel, PermissionLevel,
QuotaUsageType, QuotaUsageType,
Row, Row,
SaveRowRequest,
SaveTableRequest, SaveTableRequest,
SortOrder, SortOrder,
SortType, SortType,

View File

@ -1,6 +1,6 @@
import { generator } from "@budibase/backend-core/tests" import { generator } from "@budibase/backend-core/tests"
import { events, context } from "@budibase/backend-core" import { events, context } from "@budibase/backend-core"
import { FieldType, Table } from "@budibase/types" import { FieldType, Table, ViewCalculation } from "@budibase/types"
import { checkBuilderEndpoint } from "./utilities/TestFunctions" import { checkBuilderEndpoint } from "./utilities/TestFunctions"
import * as setup from "./utilities" import * as setup from "./utilities"
const { basicTable } = setup.structures const { basicTable } = setup.structures
@ -90,8 +90,10 @@ describe("/tables", () => {
await config.createLegacyView({ await config.createLegacyView({
name: "TestView", name: "TestView",
field: "Price", field: "Price",
calculation: "stats", calculation: ViewCalculation.STATISTICS,
tableId: testTable._id, tableId: testTable._id!,
schema: {},
filters: [],
}) })
const testRow = await request const testRow = await request
@ -254,7 +256,7 @@ describe("/tables", () => {
})) }))
await config.api.viewV2.create({ tableId }) await config.api.viewV2.create({ tableId })
await config.createLegacyView({ tableId, name: generator.guid() }) await config.createLegacyView()
const res = await config.api.table.fetch() const res = await config.api.table.fetch()
@ -366,7 +368,7 @@ describe("/tables", () => {
.expect("Content-Type", /json/) .expect("Content-Type", /json/)
.expect(200) .expect(200)
expect(res.body.message).toEqual(`Table ${testTable._id} deleted.`) expect(res.body.message).toEqual(`Table ${testTable._id} deleted.`)
const dependentTable = await config.getTable(linkedTable._id) const dependentTable = await config.api.table.get(linkedTable._id!)
expect(dependentTable.schema.TestTable).not.toBeDefined() expect(dependentTable.schema.TestTable).not.toBeDefined()
}) })

View File

@ -6,6 +6,7 @@ import {
SortOrder, SortOrder,
SortType, SortType,
Table, Table,
UIFieldMetadata,
UpdateViewRequest, UpdateViewRequest,
ViewV2, ViewV2,
} from "@budibase/types" } from "@budibase/types"
@ -418,9 +419,12 @@ describe.each([
const res = await config.api.viewV2.create(newView) const res = await config.api.viewV2.create(newView)
const view = await config.api.viewV2.get(res.id) const view = await config.api.viewV2.get(res.id)
expect(view!.schema?.Price).toBeUndefined() expect(view!.schema?.Price).toBeUndefined()
const updatedTable = await config.getTable(table._id!) const updatedTable = await config.api.table.get(table._id!)
const viewSchema = updatedTable.views[view!.name!].schema const viewSchema = updatedTable.views![view!.name!].schema as Record<
expect(viewSchema.Price.visible).toEqual(false) string,
UIFieldMetadata
>
expect(viewSchema.Price?.visible).toEqual(false)
}) })
}) })
}) })

View File

@ -397,7 +397,7 @@ describe("postgres integrations", () => {
expect(res.status).toBe(200) expect(res.status).toBe(200)
expect(res.body).toEqual(updatedRow) expect(res.body).toEqual(updatedRow)
const persistedRow = await config.getRow( const persistedRow = await config.api.row.get(
primaryPostgresTable._id!, primaryPostgresTable._id!,
row.id row.id
) )

View File

@ -557,11 +557,6 @@ class TestConfiguration {
return this.updateTable(config, options) return this.updateTable(config, options)
} }
async getTable(tableId?: string) {
tableId = tableId || this.table?._id
return this._req(null, { tableId }, controllers.table.find)
}
async createLinkedTable( async createLinkedTable(
config?: Table, config?: Table,
relationshipType = RelationshipType.ONE_TO_MANY, relationshipType = RelationshipType.ONE_TO_MANY,
@ -609,11 +604,7 @@ class TestConfiguration {
} }
const tableId = (config && config.tableId) || this.table._id const tableId = (config && config.tableId) || this.table._id
config = config || basicRow(tableId!) config = config || basicRow(tableId!)
return this._req(config, { tableId }, controllers.row.save) return this.api.row.save(tableId!, config)
}
async getRow(tableId: string, rowId: string): Promise<Row> {
return this._req(null, { tableId, rowId }, controllers.row.find)
} }
async getRows(tableId: string) { async getRows(tableId: string) {
@ -648,7 +639,7 @@ class TestConfiguration {
} }
const view = config || { const view = config || {
tableId: this.table!._id, tableId: this.table!._id,
name: "ViewTest", name: generator.guid(),
} }
return this._req(view, null, controllers.view.v1.save) return this._req(view, null, controllers.view.v1.save)
} }
@ -721,12 +712,8 @@ class TestConfiguration {
} }
async updateDatasource(datasource: Datasource): Promise<Datasource> { async updateDatasource(datasource: Datasource): Promise<Datasource> {
const response = await this._req( const response = await this.api.datasource.update(datasource)
datasource, this.datasource = response
{ datasourceId: datasource._id },
controllers.datasource.update
)
this.datasource = response.datasource
return this.datasource! return this.datasource!
} }

View File

@ -29,6 +29,23 @@ export class DatasourceAPI extends TestAPI {
return result.body.datasource as Datasource return result.body.datasource as Datasource
} }
update = async (
config: Datasource,
{ expectStatus } = { expectStatus: 200 }
): Promise<Datasource> => {
const body: CreateDatasourceRequest = {
datasource: config,
tablesFilter: [],
}
const result = await this.request
.put(`/api/datasources/${config._id}`)
.send(body)
.set(this.config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(expectStatus)
return result.body.datasource as Datasource
}
verify = async ( verify = async (
data: VerifyDatasourceRequest, data: VerifyDatasourceRequest,
{ expectStatus } = { expectStatus: 200 } { expectStatus } = { expectStatus: 200 }