From 0cec0269323aeb14fcad22d090a75003460eb8bc Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 27 Sep 2023 18:32:18 +0200 Subject: [PATCH] Datasource usage on tests --- .../src/api/controllers/row/external.ts | 7 +-- .../server/src/api/routes/tests/row.spec.ts | 53 ++++++++++++------- .../src/sdk/app/rows/search/external.ts | 8 ++- 3 files changed, 45 insertions(+), 23 deletions(-) diff --git a/packages/server/src/api/controllers/row/external.ts b/packages/server/src/api/controllers/row/external.ts index 1b0a4762b7..57b808a85f 100644 --- a/packages/server/src/api/controllers/row/external.ts +++ b/packages/server/src/api/controllers/row/external.ts @@ -73,7 +73,7 @@ export async function patch(ctx: UserCtx) { }) return { ...response, - row, + row: await outputProcessing(table, row), table, } } @@ -114,7 +114,7 @@ export async function save(ctx: UserCtx) { }) return { ...response, - row, + row: await outputProcessing(table, row), } } else { return response @@ -132,7 +132,8 @@ export async function find(ctx: UserCtx): Promise { ctx.throw(404) } - return row + const table = await sdk.tables.getTable(tableId) + return await outputProcessing(table, row) } export async function destroy(ctx: UserCtx) { diff --git a/packages/server/src/api/routes/tests/row.spec.ts b/packages/server/src/api/routes/tests/row.spec.ts index a34669de03..b4a33efdde 100644 --- a/packages/server/src/api/routes/tests/row.spec.ts +++ b/packages/server/src/api/routes/tests/row.spec.ts @@ -1518,8 +1518,16 @@ describe.each([ describe("bb reference fields", () => { let tableId: string let users: User[] + beforeAll(async () => { const tableConfig = generateTableConfig() + + if (config.datasource) { + tableConfig.sourceId = config.datasource._id + if (config.datasource.plus) { + tableConfig.type = "external" + } + } const table = await config.api.table.create({ ...tableConfig, schema: { @@ -1559,10 +1567,11 @@ describe.each([ expect(row).toEqual({ name: rowData.name, description: rowData.description, - type: "row", tableId, _id: expect.any(String), _rev: expect.any(String), + id: isInternal ? undefined : expect.any(Number), + type: isInternal ? "row" : undefined, }) }) @@ -1579,7 +1588,6 @@ describe.each([ expect(row).toEqual({ name: rowData.name, description: rowData.description, - type: "row", tableId, user: [ { @@ -1592,6 +1600,8 @@ describe.each([ ], _id: expect.any(String), _rev: expect.any(String), + id: isInternal ? undefined : expect.any(Number), + type: isInternal ? "row" : undefined, }) }) @@ -1608,7 +1618,6 @@ describe.each([ expect(row).toEqual({ name: rowData.name, description: rowData.description, - type: "row", tableId, users: selectedUsers.map(u => ({ _id: u._id, @@ -1619,6 +1628,8 @@ describe.each([ })), _id: expect.any(String), _rev: expect.any(String), + id: isInternal ? undefined : expect.any(Number), + type: isInternal ? "row" : undefined, }) }) @@ -1634,14 +1645,13 @@ describe.each([ expect(retrieved).toEqual({ name: rowData.name, description: rowData.description, - type: "row", tableId, user: undefined, users: undefined, _id: row._id, _rev: expect.any(String), - createdAt: timestamp, - updatedAt: timestamp, + id: isInternal ? undefined : expect.any(Number), + ...defaultRowFields, }) }) @@ -1661,7 +1671,6 @@ describe.each([ expect(retrieved).toEqual({ name: rowData.name, description: rowData.description, - type: "row", tableId, user: [user1].map(u => ({ _id: u._id, @@ -1679,8 +1688,8 @@ describe.each([ })), _id: row._id, _rev: expect.any(String), - createdAt: timestamp, - updatedAt: timestamp, + id: isInternal ? undefined : expect.any(Number), + ...defaultRowFields, }) }) @@ -1703,7 +1712,6 @@ describe.each([ expect(updatedRow).toEqual({ name: rowData.name, description: rowData.description, - type: "row", tableId, user: [ { @@ -1723,6 +1731,8 @@ describe.each([ })), _id: row._id, _rev: expect.any(String), + id: isInternal ? undefined : expect.any(Number), + type: isInternal ? "row" : undefined, }) }) @@ -1745,12 +1755,13 @@ describe.each([ expect(updatedRow).toEqual({ name: rowData.name, description: rowData.description, - type: "row", tableId, - user: null, - users: null, + user: isInternal ? null : undefined, + users: isInternal ? null : undefined, _id: row._id, _rev: expect.any(String), + id: isInternal ? undefined : expect.any(Number), + type: isInternal ? "row" : undefined, }) }) @@ -1796,7 +1807,6 @@ describe.each([ rows.map(r => ({ name: r.name, description: r.description, - type: "row", tableId, user: r.user?.map(u => ({ _id: u._id, @@ -1814,8 +1824,8 @@ describe.each([ })), _id: expect.any(String), _rev: expect.any(String), - createdAt: timestamp, - updatedAt: timestamp, + id: isInternal ? undefined : expect.any(Number), + ...defaultRowFields, })) ) ) @@ -1863,7 +1873,6 @@ describe.each([ rows.map(r => ({ name: r.name, description: r.description, - type: "row", tableId, user: r.user?.map(u => ({ _id: u._id, @@ -1881,10 +1890,16 @@ describe.each([ })), _id: expect.any(String), _rev: expect.any(String), - createdAt: timestamp, - updatedAt: timestamp, + id: isInternal ? undefined : expect.any(Number), + ...defaultRowFields, })) ), + ...(isInternal + ? {} + : { + hasNextPage: false, + bookmark: null, + }), }) }) }) diff --git a/packages/server/src/sdk/app/rows/search/external.ts b/packages/server/src/sdk/app/rows/search/external.ts index 447d1d7d16..817bfce33d 100644 --- a/packages/server/src/sdk/app/rows/search/external.ts +++ b/packages/server/src/sdk/app/rows/search/external.ts @@ -17,6 +17,7 @@ import { utils } from "@budibase/shared-core" import { ExportRowsParams, ExportRowsResult } from "../search" import { HTTPError, db } from "@budibase/backend-core" import pick from "lodash/pick" +import { outputProcessing } from "../../../../utilities/rowProcessor" export async function search(options: SearchParams) { const { tableId } = options @@ -75,6 +76,9 @@ export async function search(options: SearchParams) { rows = rows.map((r: any) => pick(r, fields)) } + const table = await sdk.tables.getTable(tableId) + rows = await outputProcessing(table, rows) + // need wrapper object for bookmarks etc when paginating return { rows, hasNextPage, bookmark: bookmark && bookmark + 1 } } catch (err: any) { @@ -166,9 +170,11 @@ export async function exportRows( } export async function fetch(tableId: string) { - return handleRequest(Operation.READ, tableId, { + const response = await handleRequest(Operation.READ, tableId, { includeSqlRelationships: IncludeRelationship.INCLUDE, }) + const table = await sdk.tables.getTable(tableId) + return await outputProcessing(table, response) } export async function fetchView(viewName: string) {