package db import ( "strings" "testing" ) func TestBuildDailySummaryInsertDoesNotGroupFinalAggJoin(t *testing.T) { query, err := BuildDailySummaryInsert("inventory_daily_summary_20260101", "SELECT 1") if err != nil { t.Fatalf("BuildDailySummaryInsert failed: %v", err) } if !strings.Contains(query, `FROM agg JOIN totals ON totals."Vcenter" = agg."Vcenter";`) { t.Fatalf("expected final agg/totals join with terminator, query tail changed unexpectedly") } if strings.Contains(query, `FROM agg JOIN totals ON totals."Vcenter" = agg."Vcenter" GROUP BY`) { t.Fatalf("unexpected final GROUP BY after agg/totals join; this breaks Postgres SQLSTATE 42803") } } func TestBuildMonthlySummaryInsertCastsSampleSumToBigInt(t *testing.T) { query, err := BuildMonthlySummaryInsert("inventory_monthly_summary_202601", "SELECT 1") if err != nil { t.Fatalf("BuildMonthlySummaryInsert failed: %v", err) } if !strings.Contains(query, `CAST(SUM("SamplesPresent") AS BIGINT) AS "SamplesPresent"`) { t.Fatalf("expected monthly sample sum cast to BIGINT to avoid Postgres numeric assignment issues") } }