-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite_excel_test.ts
123 lines (103 loc) · 3.17 KB
/
write_excel_test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { assertEquals, assertRejects } from "@std/assert";
import { writeExcel } from "./write_excel.ts";
import { getRows, removeTestFile } from "./test_utils.ts";
import ExcelJS from "@tinkie101/exceljs-wrapper";
import pl from "./mod.ts";
Deno.test({
name: "writeExcel - Writes a DataFrame to a valid Excel file",
async fn() {
const filePath = "./test-write-valid.xlsx";
const df = pl.DataFrame({
Name: ["Alice", "Bob"],
Age: [30, 25],
Country: ["USA", "Canada"],
});
// Write the DataFrame to Excel
await writeExcel(df, filePath, { sheetName: "Sheet1" });
// Read back and validate
const workbook = new ExcelJS.Workbook();
await workbook.xlsx.readFile(filePath);
const worksheet = workbook.getWorksheet("Sheet1");
if (!worksheet) {
throw new Error("Worksheet 'Sheet1' does not exist.");
}
const rows = getRows(worksheet);
const expected = [
["Name", "Age", "Country"],
["Alice", 30, "USA"],
["Bob", 25, "Canada"],
];
assertEquals(rows, expected);
await removeTestFile(filePath);
},
});
Deno.test({
name: "writeExcel - Handles empty DataFrame",
async fn() {
const filePath = "./test-write-empty.xlsx";
const df = pl.DataFrame({});
await assertRejects(
async () => {
await writeExcel(df, filePath);
},
Error,
"The DataFrame is empty. Nothing to write.",
);
},
});
Deno.test({
name: "writeExcel - Writes to a specific sheet",
async fn() {
const filePath = "./test-write-specific-sheet.xlsx";
const df = pl.DataFrame({
Product: ["Widget", "Gadget"],
Price: [19.99, 29.99],
});
// Write the DataFrame to a specific sheet
await writeExcel(df, filePath, { sheetName: "Products" });
// Read back and validate the correct sheet
const workbook = new ExcelJS.Workbook();
await workbook.xlsx.readFile(filePath);
const worksheet = workbook.getWorksheet("Products");
if (!worksheet) {
throw new Error("Worksheet 'Products' does not exist.");
}
const rows = getRows(worksheet);
const expected = [
["Product", "Price"],
["Widget", 19.99],
["Gadget", 29.99],
];
assertEquals(rows, expected);
await removeTestFile(filePath);
},
});
Deno.test({
name: "writeExcel - Applies table styling",
async fn() {
const filePath = "./test-write-styled.xlsx";
const df = pl.DataFrame({
Month: ["January", "February"],
Sales: [15000, 20000],
});
// Write the DataFrame with styling
await writeExcel(df, filePath, {
sheetName: "Report",
tableStyle: "TableStyleMedium4",
});
const workbook = new ExcelJS.Workbook();
await workbook.xlsx.readFile(filePath);
const worksheet = workbook.getWorksheet("Report");
if (!worksheet) {
throw new Error("Worksheet 'Report' does not exist.");
}
const tables = worksheet.getTables();
if (!tables.length) {
throw new Error("Table not found.");
}
const { table } = JSON.parse(JSON.stringify(tables[0]));
assertEquals(table.name, "Table_Report");
assertEquals(table.style.theme, "TableStyleMedium4");
await removeTestFile(filePath);
},
});