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 {