pgtype uses pgxtest
Added ValueRoundTripTest to pgxtest Removed pgtype/testutil pgtype tests now run with all (applicable) query modes. This gives better coverage than before and revealed several bugs which are also fixed in this commit.
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
package zeronull_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype/testutil"
|
||||
"github.com/jackc/pgx/v5/pgtype/zeronull"
|
||||
"github.com/jackc/pgx/v5/pgxtest"
|
||||
)
|
||||
|
||||
func isExpectedEq(a interface{}) func(interface{}) bool {
|
||||
@@ -14,7 +15,7 @@ func isExpectedEq(a interface{}) func(interface{}) bool {
|
||||
}
|
||||
|
||||
func TestFloat8Transcode(t *testing.T) {
|
||||
testutil.RunTranscodeTests(t, "float8", []testutil.TranscodeTestCase{
|
||||
pgxtest.RunValueRoundTripTests(context.Background(), t, defaultConnTestRunner, nil, "float8", []pgxtest.ValueRoundTripTest{
|
||||
{
|
||||
(zeronull.Float8)(1),
|
||||
new(zeronull.Float8),
|
||||
|
||||
@@ -2,14 +2,15 @@
|
||||
package zeronull_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype/testutil"
|
||||
"github.com/jackc/pgx/v5/pgtype/zeronull"
|
||||
"github.com/jackc/pgx/v5/pgxtest"
|
||||
)
|
||||
|
||||
func TestInt2Transcode(t *testing.T) {
|
||||
testutil.RunTranscodeTests(t, "int2", []testutil.TranscodeTestCase{
|
||||
pgxtest.RunValueRoundTripTests(context.Background(), t, defaultConnTestRunner, nil, "int2", []pgxtest.ValueRoundTripTest{
|
||||
{
|
||||
(zeronull.Int2)(1),
|
||||
new(zeronull.Int2),
|
||||
@@ -29,7 +30,7 @@ func TestInt2Transcode(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestInt4Transcode(t *testing.T) {
|
||||
testutil.RunTranscodeTests(t, "int4", []testutil.TranscodeTestCase{
|
||||
pgxtest.RunValueRoundTripTests(context.Background(), t, defaultConnTestRunner, nil, "int4", []pgxtest.ValueRoundTripTest{
|
||||
{
|
||||
(zeronull.Int4)(1),
|
||||
new(zeronull.Int4),
|
||||
@@ -49,7 +50,7 @@ func TestInt4Transcode(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestInt8Transcode(t *testing.T) {
|
||||
testutil.RunTranscodeTests(t, "int8", []testutil.TranscodeTestCase{
|
||||
pgxtest.RunValueRoundTripTests(context.Background(), t, defaultConnTestRunner, nil, "int8", []pgxtest.ValueRoundTripTest{
|
||||
{
|
||||
(zeronull.Int8)(1),
|
||||
new(zeronull.Int8),
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
<% [2, 4, 8].each do |pg_byte_size| %>
|
||||
<% pg_bit_size = pg_byte_size * 8 %>
|
||||
func TestInt<%= pg_byte_size %>Transcode(t *testing.T) {
|
||||
testutil.RunTranscodeTests(t, "int<%= pg_byte_size %>", []testutil.TranscodeTestCase{
|
||||
pgxtest.RunValueRoundTripTests(context.Background(), t, defaultConnTestRunner, nil, "int<%= pg_byte_size %>", []pgxtest.ValueRoundTripTest{
|
||||
{
|
||||
(zeronull.Int<%= pg_byte_size %>)(1),
|
||||
new(zeronull.Int<%= pg_byte_size %>),
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
package zeronull_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype/testutil"
|
||||
"github.com/jackc/pgx/v5/pgtype/zeronull"
|
||||
"github.com/jackc/pgx/v5/pgxtest"
|
||||
)
|
||||
|
||||
func TestTextTranscode(t *testing.T) {
|
||||
testutil.RunTranscodeTests(t, "text", []testutil.TranscodeTestCase{
|
||||
pgxtest.RunValueRoundTripTests(context.Background(), t, defaultConnTestRunner, nil, "text", []pgxtest.ValueRoundTripTest{
|
||||
{
|
||||
(zeronull.Text)("foo"),
|
||||
new(zeronull.Text),
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
package zeronull_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype/testutil"
|
||||
"github.com/jackc/pgx/v5/pgtype/zeronull"
|
||||
"github.com/jackc/pgx/v5/pgxtest"
|
||||
)
|
||||
|
||||
func isExpectedEqTimestamp(a interface{}) func(interface{}) bool {
|
||||
@@ -18,7 +19,7 @@ func isExpectedEqTimestamp(a interface{}) func(interface{}) bool {
|
||||
}
|
||||
|
||||
func TestTimestampTranscode(t *testing.T) {
|
||||
testutil.RunTranscodeTests(t, "timestamp", []testutil.TranscodeTestCase{
|
||||
pgxtest.RunValueRoundTripTests(context.Background(), t, defaultConnTestRunner, nil, "timestamp", []pgxtest.ValueRoundTripTest{
|
||||
{
|
||||
(zeronull.Timestamp)(time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)),
|
||||
new(zeronull.Timestamp),
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
package zeronull_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype/testutil"
|
||||
"github.com/jackc/pgx/v5/pgtype/zeronull"
|
||||
"github.com/jackc/pgx/v5/pgxtest"
|
||||
)
|
||||
|
||||
func isExpectedEqTimestamptz(a interface{}) func(interface{}) bool {
|
||||
@@ -18,7 +19,7 @@ func isExpectedEqTimestamptz(a interface{}) func(interface{}) bool {
|
||||
}
|
||||
|
||||
func TestTimestamptzTranscode(t *testing.T) {
|
||||
testutil.RunTranscodeTests(t, "timestamptz", []testutil.TranscodeTestCase{
|
||||
pgxtest.RunValueRoundTripTests(context.Background(), t, defaultConnTestRunner, nil, "timestamptz", []pgxtest.ValueRoundTripTest{
|
||||
{
|
||||
(zeronull.Timestamptz)(time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)),
|
||||
new(zeronull.Timestamptz),
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
package zeronull_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype/testutil"
|
||||
"github.com/jackc/pgx/v5/pgtype/zeronull"
|
||||
"github.com/jackc/pgx/v5/pgxtest"
|
||||
)
|
||||
|
||||
func TestUUIDTranscode(t *testing.T) {
|
||||
testutil.RunTranscodeTests(t, "uuid", []testutil.TranscodeTestCase{
|
||||
pgxtest.RunValueRoundTripTests(context.Background(), t, defaultConnTestRunner, nil, "uuid", []pgxtest.ValueRoundTripTest{
|
||||
{
|
||||
(zeronull.UUID)([16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}),
|
||||
new(zeronull.UUID),
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package zeronull
|
||||
|
||||
import (
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
// Register registers the zeronull types so they can be used in query exec modes that do not know the server OIDs.
|
||||
func Register(m *pgtype.Map) {
|
||||
m.RegisterDefaultPgType(Float8(0), "float8")
|
||||
m.RegisterDefaultPgType(Int2(0), "int2")
|
||||
m.RegisterDefaultPgType(Int4(0), "int4")
|
||||
m.RegisterDefaultPgType(Int8(0), "int8")
|
||||
m.RegisterDefaultPgType(Text(""), "text")
|
||||
m.RegisterDefaultPgType(Timestamp{}, "timestamp")
|
||||
m.RegisterDefaultPgType(Timestamptz{}, "timestamptz")
|
||||
m.RegisterDefaultPgType(UUID{}, "uuid")
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package zeronull_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgtype/zeronull"
|
||||
"github.com/jackc/pgx/v5/pgxtest"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
var defaultConnTestRunner pgxtest.ConnTestRunner
|
||||
|
||||
func init() {
|
||||
defaultConnTestRunner = pgxtest.DefaultConnTestRunner()
|
||||
defaultConnTestRunner.CreateConfig = func(ctx context.Context, t testing.TB) *pgx.ConnConfig {
|
||||
config, err := pgx.ParseConfig(os.Getenv("PGX_TEST_DATABASE"))
|
||||
require.NoError(t, err)
|
||||
return config
|
||||
}
|
||||
defaultConnTestRunner.AfterConnect = func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
|
||||
zeronull.Register(conn.TypeMap())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user