Convert timestamp and timestamptz to Codec
This commit is contained in:
+22
-164
@@ -2,7 +2,6 @@ package pgtype_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -11,41 +10,35 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestTimestampTranscode(t *testing.T) {
|
||||
testutil.TestSuccessfulTranscodeEqFunc(t, "timestamp", []interface{}{
|
||||
&pgtype.Timestamp{Time: time.Date(1800, 1, 1, 0, 0, 0, 0, time.UTC), Valid: true},
|
||||
&pgtype.Timestamp{Time: time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC), Valid: true},
|
||||
&pgtype.Timestamp{Time: time.Date(1905, 1, 1, 0, 0, 0, 0, time.UTC), Valid: true},
|
||||
&pgtype.Timestamp{Time: time.Date(1940, 1, 1, 0, 0, 0, 0, time.UTC), Valid: true},
|
||||
&pgtype.Timestamp{Time: time.Date(1960, 1, 1, 0, 0, 0, 0, time.UTC), Valid: true},
|
||||
&pgtype.Timestamp{Time: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC), Valid: true},
|
||||
&pgtype.Timestamp{Time: time.Date(1999, 12, 31, 0, 0, 0, 0, time.UTC), Valid: true},
|
||||
&pgtype.Timestamp{Time: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), Valid: true},
|
||||
&pgtype.Timestamp{Time: time.Date(2000, 1, 2, 0, 0, 0, 0, time.UTC), Valid: true},
|
||||
&pgtype.Timestamp{Time: time.Date(2200, 1, 1, 0, 0, 0, 0, time.UTC), Valid: true},
|
||||
&pgtype.Timestamp{},
|
||||
&pgtype.Timestamp{Valid: true, InfinityModifier: pgtype.Infinity},
|
||||
&pgtype.Timestamp{Valid: true, InfinityModifier: -pgtype.Infinity},
|
||||
}, func(a, b interface{}) bool {
|
||||
at := a.(pgtype.Timestamp)
|
||||
bt := b.(pgtype.Timestamp)
|
||||
func TestTimestampCodec(t *testing.T) {
|
||||
testPgxCodec(t, "timestamp", []PgxTranscodeTestCase{
|
||||
{time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC), new(time.Time), isExpectedEqTime(time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC))},
|
||||
{time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC), new(time.Time), isExpectedEqTime(time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC))},
|
||||
{time.Date(1999, 12, 31, 0, 0, 0, 0, time.UTC), new(time.Time), isExpectedEqTime(time.Date(1999, 12, 31, 0, 0, 0, 0, time.UTC))},
|
||||
{time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), new(time.Time), isExpectedEqTime(time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC))},
|
||||
{time.Date(2000, 1, 2, 0, 0, 0, 0, time.UTC), new(time.Time), isExpectedEqTime(time.Date(2000, 1, 2, 0, 0, 0, 0, time.UTC))},
|
||||
{time.Date(2200, 1, 1, 0, 0, 0, 0, time.UTC), new(time.Time), isExpectedEqTime(time.Date(2200, 1, 1, 0, 0, 0, 0, time.UTC))},
|
||||
|
||||
return at.Time.Equal(bt.Time) && at.Valid == bt.Valid && at.InfinityModifier == bt.InfinityModifier
|
||||
// Nanosecond truncation
|
||||
{time.Date(2020, 1, 1, 0, 0, 0, 999999999, time.UTC), new(time.Time), isExpectedEqTime(time.Date(2020, 1, 1, 0, 0, 0, 999999000, time.UTC))},
|
||||
{time.Date(2020, 1, 1, 0, 0, 0, 999999001, time.UTC), new(time.Time), isExpectedEqTime(time.Date(2020, 1, 1, 0, 0, 0, 999999000, time.UTC))},
|
||||
|
||||
{pgtype.Timestamp{InfinityModifier: pgtype.Infinity, Valid: true}, new(pgtype.Timestamp), isExpectedEq(pgtype.Timestamp{InfinityModifier: pgtype.Infinity, Valid: true})},
|
||||
{pgtype.Timestamp{InfinityModifier: pgtype.NegativeInfinity, Valid: true}, new(pgtype.Timestamp), isExpectedEq(pgtype.Timestamp{InfinityModifier: pgtype.NegativeInfinity, Valid: true})},
|
||||
{pgtype.Timestamp{}, new(pgtype.Timestamp), isExpectedEq(pgtype.Timestamp{})},
|
||||
{nil, new(*time.Time), isExpectedEq((*time.Time)(nil))},
|
||||
})
|
||||
}
|
||||
|
||||
// https://github.com/jackc/pgx/v4/pgtype/pull/128
|
||||
func TestTimestampTranscodeBigTimeBinary(t *testing.T) {
|
||||
conn := testutil.MustConnectPgx(t)
|
||||
if _, ok := conn.ConnInfo().DataTypeForName("line"); !ok {
|
||||
t.Skip("Skipping due to no line type")
|
||||
}
|
||||
defer testutil.MustCloseContext(t, conn)
|
||||
|
||||
in := &pgtype.Timestamp{Time: time.Date(294276, 12, 31, 23, 59, 59, 999999000, time.UTC), Valid: true}
|
||||
var out pgtype.Timestamp
|
||||
|
||||
err := conn.QueryRow(context.Background(), "select $1::timestamptz", in).Scan(&out)
|
||||
err := conn.QueryRow(context.Background(), "select $1::timestamp", in).Scan(&out)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -54,146 +47,11 @@ func TestTimestampTranscodeBigTimeBinary(t *testing.T) {
|
||||
require.Truef(t, in.Time.Equal(out.Time), "expected %v got %v", in.Time, out.Time)
|
||||
}
|
||||
|
||||
func TestTimestampNanosecondsTruncated(t *testing.T) {
|
||||
tests := []struct {
|
||||
input time.Time
|
||||
expected time.Time
|
||||
}{
|
||||
{time.Date(2020, 1, 1, 0, 0, 0, 999999999, time.UTC), time.Date(2020, 1, 1, 0, 0, 0, 999999000, time.UTC)},
|
||||
{time.Date(2020, 1, 1, 0, 0, 0, 999999001, time.UTC), time.Date(2020, 1, 1, 0, 0, 0, 999999000, time.UTC)},
|
||||
}
|
||||
for i, tt := range tests {
|
||||
{
|
||||
ts := pgtype.Timestamp{Time: tt.input, Valid: true}
|
||||
buf, err := ts.EncodeText(nil, nil)
|
||||
if err != nil {
|
||||
t.Errorf("%d. EncodeText failed - %v", i, err)
|
||||
}
|
||||
|
||||
ts.DecodeText(nil, buf)
|
||||
if err != nil {
|
||||
t.Errorf("%d. DecodeText failed - %v", i, err)
|
||||
}
|
||||
|
||||
if !(ts.Valid && ts.Time.Equal(tt.expected)) {
|
||||
t.Errorf("%d. EncodeText did not truncate nanoseconds", i)
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
ts := pgtype.Timestamp{Time: tt.input, Valid: true}
|
||||
buf, err := ts.EncodeBinary(nil, nil)
|
||||
if err != nil {
|
||||
t.Errorf("%d. EncodeBinary failed - %v", i, err)
|
||||
}
|
||||
|
||||
ts.DecodeBinary(nil, buf)
|
||||
if err != nil {
|
||||
t.Errorf("%d. DecodeBinary failed - %v", i, err)
|
||||
}
|
||||
|
||||
if !(ts.Valid && ts.Time.Equal(tt.expected)) {
|
||||
t.Errorf("%d. EncodeBinary did not truncate nanoseconds", i)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// https://github.com/jackc/pgtype/issues/74
|
||||
func TestTimestampDecodeTextInvalid(t *testing.T) {
|
||||
tstz := &pgtype.Timestamp{}
|
||||
err := tstz.DecodeText(nil, []byte(`eeeee`))
|
||||
func TestTimestampCodecDecodeTextInvalid(t *testing.T) {
|
||||
c := &pgtype.TimestampCodec{}
|
||||
var ts pgtype.Timestamp
|
||||
plan := c.PlanScan(nil, pgtype.TimestampOID, pgtype.TextFormatCode, &ts, false)
|
||||
err := plan.Scan(nil, pgtype.TimestampOID, pgtype.TextFormatCode, []byte(`eeeee`), &ts)
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
||||
func TestTimestampSet(t *testing.T) {
|
||||
type _time time.Time
|
||||
|
||||
successfulTests := []struct {
|
||||
source interface{}
|
||||
result pgtype.Timestamp
|
||||
}{
|
||||
{source: time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC), result: pgtype.Timestamp{Time: time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC), Valid: true}},
|
||||
{source: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC), result: pgtype.Timestamp{Time: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC), Valid: true}},
|
||||
{source: time.Date(1999, 12, 31, 12, 59, 59, 0, time.UTC), result: pgtype.Timestamp{Time: time.Date(1999, 12, 31, 12, 59, 59, 0, time.UTC), Valid: true}},
|
||||
{source: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), result: pgtype.Timestamp{Time: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), Valid: true}},
|
||||
{source: time.Date(2000, 1, 1, 0, 0, 1, 0, time.UTC), result: pgtype.Timestamp{Time: time.Date(2000, 1, 1, 0, 0, 1, 0, time.UTC), Valid: true}},
|
||||
{source: time.Date(2200, 1, 1, 0, 0, 0, 0, time.UTC), result: pgtype.Timestamp{Time: time.Date(2200, 1, 1, 0, 0, 0, 0, time.UTC), Valid: true}},
|
||||
{source: time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local), result: pgtype.Timestamp{Time: time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC), Valid: true}},
|
||||
{source: _time(time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC)), result: pgtype.Timestamp{Time: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC), Valid: true}},
|
||||
{source: pgtype.Infinity, result: pgtype.Timestamp{InfinityModifier: pgtype.Infinity, Valid: true}},
|
||||
{source: pgtype.NegativeInfinity, result: pgtype.Timestamp{InfinityModifier: pgtype.NegativeInfinity, Valid: true}},
|
||||
}
|
||||
|
||||
for i, tt := range successfulTests {
|
||||
var r pgtype.Timestamp
|
||||
err := r.Set(tt.source)
|
||||
if err != nil {
|
||||
t.Errorf("%d: %v", i, err)
|
||||
}
|
||||
|
||||
if r != tt.result {
|
||||
t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTimestampAssignTo(t *testing.T) {
|
||||
var tim time.Time
|
||||
var ptim *time.Time
|
||||
|
||||
simpleTests := []struct {
|
||||
src pgtype.Timestamp
|
||||
dst interface{}
|
||||
expected interface{}
|
||||
}{
|
||||
{src: pgtype.Timestamp{Time: time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC), Valid: true}, dst: &tim, expected: time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC)},
|
||||
{src: pgtype.Timestamp{Time: time.Time{}}, dst: &ptim, expected: ((*time.Time)(nil))},
|
||||
}
|
||||
|
||||
for i, tt := range simpleTests {
|
||||
err := tt.src.AssignTo(tt.dst)
|
||||
if err != nil {
|
||||
t.Errorf("%d: %v", i, err)
|
||||
}
|
||||
|
||||
if dst := reflect.ValueOf(tt.dst).Elem().Interface(); dst != tt.expected {
|
||||
t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst)
|
||||
}
|
||||
}
|
||||
|
||||
pointerAllocTests := []struct {
|
||||
src pgtype.Timestamp
|
||||
dst interface{}
|
||||
expected interface{}
|
||||
}{
|
||||
{src: pgtype.Timestamp{Time: time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local), Valid: true}, dst: &ptim, expected: time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local)},
|
||||
}
|
||||
|
||||
for i, tt := range pointerAllocTests {
|
||||
err := tt.src.AssignTo(tt.dst)
|
||||
if err != nil {
|
||||
t.Errorf("%d: %v", i, err)
|
||||
}
|
||||
|
||||
if dst := reflect.ValueOf(tt.dst).Elem().Elem().Interface(); dst != tt.expected {
|
||||
t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst)
|
||||
}
|
||||
}
|
||||
|
||||
errorTests := []struct {
|
||||
src pgtype.Timestamp
|
||||
dst interface{}
|
||||
}{
|
||||
{src: pgtype.Timestamp{Time: time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local), InfinityModifier: pgtype.Infinity, Valid: true}, dst: &tim},
|
||||
{src: pgtype.Timestamp{Time: time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local), InfinityModifier: pgtype.NegativeInfinity, Valid: true}, dst: &tim},
|
||||
{src: pgtype.Timestamp{Time: time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local)}, dst: &tim},
|
||||
}
|
||||
|
||||
for i, tt := range errorTests {
|
||||
err := tt.src.AssignTo(tt.dst)
|
||||
if err == nil {
|
||||
t.Errorf("%d: expected error but none was returned (%v -> %v)", i, tt.src, tt.dst)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user