Files
xls_doc/internal/docfill/case_test.go
2026-06-18 20:03:47 +08:00

161 lines
5.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package docfill
import (
"database/sql"
"path/filepath"
"strings"
"testing"
)
func TestCaseExcelDailyDataToExcelReports(t *testing.T) {
dir := t.TempDir()
dataPath := filepath.Join(dir, "daily-data.xlsx")
templatePath := filepath.Join(dir, "daily-template.xlsx")
configPath := filepath.Join(dir, "task.json")
outputPattern := filepath.Join(dir, "out", "日报_{{date}}_{{name}}.xlsx")
writeWorkbook(t, dataPath, "Daily", [][]string{
{"name", "date", "system_a_count", "system_b_amount", "summary"},
{"张三", "2026-06-18", "12", "3500", "接口数据已核对"},
{"李四", "2026-06-18", "8", "2100", "报表已提交"},
})
writeWorkbook(t, templatePath, "日报", [][]string{
{"员工", "{{name}}"},
{"日期", "{{date}}"},
{"A系统数量", "{{system_a_count}}"},
{"B系统金额", "{{system_b_amount}}"},
{"今日摘要", "{{summary}}"},
})
writeJSON(t, configPath, map[string]any{
"data": map[string]any{
"type": "excel",
"path": dataPath,
"sheet": "Daily",
},
"template": map[string]any{
"path": templatePath,
},
"output": map[string]any{
"path": outputPattern,
},
})
if err := Run(configPath); err != nil {
t.Fatalf("Run returned error: %v", err)
}
assertWorkbookCell(t, filepath.Join(dir, "out", "日报_2026-06-18_张三.xlsx"), "日报", "B1", "张三")
assertWorkbookCell(t, filepath.Join(dir, "out", "日报_2026-06-18_张三.xlsx"), "日报", "B4", "3500")
assertWorkbookCell(t, filepath.Join(dir, "out", "日报_2026-06-18_李四.xlsx"), "日报", "B5", "报表已提交")
}
func TestCaseSQLiteDailyDataToWordReports(t *testing.T) {
dir := t.TempDir()
dbPath := filepath.Join(dir, "daily.db")
templatePath := filepath.Join(dir, "daily-template.docx")
configPath := filepath.Join(dir, "task.json")
outputPattern := filepath.Join(dir, "out", "日报_{{date}}_{{name}}.docx")
seedCaseSQLite(t, dbPath, `
create table daily_report (
name text,
date text,
done text,
plan text,
risk text
);
insert into daily_report values
('张三', '2026-06-18', '完成接口数据核对', '继续处理异常数据', '无'),
('李四', '2026-06-18', '完成日报提交', '补充系统截图', '等待业务确认');
`)
writeMinimalDocx(t, templatePath, "员工:{{name}}\n日期{{date}}\n完成{{done}}\n计划{{plan}}\n风险{{risk}}")
writeJSON(t, configPath, map[string]any{
"data": map[string]any{
"type": "sqlite",
"path": dbPath,
"query": "select name, date, done, plan, risk from daily_report order by name",
},
"template": map[string]any{
"path": templatePath,
},
"output": map[string]any{
"path": outputPattern,
},
})
if err := Run(configPath); err != nil {
t.Fatalf("Run returned error: %v", err)
}
zhangsanDoc := readZipEntry(t, filepath.Join(dir, "out", "日报_2026-06-18_张三.docx"), "word/document.xml")
if !strings.Contains(zhangsanDoc, "完成接口数据核对") || !strings.Contains(zhangsanDoc, "风险:无") {
t.Fatalf("unexpected 张三 docx body: %q", zhangsanDoc)
}
lisiDoc := readZipEntry(t, filepath.Join(dir, "out", "日报_2026-06-18_李四.docx"), "word/document.xml")
if !strings.Contains(lisiDoc, "补充系统截图") || !strings.Contains(lisiDoc, "等待业务确认") {
t.Fatalf("unexpected 李四 docx body: %q", lisiDoc)
}
}
func TestCaseSQLiteSummaryDataToExcelReports(t *testing.T) {
dir := t.TempDir()
dbPath := filepath.Join(dir, "summary.db")
templatePath := filepath.Join(dir, "summary-template.xlsx")
configPath := filepath.Join(dir, "task.json")
outputPattern := filepath.Join(dir, "out", "汇总_{{department}}_{{_row}}.xlsx")
seedCaseSQLite(t, dbPath, `
create table department_summary (
department text,
report_date text,
total_count integer,
abnormal_count integer
);
insert into department_summary values
('营销部', '2026-06-18', 128, 3),
('客服部', '2026-06-18', 76, 0);
`)
writeWorkbook(t, templatePath, "汇总", [][]string{
{"部门", "{{department}}"},
{"日期", "{{report_date}}"},
{"总数", "{{total_count}}"},
{"异常数", "{{abnormal_count}}"},
})
writeJSON(t, configPath, map[string]any{
"data": map[string]any{
"type": "sqlite",
"path": dbPath,
"query": "select department, report_date, total_count, abnormal_count from department_summary order by department desc",
},
"template": map[string]any{
"path": templatePath,
},
"output": map[string]any{
"path": outputPattern,
},
})
if err := Run(configPath); err != nil {
t.Fatalf("Run returned error: %v", err)
}
assertWorkbookCell(t, filepath.Join(dir, "out", "汇总_营销部_1.xlsx"), "汇总", "B3", "128")
assertWorkbookCell(t, filepath.Join(dir, "out", "汇总_营销部_1.xlsx"), "汇总", "B4", "3")
assertWorkbookCell(t, filepath.Join(dir, "out", "汇总_客服部_2.xlsx"), "汇总", "B4", "0")
}
func seedCaseSQLite(t *testing.T, path, statements string) {
t.Helper()
db, err := sql.Open("sqlite", path)
if err != nil {
t.Fatalf("open sqlite: %v", err)
}
defer db.Close()
if _, err := db.Exec(statements); err != nil {
t.Fatalf("seed sqlite: %v", err)
}
}