Add test for autoid when importing
This commit is contained in:
parent
6c610a3181
commit
eb0d445295
|
@ -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",
|
||||||
|
}),
|
||||||
|
])
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
|
@ -20,7 +20,13 @@ import viewTemplate from "../view/viewBuilder"
|
||||||
import { cloneDeep } from "lodash/fp"
|
import { cloneDeep } from "lodash/fp"
|
||||||
import { quotas } from "@budibase/pro"
|
import { quotas } from "@budibase/pro"
|
||||||
import { events, context } from "@budibase/backend-core"
|
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) {
|
export async function clearColumns(table: any, columnNames: any) {
|
||||||
const db: Database = context.getAppDB()
|
const db: Database = context.getAppDB()
|
||||||
|
@ -99,7 +105,7 @@ export function makeSureTableUpToDate(table: any, tableToSave: any) {
|
||||||
return tableToSave
|
return tableToSave
|
||||||
}
|
}
|
||||||
|
|
||||||
export function importToRows(data: any, table: Table, user: any = {}) {
|
export function importToRows(data: any[], table: Table, user: ContextUser) {
|
||||||
let finalData: any = []
|
let finalData: any = []
|
||||||
for (let i = 0; i < data.length; i++) {
|
for (let i = 0; i < data.length; i++) {
|
||||||
let row = data[i]
|
let row = data[i]
|
||||||
|
|
Loading…
Reference in New Issue