89 lines
2.3 KiB
Go
89 lines
2.3 KiB
Go
package usecase
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"cmroubao/backend-api/internal/domain"
|
|
)
|
|
|
|
func TestFreightNormalizationHashExcludesInternalIDs(t *testing.T) {
|
|
source := validFreightSource()
|
|
firstService := &FreightService{ids: &sequenceIDs{next: 10}}
|
|
first, err := firstService.normalize(source)
|
|
if err != nil {
|
|
t.Fatalf("first normalize error = %v", err)
|
|
}
|
|
secondService := &FreightService{ids: &sequenceIDs{next: 100}}
|
|
second, err := secondService.normalize(source)
|
|
if err != nil {
|
|
t.Fatalf("second normalize error = %v", err)
|
|
}
|
|
if first.Orders[0].ID == second.Orders[0].ID {
|
|
t.Fatal("test IDs did not differ")
|
|
}
|
|
if first.Orders[0].CanonicalSHA256 !=
|
|
second.Orders[0].CanonicalSHA256 {
|
|
t.Fatalf(
|
|
"canonical hash depends on internal ID: %s != %s",
|
|
first.Orders[0].CanonicalSHA256,
|
|
second.Orders[0].CanonicalSHA256,
|
|
)
|
|
}
|
|
}
|
|
|
|
func TestFreightNormalizationRejectsConflictingIdentityAndInvalidTime(
|
|
t *testing.T,
|
|
) {
|
|
duplicate := validFreightSource()
|
|
duplicate.Orders[0].Items = append(
|
|
duplicate.Orders[0].Items,
|
|
duplicate.Orders[0].Items[0],
|
|
)
|
|
service := &FreightService{ids: &sequenceIDs{}}
|
|
if _, err := service.normalize(duplicate); err == nil {
|
|
t.Fatal("duplicate item normalize error = nil")
|
|
}
|
|
|
|
invalidTime := validFreightSource()
|
|
value := "not-a-time"
|
|
invalidTime.Orders[0].SourceCreatedAt = &value
|
|
if _, err := service.normalize(invalidTime); err == nil {
|
|
t.Fatal("invalid source time normalize error = nil")
|
|
}
|
|
}
|
|
|
|
func validFreightSource() domain.FreightSourceBatch {
|
|
shop := "测试店铺"
|
|
sourceCreatedAt := "2026-07-28 08:00:00"
|
|
orderStatus := "0"
|
|
purchaseStatus := "1"
|
|
thumb := "190"
|
|
itemPurchaseStatus := "0"
|
|
quantity := 2
|
|
canceled := false
|
|
return domain.FreightSourceBatch{
|
|
SchemaVersion: 1,
|
|
Query: domain.FreightSourceQuery{
|
|
Mode: domain.FreightSyncOrderNumber,
|
|
},
|
|
Orders: []domain.FreightSourceOrder{{
|
|
ExternalStockID: "12",
|
|
SourceCode: "SOURCE-12",
|
|
ShopName: &shop,
|
|
SourceCreatedAt: &sourceCreatedAt,
|
|
OrderStatus: &orderStatus,
|
|
PurchaseStatus: &purchaseStatus,
|
|
IsCanceled: &canceled,
|
|
Items: []domain.FreightSourceItem{{
|
|
ExternalItemID: "88",
|
|
Title: "商品",
|
|
ProductSpec: "黑色,L",
|
|
SKU: "BLACK-L",
|
|
Quantity: &quantity,
|
|
ProductThumbRef: &thumb,
|
|
PurchaseStatus: &itemPurchaseStatus,
|
|
}},
|
|
}},
|
|
}
|
|
}
|