From 898e1b023a906d2b4df2aa7a63994d9d0f29608e Mon Sep 17 00:00:00 2001 From: zhangwenjian Date: Wed, 16 Sep 2026 17:57:28 +0800 Subject: [PATCH] =?UTF-8?q?refactor=F0=9F=8E=A8:=20share=20the=20generator?= =?UTF-8?q?=20tests'=20engine=20and=20response=20decoding?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit newEngine takes the method, path and handler, so a second test file does not have to restate the sqlite connection, the driver override and its cleanup, the CustomError middleware and the two context keys. serveJSON does the same for running one request and decoding the envelope. Nothing about what is asserted changes; newColumnListEngine and columnListMsg keep their names and their callers. --- app/other/apis/tools/db_columns_test.go | 34 +++++++++++++++++++------ 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/app/other/apis/tools/db_columns_test.go b/app/other/apis/tools/db_columns_test.go index 34898242..e4681cfe 100644 --- a/app/other/apis/tools/db_columns_test.go +++ b/app/other/apis/tools/db_columns_test.go @@ -24,9 +24,15 @@ type bodyOf struct { Msg string `json:"msg"` } -// newColumnListEngine wires the handler the way the router does, including the +// newEngine wires one generator handler the way the router does, including the // middleware that turns pkg.Assert's panic into a response. -func newColumnListEngine(t *testing.T) *gin.Engine { +// +// The generator's queries target MySQL's information_schema and cannot run on +// the sqlite connection behind them; the driver setting only has to select that +// branch, since no statement here is expected to succeed. That makes this +// serviceable for any handler in this package whose behaviour is decided before +// the query goes out -- which is what these tests are about. +func newEngine(t *testing.T, method, path string, h gin.HandlerFunc) *gin.Engine { t.Helper() gin.SetMode(gin.TestMode) @@ -35,26 +41,33 @@ func newColumnListEngine(t *testing.T) *gin.Engine { t.Fatalf("open sqlite: %v", err) } - // The query targets MySQL's information_schema; the driver setting only has - // to select that branch, the statement itself is never expected to succeed. previous := config.DatabaseConfig.Driver config.DatabaseConfig.Driver = "mysql" t.Cleanup(func() { config.DatabaseConfig.Driver = previous }) r := gin.New() r.Use(middleware.CustomError) - r.GET("/db/columns/page", func(c *gin.Context) { + r.Handle(method, path, func(c *gin.Context) { c.Set("db", db) c.Set(pkg.LoggerKey, logger.NewHelper(logger.DefaultLogger)) - Gen{}.GetDBColumnList(c) + h(c) }) return r } -func columnListMsg(t *testing.T, r *gin.Engine, query string) bodyOf { +func newColumnListEngine(t *testing.T) *gin.Engine { + t.Helper() + return newEngine(t, http.MethodGet, "/db/columns/page", Gen{}.GetDBColumnList) +} + +// serveJSON runs one request through the engine and decodes the envelope every +// handler here answers with. A body that will not decode fails the test rather +// than being reported as a mismatched message, which reads as the handler +// having answered something unexpected instead of not having answered at all. +func serveJSON(t *testing.T, r *gin.Engine, req *http.Request) bodyOf { t.Helper() w := httptest.NewRecorder() - r.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/db/columns/page"+query, nil)) + r.ServeHTTP(w, req) var body bodyOf if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil { @@ -63,6 +76,11 @@ func columnListMsg(t *testing.T, r *gin.Engine, query string) bodyOf { return body } +func columnListMsg(t *testing.T, r *gin.Engine, query string) bodyOf { + t.Helper() + return serveJSON(t, r, httptest.NewRequest(http.MethodGet, "/db/columns/page"+query, nil)) +} + func TestGetDBColumnList_AcceptsATableName(t *testing.T) { body := columnListMsg(t, newColumnListEngine(t), "?tableName=sys_user") if body.Msg == emptyTableNameMsg {