More progress on fixing up table.spec.ts.

This commit is contained in:
Sam Rose 2024-04-05 11:50:27 +01:00
parent f80a207d28
commit bc072e1424
No known key found for this signature in database
2 changed files with 121 additions and 89 deletions

View File

@ -17,7 +17,6 @@ import {
} from "@budibase/types" } from "@budibase/types"
import { checkBuilderEndpoint } from "./utilities/TestFunctions" import { checkBuilderEndpoint } from "./utilities/TestFunctions"
import * as setup from "./utilities" import * as setup from "./utilities"
import sdk from "../../../sdk"
import * as uuid from "uuid" import * as uuid from "uuid"
import tk from "timekeeper" import tk from "timekeeper"
@ -59,8 +58,11 @@ describe.each([
}) })
it("returns a success message when the table is successfully created", async () => { it("returns a success message when the table is successfully created", async () => {
const table = await config.api.table.save(basicTable(datasource)) const name = generator.guid()
expect(table.name).toEqual("TestTable") const table = await config.api.table.save(
tableForDatasource(datasource, { name })
)
expect(table.name).toEqual(name)
expect(events.table.created).toHaveBeenCalledTimes(1) expect(events.table.created).toHaveBeenCalledTimes(1)
expect(events.table.created).toHaveBeenCalledWith(table) expect(events.table.created).toHaveBeenCalledWith(table)
}) })
@ -149,20 +151,23 @@ describe.each([
describe("update", () => { describe("update", () => {
it("updates a table", async () => { it("updates a table", async () => {
const table = await config.api.table.save({ const table = await config.api.table.save(
name: "TestTable", tableForDatasource(datasource, {
type: "table", schema: {
sourceId: INTERNAL_TABLE_SOURCE_ID, name: {
sourceType: TableSourceType.INTERNAL, type: FieldType.STRING,
schema: { name: "name",
name: { constraints: {
type: FieldType.STRING, type: "string",
name: "name", },
constraints: {
type: "string",
}, },
}, },
}, })
)
await config.api.table.save({
...table,
name: generator.guid(),
}) })
expect(events.table.updated).toHaveBeenCalledTimes(1) expect(events.table.updated).toHaveBeenCalledTimes(1)
@ -209,7 +214,7 @@ describe.each([
expect(res.name).toBeUndefined() expect(res.name).toBeUndefined()
}) })
it.only("updates only the passed fields", async () => { it("updates only the passed fields", async () => {
const table = await config.api.table.save( const table = await config.api.table.save(
tableForDatasource(datasource, { tableForDatasource(datasource, {
name: "TestTable", name: "TestTable",
@ -350,23 +355,20 @@ describe.each([
describe("fetch", () => { describe("fetch", () => {
let testTable: Table let testTable: Table
const enrichViewSchemasMock = jest.spyOn(sdk.tables, "enrichViewSchemas")
beforeEach(async () => { beforeEach(async () => {
testTable = await config.api.table.save(basicTable(datasource)) testTable = await config.api.table.save(
basicTable(datasource, { name: generator.guid() })
)
}) })
afterAll(() => { it("returns all tables", async () => {
enrichViewSchemasMock.mockRestore()
})
it("returns all the tables for that instance in the response body", async () => {
const res = await config.api.table.fetch() const res = await config.api.table.fetch()
const table = res.find(t => t._id === testTable._id) const table = res.find(t => t._id === testTable._id)
expect(table).toBeDefined() expect(table).toBeDefined()
expect(table!.name).toEqual(testTable.name) expect(table!.name).toEqual(testTable.name)
expect(table!.type).toEqual("table") expect(table!.type).toEqual("table")
expect(table!.sourceType).toEqual("internal") expect(table!.sourceType).toEqual(testTable.sourceType)
}) })
it("should apply authorization to endpoint", async () => { it("should apply authorization to endpoint", async () => {
@ -377,63 +379,84 @@ describe.each([
}) })
}) })
it("should fetch views", async () => { it("should enrich the view schemas", async () => {
const tableId = config.table!._id! const viewV2 = await config.api.viewV2.create({
const views = [ tableId: testTable._id!,
await config.api.viewV2.create({ tableId, name: generator.guid() }), name: generator.guid(),
await config.api.viewV2.create({ tableId, name: generator.guid() }), })
] const legacyView = await config.api.legacyView.save({
tableId: testTable._id!,
name: generator.guid(),
filters: [],
schema: {},
})
const res = await config.api.table.fetch() const res = await config.api.table.fetch()
expect(res).toEqual(
expect.arrayContaining([
expect.objectContaining({
_id: tableId,
views: views.reduce((p, c) => {
p[c.name] = { ...c, schema: expect.anything() }
return p
}, {} as any),
}),
])
)
})
it("should enrich the view schemas for viewsV2", async () => { const table = res.find(t => t._id === testTable._id)
const tableId = config.table!._id! expect(table).toBeDefined()
enrichViewSchemasMock.mockImplementation(t => ({ expect(table!.views![viewV2.name]).toBeDefined()
...t, expect(table!.views![viewV2.name!]).toEqual({
views: { ...viewV2,
view1: { schema: {
version: 2, description: {
name: "view1", constraints: {
schema: {}, type: "string",
id: "new_view_id", },
tableId: t._id!, name: "description",
type: "string",
visible: false,
},
name: {
constraints: {
type: "string",
},
name: "name",
type: "string",
visible: false,
}, },
}, },
})) })
await config.api.viewV2.create({ tableId, name: generator.guid() }) if (isInternal) {
await config.createLegacyView() expect(table!.views![legacyView.name!]).toBeDefined()
expect(table!.views![legacyView.name!]).toEqual({
const res = await config.api.table.fetch() ...legacyView,
schema: {
expect(res).toEqual( description: {
expect.arrayContaining([ constraints: {
expect.objectContaining({ type: "string",
_id: tableId,
views: {
view1: {
version: 2,
name: "view1",
schema: {},
id: "new_view_id",
tableId,
}, },
name: "description",
type: "string",
}, },
}), name: {
]) constraints: {
) type: "string",
},
name: "name",
type: "string",
},
},
})
}
// expect(res).toEqual(
// expect.arrayContaining([
// expect.objectContaining({
// _id: tableId,
// views: {
// view1: {
// version: 2,
// name: "view1",
// schema: {},
// id: "new_view_id",
// tableId,
// },
// },
// }),
// ])
// )
}) })
}) })

View File

@ -27,6 +27,7 @@ import {
} from "@budibase/types" } from "@budibase/types"
import { LoopInput, LoopStepType } from "../../definitions/automations" import { LoopInput, LoopStepType } from "../../definitions/automations"
import { merge } from "lodash" import { merge } from "lodash"
import { generator } from "@budibase/backend-core/tests"
const { BUILTIN_ROLE_IDS } = roles const { BUILTIN_ROLE_IDS } = roles
@ -36,7 +37,7 @@ export function tableForDatasource(
): Table { ): Table {
return merge( return merge(
{ {
name: "TestTable", name: generator.guid(),
type: "table", type: "table",
sourceType: datasource sourceType: datasource
? TableSourceType.EXTERNAL ? TableSourceType.EXTERNAL
@ -48,25 +49,33 @@ export function tableForDatasource(
) )
} }
export function basicTable(datasource?: Datasource): Table { export function basicTable(
return tableForDatasource(datasource, { datasource?: Datasource,
schema: { ...extra: Partial<Table>[]
name: { ): Table {
type: FieldType.STRING, return tableForDatasource(
name: "name", datasource,
constraints: { {
type: "string", name: "TestTable",
schema: {
name: {
type: FieldType.STRING,
name: "name",
constraints: {
type: "string",
},
}, },
}, description: {
description: { type: FieldType.STRING,
type: FieldType.STRING, name: "description",
name: "description", constraints: {
constraints: { type: "string",
type: "string", },
}, },
}, },
}, },
}) ...extra
)
} }
export function basicView(tableId: string) { export function basicView(tableId: string) {