1 package pgtype_test
2
3 import (
4 "context"
5 "testing"
6 "time"
7
8 pgx "github.com/jackc/pgx/v5"
9 "github.com/jackc/pgx/v5/pgtype"
10 "github.com/jackc/pgx/v5/pgxtest"
11 "github.com/stretchr/testify/require"
12 )
13
14 func TestTimestampCodec(t *testing.T) {
15 skipCockroachDB(t, "Server does not support infinite timestamps (see https://github.com/cockroachdb/cockroach/issues/41564)")
16
17 pgxtest.RunValueRoundTripTests(context.Background(), t, defaultConnTestRunner, nil, "timestamp", []pgxtest.ValueRoundTripTest{
18 {time.Date(-100, 1, 1, 0, 0, 0, 0, time.UTC), new(time.Time), isExpectedEqTime(time.Date(-100, 1, 1, 0, 0, 0, 0, time.UTC))},
19 {time.Date(-1, 1, 1, 0, 0, 0, 0, time.UTC), new(time.Time), isExpectedEqTime(time.Date(-1, 1, 1, 0, 0, 0, 0, time.UTC))},
20 {time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC), new(time.Time), isExpectedEqTime(time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC))},
21 {time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC), new(time.Time), isExpectedEqTime(time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC))},
22
23 {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))},
24 {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))},
25 {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))},
26 {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))},
27 {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))},
28 {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))},
29
30
31 {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))},
32 {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))},
33
34 {pgtype.Timestamp{InfinityModifier: pgtype.Infinity, Valid: true}, new(pgtype.Timestamp), isExpectedEq(pgtype.Timestamp{InfinityModifier: pgtype.Infinity, Valid: true})},
35 {pgtype.Timestamp{InfinityModifier: pgtype.NegativeInfinity, Valid: true}, new(pgtype.Timestamp), isExpectedEq(pgtype.Timestamp{InfinityModifier: pgtype.NegativeInfinity, Valid: true})},
36 {pgtype.Timestamp{}, new(pgtype.Timestamp), isExpectedEq(pgtype.Timestamp{})},
37 {nil, new(*time.Time), isExpectedEq((*time.Time)(nil))},
38 })
39 }
40
41
42 func TestTimestampTranscodeBigTimeBinary(t *testing.T) {
43 defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
44 in := &pgtype.Timestamp{Time: time.Date(294276, 12, 31, 23, 59, 59, 999999000, time.UTC), Valid: true}
45 var out pgtype.Timestamp
46
47 err := conn.QueryRow(ctx, "select $1::timestamp", in).Scan(&out)
48 if err != nil {
49 t.Fatal(err)
50 }
51
52 require.Equal(t, in.Valid, out.Valid)
53 require.Truef(t, in.Time.Equal(out.Time), "expected %v got %v", in.Time, out.Time)
54 })
55 }
56
57
58 func TestTimestampCodecDecodeTextInvalid(t *testing.T) {
59 c := &pgtype.TimestampCodec{}
60 var ts pgtype.Timestamp
61 plan := c.PlanScan(nil, pgtype.TimestampOID, pgtype.TextFormatCode, &ts)
62 err := plan.Scan([]byte(`eeeee`), &ts)
63 require.Error(t, err)
64 }
65
66 func TestTimestampMarshalJSON(t *testing.T) {
67 successfulTests := []struct {
68 source pgtype.Timestamp
69 result string
70 }{
71 {source: pgtype.Timestamp{}, result: "null"},
72 {source: pgtype.Timestamp{Time: time.Date(2012, 3, 29, 10, 5, 45, 0, time.UTC), Valid: true}, result: "\"2012-03-29T10:05:45Z\""},
73 {source: pgtype.Timestamp{Time: time.Date(2012, 3, 29, 10, 5, 45, 555*1000*1000, time.UTC), Valid: true}, result: "\"2012-03-29T10:05:45.555Z\""},
74 {source: pgtype.Timestamp{InfinityModifier: pgtype.Infinity, Valid: true}, result: "\"infinity\""},
75 {source: pgtype.Timestamp{InfinityModifier: pgtype.NegativeInfinity, Valid: true}, result: "\"-infinity\""},
76 }
77 for i, tt := range successfulTests {
78 r, err := tt.source.MarshalJSON()
79 if err != nil {
80 t.Errorf("%d: %v", i, err)
81 }
82
83 if string(r) != tt.result {
84 t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, string(r))
85 }
86 }
87 }
88
89 func TestTimestampUnmarshalJSON(t *testing.T) {
90 successfulTests := []struct {
91 source string
92 result pgtype.Timestamp
93 }{
94 {source: "null", result: pgtype.Timestamp{}},
95 {source: "\"2012-03-29T10:05:45Z\"", result: pgtype.Timestamp{Time: time.Date(2012, 3, 29, 10, 5, 45, 0, time.UTC), Valid: true}},
96 {source: "\"2012-03-29T10:05:45.555Z\"", result: pgtype.Timestamp{Time: time.Date(2012, 3, 29, 10, 5, 45, 555*1000*1000, time.UTC), Valid: true}},
97 {source: "\"infinity\"", result: pgtype.Timestamp{InfinityModifier: pgtype.Infinity, Valid: true}},
98 {source: "\"-infinity\"", result: pgtype.Timestamp{InfinityModifier: pgtype.NegativeInfinity, Valid: true}},
99 }
100 for i, tt := range successfulTests {
101 var r pgtype.Timestamp
102 err := r.UnmarshalJSON([]byte(tt.source))
103 if err != nil {
104 t.Errorf("%d: %v", i, err)
105 }
106
107 if !r.Time.Equal(tt.result.Time) || r.Valid != tt.result.Valid || r.InfinityModifier != tt.result.InfinityModifier {
108 t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r)
109 }
110 }
111 }
112
View as plain text