fix: 档口入库码按货运单号直查顺运宝 (#245)

This commit is contained in:
chengma
2026-08-15 16:27:03 +08:00
parent 4d8434b499
commit b47e7ed9a9
5 changed files with 412 additions and 111 deletions
+138
View File
@@ -9,6 +9,7 @@ import (
"io"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"sync"
"sync/atomic"
@@ -334,6 +335,143 @@ func TestClient_ListTotal和ListPage(t *testing.T) {
}
}
func TestClient_ListByOrderNumber_使用Allcode精确查询(t *testing.T) {
const orderNumber = "260812711E49CU"
var paths []string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
paths = append(paths, r.URL.Path)
var body map[string]any
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
t.Fatalf("解析请求体失败: %v", err)
}
queries, _ := body["queries"].([]any)
if len(queries) != 1 {
t.Fatalf("queries=%v", body["queries"])
}
query, _ := queries[0].(map[string]any)
if query["dvalue"] != orderNumber || query["tableName"] != "t_stock" ||
query["colName"] != "allcode" || query["op"] != float64(6) ||
query["type"] != float64(0) || query["tableAlias"] != "t" || query["optType"] != float64(1) {
t.Fatalf("allcode 查询条件不正确: %v", query)
}
switch r.URL.Path {
case "/am/stock/listTotal":
w.Write(envelopeBody(t, true, "ok", 1, nil))
case "/am/stock/list":
w.Write(envelopeBody(t, true, "ok", map[string]any{
"total": 1,
"list": []map[string]any{{"id": 76000001, "code": orderNumber}},
}, nil))
default:
t.Fatalf("意外路径: %s", r.URL.Path)
}
}))
defer srv.Close()
client, _ := New(srv.URL)
rows, err := client.ListByOrderNumber(context.Background(), " "+orderNumber+" ")
if err != nil {
t.Fatal(err)
}
if len(rows) != 1 || rows[0].ID != 76000001 || rows[0].Code != orderNumber {
t.Fatalf("货运单查询结果不正确: %+v", rows)
}
if !reflect.DeepEqual(paths, []string{"/am/stock/listTotal", "/am/stock/list"}) {
t.Fatalf("请求路径不正确: %v", paths)
}
}
func TestClient_ListByOrderNumber_按20条分页读取(t *testing.T) {
const orderNumber = "ORDER-21"
listRequests := 0
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var body map[string]any
json.NewDecoder(r.Body).Decode(&body)
if r.URL.Path == "/am/stock/listTotal" {
w.Write(envelopeBody(t, true, "ok", 21, nil))
return
}
listRequests++
start := int(body["start"].(float64))
count := 20
if start == 20 {
count = 1
}
rows := make([]map[string]any, 0, count)
for index := 0; index < count; index++ {
rows = append(rows, map[string]any{"id": start + index + 1, "code": orderNumber})
}
w.Write(envelopeBody(t, true, "ok", map[string]any{"total": count, "list": rows}, nil))
}))
defer srv.Close()
client, _ := New(srv.URL)
rows, err := client.ListByOrderNumber(context.Background(), orderNumber)
if err != nil {
t.Fatal(err)
}
if len(rows) != 21 || listRequests != 2 || rows[20].ID != 21 {
t.Fatalf("分页结果不正确 rows=%d requests=%d last=%+v", len(rows), listRequests, rows[len(rows)-1])
}
}
func TestClient_ListByOrderNumber_拒绝不一致Code和不完整页(t *testing.T) {
for _, test := range []struct {
name string
total int
pageTotal int
rows []map[string]any
}{
{"code不一致", 1, 1, []map[string]any{{"id": 1, "code": "OTHER"}}},
{"分页不完整", 2, 1, []map[string]any{{"id": 1, "code": "ORDER"}}},
} {
t.Run(test.name, func(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/am/stock/listTotal" {
w.Write(envelopeBody(t, true, "ok", test.total, nil))
return
}
w.Write(envelopeBody(t, true, "ok", map[string]any{
"total": test.pageTotal, "list": test.rows,
}, nil))
}))
defer srv.Close()
client, _ := New(srv.URL)
if _, err := client.ListByOrderNumber(context.Background(), "ORDER"); err == nil {
t.Fatal("非法查询结果必须报错")
}
})
}
}
func TestClient_ListByOrderNumber_零命中不查列表且限制安全上限(t *testing.T) {
for _, test := range []struct {
name string
total int
wantError bool
}{
{"零命中", 0, false},
{"超过安全上限", orderNumberMaxMatches + 1, true},
} {
t.Run(test.name, func(t *testing.T) {
listRequests := 0
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/am/stock/list" {
listRequests++
}
w.Write(envelopeBody(t, true, "ok", test.total, nil))
}))
defer srv.Close()
client, _ := New(srv.URL)
rows, err := client.ListByOrderNumber(context.Background(), "ORDER")
if (err != nil) != test.wantError || (!test.wantError && len(rows) != 0) || listRequests != 0 {
t.Fatalf("rows=%v err=%v listRequests=%d", rows, err, listRequests)
}
})
}
}
func TestClient_ListPage要求合法Total(t *testing.T) {
for _, test := range []struct {
name string