1
0
Fork 0
mirror of synced 2024-10-06 04:54:52 +13:00

Add test for autoid when importing

This commit is contained in:
adrinr 2023-03-30 10:37:38 +01:00
parent 6c610a3181
commit eb0d445295
2 changed files with 71 additions and 2 deletions

View file

@ -0,0 +1,63 @@
import { FieldType } from "@budibase/types"
import { AutoFieldSubTypes } from "../../../../constants"
import TestConfiguration from "../../../../tests/utilities/TestConfiguration"
import { importToRows } from "../utils"
describe("utils", () => {
const config = new TestConfiguration()
beforeEach(async () => {
await config.init()
})
afterAll(config.end)
describe("importToRows", () => {
it("consecutive row have consecutive auto ids", async () => {
await config.doInContext(config.appId, async () => {
const table = await config.createTable({
name: "table",
type: "table",
schema: {
autoId: {
name: "autoId",
type: FieldType.NUMBER,
subtype: AutoFieldSubTypes.AUTO_ID,
autocolumn: true,
constraints: {
type: FieldType.NUMBER,
presence: true,
},
},
name: {
name: "name",
type: FieldType.STRING,
constraints: {
type: FieldType.STRING,
presence: true,
},
},
},
})
const data = [{ name: "Alice" }, { name: "Bob" }, { name: "Claire" }]
const result = importToRows(data, table, config.user)
expect(result).toEqual([
expect.objectContaining({
autoId: 1,
name: "Alice",
}),
expect.objectContaining({
autoId: 2,
name: "Bob",
}),
expect.objectContaining({
autoId: 3,
name: "Claire",
}),
])
})
})
})
})

View file

@ -20,7 +20,13 @@ import viewTemplate from "../view/viewBuilder"
import { cloneDeep } from "lodash/fp"
import { quotas } from "@budibase/pro"
import { events, context } from "@budibase/backend-core"
import { Database, Datasource, SourceName, Table } from "@budibase/types"
import {
ContextUser,
Database,
Datasource,
SourceName,
Table,
} from "@budibase/types"
export async function clearColumns(table: any, columnNames: any) {
const db: Database = context.getAppDB()
@ -99,7 +105,7 @@ export function makeSureTableUpToDate(table: any, tableToSave: any) {
return tableToSave
}
export function importToRows(data: any, table: Table, user: any = {}) {
export function importToRows(data: any[], table: Table, user: ContextUser) {
let finalData: any = []
for (let i = 0; i < data.length; i++) {
let row = data[i]