2
0

Removed TextEncoder and BinaryEncoder

Restructured / fixed a lot of tests along the way.
This commit is contained in:
Jack Christensen
2022-01-22 12:07:35 -06:00
parent eb0a4c9626
commit 3a90c6c879
50 changed files with 295 additions and 758 deletions
+2
View File
@@ -8,6 +8,8 @@ import (
type Float8 float64
func (Float8) SkipUnderlyingTypePlan() {}
// ScanFloat64 implements the Float64Scanner interface.
func (f *Float8) ScanFloat64(n pgtype.Float8) error {
if !n.Valid {
+22 -11
View File
@@ -7,17 +7,28 @@ import (
"github.com/jackc/pgx/v5/pgtype/zeronull"
)
func isExpectedEq(a interface{}) func(interface{}) bool {
return func(v interface{}) bool {
return a == v
}
}
func TestFloat8Transcode(t *testing.T) {
testutil.TestSuccessfulTranscode(t, "float8", []interface{}{
(zeronull.Float8)(1),
(zeronull.Float8)(0),
testutil.RunTranscodeTests(t, "float8", []testutil.TranscodeTestCase{
{
(zeronull.Float8)(1),
new(zeronull.Float8),
isExpectedEq((zeronull.Float8)(1)),
},
{
nil,
new(zeronull.Float8),
isExpectedEq((zeronull.Float8)(0)),
},
{
(zeronull.Float8)(0),
new(interface{}),
isExpectedEq(nil),
},
})
}
func TestFloat8ConvertsGoZeroToNull(t *testing.T) {
testutil.TestGoZeroToNullConversion(t, "float8", (zeronull.Float8)(0))
}
func TestFloat8ConvertsNullToGoZero(t *testing.T) {
testutil.TestNullToGoZeroConversion(t, "float8", (zeronull.Float8)(0))
}
+6
View File
@@ -11,6 +11,8 @@ import (
type Int2 int16
func (Int2) SkipUnderlyingTypePlan() {}
// ScanInt64 implements the Int64Scanner interface.
func (dst *Int2) ScanInt64(n int64, valid bool) error {
if !valid {
@@ -57,6 +59,8 @@ func (src Int2) Value() (driver.Value, error) {
type Int4 int32
func (Int4) SkipUnderlyingTypePlan() {}
// ScanInt64 implements the Int64Scanner interface.
func (dst *Int4) ScanInt64(n int64, valid bool) error {
if !valid {
@@ -103,6 +107,8 @@ func (src Int4) Value() (driver.Value, error) {
type Int8 int64
func (Int8) SkipUnderlyingTypePlan() {}
// ScanInt64 implements the Int64Scanner interface.
func (dst *Int8) ScanInt64(n int64, valid bool) error {
if !valid {
+2
View File
@@ -12,6 +12,8 @@ import (
<% pg_bit_size = pg_byte_size * 8 %>
type Int<%= pg_byte_size %> int<%= pg_bit_size %>
func (Int<%= pg_byte_size %>) SkipUnderlyingTypePlan() {}
// ScanInt64 implements the Int64Scanner interface.
func (dst *Int<%= pg_byte_size %>) ScanInt64(n int64, valid bool) error {
if !valid {
+48 -33
View File
@@ -9,46 +9,61 @@ import (
)
func TestInt2Transcode(t *testing.T) {
testutil.TestSuccessfulTranscode(t, "int2", []interface{}{
(zeronull.Int2)(1),
(zeronull.Int2)(0),
testutil.RunTranscodeTests(t, "int2", []testutil.TranscodeTestCase{
{
(zeronull.Int2)(1),
new(zeronull.Int2),
isExpectedEq((zeronull.Int2)(1)),
},
{
nil,
new(zeronull.Int2),
isExpectedEq((zeronull.Int2)(0)),
},
{
(zeronull.Int2)(0),
new(interface{}),
isExpectedEq(nil),
},
})
}
func TestInt2ConvertsGoZeroToNull(t *testing.T) {
testutil.TestGoZeroToNullConversion(t, "int2", (zeronull.Int2)(0))
}
func TestInt2ConvertsNullToGoZero(t *testing.T) {
testutil.TestNullToGoZeroConversion(t, "int2", (zeronull.Int2)(0))
}
func TestInt4Transcode(t *testing.T) {
testutil.TestSuccessfulTranscode(t, "int4", []interface{}{
(zeronull.Int4)(1),
(zeronull.Int4)(0),
testutil.RunTranscodeTests(t, "int4", []testutil.TranscodeTestCase{
{
(zeronull.Int4)(1),
new(zeronull.Int4),
isExpectedEq((zeronull.Int4)(1)),
},
{
nil,
new(zeronull.Int4),
isExpectedEq((zeronull.Int4)(0)),
},
{
(zeronull.Int4)(0),
new(interface{}),
isExpectedEq(nil),
},
})
}
func TestInt4ConvertsGoZeroToNull(t *testing.T) {
testutil.TestGoZeroToNullConversion(t, "int4", (zeronull.Int4)(0))
}
func TestInt4ConvertsNullToGoZero(t *testing.T) {
testutil.TestNullToGoZeroConversion(t, "int4", (zeronull.Int4)(0))
}
func TestInt8Transcode(t *testing.T) {
testutil.TestSuccessfulTranscode(t, "int8", []interface{}{
(zeronull.Int8)(1),
(zeronull.Int8)(0),
testutil.RunTranscodeTests(t, "int8", []testutil.TranscodeTestCase{
{
(zeronull.Int8)(1),
new(zeronull.Int8),
isExpectedEq((zeronull.Int8)(1)),
},
{
nil,
new(zeronull.Int8),
isExpectedEq((zeronull.Int8)(0)),
},
{
(zeronull.Int8)(0),
new(interface{}),
isExpectedEq(nil),
},
})
}
func TestInt8ConvertsGoZeroToNull(t *testing.T) {
testutil.TestGoZeroToNullConversion(t, "int8", (zeronull.Int8)(0))
}
func TestInt8ConvertsNullToGoZero(t *testing.T) {
testutil.TestNullToGoZeroConversion(t, "int8", (zeronull.Int8)(0))
}
+16 -11
View File
@@ -10,17 +10,22 @@ 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.TestSuccessfulTranscode(t, "int<%= pg_byte_size %>", []interface{}{
(zeronull.Int<%= pg_byte_size %>)(1),
(zeronull.Int<%= pg_byte_size %>)(0),
testutil.RunTranscodeTests(t, "int<%= pg_byte_size %>", []testutil.TranscodeTestCase{
{
(zeronull.Int<%= pg_byte_size %>)(1),
new(zeronull.Int<%= pg_byte_size %>),
isExpectedEq((zeronull.Int<%= pg_byte_size %>)(1)),
},
{
nil,
new(zeronull.Int<%= pg_byte_size %>),
isExpectedEq((zeronull.Int<%= pg_byte_size %>)(0)),
},
{
(zeronull.Int<%= pg_byte_size %>)(0),
new(interface{}),
isExpectedEq(nil),
},
})
}
func TestInt<%= pg_byte_size %>ConvertsGoZeroToNull(t *testing.T) {
testutil.TestGoZeroToNullConversion(t, "int<%= pg_byte_size %>", (zeronull.Int<%= pg_byte_size %>)(0))
}
func TestInt<%= pg_byte_size %>ConvertsNullToGoZero(t *testing.T) {
testutil.TestNullToGoZeroConversion(t, "int<%= pg_byte_size %>", (zeronull.Int<%= pg_byte_size %>)(0))
}
<% end %>
+2
View File
@@ -8,6 +8,8 @@ import (
type Text string
func (Text) SkipUnderlyingTypePlan() {}
// ScanText implements the TextScanner interface.
func (dst *Text) ScanText(v pgtype.Text) error {
if !v.Valid {
+16 -11
View File
@@ -8,16 +8,21 @@ import (
)
func TestTextTranscode(t *testing.T) {
testutil.TestSuccessfulTranscode(t, "text", []interface{}{
(zeronull.Text)("foo"),
(zeronull.Text)(""),
testutil.RunTranscodeTests(t, "text", []testutil.TranscodeTestCase{
{
(zeronull.Text)("foo"),
new(zeronull.Text),
isExpectedEq((zeronull.Text)("foo")),
},
{
nil,
new(zeronull.Text),
isExpectedEq((zeronull.Text)("")),
},
{
(zeronull.Text)(""),
new(interface{}),
isExpectedEq(nil),
},
})
}
func TestTextConvertsGoZeroToNull(t *testing.T) {
testutil.TestGoZeroToNullConversion(t, "text", (zeronull.Text)(""))
}
func TestTextConvertsNullToGoZero(t *testing.T) {
testutil.TestNullToGoZeroConversion(t, "text", (zeronull.Text)(""))
}
+2
View File
@@ -10,6 +10,8 @@ import (
type Timestamp time.Time
func (Timestamp) SkipUnderlyingTypePlan() {}
func (ts *Timestamp) ScanTimestamp(v pgtype.Timestamp) error {
if !v.Valid {
*ts = Timestamp{}
+25 -16
View File
@@ -8,22 +8,31 @@ import (
"github.com/jackc/pgx/v5/pgtype/zeronull"
)
func TestTimestampTranscode(t *testing.T) {
testutil.TestSuccessfulTranscodeEqFunc(t, "timestamp", []interface{}{
(zeronull.Timestamp)(time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)),
(zeronull.Timestamp)(time.Time{}),
}, func(a, b interface{}) bool {
at := a.(zeronull.Timestamp)
bt := b.(zeronull.Timestamp)
func isExpectedEqTimestamp(a interface{}) func(interface{}) bool {
return func(v interface{}) bool {
at := time.Time(a.(zeronull.Timestamp))
vt := time.Time(v.(zeronull.Timestamp))
return time.Time(at).Equal(time.Time(bt))
return at.Equal(vt)
}
}
func TestTimestampTranscode(t *testing.T) {
testutil.RunTranscodeTests(t, "timestamp", []testutil.TranscodeTestCase{
{
(zeronull.Timestamp)(time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)),
new(zeronull.Timestamp),
isExpectedEqTimestamp((zeronull.Timestamp)(time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC))),
},
{
nil,
new(zeronull.Timestamp),
isExpectedEqTimestamp((zeronull.Timestamp)(time.Time{})),
},
{
(zeronull.Timestamp)(time.Time{}),
new(interface{}),
isExpectedEq(nil),
},
})
}
func TestTimestampConvertsGoZeroToNull(t *testing.T) {
testutil.TestGoZeroToNullConversion(t, "timestamp", (zeronull.Timestamp)(time.Time{}))
}
func TestTimestampConvertsNullToGoZero(t *testing.T) {
testutil.TestNullToGoZeroConversion(t, "timestamp", (zeronull.Timestamp)(time.Time{}))
}
+2
View File
@@ -10,6 +10,8 @@ import (
type Timestamptz time.Time
func (Timestamptz) SkipUnderlyingTypePlan() {}
func (ts *Timestamptz) ScanTimestamptz(v pgtype.Timestamptz) error {
if !v.Valid {
*ts = Timestamptz{}
+25 -16
View File
@@ -8,22 +8,31 @@ import (
"github.com/jackc/pgx/v5/pgtype/zeronull"
)
func TestTimestamptzTranscode(t *testing.T) {
testutil.TestSuccessfulTranscodeEqFunc(t, "timestamptz", []interface{}{
(zeronull.Timestamptz)(time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)),
(zeronull.Timestamptz)(time.Time{}),
}, func(a, b interface{}) bool {
at := a.(zeronull.Timestamptz)
bt := b.(zeronull.Timestamptz)
func isExpectedEqTimestamptz(a interface{}) func(interface{}) bool {
return func(v interface{}) bool {
at := time.Time(a.(zeronull.Timestamptz))
vt := time.Time(v.(zeronull.Timestamptz))
return time.Time(at).Equal(time.Time(bt))
return at.Equal(vt)
}
}
func TestTimestamptzTranscode(t *testing.T) {
testutil.RunTranscodeTests(t, "timestamptz", []testutil.TranscodeTestCase{
{
(zeronull.Timestamptz)(time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)),
new(zeronull.Timestamptz),
isExpectedEqTimestamptz((zeronull.Timestamptz)(time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC))),
},
{
nil,
new(zeronull.Timestamptz),
isExpectedEqTimestamptz((zeronull.Timestamptz)(time.Time{})),
},
{
(zeronull.Timestamptz)(time.Time{}),
new(interface{}),
isExpectedEq(nil),
},
})
}
func TestTimestamptzConvertsGoZeroToNull(t *testing.T) {
testutil.TestGoZeroToNullConversion(t, "timestamptz", (zeronull.Timestamptz)(time.Time{}))
}
func TestTimestamptzConvertsNullToGoZero(t *testing.T) {
testutil.TestNullToGoZeroConversion(t, "timestamptz", (zeronull.Timestamptz)(time.Time{}))
}
+2
View File
@@ -8,6 +8,8 @@ import (
type UUID [16]byte
func (UUID) SkipUnderlyingTypePlan() {}
// ScanUUID implements the UUIDScanner interface.
func (u *UUID) ScanUUID(v pgtype.UUID) error {
if !v.Valid {
+16 -11
View File
@@ -8,16 +8,21 @@ import (
)
func TestUUIDTranscode(t *testing.T) {
testutil.TestSuccessfulTranscode(t, "uuid", []interface{}{
(*zeronull.UUID)(&[16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}),
(*zeronull.UUID)(&[16]byte{}),
testutil.RunTranscodeTests(t, "uuid", []testutil.TranscodeTestCase{
{
(zeronull.UUID)([16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}),
new(zeronull.UUID),
isExpectedEq((zeronull.UUID)([16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15})),
},
{
nil,
new(zeronull.UUID),
isExpectedEq((zeronull.UUID)([16]byte{})),
},
{
(zeronull.UUID)([16]byte{}),
new(interface{}),
isExpectedEq(nil),
},
})
}
func TestUUIDConvertsGoZeroToNull(t *testing.T) {
testutil.TestGoZeroToNullConversion(t, "uuid", (*zeronull.UUID)(&[16]byte{}))
}
func TestUUIDConvertsNullToGoZero(t *testing.T) {
testutil.TestNullToGoZeroConversion(t, "uuid", (*zeronull.UUID)(&[16]byte{}))
}