All checks were successful
continuous-integration/drone/push Build is passing
63 lines
1.8 KiB
Go
63 lines
1.8 KiB
Go
package report
|
|
|
|
import (
|
|
"io"
|
|
"log/slog"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/xuri/excelize/v2"
|
|
)
|
|
|
|
func TestAddSummaryPivotSheetCreatesPivotTables(t *testing.T) {
|
|
xlsx := excelize.NewFile()
|
|
const dataSheet = "Snapshot Report"
|
|
if err := xlsx.SetSheetName("Sheet1", dataSheet); err != nil {
|
|
t.Fatalf("SetSheetName failed: %v", err)
|
|
}
|
|
|
|
headers := []string{"Name", "Datacenter", "ResourcePool", "AvgVcpuCount", "AvgRamGB", "AvgIsPresent"}
|
|
if err := xlsx.SetSheetRow(dataSheet, "A1", &headers); err != nil {
|
|
t.Fatalf("SetSheetRow header failed: %v", err)
|
|
}
|
|
|
|
row1 := []interface{}{"vm-1", "dc-1", "pool-1", 4.0, 16.0, 1.0}
|
|
if err := xlsx.SetSheetRow(dataSheet, "A2", &row1); err != nil {
|
|
t.Fatalf("SetSheetRow data failed: %v", err)
|
|
}
|
|
|
|
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
|
addSummaryPivotSheet(logger, xlsx, dataSheet, headers, 1, "inventory_daily_summary_20260215")
|
|
|
|
pivots, err := xlsx.GetPivotTables("Summary")
|
|
if err != nil {
|
|
t.Fatalf("GetPivotTables failed: %v", err)
|
|
}
|
|
if len(pivots) != 4 {
|
|
t.Fatalf("expected 4 pivot tables, got %d", len(pivots))
|
|
}
|
|
|
|
expectedNames := map[string]bool{
|
|
"PivotAvgVcpu": false,
|
|
"PivotAvgRam": false,
|
|
"PivotProratedVmCount": false,
|
|
"PivotVmNameCount": false,
|
|
}
|
|
for _, pivot := range pivots {
|
|
if _, ok := expectedNames[pivot.Name]; ok {
|
|
expectedNames[pivot.Name] = true
|
|
}
|
|
if strings.Contains(pivot.DataRange, "'") {
|
|
t.Fatalf("pivot %q has quoted DataRange %q; expected unquoted sheet reference", pivot.Name, pivot.DataRange)
|
|
}
|
|
if strings.Contains(pivot.PivotTableRange, "'") {
|
|
t.Fatalf("pivot %q has quoted PivotTableRange %q; expected unquoted sheet reference", pivot.Name, pivot.PivotTableRange)
|
|
}
|
|
}
|
|
for name, seen := range expectedNames {
|
|
if !seen {
|
|
t.Fatalf("missing expected pivot table %q", name)
|
|
}
|
|
}
|
|
}
|