-
I am still fairly new to golang especially the testing side of things. I have been digging through the source code and I wanted to see how I can test an instrumentation middleware for echo I have. To me it seems like it gets stubbed out in the reltest repository mock. For reference this is the middleware I am talking about, it adds queries for debugging for admins in my application: func RelQueryLoggerMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
// Don't do any of this when public assets
if ignorePublicAssetRoutes(c) {
return next(c)
}
templateData := c.Get("tmpl").(map[string]interface{})
debugging, ok := c.Get("sql").(db.Debugging)
if !ok {
debugging = db.Debugging{}
}
// Log the executed SQL queries
db.Database.Instrumentation(func(ctx context.Context, op string, message string, args ...interface{}) func(err error) {
t := time.Now()
return func(err error) {
if op != "adapter-query" {
return
}
duration := time.Since(t)
debugging.Queries = append(debugging.Queries, db.Query{
Statement: message,
Duration: duration,
Err: err,
Args: args,
})
if err != nil {
debugging.Errors += 1
}
debugging.Duration = debugging.Duration + duration
helper.SetDBDebugging(templateData, debugging)
}
})
return next(c)
}
} Is this even a sane approach? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Beta Was this translation helpful? Give feedback.
-
you can extract the function inside instrumentation as standalone function, and test it separately |
Beta Was this translation helpful? Give feedback.
you can extract the function inside instrumentation as standalone function, and test it separately