Fix tests

This commit is contained in:
Adria Navarro 2023-07-17 10:51:52 +02:00
parent d1b64bcd9c
commit 9fbad37218
3 changed files with 42 additions and 22 deletions

View File

@ -5,7 +5,6 @@ import {
} from "../../../integrations/utils" } from "../../../integrations/utils"
import { ExternalRequest, RunConfig } from "./ExternalRequest" import { ExternalRequest, RunConfig } from "./ExternalRequest"
import { import {
Ctx,
Datasource, Datasource,
IncludeRelationship, IncludeRelationship,
Operation, Operation,

View File

@ -7,11 +7,13 @@ import {
Row, Row,
Ctx, Ctx,
} from "@budibase/types" } from "@budibase/types"
import * as exporters from "../../../../api/controllers/view/exporters"
import sdk from "../../../../sdk" import sdk from "../../../../sdk"
import { handleRequest } from "../../../../api/controllers/row/external" import { handleRequest } from "../../../../api/controllers/row/external"
import { breakExternalTableId } from "../../../../integrations/utils" import { breakExternalTableId } from "../../../../integrations/utils"
import { cleanExportRows } from "../utils" import { cleanExportRows } from "../utils"
import { apiFileReturn } from "../../../../utilities/fileSystem" import { apiFileReturn } from "../../../../utilities/fileSystem"
import { utils } from "@budibase/shared-core"
export async function search(ctx: Ctx) { export async function search(ctx: Ctx) {
const tableId = ctx.params.tableId 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.") 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) { if (ctx.request.body.rows) {
ctx.request.body = { ctx.request.body = {
query: { query: {
@ -127,13 +138,27 @@ export async function exportRows(ctx: Ctx) {
let headers = Object.keys(schema) let headers = Object.keys(schema)
// @ts-ignore let content
const exporter = exporters[format] 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}` const filename = `export.${format}`
// send down the file // send down the file
ctx.attachment(filename) ctx.attachment(filename)
return apiFileReturn(exporter(headers, exportRows)) return apiFileReturn(content)
} }
export async function fetch(ctx: Ctx) { export async function fetch(ctx: Ctx) {

View File

@ -1,14 +1,14 @@
import { exportRows } from "../row/external" import { exportRows } from "../../app/rows/search/external"
import sdk from "../../../sdk" import sdk from "../.."
import { ExternalRequest } from "../row/ExternalRequest" import { ExternalRequest } from "../../../api/controllers/row/ExternalRequest"
// @ts-ignore const mockDatasourcesGet = jest.fn()
sdk.datasources = { sdk.datasources.get = mockDatasourcesGet
get: jest.fn(),
}
jest.mock("../row/ExternalRequest") jest.mock("../../../api/controllers/row/ExternalRequest")
jest.mock("../view/exporters", () => ({
jest.mock("../../../api/controllers/view/exporters", () => ({
...jest.requireActual("../../../api/controllers/view/exporters"),
csv: jest.fn(), csv: jest.fn(),
Format: { Format: {
CSV: "csv", CSV: "csv",
@ -31,14 +31,15 @@ function getUserCtx() {
throw "Err" throw "Err"
}), }),
attachment: jest.fn(), attachment: jest.fn(),
} } as any
} }
describe("external row controller", () => { describe("external row controller", () => {
describe("exportRows", () => { describe("exportRows", () => {
beforeAll(() => { beforeAll(() => {
//@ts-ignore jest
jest.spyOn(ExternalRequest.prototype, "run").mockImplementation(() => []) .spyOn(ExternalRequest.prototype, "run")
.mockImplementation(() => Promise.resolve([]))
}) })
afterEach(() => { afterEach(() => {
@ -48,7 +49,6 @@ describe("external row controller", () => {
it("should throw a 400 if no datasource entities are present", async () => { it("should throw a 400 if no datasource entities are present", async () => {
let userCtx = getUserCtx() let userCtx = getUserCtx()
try { try {
//@ts-ignore
await exportRows(userCtx) await exportRows(userCtx)
} catch (e) { } catch (e) {
expect(userCtx.throw).toHaveBeenCalledWith( expect(userCtx.throw).toHaveBeenCalledWith(
@ -59,8 +59,7 @@ describe("external row controller", () => {
}) })
it("should handle single quotes from a row ID", async () => { it("should handle single quotes from a row ID", async () => {
//@ts-ignore mockDatasourcesGet.mockImplementation(async () => ({
sdk.datasources.get.mockImplementation(() => ({
entities: { entities: {
tablename: { tablename: {
schema: {}, schema: {},
@ -72,7 +71,6 @@ describe("external row controller", () => {
rows: ["['d001']"], rows: ["['d001']"],
} }
//@ts-ignore
await exportRows(userCtx) await exportRows(userCtx)
expect(userCtx.request.body).toEqual({ expect(userCtx.request.body).toEqual({
@ -90,7 +88,6 @@ describe("external row controller", () => {
rows: ["[123]", "['d001'%2C'10111']"], rows: ["[123]", "['d001'%2C'10111']"],
} }
try { try {
//@ts-ignore
await exportRows(userCtx) await exportRows(userCtx)
} catch (e) { } catch (e) {
expect(userCtx.throw).toHaveBeenCalledWith( expect(userCtx.throw).toHaveBeenCalledWith(
@ -107,7 +104,6 @@ describe("external row controller", () => {
rows: ["[123]"], rows: ["[123]"],
} }
try { try {
//@ts-ignore
await exportRows(userCtx) await exportRows(userCtx)
} catch (e) { } catch (e) {
expect(userCtx.throw).toHaveBeenCalledWith( expect(userCtx.throw).toHaveBeenCalledWith(