Fix tests
This commit is contained in:
parent
d1b64bcd9c
commit
9fbad37218
|
@ -5,7 +5,6 @@ import {
|
|||
} from "../../../integrations/utils"
|
||||
import { ExternalRequest, RunConfig } from "./ExternalRequest"
|
||||
import {
|
||||
Ctx,
|
||||
Datasource,
|
||||
IncludeRelationship,
|
||||
Operation,
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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(
|
Loading…
Reference in New Issue