2
0

Convert time to Codec

This commit is contained in:
Jack Christensen
2022-01-21 16:50:30 -06:00
parent c8b8764401
commit 61b4fb7689
4 changed files with 211 additions and 236 deletions
+34 -106
View File
@@ -1,117 +1,45 @@
package pgtype_test
import (
"reflect"
"testing"
"time"
"github.com/jackc/pgx/v5/pgtype"
"github.com/jackc/pgx/v5/pgtype/testutil"
)
func TestTimeTranscode(t *testing.T) {
testutil.TestSuccessfulTranscode(t, "time", []interface{}{
&pgtype.Time{Microseconds: 0, Valid: true},
&pgtype.Time{Microseconds: 1, Valid: true},
&pgtype.Time{Microseconds: 86399999999, Valid: true},
&pgtype.Time{Microseconds: 86400000000, Valid: true},
&pgtype.Time{},
func TestTimeCodec(t *testing.T) {
testPgxCodec(t, "time", []PgxTranscodeTestCase{
{
pgtype.Time{Microseconds: 0, Valid: true},
new(pgtype.Time),
isExpectedEq(pgtype.Time{Microseconds: 0, Valid: true}),
},
{
pgtype.Time{Microseconds: 1, Valid: true},
new(pgtype.Time),
isExpectedEq(pgtype.Time{Microseconds: 1, Valid: true}),
},
{
pgtype.Time{Microseconds: 86399999999, Valid: true},
new(pgtype.Time),
isExpectedEq(pgtype.Time{Microseconds: 86399999999, Valid: true}),
},
{
pgtype.Time{Microseconds: 86400000000, Valid: true},
new(pgtype.Time),
isExpectedEq(pgtype.Time{Microseconds: 86400000000, Valid: true}),
},
{
time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC),
new(pgtype.Time),
isExpectedEq(pgtype.Time{Microseconds: 0, Valid: true}),
},
{
pgtype.Time{Microseconds: 0, Valid: true},
new(time.Time),
isExpectedEq(time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)),
},
{pgtype.Time{}, new(pgtype.Time), isExpectedEq(pgtype.Time{})},
{nil, new(pgtype.Time), isExpectedEq(pgtype.Time{})},
})
}
func TestTimeSet(t *testing.T) {
type _time time.Time
successfulTests := []struct {
source interface{}
result pgtype.Time
}{
{source: time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC), result: pgtype.Time{Microseconds: 0, Valid: true}},
{source: time.Date(1900, 1, 1, 1, 0, 0, 0, time.UTC), result: pgtype.Time{Microseconds: 3600000000, Valid: true}},
{source: time.Date(1900, 1, 1, 0, 1, 0, 0, time.UTC), result: pgtype.Time{Microseconds: 60000000, Valid: true}},
{source: time.Date(1900, 1, 1, 0, 0, 1, 0, time.UTC), result: pgtype.Time{Microseconds: 1000000, Valid: true}},
{source: time.Date(1970, 1, 1, 0, 0, 0, 1, time.UTC), result: pgtype.Time{Microseconds: 0, Valid: true}},
{source: time.Date(1970, 1, 1, 0, 0, 0, 1000, time.UTC), result: pgtype.Time{Microseconds: 1, Valid: true}},
{source: time.Date(1999, 12, 31, 23, 59, 59, 999999999, time.UTC), result: pgtype.Time{Microseconds: 86399999999, Valid: true}},
{source: time.Date(2015, 1, 1, 0, 0, 0, 2000, time.Local), result: pgtype.Time{Microseconds: 2, Valid: true}},
{source: func(t time.Time) *time.Time { return &t }(time.Date(2015, 1, 1, 0, 0, 0, 2000, time.Local)), result: pgtype.Time{Microseconds: 2, Valid: true}},
{source: nil, result: pgtype.Time{}},
{source: (*time.Time)(nil), result: pgtype.Time{}},
{source: _time(time.Date(1970, 1, 1, 0, 0, 0, 3000, time.UTC)), result: pgtype.Time{Microseconds: 3, Valid: true}},
}
for i, tt := range successfulTests {
var r pgtype.Time
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 TestTimeAssignTo(t *testing.T) {
var tim time.Time
var ptim *time.Time
simpleTests := []struct {
src pgtype.Time
dst interface{}
expected interface{}
}{
{src: pgtype.Time{Microseconds: 0, Valid: true}, dst: &tim, expected: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)},
{src: pgtype.Time{Microseconds: 3600000000, Valid: true}, dst: &tim, expected: time.Date(2000, 1, 1, 1, 0, 0, 0, time.UTC)},
{src: pgtype.Time{Microseconds: 60000000, Valid: true}, dst: &tim, expected: time.Date(2000, 1, 1, 0, 1, 0, 0, time.UTC)},
{src: pgtype.Time{Microseconds: 1000000, Valid: true}, dst: &tim, expected: time.Date(2000, 1, 1, 0, 0, 1, 0, time.UTC)},
{src: pgtype.Time{Microseconds: 1, Valid: true}, dst: &tim, expected: time.Date(2000, 1, 1, 0, 0, 0, 1000, time.UTC)},
{src: pgtype.Time{Microseconds: 86399999999, Valid: true}, dst: &tim, expected: time.Date(2000, 1, 1, 23, 59, 59, 999999000, time.UTC)},
{src: pgtype.Time{Microseconds: 0}, 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.Time
dst interface{}
expected interface{}
}{
{src: pgtype.Time{Microseconds: 0, Valid: true}, dst: &ptim, expected: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)},
}
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.Time
dst interface{}
}{
{src: pgtype.Time{Microseconds: 86400000000, Valid: true}, 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)
}
}
}