Removed TextEncoder and BinaryEncoder
Restructured / fixed a lot of tests along the way.
This commit is contained in:
@@ -2,17 +2,13 @@ package pgtype_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
"github.com/jackc/pgx/v5/pgtype/testutil"
|
||||
_ "github.com/jackc/pgx/v5/stdlib"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
@@ -292,54 +288,8 @@ func BenchmarkScanPlanScanInt4IntoGoInt32(b *testing.B) {
|
||||
}
|
||||
}
|
||||
|
||||
type PgxTranscodeTestCase struct {
|
||||
src interface{}
|
||||
dst interface{}
|
||||
test func(interface{}) bool
|
||||
}
|
||||
|
||||
func isExpectedEq(a interface{}) func(interface{}) bool {
|
||||
return func(v interface{}) bool {
|
||||
return a == v
|
||||
}
|
||||
}
|
||||
|
||||
func testPgxCodec(t testing.TB, pgTypeName string, tests []PgxTranscodeTestCase) {
|
||||
conn := testutil.MustConnectPgx(t)
|
||||
defer testutil.MustCloseContext(t, conn)
|
||||
|
||||
formats := []struct {
|
||||
name string
|
||||
code int16
|
||||
}{
|
||||
{name: "TextFormat", code: pgx.TextFormatCode},
|
||||
{name: "BinaryFormat", code: pgx.BinaryFormatCode},
|
||||
}
|
||||
|
||||
for _, format := range formats {
|
||||
testPgxCodecFormat(t, pgTypeName, tests, conn, format.name, format.code)
|
||||
}
|
||||
}
|
||||
|
||||
func testPgxCodecFormat(t testing.TB, pgTypeName string, tests []PgxTranscodeTestCase, conn *pgx.Conn, formatName string, formatCode int16) {
|
||||
_, err := conn.Prepare(context.Background(), "test", fmt.Sprintf("select $1::%s", pgTypeName))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for i, tt := range tests {
|
||||
err := conn.QueryRow(context.Background(), "test", pgx.QueryResultFormats{formatCode}, tt.src).Scan(tt.dst)
|
||||
if err != nil {
|
||||
t.Errorf("%s %d: %v", formatName, i, err)
|
||||
}
|
||||
|
||||
dst := reflect.ValueOf(tt.dst)
|
||||
if dst.Kind() == reflect.Ptr {
|
||||
dst = dst.Elem()
|
||||
}
|
||||
|
||||
if !tt.test(dst.Interface()) {
|
||||
t.Errorf("%s %d: unexpected result for %v: %v", formatName, i, tt.src, dst.Interface())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user