- 新增 response.ErrorWithData:校验失败要把逐行明细一起带回,不能只给一句话 - 新增 GET /api/material-import/template 与 POST /api/material-import - 路由挂在独立前缀 /material-import:materials 组已有 GET /:id,gin 的路由树 不允许同一段既有静态前缀又有通配参数 - 权限沿用物料新增的口径:登录 + 公司头校验,不额外要求 super_admin - 模板随代码生成不落盘,下载到的永远与当前校验规则一致
30 lines
720 B
Go
30 lines
720 B
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/wmms/wmms/pkg/response"
|
|
)
|
|
|
|
func TestErrorWithDataKeepsPayload(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
recorder := httptest.NewRecorder()
|
|
c, _ := gin.CreateTestContext(recorder)
|
|
|
|
response.ErrorWithData(c, http.StatusBadRequest, "有 1 行校验未通过", map[string]interface{}{
|
|
"total_rows": 1, "failed_rows": 1,
|
|
})
|
|
|
|
if recorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("status = %d, want 400", recorder.Code)
|
|
}
|
|
body := recorder.Body.String()
|
|
if !strings.Contains(body, "有 1 行校验未通过") || !strings.Contains(body, `"failed_rows":1`) {
|
|
t.Fatalf("body = %s", body)
|
|
}
|
|
}
|