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) }