Relationship tests

This commit is contained in:
Adria Navarro 2024-12-18 15:50:51 +01:00
parent 27d1929388
commit 875319e85c
1 changed files with 101 additions and 4 deletions

View File

@ -2,28 +2,34 @@ import {
AIOperationEnum, AIOperationEnum,
FieldType, FieldType,
RelationshipType, RelationshipType,
SourceName,
Table, Table,
} from "@budibase/types" } from "@budibase/types"
import { buildSqlFieldList } from "../sqlUtils" import { buildSqlFieldList } from "../sqlUtils"
import { structures } from "../../../../routes/tests/utilities" import { structures } from "../../../../routes/tests/utilities"
import { cloneDeep } from "lodash" import { cloneDeep } from "lodash"
import { sql } from "@budibase/backend-core"
describe("buildSqlFieldList", () => { describe("buildSqlFieldList", () => {
const basicTable: Table = { const basicTable: Table = {
...structures.basicTable(), ...structures.tableForDatasource({
type: "datasource",
source: SourceName.POSTGRES,
}),
name: "table", name: "table",
_id: sql.utils.buildExternalTableId("ds_id", "table"),
schema: { schema: {
name: { name: {
type: FieldType.STRING,
name: "name", name: "name",
type: FieldType.STRING,
}, },
description: { description: {
type: FieldType.STRING,
name: "description", name: "description",
type: FieldType.STRING,
}, },
amount: { amount: {
type: FieldType.NUMBER,
name: "amount", name: "amount",
type: FieldType.NUMBER,
}, },
}, },
} }
@ -76,4 +82,95 @@ describe("buildSqlFieldList", () => {
const result = await buildSqlFieldList(table, {}) const result = await buildSqlFieldList(table, {})
expect(result).toEqual(["table.name", "table.description", "table.amount"]) expect(result).toEqual(["table.name", "table.description", "table.amount"])
}) })
it("includes relationships fields when flag", async () => {
const table = cloneDeep(basicTable)
table.schema.link = {
name: "link",
type: FieldType.LINK,
relationshipType: RelationshipType.ONE_TO_MANY,
fieldName: "link",
tableId: sql.utils.buildExternalTableId("ds_id", "otherTableId"),
}
const otherTable: Table = {
...cloneDeep(basicTable),
name: "linkedTable",
primary: ["id"],
primaryDisplay: "name",
schema: {
...cloneDeep(basicTable).schema,
id: {
name: "id",
type: FieldType.NUMBER,
},
},
}
const allTables: Record<string, Table> = {
otherTableId: otherTable,
}
const result = await buildSqlFieldList(table, allTables, {
relationships: true,
})
expect(result).toEqual([
"table.name",
"table.description",
"table.amount",
"linkedTable.id",
"linkedTable.name",
])
})
it("includes all relationship fields if there is a formula column", async () => {
const table = cloneDeep(basicTable)
table.schema.link = {
name: "link",
type: FieldType.LINK,
relationshipType: RelationshipType.ONE_TO_MANY,
fieldName: "link",
tableId: sql.utils.buildExternalTableId("ds_id", "otherTableId"),
}
table.schema.formula = {
name: "formula",
type: FieldType.FORMULA,
formula: "any",
}
const otherTable: Table = {
...cloneDeep(basicTable),
name: "linkedTable",
schema: {
...cloneDeep(basicTable).schema,
id: {
name: "id",
type: FieldType.NUMBER,
},
hidden: {
name: "other",
type: FieldType.STRING,
visible: false,
},
},
}
const allTables: Record<string, Table> = {
otherTableId: otherTable,
}
const result = await buildSqlFieldList(table, allTables, {
relationships: true,
})
expect(result).toEqual([
"table.name",
"table.description",
"table.amount",
"linkedTable.name",
"linkedTable.description",
"linkedTable.amount",
"linkedTable.id",
"linkedTable.hidden",
])
})
}) })