Test sdks
This commit is contained in:
parent
d8b801e647
commit
c58b145afd
|
@ -10,8 +10,6 @@ import {
|
|||
Database,
|
||||
TableResponse,
|
||||
TableViewsResponse,
|
||||
TableSchema,
|
||||
UIFieldMetadata,
|
||||
} from "@budibase/types"
|
||||
import datasources from "../datasources"
|
||||
import { populateExternalTableSchemas, isEditableColumn } from "./validation"
|
||||
|
@ -64,49 +62,15 @@ async function getTable(tableId: any): Promise<Table> {
|
|||
}
|
||||
}
|
||||
|
||||
function enrichSchema(
|
||||
tableSchema: TableSchema,
|
||||
viewOverrides: Record<string, UIFieldMetadata>
|
||||
) {
|
||||
const result: TableSchema = {}
|
||||
const viewOverridesEntries = Object.entries(viewOverrides)
|
||||
const viewSetsOrder = viewOverridesEntries.some(([_, v]) => v.order)
|
||||
for (const [columnName, columnUIMetadata] of viewOverridesEntries) {
|
||||
if (!columnUIMetadata.visible) {
|
||||
continue
|
||||
}
|
||||
|
||||
if (!tableSchema[columnName]) {
|
||||
continue
|
||||
}
|
||||
|
||||
const tableFieldSchema = tableSchema[columnName]
|
||||
if (viewSetsOrder) {
|
||||
delete tableFieldSchema.order
|
||||
}
|
||||
|
||||
result[columnName] = _.merge(tableFieldSchema, columnUIMetadata)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
function enrichViewSchemas(table: Table): TableResponse {
|
||||
const result: TableResponse = {
|
||||
...table,
|
||||
views: Object.values(table.views ?? []).reduce((p, v) => {
|
||||
if (!sdk.views.isV2(v)) {
|
||||
views: Object.values(table.views ?? [])
|
||||
.map(v => sdk.views.enrichSchema(v, table.schema))
|
||||
.reduce((p, v) => {
|
||||
p[v.name] = v
|
||||
} else {
|
||||
p[v.name] = {
|
||||
...v,
|
||||
schema:
|
||||
!v?.columns || !Object.entries(v?.columns).length
|
||||
? table.schema
|
||||
: enrichSchema(table.schema, v.columns),
|
||||
}
|
||||
}
|
||||
return p
|
||||
}, {} as TableViewsResponse),
|
||||
return p
|
||||
}, {} as TableViewsResponse),
|
||||
}
|
||||
|
||||
return result
|
||||
|
|
|
@ -2,6 +2,11 @@ import { FieldType, Table, ViewV2 } from "@budibase/types"
|
|||
import { generator } from "@budibase/backend-core/tests"
|
||||
import sdk from "../../.."
|
||||
|
||||
jest.mock("../../views", () => ({
|
||||
...jest.requireActual("../../views"),
|
||||
enrichSchema: jest.fn().mockImplementation(v => ({ ...v, mocked: true })),
|
||||
}))
|
||||
|
||||
describe("table sdk", () => {
|
||||
describe("enrichViewSchemas", () => {
|
||||
const basicTable: Table = {
|
||||
|
@ -50,257 +55,46 @@ describe("table sdk", () => {
|
|||
|
||||
it("should fetch the default schema if not overriden", async () => {
|
||||
const tableId = basicTable._id!
|
||||
const view: ViewV2 = {
|
||||
version: 2,
|
||||
id: generator.guid(),
|
||||
name: generator.guid(),
|
||||
tableId,
|
||||
function getTable() {
|
||||
const view: ViewV2 = {
|
||||
version: 2,
|
||||
id: generator.guid(),
|
||||
name: generator.guid(),
|
||||
tableId,
|
||||
}
|
||||
return view
|
||||
}
|
||||
const view1 = getTable()
|
||||
const view2 = getTable()
|
||||
const view3 = getTable()
|
||||
const res = sdk.tables.enrichViewSchemas({
|
||||
...basicTable,
|
||||
views: { [view.name]: view },
|
||||
views: {
|
||||
[view1.name]: view1,
|
||||
[view2.name]: view2,
|
||||
[view3.name]: view3,
|
||||
},
|
||||
})
|
||||
|
||||
expect(sdk.views.enrichSchema).toBeCalledTimes(3)
|
||||
|
||||
expect(res).toEqual({
|
||||
...basicTable,
|
||||
views: {
|
||||
[view.name]: {
|
||||
...view,
|
||||
schema: {
|
||||
name: {
|
||||
type: "string",
|
||||
name: "name",
|
||||
visible: true,
|
||||
order: 2,
|
||||
width: 80,
|
||||
constraints: {
|
||||
type: "string",
|
||||
},
|
||||
},
|
||||
description: {
|
||||
type: "string",
|
||||
name: "description",
|
||||
visible: true,
|
||||
width: 200,
|
||||
constraints: {
|
||||
type: "string",
|
||||
},
|
||||
},
|
||||
id: {
|
||||
type: "number",
|
||||
name: "id",
|
||||
visible: true,
|
||||
order: 1,
|
||||
constraints: {
|
||||
type: "number",
|
||||
},
|
||||
},
|
||||
hiddenField: {
|
||||
type: "string",
|
||||
name: "hiddenField",
|
||||
visible: false,
|
||||
constraints: {
|
||||
type: "string",
|
||||
},
|
||||
},
|
||||
},
|
||||
[view1.name]: {
|
||||
...view1,
|
||||
mocked: true,
|
||||
},
|
||||
[view2.name]: {
|
||||
...view2,
|
||||
mocked: true,
|
||||
},
|
||||
[view3.name]: {
|
||||
...view3,
|
||||
mocked: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
it("if view schema only defines visiblility, should only fetch the selected fields", async () => {
|
||||
const tableId = basicTable._id!
|
||||
const view: ViewV2 = {
|
||||
version: 2,
|
||||
id: generator.guid(),
|
||||
name: generator.guid(),
|
||||
tableId,
|
||||
columns: {
|
||||
name: { visible: true },
|
||||
id: { visible: true },
|
||||
description: { visible: false },
|
||||
},
|
||||
}
|
||||
|
||||
const res = sdk.tables.enrichViewSchemas({
|
||||
...basicTable,
|
||||
views: { [view.name]: view },
|
||||
})
|
||||
|
||||
expect(res).toEqual(
|
||||
expect.objectContaining({
|
||||
...basicTable,
|
||||
views: {
|
||||
[view.name]: {
|
||||
...view,
|
||||
schema: {
|
||||
name: {
|
||||
type: "string",
|
||||
name: "name",
|
||||
visible: true,
|
||||
order: 2,
|
||||
width: 80,
|
||||
constraints: {
|
||||
type: "string",
|
||||
},
|
||||
},
|
||||
id: {
|
||||
type: "number",
|
||||
name: "id",
|
||||
visible: true,
|
||||
order: 1,
|
||||
constraints: {
|
||||
type: "number",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("schema does not break if the view has corrupted columns", async () => {
|
||||
const tableId = basicTable._id!
|
||||
const view: ViewV2 = {
|
||||
version: 2,
|
||||
id: generator.guid(),
|
||||
name: generator.guid(),
|
||||
tableId,
|
||||
columns: { unnexisting: { visible: true }, name: { visible: true } },
|
||||
}
|
||||
|
||||
const res = sdk.tables.enrichViewSchemas({
|
||||
...basicTable,
|
||||
views: { [view.name]: view },
|
||||
})
|
||||
|
||||
expect(res).toEqual(
|
||||
expect.objectContaining({
|
||||
...basicTable,
|
||||
views: {
|
||||
[view.name]: {
|
||||
...view,
|
||||
schema: {
|
||||
name: {
|
||||
type: "string",
|
||||
name: "name",
|
||||
order: 2,
|
||||
visible: true,
|
||||
width: 80,
|
||||
constraints: {
|
||||
type: "string",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("if view schema only defines visiblility, should only fetch the selected fields", async () => {
|
||||
const tableId = basicTable._id!
|
||||
const view: ViewV2 = {
|
||||
version: 2,
|
||||
id: generator.guid(),
|
||||
name: generator.guid(),
|
||||
tableId,
|
||||
columns: {
|
||||
name: { visible: true },
|
||||
id: { visible: true },
|
||||
description: { visible: false },
|
||||
},
|
||||
}
|
||||
|
||||
const res = sdk.tables.enrichViewSchemas({
|
||||
...basicTable,
|
||||
views: { [view.name]: view },
|
||||
})
|
||||
|
||||
expect(res).toEqual(
|
||||
expect.objectContaining({
|
||||
...basicTable,
|
||||
views: {
|
||||
[view.name]: {
|
||||
...view,
|
||||
schema: {
|
||||
name: {
|
||||
type: "string",
|
||||
name: "name",
|
||||
order: 2,
|
||||
visible: true,
|
||||
width: 80,
|
||||
constraints: {
|
||||
type: "string",
|
||||
},
|
||||
},
|
||||
id: {
|
||||
type: "number",
|
||||
name: "id",
|
||||
order: 1,
|
||||
visible: true,
|
||||
constraints: {
|
||||
type: "number",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("if view defines order, the table schema order should be ignored", async () => {
|
||||
const tableId = basicTable._id!
|
||||
const view: ViewV2 = {
|
||||
version: 2,
|
||||
id: generator.guid(),
|
||||
name: generator.guid(),
|
||||
tableId,
|
||||
columns: {
|
||||
name: { visible: true, order: 1 },
|
||||
id: { visible: true },
|
||||
description: { visible: false, order: 2 },
|
||||
},
|
||||
}
|
||||
|
||||
const res = sdk.tables.enrichViewSchemas({
|
||||
...basicTable,
|
||||
views: { [view.name]: view },
|
||||
})
|
||||
|
||||
expect(res).toEqual(
|
||||
expect.objectContaining({
|
||||
...basicTable,
|
||||
views: {
|
||||
[view.name]: {
|
||||
...view,
|
||||
schema: {
|
||||
name: {
|
||||
type: "string",
|
||||
name: "name",
|
||||
order: 1,
|
||||
visible: true,
|
||||
width: 80,
|
||||
constraints: {
|
||||
type: "string",
|
||||
},
|
||||
},
|
||||
id: {
|
||||
type: "number",
|
||||
name: "id",
|
||||
visible: true,
|
||||
constraints: {
|
||||
type: "number",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import { FieldType, Table, ViewV2 } from "@budibase/types"
|
||||
import { generator } from "@budibase/backend-core/tests"
|
||||
import views from ".."
|
||||
import { enrichSchema } from ".."
|
||||
|
||||
describe("table sdk", () => {
|
||||
describe("enrichViewSchemas", () => {
|
||||
|
@ -57,7 +57,7 @@ describe("table sdk", () => {
|
|||
tableId,
|
||||
}
|
||||
|
||||
const res = views.enrichSchema(view, basicTable.schema)
|
||||
const res = enrichSchema(view, basicTable.schema)
|
||||
|
||||
expect(res).toEqual({
|
||||
...view,
|
||||
|
@ -116,7 +116,7 @@ describe("table sdk", () => {
|
|||
},
|
||||
}
|
||||
|
||||
const res = views.enrichSchema(view, basicTable.schema)
|
||||
const res = enrichSchema(view, basicTable.schema)
|
||||
|
||||
expect(res).toEqual({
|
||||
...view,
|
||||
|
@ -154,7 +154,7 @@ describe("table sdk", () => {
|
|||
columns: { unnexisting: { visible: true }, name: { visible: true } },
|
||||
}
|
||||
|
||||
const res = views.enrichSchema(view, basicTable.schema)
|
||||
const res = enrichSchema(view, basicTable.schema)
|
||||
|
||||
expect(res).toEqual(
|
||||
expect.objectContaining({
|
||||
|
@ -189,7 +189,7 @@ describe("table sdk", () => {
|
|||
},
|
||||
}
|
||||
|
||||
const res = views.enrichSchema(view, basicTable.schema)
|
||||
const res = enrichSchema(view, basicTable.schema)
|
||||
|
||||
expect(res).toEqual(
|
||||
expect.objectContaining({
|
||||
|
@ -233,7 +233,7 @@ describe("table sdk", () => {
|
|||
},
|
||||
}
|
||||
|
||||
const res = views.enrichSchema(view, basicTable.schema)
|
||||
const res = enrichSchema(view, basicTable.schema)
|
||||
|
||||
expect(res).toEqual(
|
||||
expect.objectContaining({
|
||||
|
|
Loading…
Reference in New Issue