46 lines
3.3 KiB
TypeScript
46 lines
3.3 KiB
TypeScript
import { mkdtemp, rm } from "node:fs/promises";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import { afterEach, describe, expect, it } from "vitest";
|
|
import type { CatalogComponent, FlowNode } from "../../src/types";
|
|
import type { RunContext } from "../../src/engine/types";
|
|
import { getHandler } from "../../src/engine/registry";
|
|
import "../../src/capabilities/data-table-common";
|
|
|
|
const dirs: string[] = [];
|
|
const context = { vars: {} } as RunContext;
|
|
const component = {} as CatalogComponent;
|
|
const node = (id: string): FlowNode => ({ id, componentId: id, position: { x: 0, y: 0 }, values: {}, options: {} });
|
|
async function run(id: string, args: Record<string, unknown> = {}) { const handler = getHandler(id); expect(handler, id).toBeTruthy(); return handler!(context, node(id), args, component); }
|
|
afterEach(async () => { context.vars = {}; await Promise.all(dirs.splice(0).map((dir) => rm(dir, { recursive: true, force: true }))); });
|
|
|
|
describe("common data table capabilities", () => {
|
|
it("reads, writes, calculates and joins columns", async () => {
|
|
const source = [["A", "B"], [1, 2], [3, 4]];
|
|
await expect(run("Bivartable_ReadDfData", { df: source, readRange: "cell", row_num: 2, col_name: "B" })).resolves.toBe(2);
|
|
await expect(run("Bivartable_ColumnOperationDf", { df: source, name1: "A", name2: "B", operator: "+", name: "Total" })).resolves.toEqual([["A", "B", "Total"], [1, 2, 3], [3, 4, 7]]);
|
|
await expect(run("Bivartable_DoJoint", { df: source, cola: "A", colb: "B", colnew: "AB" })).resolves.toEqual([["A", "B", "AB"], [1, 2, "12"], [3, 4, "34"]]);
|
|
});
|
|
|
|
it("fills missing values and modifies column names", async () => {
|
|
await expect(run("Bivartable_HandleDfMissValue", { df: [["A"], [null], [2]], handleType: "1", value: 0 })).resolves.toEqual([["A"], [0], [2]]);
|
|
await expect(run("Bivartable_ModifyColName", { df: [["A"], [1]], srcname: "A", newname: "Value" })).resolves.toEqual([["Value"], [1]]);
|
|
});
|
|
|
|
it("converts column types and builds a pivot table", async () => {
|
|
await expect(run("Bivartable_Dtype", { df: [["id", "amount"], ["1", "2.5"]], change_type: "name", col_name: ["id", "amount"], dtype: "float" })).resolves.toEqual([["id", "amount"], [1, 2.5]]);
|
|
const source = [["team", "month", "amount"], ["A", "Jan", 2], ["A", "Feb", 3], ["B", "Jan", 4]];
|
|
await expect(run("Bivartable_PivotDf", { df: source, row_list: ["team"], col_list: ["month"], operator: { amount: "sum" }, value: 0 })).resolves.toEqual([["team", "Jan", "Feb"], ["A", 2, 3], ["B", 4, 0]]);
|
|
});
|
|
|
|
it("exports and imports Excel and databook metadata", async () => {
|
|
const directory = await mkdtemp(path.join(os.tmpdir(), "rpa-table-common-")); dirs.push(directory); const file = path.join(directory, "table.xlsx");
|
|
await run("Bivartable_DoSave", { df: [["A", "B"], [1, 2]], filePath: file });
|
|
await expect(run("Bivartable_DoOpenExcel", { filePath: file, sheetName: "Sheet1" })).resolves.toEqual([["A", "B"], [1, 2]]);
|
|
await run("Databook_Import", { excel_path: file, sheet_name: "Sheet1" });
|
|
await run("Databook_SetColumnInfo", { column_name: "A", column_description: "编号" });
|
|
await expect(run("Databook_GetColumnInfo", { column_name: "A" })).resolves.toBe("编号");
|
|
await expect(run("Databook_Export", { export_path: directory, export_name: "book.xlsx" })).resolves.toBe(path.join(directory, "book.xlsx"));
|
|
});
|
|
});
|