All checks were successful
continuous-integration/drone/push Build is passing
78 lines
2.4 KiB
Go
78 lines
2.4 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 := []any{"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", nil)
|
|
|
|
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,
|
|
}
|
|
var avgVcpuPivot excelize.PivotTableOptions
|
|
avgVcpuFound := false
|
|
for _, pivot := range pivots {
|
|
if _, ok := expectedNames[pivot.Name]; ok {
|
|
expectedNames[pivot.Name] = true
|
|
}
|
|
if pivot.Name == "PivotAvgVcpu" {
|
|
avgVcpuPivot = pivot
|
|
avgVcpuFound = 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)
|
|
}
|
|
}
|
|
if !avgVcpuFound {
|
|
t.Fatal("missing PivotAvgVcpu definition")
|
|
}
|
|
if len(avgVcpuPivot.Rows) != 1 || avgVcpuPivot.Rows[0].Data != "Datacenter" {
|
|
t.Fatalf("PivotAvgVcpu rows = %#v; expected Datacenter only", avgVcpuPivot.Rows)
|
|
}
|
|
if len(avgVcpuPivot.Columns) != 1 || avgVcpuPivot.Columns[0].Data != "ResourcePool" {
|
|
t.Fatalf("PivotAvgVcpu columns = %#v; expected ResourcePool only", avgVcpuPivot.Columns)
|
|
}
|
|
}
|