Files
vctp2/internal/tasks/inventoryDatabase_test.go
nathan 8ccf5a7009
continuous-integration/drone/push Build is passing
enhance utilisation of postgres features
2026-04-20 10:19:27 +10:00

54 lines
1.6 KiB
Go

package tasks
import "testing"
func TestPostgresMaxRowsPerStatement(t *testing.T) {
tests := []struct {
name string
cols int
expect int
}{
{name: "zero columns", cols: 0, expect: 1},
{name: "hourly cache columns", cols: 17, expect: 3855},
{name: "hourly snapshot columns", cols: 20, expect: 3276},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got := postgresMaxRowsPerStatement(tc.cols)
if got != tc.expect {
t.Fatalf("unexpected max rows: cols=%d got=%d want=%d", tc.cols, got, tc.expect)
}
})
}
}
func TestBuildPostgresMultiRowInsertSQL(t *testing.T) {
got := buildPostgresMultiRowInsertSQL("vm_hourly_stats", []string{"A", "B"}, 2, "")
want := `INSERT INTO vm_hourly_stats ("A","B") VALUES ($1,$2),($3,$4)`
if got != want {
t.Fatalf("unexpected SQL\nwant: %s\ngot: %s", want, got)
}
withSuffix := buildPostgresMultiRowInsertSQL("vm_hourly_stats", []string{"A"}, 1, ` ON CONFLICT ("A") DO NOTHING`)
wantSuffix := `INSERT INTO vm_hourly_stats ("A") VALUES ($1) ON CONFLICT ("A") DO NOTHING`
if withSuffix != wantSuffix {
t.Fatalf("unexpected SQL with suffix\nwant: %s\ngot: %s", wantSuffix, withSuffix)
}
}
func TestIsLegacyIsPresentError(t *testing.T) {
if !isLegacyIsPresentError(assertErr(`null value in column "IsPresent" violates not-null constraint`)) {
t.Fatal("expected legacy IsPresent error to be detected")
}
if isLegacyIsPresentError(assertErr("duplicate key value violates unique constraint")) {
t.Fatal("expected non-IsPresent errors to be ignored")
}
}
type testErr string
func (e testErr) Error() string { return string(e) }
func assertErr(msg string) error { return testErr(msg) }