Add automatic query logger function with additional execution details.
This commit is contained in:
parent
7377e078cd
commit
4955bfc4b5
18 changed files with 266 additions and 59 deletions
|
|
@ -7,6 +7,7 @@ import (
|
|||
"github.com/go-jet/jet/v2/tests/internal/utils/repo"
|
||||
"math/rand"
|
||||
"os"
|
||||
"runtime"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
|
|
@ -59,11 +60,21 @@ var loggedSQL string
|
|||
var loggedSQLArgs []interface{}
|
||||
var loggedDebugSQL string
|
||||
|
||||
var queryInfo postgres.QueryInfo
|
||||
var callerFile string
|
||||
var callerLine int
|
||||
var callerFunction string
|
||||
|
||||
func init() {
|
||||
postgres.SetLogger(func(ctx context.Context, statement postgres.PrintableStatement) {
|
||||
loggedSQL, loggedSQLArgs = statement.Sql()
|
||||
loggedDebugSQL = statement.DebugSql()
|
||||
})
|
||||
|
||||
postgres.SetQueryLoggerFunc(func(ctx context.Context, info postgres.QueryInfo) {
|
||||
queryInfo = info
|
||||
callerFile, callerLine, callerFunction = info.Caller()
|
||||
})
|
||||
}
|
||||
|
||||
func requireLogged(t *testing.T, statement postgres.Statement) {
|
||||
|
|
@ -73,6 +84,21 @@ func requireLogged(t *testing.T, statement postgres.Statement) {
|
|||
require.Equal(t, loggedDebugSQL, statement.DebugSql())
|
||||
}
|
||||
|
||||
func requireQueryLogged(t *testing.T, statement postgres.Statement, rowsProcessed int64) {
|
||||
query, args := statement.Sql()
|
||||
queryLogged, argsLogged := queryInfo.Statement.Sql()
|
||||
|
||||
require.Equal(t, query, queryLogged)
|
||||
require.Equal(t, args, argsLogged)
|
||||
require.Equal(t, queryInfo.RowsProcessed, rowsProcessed)
|
||||
|
||||
pc, file, _, _ := runtime.Caller(1)
|
||||
funcDetails := runtime.FuncForPC(pc)
|
||||
require.Equal(t, file, callerFile)
|
||||
require.NotEmpty(t, callerLine)
|
||||
require.Equal(t, funcDetails.Name(), callerFunction)
|
||||
}
|
||||
|
||||
func skipForPgxDriver(t *testing.T) {
|
||||
if isPgxDriver() {
|
||||
t.SkipNow()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue