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,11 +151,8 @@ 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",
sourceId: INTERNAL_TABLE_SOURCE_ID,
sourceType: TableSourceType.INTERNAL,
schema: { schema: {
name: { name: {
type: FieldType.STRING, type: FieldType.STRING,
@ -164,6 +163,12 @@ describe.each([
}, },
}, },
}) })
)
await config.api.table.save({
...table,
name: generator.guid(),
})
expect(events.table.updated).toHaveBeenCalledTimes(1) expect(events.table.updated).toHaveBeenCalledTimes(1)
expect(events.table.updated).toHaveBeenCalledWith(table) expect(events.table.updated).toHaveBeenCalledWith(table)
@ -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!,
const res = await config.api.table.fetch() name: generator.guid(),
expect(res).toEqual( filters: [],
expect.arrayContaining([ schema: {},
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 tableId = config.table!._id!
enrichViewSchemasMock.mockImplementation(t => ({
...t,
views: {
view1: {
version: 2,
name: "view1",
schema: {},
id: "new_view_id",
tableId: t._id!,
},
},
}))
await config.api.viewV2.create({ tableId, name: generator.guid() })
await config.createLegacyView()
const res = await config.api.table.fetch() const res = await config.api.table.fetch()
expect(res).toEqual( const table = res.find(t => t._id === testTable._id)
expect.arrayContaining([ expect(table).toBeDefined()
expect.objectContaining({ expect(table!.views![viewV2.name]).toBeDefined()
_id: tableId, expect(table!.views![viewV2.name!]).toEqual({
views: { ...viewV2,
view1: { schema: {
version: 2, description: {
name: "view1", constraints: {
schema: {}, type: "string",
id: "new_view_id", },
tableId, name: "description",
type: "string",
visible: false,
},
name: {
constraints: {
type: "string",
},
name: "name",
type: "string",
visible: false,
}, },
}, },
}), })
])
) if (isInternal) {
expect(table!.views![legacyView.name!]).toBeDefined()
expect(table!.views![legacyView.name!]).toEqual({
...legacyView,
schema: {
description: {
constraints: {
type: "string",
},
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,8 +49,14 @@ export function tableForDatasource(
) )
} }
export function basicTable(datasource?: Datasource): Table { export function basicTable(
return tableForDatasource(datasource, { datasource?: Datasource,
...extra: Partial<Table>[]
): Table {
return tableForDatasource(
datasource,
{
name: "TestTable",
schema: { schema: {
name: { name: {
type: FieldType.STRING, type: FieldType.STRING,
@ -66,7 +73,9 @@ export function basicTable(datasource?: Datasource): Table {
}, },
}, },
}, },
}) },
...extra
)
} }
export function basicView(tableId: string) { export function basicView(tableId: string) {