mirror of
https://ids-dev.idsbuybuybuy.com/ids-ecommerce/ids-svc-product.git
synced 2026-11-29 16:57:41 +08:00
84 lines
2.0 KiB
Go
84 lines
2.0 KiB
Go
package server
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"llms.idsbuybuybuy.com/ecommerce/ids-svc-product/internal/repository"
|
|
)
|
|
|
|
func TestValidateProductDeliveryConfiguration(t *testing.T) {
|
|
scheduled := time.Date(2026, 11, 1, 0, 0, 0, 0, time.UTC)
|
|
tests := []struct {
|
|
name string
|
|
product repository.ProductRecord
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "specified delivery time",
|
|
product: repository.ProductRecord{
|
|
PresaleType: 1,
|
|
DeliveryTimeType: 1,
|
|
DeliveryStartAt: &scheduled,
|
|
PurchaseLimitQuantity: 2,
|
|
},
|
|
},
|
|
{
|
|
name: "after paid days",
|
|
product: repository.ProductRecord{
|
|
PresaleType: 2,
|
|
DeliveryTimeType: 2,
|
|
DeliveryAfterPaidDays: 7,
|
|
PurchaseLimitQuantity: 0,
|
|
},
|
|
},
|
|
{
|
|
name: "specified delivery time requires date",
|
|
product: repository.ProductRecord{
|
|
DeliveryTimeType: 1,
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "after paid days must be positive",
|
|
product: repository.ProductRecord{
|
|
DeliveryTimeType: 2,
|
|
DeliveryAfterPaidDays: 0,
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "purchase limit cannot be negative",
|
|
product: repository.ProductRecord{
|
|
PurchaseLimitQuantity: -1,
|
|
},
|
|
wantErr: true,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
product := tt.product
|
|
err := validateProductDeliveryConfiguration(&product)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Fatalf("validateProductDeliveryConfiguration() error = %v, wantErr %v", err, tt.wantErr)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestValidateProductDeliveryConfigurationClearsInapplicableValues(t *testing.T) {
|
|
scheduled := time.Date(2026, 11, 1, 0, 0, 0, 0, time.UTC)
|
|
product := repository.ProductRecord{
|
|
DeliveryTimeType: 0,
|
|
DeliveryStartAt: &scheduled,
|
|
DeliveryAfterPaidDays: 5,
|
|
}
|
|
if err := validateProductDeliveryConfiguration(&product); err != nil {
|
|
t.Fatalf("validateProductDeliveryConfiguration() error = %v", err)
|
|
}
|
|
if product.DeliveryStartAt != nil || product.DeliveryAfterPaidDays != 0 {
|
|
t.Fatalf("inapplicable delivery values were not cleared: %#v", product)
|
|
}
|
|
}
|