package postgres import ( "context" "encoding/json" "reflect" "strings" "testing" "git.nianxx.cn/wangxuming/NianAIGC/backend/internal/billing" ) func TestSeedBillingPriceRulesUsesOneIdempotentUpsert(t *testing.T) { q := &fakeQuerier{} db := NewDatabase(Config{Backend: BackendPostgres}, q) rules := []billing.PriceRule{{ID: "r", Provider: "seedance", Capability: "video.generate", ReqKey: "m", VariantKey: "resolution=720p", Unit: billing.UnitVideoSecond, StandardUnitPriceFen: 99, MarkupMultiplier: 1.2, Enabled: true}} if err := db.SeedBillingPriceRules(context.Background(), rules); err != nil { t.Fatal(err) } if q.sql != SeedBillingPriceRulesSQL || !strings.Contains(q.sql, "ON CONFLICT (provider, capability, COALESCE(req_key, ''), COALESCE(variant_key, ''), COALESCE(conditions, '{}'::jsonb)) DO UPDATE") { t.Fatalf("sql = %q", q.sql) } if len(q.args) != 1 { t.Fatalf("args = %#v", q.args) } var decoded []billing.PriceRule if err := json.Unmarshal(q.args[0].([]byte), &decoded); err != nil { t.Fatal(err) } if !reflect.DeepEqual(decoded, rules) { t.Fatalf("payload = %#v", decoded) } }