package billing import ( "context" "errors" "math" "testing" ) type bulkStoreStub struct { *quoteStoreStub patch BulkPricePatch err error calls int } func (s *bulkStoreStub) UpdateBillingPriceRules(_ context.Context, patch BulkPricePatch) ([]PriceRule, error) { s.calls++ s.patch = patch return []PriceRule{{ID: "a"}}, s.err } func TestUpdatePricesValidatesExplicitIDsAndNormalizesMultiplier(t *testing.T) { store := &bulkStoreStub{quoteStoreStub: "eStoreStub{}} service := NewService(store, nil) for _, patch := range []BulkPricePatch{ {MarkupMultiplier: 1.2}, {RuleIDs: []string{""}, MarkupMultiplier: 1.2}, {RuleIDs: []string{" a"}, MarkupMultiplier: 1.2}, {RuleIDs: []string{"a", "a"}, MarkupMultiplier: 1.2}, {RuleIDs: []string{"a"}, MarkupMultiplier: 0.9}, {RuleIDs: []string{"a"}, MarkupMultiplier: 1000.1}, {RuleIDs: []string{"a"}, MarkupMultiplier: math.NaN()}, {RuleIDs: []string{"a"}, MarkupMultiplier: math.Inf(1)}, } { if _, err := service.UpdatePrices(context.Background(), patch); HTTPStatus(err) != 400 { t.Fatalf("patch %+v: status = %d, err = %v", patch, HTTPStatus(err), err) } } if store.calls != 0 { t.Fatalf("invalid patch reached store %d times", store.calls) } rules, err := service.UpdatePrices(context.Background(), BulkPricePatch{RuleIDs: []string{"a"}, MarkupMultiplier: 1.234567}) if err != nil || len(rules) != 1 || store.patch.MarkupMultiplier != 1.2346 { t.Fatalf("rules = %+v, patch = %+v, err = %v", rules, store.patch, err) } store.err = ErrPriceNotFound if _, err := service.UpdatePrices(context.Background(), BulkPricePatch{RuleIDs: []string{"a"}, MarkupMultiplier: 2}); HTTPStatus(err) != 404 || !errors.Is(err, ErrPriceNotFound) { t.Fatalf("missing rule status = %d, err = %v", HTTPStatus(err), err) } }