ids-svc-product/internal/server/product_freight_charge_test.go

84 lines
2.3 KiB
Go

package server
import (
"strings"
"testing"
productv1 "llms.idsbuybuybuy.com/ecommerce/ids-proto/gen/go/ids/product/v1"
)
func TestProductFreightChargeFromUpdateRequest(t *testing.T) {
fixedAmount := int64(1000)
templateID := "2114688315977371648"
tooLongSPU := strings.Repeat("a", 101)
tests := []struct {
name string
req *productv1.UpdateProductFreightChargeRequest
wantErr string
wantType int32
wantFee int64
wantID string
}{
{
name: "fixed freight",
req: &productv1.UpdateProductFreightChargeRequest{
SpuNo: " SPU-001 ",
FreightChargeType: 1,
FreightFixedAmount: &fixedAmount,
},
wantType: 1,
wantFee: 1000,
},
{
name: "freight template",
req: &productv1.UpdateProductFreightChargeRequest{
SpuNo: "SPU-001",
FreightChargeType: 2,
FreightTemplateId: &templateID,
},
wantType: 2,
wantID: templateID,
},
{
name: "fixed freight needs amount",
req: &productv1.UpdateProductFreightChargeRequest{
SpuNo: "SPU-001", FreightChargeType: 1,
},
wantErr: "freight_fixed_amount is required",
},
{
name: "template mode rejects amount",
req: &productv1.UpdateProductFreightChargeRequest{
SpuNo: "SPU-001", FreightChargeType: 2, FreightFixedAmount: &fixedAmount, FreightTemplateId: &templateID,
},
wantErr: "freight_fixed_amount must be omitted",
},
{
name: "spu cannot exceed limit",
req: &productv1.UpdateProductFreightChargeRequest{
SpuNo: tooLongSPU, FreightChargeType: 1, FreightFixedAmount: &fixedAmount,
},
wantErr: "spu_no must contain at most 100 characters",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
spuNo, chargeType, fee, gotTemplateID, err := productFreightChargeFromUpdateRequest(tt.req)
if tt.wantErr != "" {
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
t.Fatalf("error = %v, want containing %q", err, tt.wantErr)
}
return
}
if err != nil {
t.Fatalf("productFreightChargeFromUpdateRequest() error = %v", err)
}
if spuNo != "SPU-001" || chargeType != tt.wantType || fee != tt.wantFee || gotTemplateID != tt.wantID {
t.Fatalf("result = (%q, %d, %d, %q), want (%q, %d, %d, %q)", spuNo, chargeType, fee, gotTemplateID, "SPU-001", tt.wantType, tt.wantFee, tt.wantID)
}
})
}
}