Merge branch 'master' into chore/allow-logging-js-execution-errors

This commit is contained in:
Adria Navarro 2024-02-12 14:05:41 +01:00 committed by GitHub
commit 165cf9c832
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 146 additions and 21 deletions

View File

@ -14,7 +14,6 @@ import { events } from "@budibase/backend-core"
import {
BulkImportRequest,
BulkImportResponse,
DocumentType,
FetchTablesResponse,
MigrateRequest,
MigrateResponse,
@ -25,7 +24,6 @@ import {
TableResponse,
TableSourceType,
UserCtx,
SEPARATOR,
} from "@budibase/types"
import sdk from "../../../sdk"
import { jsonFromCsvString } from "../../../utilities/csv"
@ -77,9 +75,10 @@ export async function save(ctx: UserCtx<SaveTableRequest, SaveTableResponse>) {
const table = ctx.request.body
const isImport = table.rows
const savedTable = await pickApi({ table }).save(ctx)
let savedTable = await pickApi({ table }).save(ctx)
if (!table._id) {
await events.table.created(savedTable)
savedTable = sdk.tables.enrichViewSchemas(savedTable)
} else {
await events.table.updated(savedTable)
}

View File

@ -19,10 +19,15 @@ export async function save(ctx: UserCtx<SaveTableRequest, SaveTableResponse>) {
} = {
_id: generateTableID(),
...rest,
type: "table",
sourceType: TableSourceType.INTERNAL,
views: {},
// Ensure these fields are populated, even if not sent in the request
type: rest.type || "table",
sourceType: rest.sourceType || TableSourceType.INTERNAL,
}
if (!tableToSave.views) {
tableToSave.views = {}
}
const renaming = tableToSave._rename
delete tableToSave._rename

View File

@ -5,6 +5,7 @@ import {
FieldType,
INTERNAL_TABLE_SOURCE_ID,
InternalTable,
NumberFieldMetadata,
RelationshipType,
Row,
SaveTableRequest,
@ -18,6 +19,12 @@ import * as setup from "./utilities"
import sdk from "../../../sdk"
import * as uuid from "uuid"
import tk from "timekeeper"
import { mocks } from "@budibase/backend-core/tests"
import { TableToBuild } from "../../../tests/utilities/TestConfiguration"
tk.freeze(mocks.date.MOCK_DATE)
const { basicTable } = setup.structures
describe("/tables", () => {
@ -60,6 +67,67 @@ describe("/tables", () => {
expect(events.table.created).toBeCalledWith(res.body)
})
it("creates all the passed fields", async () => {
const tableData: TableToBuild = {
name: "TestTable",
type: "table",
schema: {
autoId: {
name: "id",
type: FieldType.NUMBER,
subtype: AutoFieldSubType.AUTO_ID,
autocolumn: true,
constraints: {
type: "number",
presence: false,
},
},
},
views: {
"table view": {
id: "viewId",
version: 2,
name: "table view",
tableId: "tableId",
},
},
}
const testTable = await config.createTable(tableData)
const expected: Table = {
...tableData,
type: "table",
views: {
"table view": {
...tableData.views!["table view"],
schema: {
autoId: {
autocolumn: true,
constraints: {
presence: false,
type: "number",
},
name: "id",
type: FieldType.NUMBER,
subtype: AutoFieldSubType.AUTO_ID,
visible: false,
} as NumberFieldMetadata,
},
},
},
sourceType: TableSourceType.INTERNAL,
sourceId: expect.any(String),
_rev: expect.stringMatching(/^1-.+/),
_id: expect.any(String),
createdAt: mocks.date.MOCK_DATE.toISOString(),
updatedAt: mocks.date.MOCK_DATE.toISOString(),
}
expect(testTable).toEqual(expected)
const persistedTable = await config.api.table.get(testTable._id!)
expect(persistedTable).toEqual(expected)
})
it("creates a table via data import", async () => {
const table: SaveTableRequest = basicTable()
table.rows = [{ name: "test-name", description: "test-desc" }]
@ -152,6 +220,56 @@ describe("/tables", () => {
expect(res.body.name).toBeUndefined()
})
it("updates only the passed fields", async () => {
const testTable = await config.createTable({
name: "TestTable",
type: "table",
schema: {
autoId: {
name: "id",
type: FieldType.NUMBER,
subtype: AutoFieldSubType.AUTO_ID,
autocolumn: true,
constraints: {
type: "number",
presence: false,
},
},
},
views: {
view1: {
id: "viewId",
version: 2,
name: "table view",
tableId: "tableId",
},
},
})
const response = await request
.post(`/api/tables`)
.send({
...testTable,
name: "UpdatedName",
})
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
expect(response.body).toEqual({
...testTable,
name: "UpdatedName",
_rev: expect.stringMatching(/^2-.+/),
})
const persistedTable = await config.api.table.get(testTable._id!)
expect(persistedTable).toEqual({
...testTable,
name: "UpdatedName",
_rev: expect.stringMatching(/^2-.+/),
})
})
describe("user table", () => {
it("should add roleId and email field when adjusting user table schema", async () => {
const res = await request
@ -230,6 +348,7 @@ describe("/tables", () => {
describe("fetch", () => {
let testTable: Table
const enrichViewSchemasMock = jest.spyOn(sdk.tables, "enrichViewSchemas")
beforeEach(async () => {
testTable = await config.createTable(testTable)
@ -239,6 +358,10 @@ describe("/tables", () => {
delete testTable._rev
})
afterAll(() => {
enrichViewSchemasMock.mockRestore()
})
it("returns all the tables for that instance in the response body", async () => {
const res = await request
.get(`/api/tables`)
@ -287,7 +410,7 @@ describe("/tables", () => {
it("should enrich the view schemas for viewsV2", async () => {
const tableId = config.table!._id!
jest.spyOn(sdk.tables, "enrichViewSchemas").mockImplementation(t => ({
enrichViewSchemasMock.mockImplementation(t => ({
...t,
views: {
view1: {
@ -295,7 +418,7 @@ describe("/tables", () => {
name: "view1",
schema: {},
id: "new_view_id",
tableId,
tableId: t._id!,
},
},
}))
@ -362,11 +485,7 @@ describe("/tables", () => {
let testTable: Table
beforeEach(async () => {
testTable = await config.createTable(testTable)
})
afterEach(() => {
delete testTable._rev
testTable = await config.createTable()
})
it("returns a success response when a table is deleted.", async () => {

View File

@ -75,11 +75,13 @@ export async function save(
if (!tableView) continue
if (viewsSdk.isV2(tableView)) {
if (oldTable?.views && oldTable.views[view]) {
table.views[view] = viewsSdk.syncSchema(
oldTable!.views![view] as ViewV2,
oldTable.views[view] as ViewV2,
table.schema,
renaming
)
}
continue
}

View File

@ -84,12 +84,12 @@ type DefaultUserValues = {
csrfToken: string
}
interface TableToBuild extends Omit<Table, "sourceId" | "sourceType"> {
export interface TableToBuild extends Omit<Table, "sourceId" | "sourceType"> {
sourceId?: string
sourceType?: TableSourceType
}
class TestConfiguration {
export default class TestConfiguration {
server: any
request: supertest.SuperTest<supertest.Test> | undefined
started: boolean
@ -912,4 +912,4 @@ class TestConfiguration {
}
}
export = TestConfiguration
module.exports = TestConfiguration