jet/tests/postgres/main_test.go

137 lines
2.8 KiB
Go
Raw Normal View History

2019-07-30 11:45:10 +02:00
package postgres
2019-04-07 09:58:12 +02:00
import (
"context"
2019-04-07 09:58:12 +02:00
"database/sql"
2021-05-14 14:13:42 +02:00
"fmt"
"github.com/go-jet/jet/v2/tests/internal/utils/repo"
"github.com/jackc/pgx/v4/stdlib"
2019-04-07 09:58:12 +02:00
"os"
"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
)
var db *sql.DB
var testRoot string
2019-04-14 17:55:10 +02:00
2022-05-05 13:01:42 +02:00
var source string
const CockroachDB = "COCKROACH_DB"
func init() {
source = os.Getenv("PG_SOURCE")
}
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()
setTestRoot()
for _, driverName := range []string{"pgx", "postgres"} {
fmt.Printf("\nRunning postgres tests for '%s' driver\n", driverName)
2021-05-14 14:13:42 +02:00
func() {
2022-05-05 13:01:42 +02:00
connectionString := dbconfig.PostgresConnectString
if sourceIsCockroachDB() {
connectionString = dbconfig.CockroachConnectString
}
2021-05-14 14:13:42 +02:00
var err error
2022-05-05 13:01:42 +02:00
db, err = sql.Open(driverName, connectionString)
2021-05-14 14:13:42 +02:00
if err != nil {
fmt.Println(err.Error())
panic("Failed to connect to test db")
}
defer db.Close()
2019-04-07 09:58:12 +02:00
2021-05-14 14:13:42 +02:00
ret := m.Run()
2019-04-07 09:58:12 +02:00
2021-05-14 14:13:42 +02:00
if ret != 0 {
os.Exit(ret)
}
}()
}
2019-04-07 09:58:12 +02:00
}
func setTestRoot() {
testRoot = repo.GetTestsDirPath()
}
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.SetQueryLogger(func(ctx context.Context, info postgres.QueryInfo) {
queryInfo = info
callerFile, callerLine, callerFunction = info.Caller()
})
}
func requireLogged(t *testing.T, statement postgres.Statement) {
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
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)
}
2021-05-14 14:13:42 +02:00
func skipForPgxDriver(t *testing.T) {
if isPgxDriver() {
t.SkipNow()
}
}
func isPgxDriver() bool {
2021-05-14 14:13:42 +02:00
switch db.Driver().(type) {
case *stdlib.Driver:
return true
2021-05-14 14:13:42 +02:00
}
return false
2021-05-14 14:13:42 +02:00
}