feat(suffix): 支持根目录重命名、列宽保存、锚定值与文件发布
This commit is contained in:
@@ -11,6 +11,8 @@ import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -33,7 +35,7 @@ func TestSuffixAdminIntegration(t *testing.T) {
|
||||
if err = db.Ping(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, table := range []string{"suffix_files", "suffix_dir_anchors", "suffix_cells", "suffix_columns", "suffixes", "suffix_groups", "suffix_dirs"} {
|
||||
for _, table := range []string{"suffix_files", "suffix_dir_anchors", "suffix_cells", "suffix_columns", "suffixes", "suffix_groups", "suffix_dirs", "suffix_col_widths"} {
|
||||
if _, err = db.Exec("DELETE FROM " + table); err != nil {
|
||||
t.Fatalf("清理 %s: %v", table, err)
|
||||
}
|
||||
@@ -55,7 +57,8 @@ func TestSuffixAdminIntegration(t *testing.T) {
|
||||
|
||||
router := NewRouter()
|
||||
RegisterBusiness(router, db)
|
||||
t.Setenv("PACKAGE_STORAGE_ROOT", t.TempDir())
|
||||
storageRoot := t.TempDir()
|
||||
t.Setenv("PACKAGE_STORAGE_ROOT", storageRoot)
|
||||
do := func(method, path, body string) *httptest.ResponseRecorder {
|
||||
var reader *strings.Reader
|
||||
if body == "" {
|
||||
@@ -166,6 +169,16 @@ func TestSuffixAdminIntegration(t *testing.T) {
|
||||
if w.Code != 403 {
|
||||
t.Fatalf("删根 status=%d, want 403", w.Code)
|
||||
}
|
||||
// 根目录支持重命名
|
||||
w = authed(http.MethodPut, fmt.Sprintf("/api/v1/suffix-admin/dirs/%d", rootID), `{"name":"建设项目"}`)
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("改根名 status=%d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
w = authed(http.MethodGet, "/api/v1/suffix-admin/tree", "")
|
||||
dirs = getJSON(w)["data"].(map[string]any)["dirs"].([]any)
|
||||
if dirs[0].(map[string]any)["name"] != "建设项目" {
|
||||
t.Fatalf("改根名后=%v", dirs[0].(map[string]any)["name"])
|
||||
}
|
||||
|
||||
// 列(值设置勾选)+ 单元格 + 锚定
|
||||
w = authed(http.MethodPost, fmt.Sprintf("/api/v1/suffix-admin/dirs/%d/columns", dirID), `{"name":"招标人","is_value":false}`)
|
||||
@@ -235,9 +248,84 @@ func TestSuffixAdminIntegration(t *testing.T) {
|
||||
t.Fatal("第二列应为值列")
|
||||
}
|
||||
|
||||
// 锚定开与关
|
||||
// 列重命名:成功、同目录重名 409、空名 400
|
||||
w = authed(http.MethodPatch, fmt.Sprintf("/api/v1/suffix-admin/columns/%d", plainColID), `{"name":"招标单位"}`)
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("重命名列 status=%d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
if name := getJSON(w)["data"].(map[string]any)["name"].(string); name != "招标单位" {
|
||||
t.Fatalf("重命名后 name=%q, want 招标单位", name)
|
||||
}
|
||||
w = authed(http.MethodPatch, fmt.Sprintf("/api/v1/suffix-admin/columns/%d", plainColID), `{"name":"工料机类型"}`)
|
||||
if w.Code != 409 {
|
||||
t.Fatalf("重名列 status=%d, want 409", w.Code)
|
||||
}
|
||||
w = authed(http.MethodPatch, fmt.Sprintf("/api/v1/suffix-admin/columns/%d", plainColID), `{"name":" "}`)
|
||||
if w.Code != 400 {
|
||||
t.Fatalf("空名列 status=%d, want 400", w.Code)
|
||||
}
|
||||
|
||||
// 列有值不可删除:普通列已存「普通文本」
|
||||
w = authed(http.MethodDelete, fmt.Sprintf("/api/v1/suffix-admin/columns/%d", plainColID), "")
|
||||
if w.Code != 409 {
|
||||
t.Fatalf("删有值列 status=%d, want 409", w.Code)
|
||||
}
|
||||
// 空列可直接删除
|
||||
w = authed(http.MethodDelete, fmt.Sprintf("/api/v1/suffix-admin/columns/%d", insertedID), "")
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("删空列 status=%d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
// 清空单元格后即可删除
|
||||
w = authed(http.MethodPut, "/api/v1/suffix-admin/cells",
|
||||
fmt.Sprintf(`{"dir_id":%d,"suffix_id":%d,"column_id":%d,"value":""}`, dirID, suffixID, plainColID))
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("清空单元格 status=%d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
w = authed(http.MethodDelete, fmt.Sprintf("/api/v1/suffix-admin/columns/%d", plainColID), "")
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("清空后删列 status=%d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
|
||||
// 固定列列宽:默认值(后缀 50)→ 保存 → 回读;非法参数拒绝
|
||||
w = authed(http.MethodGet, "/api/v1/suffix-admin/col-widths", "")
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("读列宽 status=%d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
widths := getJSON(w)["data"].(map[string]any)["widths"].(map[string]any)
|
||||
if widths["basic"].(map[string]any)["suffix"].(float64) != 50 {
|
||||
t.Fatalf("basic.suffix 默认应为 50,got %v", widths["basic"].(map[string]any)["suffix"])
|
||||
}
|
||||
if _, has := widths["basic"].(map[string]any)["files"]; !has {
|
||||
t.Fatal("basic 固定列应含 files")
|
||||
}
|
||||
w = authed(http.MethodPut, "/api/v1/suffix-admin/col-widths", `{"scope":"workspace","key":"ops","width":66}`)
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("存列宽 status=%d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
w = authed(http.MethodGet, "/api/v1/suffix-admin/col-widths", "")
|
||||
widths = getJSON(w)["data"].(map[string]any)["widths"].(map[string]any)
|
||||
if widths["workspace"].(map[string]any)["ops"].(float64) != 66 {
|
||||
t.Fatalf("workspace.ops 应为 66,got %v", widths["workspace"].(map[string]any)["ops"])
|
||||
}
|
||||
if widths["workspace"].(map[string]any)["suffix"].(float64) != 50 {
|
||||
t.Fatalf("未保存过的 workspace.suffix 应仍为默认 50,got %v", widths["workspace"].(map[string]any)["suffix"])
|
||||
}
|
||||
w = authed(http.MethodPut, "/api/v1/suffix-admin/col-widths", `{"scope":"nope","key":"ops","width":66}`)
|
||||
if w.Code != 400 {
|
||||
t.Fatalf("非法 scope status=%d, want 400", w.Code)
|
||||
}
|
||||
w = authed(http.MethodPut, "/api/v1/suffix-admin/col-widths", `{"scope":"basic","key":"files","width":9999}`)
|
||||
if w.Code != 400 {
|
||||
t.Fatalf("列宽越界 status=%d, want 400", w.Code)
|
||||
}
|
||||
w = authed(http.MethodPut, "/api/v1/suffix-admin/col-widths", `{"scope":"basic","key":"nope","width":10}`)
|
||||
if w.Code != 400 {
|
||||
t.Fatalf("未知列 key status=%d, want 400", w.Code)
|
||||
}
|
||||
|
||||
// 锚定行存值:保存 → 回读;空值清除;超长拒绝
|
||||
w = authed(http.MethodPost, fmt.Sprintf("/api/v1/suffix-admin/dirs/%d/anchor", dirID),
|
||||
fmt.Sprintf(`{"suffix_id":%d,"on":true}`, suffixID))
|
||||
fmt.Sprintf(`{"suffix_id":%d,"value":"3"}`, suffixID))
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("锚定 status=%d", w.Code)
|
||||
}
|
||||
@@ -252,8 +340,25 @@ func TestSuffixAdminIntegration(t *testing.T) {
|
||||
t.Fatal("找不到 cos4 行")
|
||||
return nil
|
||||
}()
|
||||
if !cos4["anchor"].(bool) {
|
||||
t.Fatal("锚定后 anchor 应为 true")
|
||||
if cos4["anchor_value"].(string) != "3" {
|
||||
t.Fatalf("锚定后 anchor_value=%q, want 3", cos4["anchor_value"])
|
||||
}
|
||||
w = authed(http.MethodPost, fmt.Sprintf("/api/v1/suffix-admin/dirs/%d/anchor", dirID),
|
||||
fmt.Sprintf(`{"suffix_id":%d,"value":""}`, suffixID))
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("清锚定 status=%d", w.Code)
|
||||
}
|
||||
w = authed(http.MethodGet, fmt.Sprintf("/api/v1/suffix-admin/dirs/%d/page", dirID), "")
|
||||
sfx = getJSON(w)["data"].(map[string]any)["suffixes"].([]any)
|
||||
for _, line := range sfx {
|
||||
if line.(map[string]any)["name"] == "cos4" && line.(map[string]any)["anchor_value"].(string) != "" {
|
||||
t.Fatal("清空后 anchor_value 应为空")
|
||||
}
|
||||
}
|
||||
w = authed(http.MethodPost, fmt.Sprintf("/api/v1/suffix-admin/dirs/%d/anchor", dirID),
|
||||
fmt.Sprintf(`{"suffix_id":%d,"value":"%s"}`, suffixID, strings.Repeat("长", 191)))
|
||||
if w.Code != 400 {
|
||||
t.Fatalf("超长锚定值 status=%d, want 400", w.Code)
|
||||
}
|
||||
|
||||
// 模板上传(内容与哈希校验)→ 概览 → 删除
|
||||
@@ -274,6 +379,11 @@ func TestSuffixAdminIntegration(t *testing.T) {
|
||||
t.Fatalf("传模板 status=%d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
fileID := uint64(getJSON(w)["data"].(map[string]any)["id"].(float64))
|
||||
// 附件必须落盘在 attachments/<后缀id>/ 下
|
||||
storedName := getJSON(w)["data"].(map[string]any)["file_name"].(string)
|
||||
if _, err := os.Stat(filepath.Join(storageRoot, "attachments", strconv.FormatUint(suffixID, 10), storedName)); err != nil {
|
||||
t.Fatalf("附件未落在 attachments 目录: %v", err)
|
||||
}
|
||||
badSum := sha256.Sum256([]byte("tampered"))
|
||||
w = authed(http.MethodPost, fmt.Sprintf("/api/v1/suffix-admin/suffixes/%d/files", suffixID),
|
||||
fmt.Sprintf(`{"file_name":"坏.cos4","size":%d,"sha256":"%s","content_base64":"%s"}`,
|
||||
|
||||
Reference in New Issue
Block a user