Refactor and clean export tests
This commit is contained in:
parent
07607c0fd2
commit
deb256a013
5 changed files with 59 additions and 73 deletions
|
@ -49,6 +49,7 @@ export async function handleRequest(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return new ExternalRequest(operation, tableId, opts?.datasource).run(
|
return new ExternalRequest(operation, tableId, opts?.datasource).run(
|
||||||
opts || {}
|
opts || {}
|
||||||
)
|
)
|
||||||
|
|
|
@ -37,8 +37,8 @@ export async function search(options: SearchParams) {
|
||||||
export interface ExportRowsParams {
|
export interface ExportRowsParams {
|
||||||
tableId: string
|
tableId: string
|
||||||
format: Format
|
format: Format
|
||||||
rowIds: string[]
|
rowIds?: string[]
|
||||||
columns: string[]
|
columns?: string[]
|
||||||
query: SearchFilters
|
query: SearchFilters
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -86,11 +86,6 @@ export async function exportRows(
|
||||||
const { tableId, format, columns, rowIds } = options
|
const { tableId, format, columns, rowIds } = options
|
||||||
const { datasourceId, tableName } = breakExternalTableId(tableId)
|
const { datasourceId, tableName } = breakExternalTableId(tableId)
|
||||||
|
|
||||||
const datasource = await sdk.datasources.get(datasourceId!)
|
|
||||||
if (!datasource || !datasource.entities) {
|
|
||||||
throw new HTTPError("Datasource has not been configured for plus API.", 400)
|
|
||||||
}
|
|
||||||
|
|
||||||
let query: SearchFilters = {}
|
let query: SearchFilters = {}
|
||||||
if (rowIds?.length) {
|
if (rowIds?.length) {
|
||||||
query = {
|
query = {
|
||||||
|
@ -111,6 +106,11 @@ export async function exportRows(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const datasource = await sdk.datasources.get(datasourceId!)
|
||||||
|
if (!datasource || !datasource.entities) {
|
||||||
|
throw new HTTPError("Datasource has not been configured for plus API.", 400)
|
||||||
|
}
|
||||||
|
|
||||||
let result = await search({ tableId, query })
|
let result = await search({ tableId, query })
|
||||||
let rows: Row[] = []
|
let rows: Row[] = []
|
||||||
|
|
||||||
|
|
|
@ -14,7 +14,7 @@ export function cleanExportRows(
|
||||||
rows: any[],
|
rows: any[],
|
||||||
schema: TableSchema,
|
schema: TableSchema,
|
||||||
format: string,
|
format: string,
|
||||||
columns: string[]
|
columns?: string[]
|
||||||
) {
|
) {
|
||||||
let cleanRows = [...rows]
|
let cleanRows = [...rows]
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
import { exportRows } from "../../app/rows/search/external"
|
import { exportRows } from "../../app/rows/search/external"
|
||||||
import sdk from "../.."
|
import sdk from "../.."
|
||||||
import { ExternalRequest } from "../../../api/controllers/row/ExternalRequest"
|
import { ExternalRequest } from "../../../api/controllers/row/ExternalRequest"
|
||||||
|
import { ExportRowsParams } from "../../app/rows/search"
|
||||||
|
import { Format } from "../../../api/controllers/view/exporters"
|
||||||
|
import { HTTPError } from "@budibase/backend-core"
|
||||||
|
import { Operation } from "@budibase/types"
|
||||||
|
|
||||||
const mockDatasourcesGet = jest.fn()
|
const mockDatasourcesGet = jest.fn()
|
||||||
sdk.datasources.get = mockDatasourcesGet
|
sdk.datasources.get = mockDatasourcesGet
|
||||||
|
@ -16,30 +20,21 @@ jest.mock("../../../api/controllers/view/exporters", () => ({
|
||||||
}))
|
}))
|
||||||
jest.mock("../../../utilities/fileSystem")
|
jest.mock("../../../utilities/fileSystem")
|
||||||
|
|
||||||
function getUserCtx() {
|
describe("external row sdk", () => {
|
||||||
|
describe("exportRows", () => {
|
||||||
|
function getExportOptions(): ExportRowsParams {
|
||||||
return {
|
return {
|
||||||
params: {
|
|
||||||
tableId: "datasource__tablename",
|
tableId: "datasource__tablename",
|
||||||
},
|
format: Format.CSV,
|
||||||
query: {
|
query: {},
|
||||||
format: "csv",
|
}
|
||||||
},
|
|
||||||
request: {
|
|
||||||
body: {},
|
|
||||||
},
|
|
||||||
throw: jest.fn(() => {
|
|
||||||
throw "Err"
|
|
||||||
}),
|
|
||||||
attachment: jest.fn(),
|
|
||||||
} as any
|
|
||||||
}
|
}
|
||||||
|
|
||||||
describe("external row controller", () => {
|
const externalRequestCall = jest.fn()
|
||||||
describe("exportRows", () => {
|
|
||||||
beforeAll(() => {
|
beforeAll(() => {
|
||||||
jest
|
jest
|
||||||
.spyOn(ExternalRequest.prototype, "run")
|
.spyOn(ExternalRequest.prototype, "run")
|
||||||
.mockImplementation(() => Promise.resolve([]))
|
.mockImplementation(externalRequestCall.mockResolvedValue([]))
|
||||||
})
|
})
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
|
@ -47,15 +42,10 @@ 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()
|
const exportOptions = getExportOptions()
|
||||||
try {
|
await expect(exportRows(exportOptions)).rejects.toThrowError(
|
||||||
await exportRows(userCtx)
|
new HTTPError("Datasource has not been configured for plus API.", 400)
|
||||||
} catch (e) {
|
|
||||||
expect(userCtx.throw).toHaveBeenCalledWith(
|
|
||||||
400,
|
|
||||||
"Datasource has not been configured for plus API."
|
|
||||||
)
|
)
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should handle single quotes from a row ID", async () => {
|
it("should handle single quotes from a row ID", async () => {
|
||||||
|
@ -66,51 +56,46 @@ describe("external row controller", () => {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}))
|
}))
|
||||||
let userCtx = getUserCtx()
|
const exportOptions = getExportOptions()
|
||||||
userCtx.request.body = {
|
exportOptions.rowIds = ["['d001']"]
|
||||||
rows: ["['d001']"],
|
|
||||||
}
|
|
||||||
|
|
||||||
await exportRows(userCtx)
|
await exportRows(exportOptions)
|
||||||
|
|
||||||
expect(userCtx.request.body).toEqual({
|
expect(ExternalRequest).toBeCalledTimes(1)
|
||||||
query: {
|
expect(ExternalRequest).toBeCalledWith(
|
||||||
|
Operation.READ,
|
||||||
|
exportOptions.tableId,
|
||||||
|
undefined
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(externalRequestCall).toBeCalledTimes(1)
|
||||||
|
expect(externalRequestCall).toBeCalledWith(
|
||||||
|
expect.objectContaining({
|
||||||
|
filters: {
|
||||||
oneOf: {
|
oneOf: {
|
||||||
_id: ["d001"],
|
_id: ["d001"],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should throw a 400 if any composite keys are present", async () => {
|
it("should throw a 400 if any composite keys are present", async () => {
|
||||||
let userCtx = getUserCtx()
|
const exportOptions = getExportOptions()
|
||||||
userCtx.request.body = {
|
exportOptions.rowIds = ["[123]", "['d001'%2C'10111']"]
|
||||||
rows: ["[123]", "['d001'%2C'10111']"],
|
await expect(exportRows(exportOptions)).rejects.toThrowError(
|
||||||
}
|
new HTTPError("Export data does not support composite keys.", 400)
|
||||||
try {
|
|
||||||
await exportRows(userCtx)
|
|
||||||
} catch (e) {
|
|
||||||
expect(userCtx.throw).toHaveBeenCalledWith(
|
|
||||||
400,
|
|
||||||
"Export data does not support composite keys."
|
|
||||||
)
|
)
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should throw a 400 if no table name was found", async () => {
|
it("should throw a 400 if no table name was found", async () => {
|
||||||
let userCtx = getUserCtx()
|
const exportOptions = getExportOptions()
|
||||||
userCtx.params.tableId = "datasource__"
|
exportOptions.tableId = "datasource__"
|
||||||
userCtx.request.body = {
|
exportOptions.rowIds = ["[123]"]
|
||||||
rows: ["[123]"],
|
|
||||||
}
|
await expect(exportRows(exportOptions)).rejects.toThrowError(
|
||||||
try {
|
new HTTPError("Could not find table name.", 400)
|
||||||
await exportRows(userCtx)
|
|
||||||
} catch (e) {
|
|
||||||
expect(userCtx.throw).toHaveBeenCalledWith(
|
|
||||||
400,
|
|
||||||
"Could not find table name."
|
|
||||||
)
|
)
|
||||||
}
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue