From 9fbad3721839a45812cdf1f9560909c4501cc35a Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Mon, 17 Jul 2023 10:51:52 +0200 Subject: [PATCH] Fix tests --- .../src/api/controllers/row/external.ts | 1 - .../src/sdk/app/rows/search/external.ts | 31 ++++++++++++++++-- .../tests => sdk/tests/rows}/row.spec.ts | 32 ++++++++----------- 3 files changed, 42 insertions(+), 22 deletions(-) rename packages/server/src/{api/controllers/tests => sdk/tests/rows}/row.spec.ts (77%) diff --git a/packages/server/src/api/controllers/row/external.ts b/packages/server/src/api/controllers/row/external.ts index 5a9647c63d..36329b3469 100644 --- a/packages/server/src/api/controllers/row/external.ts +++ b/packages/server/src/api/controllers/row/external.ts @@ -5,7 +5,6 @@ import { } from "../../../integrations/utils" import { ExternalRequest, RunConfig } from "./ExternalRequest" import { - Ctx, Datasource, IncludeRelationship, Operation, diff --git a/packages/server/src/sdk/app/rows/search/external.ts b/packages/server/src/sdk/app/rows/search/external.ts index 18e9d1a79b..c48f984a2d 100644 --- a/packages/server/src/sdk/app/rows/search/external.ts +++ b/packages/server/src/sdk/app/rows/search/external.ts @@ -7,11 +7,13 @@ import { Row, Ctx, } from "@budibase/types" +import * as exporters from "../../../../api/controllers/view/exporters" import sdk from "../../../../sdk" import { handleRequest } from "../../../../api/controllers/row/external" import { breakExternalTableId } from "../../../../integrations/utils" import { cleanExportRows } from "../utils" import { apiFileReturn } from "../../../../utilities/fileSystem" +import { utils } from "@budibase/shared-core" export async function search(ctx: Ctx) { const tableId = ctx.params.tableId @@ -85,6 +87,15 @@ export async function exportRows(ctx: Ctx) { ctx.throw(400, "Datasource has not been configured for plus API.") } + if (!exporters.isFormat(format)) { + ctx.throw( + 400, + `Format ${format} not valid. Valid values: ${Object.values( + exporters.Format + )}` + ) + } + if (ctx.request.body.rows) { ctx.request.body = { query: { @@ -127,13 +138,27 @@ export async function exportRows(ctx: Ctx) { let headers = Object.keys(schema) - // @ts-ignore - const exporter = exporters[format] + let content + switch (format) { + case exporters.Format.CSV: + content = exporters.csv(headers, exportRows) + break + case exporters.Format.JSON: + content = exporters.json(exportRows) + break + case exporters.Format.JSON_WITH_SCHEMA: + content = exporters.jsonWithSchema(schema, exportRows) + break + default: + utils.unreachable(format) + break + } + const filename = `export.${format}` // send down the file ctx.attachment(filename) - return apiFileReturn(exporter(headers, exportRows)) + return apiFileReturn(content) } export async function fetch(ctx: Ctx) { diff --git a/packages/server/src/api/controllers/tests/row.spec.ts b/packages/server/src/sdk/tests/rows/row.spec.ts similarity index 77% rename from packages/server/src/api/controllers/tests/row.spec.ts rename to packages/server/src/sdk/tests/rows/row.spec.ts index 19e86d08be..68140345b7 100644 --- a/packages/server/src/api/controllers/tests/row.spec.ts +++ b/packages/server/src/sdk/tests/rows/row.spec.ts @@ -1,14 +1,14 @@ -import { exportRows } from "../row/external" -import sdk from "../../../sdk" -import { ExternalRequest } from "../row/ExternalRequest" +import { exportRows } from "../../app/rows/search/external" +import sdk from "../.." +import { ExternalRequest } from "../../../api/controllers/row/ExternalRequest" -// @ts-ignore -sdk.datasources = { - get: jest.fn(), -} +const mockDatasourcesGet = jest.fn() +sdk.datasources.get = mockDatasourcesGet -jest.mock("../row/ExternalRequest") -jest.mock("../view/exporters", () => ({ +jest.mock("../../../api/controllers/row/ExternalRequest") + +jest.mock("../../../api/controllers/view/exporters", () => ({ + ...jest.requireActual("../../../api/controllers/view/exporters"), csv: jest.fn(), Format: { CSV: "csv", @@ -31,14 +31,15 @@ function getUserCtx() { throw "Err" }), attachment: jest.fn(), - } + } as any } describe("external row controller", () => { describe("exportRows", () => { beforeAll(() => { - //@ts-ignore - jest.spyOn(ExternalRequest.prototype, "run").mockImplementation(() => []) + jest + .spyOn(ExternalRequest.prototype, "run") + .mockImplementation(() => Promise.resolve([])) }) afterEach(() => { @@ -48,7 +49,6 @@ describe("external row controller", () => { it("should throw a 400 if no datasource entities are present", async () => { let userCtx = getUserCtx() try { - //@ts-ignore await exportRows(userCtx) } catch (e) { expect(userCtx.throw).toHaveBeenCalledWith( @@ -59,8 +59,7 @@ describe("external row controller", () => { }) it("should handle single quotes from a row ID", async () => { - //@ts-ignore - sdk.datasources.get.mockImplementation(() => ({ + mockDatasourcesGet.mockImplementation(async () => ({ entities: { tablename: { schema: {}, @@ -72,7 +71,6 @@ describe("external row controller", () => { rows: ["['d001']"], } - //@ts-ignore await exportRows(userCtx) expect(userCtx.request.body).toEqual({ @@ -90,7 +88,6 @@ describe("external row controller", () => { rows: ["[123]", "['d001'%2C'10111']"], } try { - //@ts-ignore await exportRows(userCtx) } catch (e) { expect(userCtx.throw).toHaveBeenCalledWith( @@ -107,7 +104,6 @@ describe("external row controller", () => { rows: ["[123]"], } try { - //@ts-ignore await exportRows(userCtx) } catch (e) { expect(userCtx.throw).toHaveBeenCalledWith(