Unify tests
This commit is contained in:
parent
3a3151b59d
commit
4efb3d6ed6
|
@ -19,6 +19,9 @@ import {
|
||||||
SearchResponse,
|
SearchResponse,
|
||||||
BasicOperator,
|
BasicOperator,
|
||||||
RelationshipType,
|
RelationshipType,
|
||||||
|
TableSchema,
|
||||||
|
ViewFieldMetadata,
|
||||||
|
RenameColumn,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import { generator, mocks } from "@budibase/backend-core/tests"
|
import { generator, mocks } from "@budibase/backend-core/tests"
|
||||||
import { DatabaseName, getDatasource } from "../../../integrations/tests/utils"
|
import { DatabaseName, getDatasource } from "../../../integrations/tests/utils"
|
||||||
|
@ -1180,8 +1183,8 @@ describe.each([
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("foreign relationship columns", () => {
|
describe("foreign relationship columns", () => {
|
||||||
it("updating a column will update link columns configuration", async () => {
|
const createAuxTable = () =>
|
||||||
let auxTable = await config.api.table.save(
|
config.api.table.save(
|
||||||
saveTableRequest({
|
saveTableRequest({
|
||||||
primaryDisplay: "name",
|
primaryDisplay: "name",
|
||||||
schema: {
|
schema: {
|
||||||
|
@ -1191,6 +1194,13 @@ describe.each([
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const createMainTable = async (
|
||||||
|
links: {
|
||||||
|
name: string
|
||||||
|
tableId: string
|
||||||
|
fk: string
|
||||||
|
}[]
|
||||||
|
) => {
|
||||||
const table = await config.api.table.save(
|
const table = await config.api.table.save(
|
||||||
saveTableRequest({
|
saveTableRequest({
|
||||||
schema: {},
|
schema: {},
|
||||||
|
@ -1200,23 +1210,57 @@ describe.each([
|
||||||
...table,
|
...table,
|
||||||
schema: {
|
schema: {
|
||||||
...table.schema,
|
...table.schema,
|
||||||
aux: {
|
...links.reduce<TableSchema>((acc, c) => {
|
||||||
name: "aux",
|
acc[c.name] = {
|
||||||
|
name: c.name,
|
||||||
relationshipType: RelationshipType.ONE_TO_MANY,
|
relationshipType: RelationshipType.ONE_TO_MANY,
|
||||||
type: FieldType.LINK,
|
type: FieldType.LINK,
|
||||||
tableId: auxTable._id!,
|
tableId: c.tableId,
|
||||||
fieldName: "fk_aux",
|
fieldName: c.fk,
|
||||||
constraints: { type: "array" },
|
constraints: { type: "array" },
|
||||||
},
|
}
|
||||||
|
return acc
|
||||||
|
}, {}),
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
return table
|
||||||
|
}
|
||||||
|
|
||||||
|
const createView = async (
|
||||||
|
tableId: string,
|
||||||
|
schema: Record<string, ViewFieldMetadata>
|
||||||
|
) =>
|
||||||
|
await config.api.viewV2.create({
|
||||||
|
name: generator.guid(),
|
||||||
|
tableId,
|
||||||
|
schema,
|
||||||
|
})
|
||||||
|
|
||||||
|
const renameColumn = async (table: Table, renaming: RenameColumn) => {
|
||||||
|
const newSchema = { ...table.schema }
|
||||||
|
;(newSchema[renaming.updated] = {
|
||||||
|
...table.schema[renaming.old],
|
||||||
|
name: renaming.updated,
|
||||||
|
}),
|
||||||
|
delete newSchema[renaming.old]
|
||||||
|
|
||||||
|
await config.api.table.save({
|
||||||
|
...table,
|
||||||
|
schema: newSchema,
|
||||||
|
_rename: renaming,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
it("updating a column will update link columns configuration", async () => {
|
||||||
|
let auxTable = await createAuxTable()
|
||||||
|
|
||||||
|
const table = await createMainTable([
|
||||||
|
{ name: "aux", tableId: auxTable._id!, fk: "fk_aux" },
|
||||||
|
])
|
||||||
// Refetch auxTable
|
// Refetch auxTable
|
||||||
auxTable = await config.api.table.get(auxTable._id!)
|
auxTable = await config.api.table.get(auxTable._id!)
|
||||||
|
|
||||||
const view = await config.api.viewV2.create({
|
const view = await createView(table._id!, {
|
||||||
name: "view a",
|
|
||||||
tableId: table._id!,
|
|
||||||
schema: {
|
|
||||||
aux: {
|
aux: {
|
||||||
visible: true,
|
visible: true,
|
||||||
columns: {
|
columns: {
|
||||||
|
@ -1224,23 +1268,9 @@ describe.each([
|
||||||
age: { visible: true, readonly: true },
|
age: { visible: true, readonly: true },
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
|
||||||
})
|
})
|
||||||
|
|
||||||
await config.api.table.save({
|
await renameColumn(auxTable, { old: "age", updated: "dob" })
|
||||||
...auxTable,
|
|
||||||
schema: {
|
|
||||||
...auxTable.schema,
|
|
||||||
// @ts-ignore deleting age to force the rename
|
|
||||||
age: undefined,
|
|
||||||
dob: {
|
|
||||||
name: "dob",
|
|
||||||
type: FieldType.NUMBER,
|
|
||||||
constraints: { presence: true },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
_rename: { old: "age", updated: "dob" },
|
|
||||||
})
|
|
||||||
|
|
||||||
const updatedView = await config.api.viewV2.get(view.id)
|
const updatedView = await config.api.viewV2.get(view.id)
|
||||||
expect(updatedView).toEqual(
|
expect(updatedView).toEqual(
|
||||||
|
@ -1259,50 +1289,16 @@ describe.each([
|
||||||
})
|
})
|
||||||
|
|
||||||
it("handles multiple fields using the same table", async () => {
|
it("handles multiple fields using the same table", async () => {
|
||||||
let auxTable = await config.api.table.save(
|
let auxTable = await createAuxTable()
|
||||||
saveTableRequest({
|
|
||||||
primaryDisplay: "name",
|
|
||||||
schema: {
|
|
||||||
name: { name: "name", type: FieldType.STRING },
|
|
||||||
age: { name: "age", type: FieldType.NUMBER },
|
|
||||||
},
|
|
||||||
})
|
|
||||||
)
|
|
||||||
|
|
||||||
const table = await config.api.table.save(
|
const table = await createMainTable([
|
||||||
saveTableRequest({
|
{ name: "aux", tableId: auxTable._id!, fk: "fk_aux" },
|
||||||
schema: {},
|
{ name: "aux2", tableId: auxTable._id!, fk: "fk_aux2" },
|
||||||
})
|
])
|
||||||
)
|
|
||||||
await config.api.table.save({
|
|
||||||
...table,
|
|
||||||
schema: {
|
|
||||||
...table.schema,
|
|
||||||
aux: {
|
|
||||||
name: "aux",
|
|
||||||
relationshipType: RelationshipType.ONE_TO_MANY,
|
|
||||||
type: FieldType.LINK,
|
|
||||||
tableId: auxTable._id!,
|
|
||||||
fieldName: "fk_aux",
|
|
||||||
constraints: { type: "array" },
|
|
||||||
},
|
|
||||||
aux2: {
|
|
||||||
name: "aux2",
|
|
||||||
relationshipType: RelationshipType.ONE_TO_MANY,
|
|
||||||
type: FieldType.LINK,
|
|
||||||
tableId: auxTable._id!,
|
|
||||||
fieldName: "fk_aux2",
|
|
||||||
constraints: { type: "array" },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
// Refetch auxTable
|
// Refetch auxTable
|
||||||
auxTable = await config.api.table.get(auxTable._id!)
|
auxTable = await config.api.table.get(auxTable._id!)
|
||||||
|
|
||||||
const view = await config.api.viewV2.create({
|
const view = await createView(table._id!, {
|
||||||
name: "view a",
|
|
||||||
tableId: table._id!,
|
|
||||||
schema: {
|
|
||||||
aux: {
|
aux: {
|
||||||
visible: true,
|
visible: true,
|
||||||
columns: {
|
columns: {
|
||||||
|
@ -1317,23 +1313,9 @@ describe.each([
|
||||||
age: { visible: true, readonly: true },
|
age: { visible: true, readonly: true },
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
|
||||||
})
|
})
|
||||||
|
|
||||||
await config.api.table.save({
|
await renameColumn(auxTable, { old: "age", updated: "dob" })
|
||||||
...auxTable,
|
|
||||||
schema: {
|
|
||||||
...auxTable.schema,
|
|
||||||
// @ts-ignore deleting age to force the rename
|
|
||||||
age: undefined,
|
|
||||||
dob: {
|
|
||||||
name: "dob",
|
|
||||||
type: FieldType.NUMBER,
|
|
||||||
constraints: { presence: true },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
_rename: { old: "age", updated: "dob" },
|
|
||||||
})
|
|
||||||
|
|
||||||
const updatedView = await config.api.viewV2.get(view.id)
|
const updatedView = await config.api.viewV2.get(view.id)
|
||||||
expect(updatedView).toEqual(
|
expect(updatedView).toEqual(
|
||||||
|
@ -1359,53 +1341,18 @@ describe.each([
|
||||||
})
|
})
|
||||||
|
|
||||||
it("does not rename columns with the same name but from other tables", async () => {
|
it("does not rename columns with the same name but from other tables", async () => {
|
||||||
let auxTable = await config.api.table.save(
|
let auxTable = await createAuxTable()
|
||||||
saveTableRequest({
|
let aux2Table = await createAuxTable()
|
||||||
primaryDisplay: "name",
|
|
||||||
schema: { name: { name: "name", type: FieldType.STRING } },
|
const table = await createMainTable([
|
||||||
})
|
{ name: "aux", tableId: auxTable._id!, fk: "fk_aux" },
|
||||||
)
|
{ name: "aux2", tableId: aux2Table._id!, fk: "fk_aux2" },
|
||||||
let aux2Table = await config.api.table.save(
|
])
|
||||||
saveTableRequest({
|
|
||||||
primaryDisplay: "name",
|
|
||||||
schema: { name: { name: "name", type: FieldType.STRING } },
|
|
||||||
})
|
|
||||||
)
|
|
||||||
|
|
||||||
const table = await config.api.table.save(
|
|
||||||
saveTableRequest({
|
|
||||||
schema: {},
|
|
||||||
})
|
|
||||||
)
|
|
||||||
await config.api.table.save({
|
|
||||||
...table,
|
|
||||||
schema: {
|
|
||||||
...table.schema,
|
|
||||||
aux: {
|
|
||||||
name: "aux",
|
|
||||||
relationshipType: RelationshipType.ONE_TO_MANY,
|
|
||||||
type: FieldType.LINK,
|
|
||||||
tableId: auxTable._id!,
|
|
||||||
fieldName: "fk_aux",
|
|
||||||
constraints: { type: "array" },
|
|
||||||
},
|
|
||||||
aux2: {
|
|
||||||
name: "aux2",
|
|
||||||
relationshipType: RelationshipType.ONE_TO_MANY,
|
|
||||||
type: FieldType.LINK,
|
|
||||||
tableId: aux2Table._id!,
|
|
||||||
fieldName: "fk_aux2",
|
|
||||||
constraints: { type: "array" },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
// Refetch auxTable
|
// Refetch auxTable
|
||||||
auxTable = await config.api.table.get(auxTable._id!)
|
auxTable = await config.api.table.get(auxTable._id!)
|
||||||
|
|
||||||
const view = await config.api.viewV2.create({
|
const view = await createView(table._id!, {
|
||||||
name: "view a",
|
|
||||||
tableId: table._id!,
|
|
||||||
schema: {
|
|
||||||
aux: {
|
aux: {
|
||||||
visible: true,
|
visible: true,
|
||||||
columns: {
|
columns: {
|
||||||
|
@ -1418,22 +1365,9 @@ describe.each([
|
||||||
name: { visible: true, readonly: true },
|
name: { visible: true, readonly: true },
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
|
||||||
})
|
})
|
||||||
|
|
||||||
await config.api.table.save({
|
await renameColumn(auxTable, { old: "name", updated: "fullName" })
|
||||||
...auxTable,
|
|
||||||
schema: {
|
|
||||||
...auxTable.schema,
|
|
||||||
// @ts-ignore deleting age to force the rename
|
|
||||||
name: undefined,
|
|
||||||
fullName: {
|
|
||||||
name: "fullName",
|
|
||||||
type: FieldType.STRING,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
_rename: { old: "name", updated: "fullName" },
|
|
||||||
})
|
|
||||||
|
|
||||||
const updatedView = await config.api.viewV2.get(view.id)
|
const updatedView = await config.api.viewV2.get(view.id)
|
||||||
expect(updatedView).toEqual(
|
expect(updatedView).toEqual(
|
||||||
|
@ -1443,12 +1377,14 @@ describe.each([
|
||||||
columns: {
|
columns: {
|
||||||
id: { visible: false, readonly: false },
|
id: { visible: false, readonly: false },
|
||||||
fullName: { visible: true, readonly: true },
|
fullName: { visible: true, readonly: true },
|
||||||
|
age: { visible: false, readonly: false },
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
aux2: expect.objectContaining({
|
aux2: expect.objectContaining({
|
||||||
columns: {
|
columns: {
|
||||||
id: { visible: false, readonly: false },
|
id: { visible: false, readonly: false },
|
||||||
name: { visible: true, readonly: true },
|
name: { visible: true, readonly: true },
|
||||||
|
age: { visible: false, readonly: false },
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
}),
|
}),
|
||||||
|
@ -1457,51 +1393,19 @@ describe.each([
|
||||||
})
|
})
|
||||||
|
|
||||||
it("updates all views references", async () => {
|
it("updates all views references", async () => {
|
||||||
let auxTable = await config.api.table.save(
|
let auxTable = await createAuxTable()
|
||||||
saveTableRequest({
|
|
||||||
primaryDisplay: "name",
|
|
||||||
schema: {
|
|
||||||
name: { name: "name", type: FieldType.STRING },
|
|
||||||
age: { name: "age", type: FieldType.NUMBER },
|
|
||||||
},
|
|
||||||
})
|
|
||||||
)
|
|
||||||
|
|
||||||
const createTableWithRelationship = async () => {
|
const table1 = await createMainTable([
|
||||||
const table = await config.api.table.save(
|
{ name: "aux", tableId: auxTable._id!, fk: "fk_aux_table1" },
|
||||||
saveTableRequest({
|
])
|
||||||
schema: {},
|
const table2 = await createMainTable([
|
||||||
})
|
{ name: "aux", tableId: auxTable._id!, fk: "fk_aux_table2" },
|
||||||
)
|
])
|
||||||
await config.api.table.save({
|
|
||||||
...table,
|
|
||||||
schema: {
|
|
||||||
...table.schema,
|
|
||||||
aux: {
|
|
||||||
name: "aux",
|
|
||||||
relationshipType: RelationshipType.ONE_TO_MANY,
|
|
||||||
type: FieldType.LINK,
|
|
||||||
tableId: auxTable._id!,
|
|
||||||
fieldName: `fk_${table.name}`,
|
|
||||||
constraints: { type: "array" },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
return table
|
|
||||||
}
|
|
||||||
|
|
||||||
const table1 = await createTableWithRelationship()
|
|
||||||
const table2 = await createTableWithRelationship()
|
|
||||||
|
|
||||||
// Refetch auxTable
|
// Refetch auxTable
|
||||||
auxTable = await config.api.table.get(auxTable._id!)
|
auxTable = await config.api.table.get(auxTable._id!)
|
||||||
|
|
||||||
const createView = async (tableId: string) => {
|
const viewSchema = {
|
||||||
const view = await config.api.viewV2.create({
|
|
||||||
name: generator.guid(),
|
|
||||||
tableId,
|
|
||||||
schema: {
|
|
||||||
aux: {
|
aux: {
|
||||||
visible: true,
|
visible: true,
|
||||||
columns: {
|
columns: {
|
||||||
|
@ -1509,29 +1413,12 @@ describe.each([
|
||||||
age: { visible: true, readonly: true },
|
age: { visible: true, readonly: true },
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
|
||||||
})
|
|
||||||
return view
|
|
||||||
}
|
}
|
||||||
|
const view1 = await createView(table1._id!, viewSchema)
|
||||||
|
const view2 = await createView(table1._id!, viewSchema)
|
||||||
|
const view3 = await createView(table2._id!, viewSchema)
|
||||||
|
|
||||||
const view1 = await createView(table1._id!)
|
await renameColumn(auxTable, { old: "age", updated: "dob" })
|
||||||
const view2 = await createView(table1._id!)
|
|
||||||
const view3 = await createView(table2._id!)
|
|
||||||
|
|
||||||
await config.api.table.save({
|
|
||||||
...auxTable,
|
|
||||||
schema: {
|
|
||||||
...auxTable.schema,
|
|
||||||
// @ts-ignore deleting age to force the rename
|
|
||||||
age: undefined,
|
|
||||||
dob: {
|
|
||||||
name: "dob",
|
|
||||||
type: FieldType.NUMBER,
|
|
||||||
constraints: { presence: true },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
_rename: { old: "age", updated: "dob" },
|
|
||||||
})
|
|
||||||
|
|
||||||
for (const view of [view1, view2, view3]) {
|
for (const view of [view1, view2, view3]) {
|
||||||
const updatedView = await config.api.viewV2.get(view.id)
|
const updatedView = await config.api.viewV2.get(view.id)
|
||||||
|
|
Loading…
Reference in New Issue