2019-07-30 11:45:10 +02:00
|
|
|
package postgres
|
2019-04-07 09:58:12 +02:00
|
|
|
|
|
|
|
|
import (
|
2020-05-10 11:41:07 +02:00
|
|
|
"context"
|
2019-04-07 09:58:12 +02:00
|
|
|
"database/sql"
|
2021-05-14 14:13:42 +02:00
|
|
|
"fmt"
|
2024-10-19 14:06:12 +02:00
|
|
|
"github.com/go-jet/jet/v2/stmtcache"
|
2021-10-21 13:35:37 +02:00
|
|
|
"github.com/go-jet/jet/v2/tests/internal/utils/repo"
|
2024-10-08 10:17:25 -04:00
|
|
|
"github.com/jackc/pgx/v4/stdlib"
|
2019-04-07 09:58:12 +02:00
|
|
|
"os"
|
2022-01-12 19:03:50 +01:00
|
|
|
"runtime"
|
2019-04-07 09:58:12 +02:00
|
|
|
"testing"
|
2021-05-14 14:13:42 +02:00
|
|
|
|
|
|
|
|
"github.com/go-jet/jet/v2/postgres"
|
|
|
|
|
"github.com/go-jet/jet/v2/tests/dbconfig"
|
|
|
|
|
_ "github.com/lib/pq"
|
|
|
|
|
"github.com/pkg/profile"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
|
|
_ "github.com/jackc/pgx/v4/stdlib"
|
2019-04-07 09:58:12 +02:00
|
|
|
)
|
|
|
|
|
|
2024-10-19 14:06:12 +02:00
|
|
|
var db *stmtcache.DB
|
2020-02-09 18:37:48 +01:00
|
|
|
var testRoot string
|
2019-04-14 17:55:10 +02:00
|
|
|
|
2022-05-05 13:01:42 +02:00
|
|
|
var source string
|
2024-10-19 14:06:12 +02:00
|
|
|
var withStatementCaching bool
|
2022-05-05 13:01:42 +02:00
|
|
|
|
|
|
|
|
const CockroachDB = "COCKROACH_DB"
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
source = os.Getenv("PG_SOURCE")
|
2024-10-19 14:06:12 +02:00
|
|
|
withStatementCaching = os.Getenv("JET_TESTS_WITH_STMT_CACHE") == "true"
|
2025-02-28 18:23:15 +01:00
|
|
|
testRoot = repo.GetTestsDirPath()
|
2022-05-05 13:01:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func sourceIsCockroachDB() bool {
|
|
|
|
|
return source == CockroachDB
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func skipForCockroachDB(t *testing.T) {
|
|
|
|
|
if sourceIsCockroachDB() {
|
|
|
|
|
t.SkipNow()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-07 09:58:12 +02:00
|
|
|
func TestMain(m *testing.M) {
|
2019-05-20 17:37:55 +02:00
|
|
|
defer profile.Start().Stop()
|
|
|
|
|
|
2024-10-19 14:06:12 +02:00
|
|
|
for _, driverName := range []string{"postgres", "pgx"} {
|
2021-07-27 17:39:21 +02:00
|
|
|
|
2024-10-19 14:06:12 +02:00
|
|
|
fmt.Printf("\nRunning postgres tests for driver: %s, caching enabled: %t \n", driverName, withStatementCaching)
|
2022-05-05 13:01:42 +02:00
|
|
|
|
2024-10-19 14:06:12 +02:00
|
|
|
func() {
|
|
|
|
|
sqlDB, err := sql.Open(driverName, getConnectionString())
|
2021-05-14 14:13:42 +02:00
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
|
panic("Failed to connect to test db")
|
|
|
|
|
}
|
2024-10-19 14:06:12 +02:00
|
|
|
db = stmtcache.New(sqlDB).SetCaching(withStatementCaching)
|
|
|
|
|
defer func(db *stmtcache.DB) {
|
|
|
|
|
err := db.Close()
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Printf("ERROR: Failed to close db connection, %v", err)
|
|
|
|
|
}
|
|
|
|
|
}(db)
|
2019-04-07 09:58:12 +02:00
|
|
|
|
2024-10-19 14:06:12 +02:00
|
|
|
for i := 0; i < runCount(withStatementCaching); i++ {
|
2024-03-07 18:01:31 +01:00
|
|
|
ret := m.Run()
|
|
|
|
|
if ret != 0 {
|
2024-10-19 14:06:12 +02:00
|
|
|
fmt.Printf("\nFAIL: Running postgres tests failed for driver: %s, caching enabled: %t \n", driverName, withStatementCaching)
|
2024-03-07 18:01:31 +01:00
|
|
|
os.Exit(ret)
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-19 14:06:12 +02:00
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2024-03-07 18:01:31 +01:00
|
|
|
|
2024-10-19 14:06:12 +02:00
|
|
|
func runCount(stmtCaching bool) int {
|
|
|
|
|
if stmtCaching {
|
|
|
|
|
return 2
|
|
|
|
|
}
|
2019-04-07 09:58:12 +02:00
|
|
|
|
2024-10-19 14:06:12 +02:00
|
|
|
return 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getConnectionString() string {
|
|
|
|
|
if sourceIsCockroachDB() {
|
|
|
|
|
return dbconfig.CockroachConnectString
|
2021-05-14 14:13:42 +02:00
|
|
|
}
|
2024-10-19 14:06:12 +02:00
|
|
|
|
|
|
|
|
return dbconfig.PostgresConnectString
|
2019-04-07 09:58:12 +02:00
|
|
|
}
|
2020-02-09 18:37:48 +01:00
|
|
|
|
2020-05-10 11:41:07 +02:00
|
|
|
var loggedSQL string
|
|
|
|
|
var loggedSQLArgs []interface{}
|
|
|
|
|
var loggedDebugSQL string
|
|
|
|
|
|
2022-01-12 19:03:50 +01:00
|
|
|
var queryInfo postgres.QueryInfo
|
|
|
|
|
var callerFile string
|
|
|
|
|
var callerLine int
|
|
|
|
|
var callerFunction string
|
|
|
|
|
|
2020-05-10 11:41:07 +02:00
|
|
|
func init() {
|
2020-06-01 18:22:24 +02:00
|
|
|
postgres.SetLogger(func(ctx context.Context, statement postgres.PrintableStatement) {
|
2020-05-10 11:41:07 +02:00
|
|
|
loggedSQL, loggedSQLArgs = statement.Sql()
|
|
|
|
|
loggedDebugSQL = statement.DebugSql()
|
|
|
|
|
})
|
2022-01-12 19:03:50 +01:00
|
|
|
|
2022-01-20 16:51:32 +01:00
|
|
|
postgres.SetQueryLogger(func(ctx context.Context, info postgres.QueryInfo) {
|
2022-01-12 19:03:50 +01:00
|
|
|
queryInfo = info
|
|
|
|
|
callerFile, callerLine, callerFunction = info.Caller()
|
|
|
|
|
})
|
2020-05-10 11:41:07 +02:00
|
|
|
}
|
|
|
|
|
|
2025-02-21 19:55:01 +01:00
|
|
|
func requireLogged(t require.TestingT, statement postgres.Statement) {
|
|
|
|
|
if _, ok := t.(*testing.B); ok {
|
|
|
|
|
return // skip assert for benchmarks
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-10 11:41:07 +02:00
|
|
|
query, args := statement.Sql()
|
|
|
|
|
require.Equal(t, loggedSQL, query)
|
|
|
|
|
require.Equal(t, loggedSQLArgs, args)
|
|
|
|
|
require.Equal(t, loggedDebugSQL, statement.DebugSql())
|
|
|
|
|
}
|
2021-05-14 14:13:42 +02:00
|
|
|
|
2025-02-21 19:55:01 +01:00
|
|
|
func requireQueryLogged(t require.TestingT, statement postgres.Statement, rowsProcessed int64) {
|
|
|
|
|
if _, ok := t.(*testing.B); ok {
|
|
|
|
|
return // skip assert for benchmarks
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-12 19:03:50 +01:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-14 14:13:42 +02:00
|
|
|
func skipForPgxDriver(t *testing.T) {
|
2021-10-15 17:43:10 +02:00
|
|
|
if isPgxDriver() {
|
|
|
|
|
t.SkipNow()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func isPgxDriver() bool {
|
2021-05-14 14:13:42 +02:00
|
|
|
switch db.Driver().(type) {
|
|
|
|
|
case *stdlib.Driver:
|
2021-10-15 17:43:10 +02:00
|
|
|
return true
|
2021-05-14 14:13:42 +02:00
|
|
|
}
|
2021-10-15 17:43:10 +02:00
|
|
|
|
|
|
|
|
return false
|
2021-05-14 14:13:42 +02:00
|
|
|
}
|