feat(api): add namespaces, batch ops, and admin logs

This commit is contained in:
zenfun
2025-12-21 23:16:27 +08:00
parent 73147fc55a
commit c2ed2f3f9e
12 changed files with 824 additions and 42 deletions

View File

@@ -147,3 +147,56 @@ func TestDeleteModel_RemovesMeta(t *testing.T) {
t.Fatalf("expected model deleted, got count=%d", remaining)
}
}
func TestBatchModels_Delete(t *testing.T) {
h, db, mr := newTestHandlerWithRedis(t)
r := gin.New()
r.POST("/admin/models", h.CreateModel)
r.POST("/admin/models/batch", h.BatchModels)
create := func(name string) uint {
reqBody := map[string]any{"name": name}
b, _ := json.Marshal(reqBody)
req := httptest.NewRequest(http.MethodPost, "/admin/models", bytes.NewReader(b))
req.Header.Set("Content-Type", "application/json")
rr := httptest.NewRecorder()
r.ServeHTTP(rr, req)
if rr.Code != http.StatusCreated {
t.Fatalf("expected 201, got %d body=%s", rr.Code, rr.Body.String())
}
var created model.Model
if err := json.Unmarshal(rr.Body.Bytes(), &created); err != nil {
t.Fatalf("unmarshal: %v", err)
}
return created.ID
}
id1 := create("ns.b1")
id2 := create("ns.b2")
payload := map[string]any{
"action": "delete",
"ids": []uint{id1, id2},
}
b, _ := json.Marshal(payload)
req := httptest.NewRequest(http.MethodPost, "/admin/models/batch", bytes.NewReader(b))
req.Header.Set("Content-Type", "application/json")
rr := httptest.NewRecorder()
r.ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("expected 200, got %d body=%s", rr.Code, rr.Body.String())
}
var remaining int64
if err := db.Model(&model.Model{}).Count(&remaining).Error; err != nil {
t.Fatalf("count: %v", err)
}
if remaining != 0 {
t.Fatalf("expected models deleted, got %d", remaining)
}
if got := mr.HGet("meta:models", "ns.b1"); got != "" {
t.Fatalf("expected meta removed, got %q", got)
}
}