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
|
|
@ -35,6 +35,7 @@ ORDER BY "Album"."AlbumId" ASC;
|
|||
testutils.AssertDeepEqual(t, dest[1], album2)
|
||||
testutils.AssertDeepEqual(t, dest[len(dest)-1], album347)
|
||||
requireLogged(t, stmt)
|
||||
requireQueryLogged(t, stmt, 347)
|
||||
}
|
||||
|
||||
func TestJoinEverything(t *testing.T) {
|
||||
|
|
@ -191,12 +192,13 @@ FROM chinook."Artist"
|
|||
ORDER BY "Artist"."ArtistId", "Album"."AlbumId", "Track"."TrackId", "Genre"."GenreId", "MediaType"."MediaTypeId", "Playlist"."PlaylistId", "Invoice"."InvoiceId", "Customer"."CustomerId";
|
||||
`)
|
||||
|
||||
err := stmt.Query(db, &dest)
|
||||
err := stmt.QueryContext(context.Background(), db, &dest)
|
||||
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, len(dest), 275)
|
||||
testutils.AssertJSONFile(t, dest, "./testdata/results/postgres/joined_everything.json")
|
||||
requireLogged(t, stmt)
|
||||
requireQueryLogged(t, stmt, 9423)
|
||||
}
|
||||
|
||||
// default column aliases from sub-CTEs are bubbled up to the main query,
|
||||
|
|
|
|||
|
|
@ -25,7 +25,14 @@ WHERE link.name IN ('Gmail', 'Outlook');
|
|||
WHERE(Link.Name.IN(String("Gmail"), String("Outlook")))
|
||||
|
||||
testutils.AssertDebugStatementSql(t, deleteStmt, expectedSQL, "Gmail", "Outlook")
|
||||
AssertExec(t, deleteStmt, 2)
|
||||
|
||||
res, err := deleteStmt.ExecContext(context.Background(), db)
|
||||
|
||||
require.NoError(t, err)
|
||||
rows, err := res.RowsAffected()
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, rows, int64(2))
|
||||
requireQueryLogged(t, deleteStmt, int64(2))
|
||||
}
|
||||
|
||||
func TestDeleteWithWhereAndReturning(t *testing.T) {
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -62,7 +62,8 @@ func TestScanToValidDestination(t *testing.T) {
|
|||
t.Run("global query function scan", func(t *testing.T) {
|
||||
queryStr, args := oneInventoryQuery.Sql()
|
||||
dest := []struct{}{}
|
||||
err := qrm.Query(nil, db, queryStr, args, &dest)
|
||||
rowProcessed, err := qrm.Query(nil, db, queryStr, args, &dest)
|
||||
require.Equal(t, rowProcessed, int64(1))
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
|
|
@ -782,6 +783,7 @@ func TestRowsScan(t *testing.T) {
|
|||
require.NoError(t, err)
|
||||
|
||||
requireLogged(t, stmt)
|
||||
requireQueryLogged(t, stmt, 0)
|
||||
}
|
||||
|
||||
func TestScanNumericToFloat(t *testing.T) {
|
||||
|
|
|
|||
|
|
@ -270,7 +270,9 @@ WHERE link.id = 201::integer;
|
|||
`
|
||||
testutils.AssertDebugStatementSql(t, stmt, expectedSQL, int32(201), "http://www.duckduckgo.com", "DuckDuckGo", nil, int32(201))
|
||||
|
||||
AssertExec(t, stmt, 1)
|
||||
_, err := stmt.Exec(db)
|
||||
require.NoError(t, err)
|
||||
requireQueryLogged(t, stmt, 1)
|
||||
}
|
||||
|
||||
func TestUpdateWithModelDataAndPredefinedColumnList(t *testing.T) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue