mirror of
https://github.com/jackc/pgx.git
synced 2025-05-21 06:50:27 +00:00
Now that Query/QueryRow always prepares statements when given SQL text there is no need to test raw SQL and prepared statements of the same query.
59 lines
1.2 KiB
Go
59 lines
1.2 KiB
Go
package pgx_test
|
|
|
|
import (
|
|
"github.com/jackc/pgx"
|
|
"testing"
|
|
)
|
|
|
|
func mustConnect(t testing.TB, config pgx.ConnConfig) *pgx.Conn {
|
|
conn, err := pgx.Connect(config)
|
|
if err != nil {
|
|
t.Fatalf("Unable to establish connection: %v", err)
|
|
}
|
|
return conn
|
|
}
|
|
|
|
func closeConn(t testing.TB, conn *pgx.Conn) {
|
|
err := conn.Close()
|
|
if err != nil {
|
|
t.Fatalf("conn.Close unexpectedly failed: %v", err)
|
|
}
|
|
}
|
|
|
|
func mustExec(t testing.TB, conn *pgx.Conn, sql string, arguments ...interface{}) (commandTag pgx.CommandTag) {
|
|
var err error
|
|
if commandTag, err = conn.Exec(sql, arguments...); err != nil {
|
|
t.Fatalf("Exec unexpectedly failed with %v: %v", sql, err)
|
|
}
|
|
return
|
|
}
|
|
|
|
// Do a simple query to ensure the connection is still usable
|
|
func ensureConnValid(t *testing.T, conn *pgx.Conn) {
|
|
var sum, rowCount int32
|
|
|
|
rows, err := conn.Query("select generate_series(1,$1)", 10)
|
|
if err != nil {
|
|
t.Fatalf("conn.Query failed: ", err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
|
var n int32
|
|
rows.Scan(&n)
|
|
sum += n
|
|
rowCount++
|
|
}
|
|
|
|
if rows.Err() != nil {
|
|
t.Fatalf("conn.Query failed: ", err)
|
|
}
|
|
|
|
if rowCount != 10 {
|
|
t.Error("Select called onDataRow wrong number of times")
|
|
}
|
|
if sum != 55 {
|
|
t.Error("Wrong values returned")
|
|
}
|
|
}
|