From 875319e85c1fa97abc4e052845ea61ef1dfde449 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 18 Dec 2024 15:50:51 +0100 Subject: [PATCH] Relationship tests --- .../row/utils/tests/sqlUtils.spec.ts | 105 +++++++++++++++++- 1 file changed, 101 insertions(+), 4 deletions(-) diff --git a/packages/server/src/api/controllers/row/utils/tests/sqlUtils.spec.ts b/packages/server/src/api/controllers/row/utils/tests/sqlUtils.spec.ts index 2ab8e251ea..5a1b8ae49e 100644 --- a/packages/server/src/api/controllers/row/utils/tests/sqlUtils.spec.ts +++ b/packages/server/src/api/controllers/row/utils/tests/sqlUtils.spec.ts @@ -2,28 +2,34 @@ import { AIOperationEnum, FieldType, RelationshipType, + SourceName, Table, } from "@budibase/types" import { buildSqlFieldList } from "../sqlUtils" import { structures } from "../../../../routes/tests/utilities" import { cloneDeep } from "lodash" +import { sql } from "@budibase/backend-core" describe("buildSqlFieldList", () => { const basicTable: Table = { - ...structures.basicTable(), + ...structures.tableForDatasource({ + type: "datasource", + source: SourceName.POSTGRES, + }), name: "table", + _id: sql.utils.buildExternalTableId("ds_id", "table"), schema: { name: { - type: FieldType.STRING, name: "name", + type: FieldType.STRING, }, description: { - type: FieldType.STRING, name: "description", + type: FieldType.STRING, }, amount: { - type: FieldType.NUMBER, name: "amount", + type: FieldType.NUMBER, }, }, } @@ -76,4 +82,95 @@ describe("buildSqlFieldList", () => { const result = await buildSqlFieldList(table, {}) 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 = { + 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 = { + 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", + ]) + }) })