33 lines
1.2 KiB
Go
33 lines
1.2 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.nianxx.cn/wangxuming/NianAIGC/backend/internal/billing"
|
|
)
|
|
|
|
func TestUpdateBillingPriceRulesUsesSingleGuardedStatement(t *testing.T) {
|
|
q := &fakeQuerier{}
|
|
db := NewDatabase(Config{Backend: BackendPostgres}, q)
|
|
patch := billing.BulkPricePatch{RuleIDs: []string{"first", "second"}, MarkupMultiplier: 1.75}
|
|
if _, err := db.UpdateBillingPriceRules(context.Background(), patch); !errors.Is(err, billing.ErrPriceNotFound) {
|
|
t.Fatalf("empty result error = %v", err)
|
|
}
|
|
if q.sql != UpdateBillingPriceRulesSQL || !reflect.DeepEqual(q.args, []any{patch.RuleIDs, patch.MarkupMultiplier}) {
|
|
t.Fatalf("query = %q, args = %#v", q.sql, q.args)
|
|
}
|
|
for _, clause := range []string{"FOR UPDATE", "count(*) = cardinality($1::text[])", "AND (SELECT all_present FROM eligible)", "jsonb_set(tier.value", "UPDATE public.billing_price_rules"} {
|
|
if !strings.Contains(q.sql, clause) {
|
|
t.Fatalf("missing atomic bulk update clause %q", clause)
|
|
}
|
|
}
|
|
q.sql = ""
|
|
if _, err := db.UpdateBillingPriceRules(context.Background(), billing.BulkPricePatch{MarkupMultiplier: 2}); !errors.Is(err, billing.ErrPriceNotFound) || q.sql != "" {
|
|
t.Fatalf("empty ID list issued a query: %q, %v", q.sql, err)
|
|
}
|
|
}
|