70 lines
2.3 KiB
Go
70 lines
2.3 KiB
Go
package model
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"github.com/go-playground/validator/v10"
|
|
)
|
|
|
|
func TestOrderRequestsAndResponsesPreserveFrontendFields(t *testing.T) {
|
|
inboundRequest := CreateInboundRequest{}
|
|
if err := json.Unmarshal([]byte(`{"handler":"张三","supplier":"供应商","remark":"到货验收通过","items":[{"material_id":1,"quantity":2}]}`), &inboundRequest); err != nil {
|
|
t.Fatalf("unmarshal inbound request: %v", err)
|
|
}
|
|
if inboundRequest.Remark != "到货验收通过" {
|
|
t.Fatalf("inbound remark = %q, want %q", inboundRequest.Remark, "到货验收通过")
|
|
}
|
|
|
|
outboundRequest := CreateOutboundRequest{}
|
|
if err := json.Unmarshal([]byte(`{"handler":"李四","receiver":"装配车间","remark":"急用","items":[{"material_id":1,"quantity":2}]}`), &outboundRequest); err != nil {
|
|
t.Fatalf("unmarshal outbound request: %v", err)
|
|
}
|
|
if outboundRequest.Remark != "急用" {
|
|
t.Fatalf("outbound remark = %q, want %q", outboundRequest.Remark, "急用")
|
|
}
|
|
|
|
payload, err := json.Marshal(struct {
|
|
Inbound InboundOrder `json:"inbound"`
|
|
Outbound OutboundOrder `json:"outbound"`
|
|
}{
|
|
Inbound: InboundOrder{InboundNo: "IN-20260920-0001", Operator: "管理员", Remark: "到货验收通过"},
|
|
Outbound: OutboundOrder{OutboundNo: "OUT-20260920-0001", Operator: "管理员", Remark: "急用"},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("marshal orders: %v", err)
|
|
}
|
|
|
|
for _, field := range []string{"inbound_no", "outbound_no", "operator", "remark"} {
|
|
if !containsJSONField(payload, field) {
|
|
t.Fatalf("order response omits %q: %s", field, payload)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestOutboundRequestDoesNotRequireClientProvidedHandler(t *testing.T) {
|
|
var request CreateOutboundRequest
|
|
if err := json.Unmarshal([]byte(`{"receiver":"装配车间","items":[{"material_id":1,"quantity":2}]}`), &request); err != nil {
|
|
t.Fatalf("unmarshal outbound request: %v", err)
|
|
}
|
|
|
|
validate := validator.New()
|
|
validate.SetTagName("binding")
|
|
if err := validate.Struct(request); err != nil {
|
|
t.Fatalf("outbound request without a client handler should be valid: %v", err)
|
|
}
|
|
}
|
|
|
|
func containsJSONField(payload []byte, field string) bool {
|
|
var decoded map[string]map[string]any
|
|
if err := json.Unmarshal(payload, &decoded); err != nil {
|
|
return false
|
|
}
|
|
for _, order := range decoded {
|
|
if _, ok := order[field]; ok {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|