1
0
Fork 0
mirror of synced 2024-08-14 01:21:41 +12:00

Test sdks

This commit is contained in:
Adria Navarro 2023-07-24 16:12:42 +02:00
parent d8b801e647
commit c58b145afd
3 changed files with 45 additions and 287 deletions

View file

@ -10,8 +10,6 @@ import {
Database, Database,
TableResponse, TableResponse,
TableViewsResponse, TableViewsResponse,
TableSchema,
UIFieldMetadata,
} from "@budibase/types" } from "@budibase/types"
import datasources from "../datasources" import datasources from "../datasources"
import { populateExternalTableSchemas, isEditableColumn } from "./validation" 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 { function enrichViewSchemas(table: Table): TableResponse {
const result: TableResponse = { const result: TableResponse = {
...table, ...table,
views: Object.values(table.views ?? []).reduce((p, v) => { views: Object.values(table.views ?? [])
if (!sdk.views.isV2(v)) { .map(v => sdk.views.enrichSchema(v, table.schema))
.reduce((p, v) => {
p[v.name] = v p[v.name] = v
} else { return p
p[v.name] = { }, {} as TableViewsResponse),
...v,
schema:
!v?.columns || !Object.entries(v?.columns).length
? table.schema
: enrichSchema(table.schema, v.columns),
}
}
return p
}, {} as TableViewsResponse),
} }
return result return result

View file

@ -2,6 +2,11 @@ import { FieldType, Table, ViewV2 } from "@budibase/types"
import { generator } from "@budibase/backend-core/tests" import { generator } from "@budibase/backend-core/tests"
import sdk from "../../.." import sdk from "../../.."
jest.mock("../../views", () => ({
...jest.requireActual("../../views"),
enrichSchema: jest.fn().mockImplementation(v => ({ ...v, mocked: true })),
}))
describe("table sdk", () => { describe("table sdk", () => {
describe("enrichViewSchemas", () => { describe("enrichViewSchemas", () => {
const basicTable: Table = { const basicTable: Table = {
@ -50,257 +55,46 @@ describe("table sdk", () => {
it("should fetch the default schema if not overriden", async () => { it("should fetch the default schema if not overriden", async () => {
const tableId = basicTable._id! const tableId = basicTable._id!
const view: ViewV2 = { function getTable() {
version: 2, const view: ViewV2 = {
id: generator.guid(), version: 2,
name: generator.guid(), id: generator.guid(),
tableId, name: generator.guid(),
tableId,
}
return view
} }
const view1 = getTable()
const view2 = getTable()
const view3 = getTable()
const res = sdk.tables.enrichViewSchemas({ const res = sdk.tables.enrichViewSchemas({
...basicTable, ...basicTable,
views: { [view.name]: view }, views: {
[view1.name]: view1,
[view2.name]: view2,
[view3.name]: view3,
},
}) })
expect(sdk.views.enrichSchema).toBeCalledTimes(3)
expect(res).toEqual({ expect(res).toEqual({
...basicTable, ...basicTable,
views: { views: {
[view.name]: { [view1.name]: {
...view, ...view1,
schema: { mocked: true,
name: { },
type: "string", [view2.name]: {
name: "name", ...view2,
visible: true, mocked: true,
order: 2, },
width: 80, [view3.name]: {
constraints: { ...view3,
type: "string", mocked: true,
},
},
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",
},
},
},
}, },
}, },
}) })
}) })
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",
},
},
},
},
},
})
)
})
}) })
}) })

View file

@ -1,6 +1,6 @@
import { FieldType, Table, ViewV2 } from "@budibase/types" import { FieldType, Table, ViewV2 } from "@budibase/types"
import { generator } from "@budibase/backend-core/tests" import { generator } from "@budibase/backend-core/tests"
import views from ".." import { enrichSchema } from ".."
describe("table sdk", () => { describe("table sdk", () => {
describe("enrichViewSchemas", () => { describe("enrichViewSchemas", () => {
@ -57,7 +57,7 @@ describe("table sdk", () => {
tableId, tableId,
} }
const res = views.enrichSchema(view, basicTable.schema) const res = enrichSchema(view, basicTable.schema)
expect(res).toEqual({ expect(res).toEqual({
...view, ...view,
@ -116,7 +116,7 @@ describe("table sdk", () => {
}, },
} }
const res = views.enrichSchema(view, basicTable.schema) const res = enrichSchema(view, basicTable.schema)
expect(res).toEqual({ expect(res).toEqual({
...view, ...view,
@ -154,7 +154,7 @@ describe("table sdk", () => {
columns: { unnexisting: { visible: true }, name: { visible: true } }, columns: { unnexisting: { visible: true }, name: { visible: true } },
} }
const res = views.enrichSchema(view, basicTable.schema) const res = enrichSchema(view, basicTable.schema)
expect(res).toEqual( expect(res).toEqual(
expect.objectContaining({ 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(res).toEqual(
expect.objectContaining({ 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(res).toEqual(
expect.objectContaining({ expect.objectContaining({