From 5fb20abcfd5ad00f9d0e1df27a9f9e7548d2e50a Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Thu, 19 Jan 2023 20:09:39 +0000 Subject: [PATCH] Add enrich test --- packages/server/src/api/routes/row.ts | 2 +- .../src/integration-test/postgres.spec.ts | 95 ++++++++++++++++++- packages/types/src/documents/app/table.ts | 8 +- 3 files changed, 101 insertions(+), 4 deletions(-) diff --git a/packages/server/src/api/routes/row.ts b/packages/server/src/api/routes/row.ts index f4462b3595..6d1cd206c6 100644 --- a/packages/server/src/api/routes/row.ts +++ b/packages/server/src/api/routes/row.ts @@ -3,7 +3,7 @@ import * as rowController from "../controllers/row" import authorized from "../../middleware/authorized" import { paramResource, paramSubResource } from "../../middleware/resourceId" import { permissions } from "@budibase/backend-core" -const { internalSearchValidator } = require("./utils/validators") +import { internalSearchValidator } from "./utils/validators" const { PermissionType, PermissionLevel } = permissions const router: Router = new Router() diff --git a/packages/server/src/integration-test/postgres.spec.ts b/packages/server/src/integration-test/postgres.spec.ts index 2b49d9a746..b194aa53db 100644 --- a/packages/server/src/integration-test/postgres.spec.ts +++ b/packages/server/src/integration-test/postgres.spec.ts @@ -5,7 +5,14 @@ import { } from "../api/routes/public/tests/utils" import * as setup from "../api/routes/tests/utilities" -import { Datasource, FieldType, Row, SourceName, Table } from "@budibase/types" +import { + Datasource, + FieldType, + RelationshipTypes, + Row, + SourceName, + Table, +} from "@budibase/types" import _ from "lodash" const config = setup.getConfig() @@ -16,7 +23,8 @@ describe("row api - postgres", () => { let apiKey, makeRequest: MakeRequestResponse, postgresDatasource: Datasource, - postgresTable: Table + postgresTable: Table, + auxPostgresTable: Table beforeEach(async () => { await config.init() @@ -61,6 +69,31 @@ describe("row api - postgres", () => { }, sourceId: postgresDatasource._id, }) + + auxPostgresTable = await config.createTable({ + name: faker.lorem.word(), + schema: { + title: { + name: "title", + type: FieldType.STRING, + constraints: { + presence: true, + }, + }, + linkedField: { + type: FieldType.LINK, + constraints: { + type: "array", + presence: true, + }, + fieldName: "foreignField", + name: "linkedField", + relationshipType: RelationshipTypes.MANY_TO_MANY, + tableId: postgresTable._id, + }, + }, + sourceId: postgresDatasource._id, + }) }) afterAll(async () => { @@ -269,6 +302,31 @@ describe("row api - postgres", () => { expect(res.body).toEqual(expect.objectContaining(rowData)) }) + + test("given having rows with relation data, only the ids are retrieved", async () => { + let [{ row }] = await populateRows(1) + + const foreignRow = await config.createRow({ + tableId: auxPostgresTable._id, + title: faker.random.alphaNumeric(10), + linkedField: row._id, + }) + + const res = await getRow(postgresTable._id, row._id) + + expect(res.status).toBe(200) + + expect(res.body).toEqual({ + ...row, + foreignField: [ + { + _id: foreignRow._id, + }, + ], + createdAt: expect.any(String), + updatedAt: expect.any(String), + }) + }) }) describe("search for rows", () => { @@ -455,6 +513,39 @@ describe("row api - postgres", () => { }) }) + describe("enrich a row", () => { + const getAll = (tableId: string | undefined, rowId: string | undefined) => + makeRequest("get", `/api/${tableId}/${rowId}/enrich`) + + test("given having rows with relation data, enrich populates the", async () => { + let [{ row }] = await populateRows(1) + + const foreignRow = await config.createRow({ + tableId: auxPostgresTable._id, + title: faker.random.alphaNumeric(10), + linkedField: row._id, + }) + + const res = await getAll(postgresTable._id, row._id) + + expect(res.status).toBe(200) + + expect(res.body).toEqual({ + ...row, + foreignField: [ + { + ...foreignRow, + linkedField: [{ _id: row._id }], + createdAt: expect.any(String), + updatedAt: expect.any(String), + }, + ], + createdAt: expect.any(String), + updatedAt: expect.any(String), + }) + }) + }) + describe("get all rows", () => { const getAll = (tableId: string | undefined) => makeRequest("get", `/api/${tableId}/rows`) diff --git a/packages/types/src/documents/app/table.ts b/packages/types/src/documents/app/table.ts index 0f85e895ac..01d2486dcb 100644 --- a/packages/types/src/documents/app/table.ts +++ b/packages/types/src/documents/app/table.ts @@ -3,6 +3,12 @@ import { View } from "./view" import { RenameColumn } from "../../sdk" import { FieldType } from "./row" +export enum RelationshipTypes { + ONE_TO_MANY = "one-to-many", + MANY_TO_ONE = "many-to-one", + MANY_TO_MANY = "many-to-many", +} + export interface FieldSchema { type: FieldType externalType?: string @@ -10,7 +16,7 @@ export interface FieldSchema { name: string sortable?: boolean tableId?: string - relationshipType?: string + relationshipType?: RelationshipTypes through?: string foreignKey?: string icon?: string