1 package pgtype_test
2
3 import (
4 "context"
5 "reflect"
6 "testing"
7 "time"
8
9 "github.com/jackc/pgtype"
10 "github.com/jackc/pgtype/testutil"
11 "github.com/stretchr/testify/require"
12 )
13
14 func TestTimestamptzTranscode(t *testing.T) {
15 testutil.TestSuccessfulTranscodeEqFunc(t, "timestamptz", []interface{}{
16 &pgtype.Timestamptz{Time: time.Date(1800, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Present},
17 &pgtype.Timestamptz{Time: time.Date(1900, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Present},
18 &pgtype.Timestamptz{Time: time.Date(1905, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Present},
19 &pgtype.Timestamptz{Time: time.Date(1940, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Present},
20 &pgtype.Timestamptz{Time: time.Date(1960, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Present},
21 &pgtype.Timestamptz{Time: time.Date(1970, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Present},
22 &pgtype.Timestamptz{Time: time.Date(1999, 12, 31, 0, 0, 0, 0, time.Local), Status: pgtype.Present},
23 &pgtype.Timestamptz{Time: time.Date(2000, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Present},
24 &pgtype.Timestamptz{Time: time.Date(2000, 1, 2, 0, 0, 0, 0, time.Local), Status: pgtype.Present},
25 &pgtype.Timestamptz{Time: time.Date(2200, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Present},
26 &pgtype.Timestamptz{Status: pgtype.Null},
27 &pgtype.Timestamptz{Status: pgtype.Present, InfinityModifier: pgtype.Infinity},
28 &pgtype.Timestamptz{Status: pgtype.Present, InfinityModifier: -pgtype.Infinity},
29 }, func(a, b interface{}) bool {
30 at := a.(pgtype.Timestamptz)
31 bt := b.(pgtype.Timestamptz)
32
33 return at.Time.Equal(bt.Time) && at.Status == bt.Status && at.InfinityModifier == bt.InfinityModifier
34 })
35 }
36
37
38 func TestTimestamptzTranscodeBigTimeBinary(t *testing.T) {
39 conn := testutil.MustConnectPgx(t)
40 if _, ok := conn.ConnInfo().DataTypeForName("line"); !ok {
41 t.Skip("Skipping due to no line type")
42 }
43 defer testutil.MustCloseContext(t, conn)
44
45 in := &pgtype.Timestamptz{Time: time.Date(294276, 12, 31, 23, 59, 59, 999999000, time.UTC), Status: pgtype.Present}
46 var out pgtype.Timestamptz
47
48 err := conn.QueryRow(context.Background(), "select $1::timestamptz", in).Scan(&out)
49 if err != nil {
50 t.Fatal(err)
51 }
52
53 require.Equal(t, in.Status, out.Status)
54 require.Truef(t, in.Time.Equal(out.Time), "expected %v got %v", in.Time, out.Time)
55 }
56
57 func TestTimestamptzNanosecondsTruncated(t *testing.T) {
58 tests := []struct {
59 input time.Time
60 expected time.Time
61 }{
62 {time.Date(2020, 1, 1, 0, 0, 0, 999999999, time.Local), time.Date(2020, 1, 1, 0, 0, 0, 999999000, time.Local)},
63 {time.Date(2020, 1, 1, 0, 0, 0, 999999001, time.Local), time.Date(2020, 1, 1, 0, 0, 0, 999999000, time.Local)},
64 }
65 for i, tt := range tests {
66 {
67 tstz := pgtype.Timestamptz{Time: tt.input, Status: pgtype.Present}
68 buf, err := tstz.EncodeText(nil, nil)
69 if err != nil {
70 t.Errorf("%d. EncodeText failed - %v", i, err)
71 }
72
73 if err := tstz.DecodeText(nil, buf); err != nil {
74 t.Errorf("%d. DecodeText failed - %v", i, err)
75 }
76
77 if !(tstz.Status == pgtype.Present && tstz.Time.Equal(tt.expected)) {
78 t.Errorf("%d. EncodeText did not truncate nanoseconds", i)
79 }
80 }
81
82 {
83 tstz := pgtype.Timestamptz{Time: tt.input, Status: pgtype.Present}
84 buf, err := tstz.EncodeBinary(nil, nil)
85 if err != nil {
86 t.Errorf("%d. EncodeBinary failed - %v", i, err)
87 }
88
89 if err := tstz.DecodeBinary(nil, buf); err != nil {
90 t.Errorf("%d. DecodeBinary failed - %v", i, err)
91 }
92
93 if !(tstz.Status == pgtype.Present && tstz.Time.Equal(tt.expected)) {
94 t.Errorf("%d. EncodeBinary did not truncate nanoseconds", i)
95 }
96 }
97 }
98 }
99
100
101 func TestTimestamptzDecodeTextInvalid(t *testing.T) {
102 tstz := &pgtype.Timestamptz{}
103 err := tstz.DecodeText(nil, []byte(`eeeee`))
104 require.Error(t, err)
105 }
106
107 func TestTimestamptzSet(t *testing.T) {
108 type _time time.Time
109
110 successfulTests := []struct {
111 source interface{}
112 result pgtype.Timestamptz
113 }{
114 {source: time.Date(1900, 1, 1, 0, 0, 0, 0, time.Local), result: pgtype.Timestamptz{Time: time.Date(1900, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Present}},
115 {source: time.Date(1970, 1, 1, 0, 0, 0, 0, time.Local), result: pgtype.Timestamptz{Time: time.Date(1970, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Present}},
116 {source: time.Date(1999, 12, 31, 12, 59, 59, 0, time.Local), result: pgtype.Timestamptz{Time: time.Date(1999, 12, 31, 12, 59, 59, 0, time.Local), Status: pgtype.Present}},
117 {source: time.Date(2000, 1, 1, 0, 0, 0, 0, time.Local), result: pgtype.Timestamptz{Time: time.Date(2000, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Present}},
118 {source: time.Date(2000, 1, 1, 0, 0, 1, 0, time.Local), result: pgtype.Timestamptz{Time: time.Date(2000, 1, 1, 0, 0, 1, 0, time.Local), Status: pgtype.Present}},
119 {source: time.Date(2200, 1, 1, 0, 0, 0, 0, time.Local), result: pgtype.Timestamptz{Time: time.Date(2200, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Present}},
120 {source: _time(time.Date(1970, 1, 1, 0, 0, 0, 0, time.Local)), result: pgtype.Timestamptz{Time: time.Date(1970, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Present}},
121 {source: pgtype.Infinity, result: pgtype.Timestamptz{InfinityModifier: pgtype.Infinity, Status: pgtype.Present}},
122 {source: pgtype.NegativeInfinity, result: pgtype.Timestamptz{InfinityModifier: pgtype.NegativeInfinity, Status: pgtype.Present}},
123 {source: "2020-04-05 06:07:08Z", result: pgtype.Timestamptz{Time: time.Date(2020, 4, 5, 6, 7, 8, 0, time.UTC), Status: pgtype.Present}},
124 }
125
126 for i, tt := range successfulTests {
127 var r pgtype.Timestamptz
128 err := r.Set(tt.source)
129 if err != nil {
130 t.Errorf("%d: %v", i, err)
131 }
132
133 if r != tt.result {
134 t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r)
135 }
136 }
137 }
138
139 func TestTimestamptzAssignTo(t *testing.T) {
140 var tim time.Time
141 var ptim *time.Time
142
143 simpleTests := []struct {
144 src pgtype.Timestamptz
145 dst interface{}
146 expected interface{}
147 }{
148 {src: pgtype.Timestamptz{Time: time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Present}, dst: &tim, expected: time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local)},
149 {src: pgtype.Timestamptz{Time: time.Time{}, Status: pgtype.Null}, dst: &ptim, expected: ((*time.Time)(nil))},
150 }
151
152 for i, tt := range simpleTests {
153 err := tt.src.AssignTo(tt.dst)
154 if err != nil {
155 t.Errorf("%d: %v", i, err)
156 }
157
158 if dst := reflect.ValueOf(tt.dst).Elem().Interface(); dst != tt.expected {
159 t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst)
160 }
161 }
162
163 pointerAllocTests := []struct {
164 src pgtype.Timestamptz
165 dst interface{}
166 expected interface{}
167 }{
168 {src: pgtype.Timestamptz{Time: time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Present}, dst: &ptim, expected: time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local)},
169 }
170
171 for i, tt := range pointerAllocTests {
172 err := tt.src.AssignTo(tt.dst)
173 if err != nil {
174 t.Errorf("%d: %v", i, err)
175 }
176
177 if dst := reflect.ValueOf(tt.dst).Elem().Elem().Interface(); dst != tt.expected {
178 t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst)
179 }
180 }
181
182 errorTests := []struct {
183 src pgtype.Timestamptz
184 dst interface{}
185 }{
186 {src: pgtype.Timestamptz{Time: time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local), InfinityModifier: pgtype.Infinity, Status: pgtype.Present}, dst: &tim},
187 {src: pgtype.Timestamptz{Time: time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local), InfinityModifier: pgtype.NegativeInfinity, Status: pgtype.Present}, dst: &tim},
188 {src: pgtype.Timestamptz{Time: time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Null}, dst: &tim},
189 }
190
191 for i, tt := range errorTests {
192 err := tt.src.AssignTo(tt.dst)
193 if err == nil {
194 t.Errorf("%d: expected error but none was returned (%v -> %v)", i, tt.src, tt.dst)
195 }
196 }
197 }
198
199 func TestTimestamptzMarshalJSON(t *testing.T) {
200 successfulTests := []struct {
201 source pgtype.Timestamptz
202 result string
203 }{
204 {source: pgtype.Timestamptz{Status: pgtype.Null}, result: "null"},
205 {source: pgtype.Timestamptz{Time: time.Date(2012, 3, 29, 10, 5, 45, 0, time.FixedZone("", -6*60*60)), Status: pgtype.Present}, result: "\"2012-03-29T10:05:45-06:00\""},
206 {source: pgtype.Timestamptz{Time: time.Date(2012, 3, 29, 10, 5, 45, 555*1000*1000, time.FixedZone("", -6*60*60)), Status: pgtype.Present}, result: "\"2012-03-29T10:05:45.555-06:00\""},
207 {source: pgtype.Timestamptz{InfinityModifier: pgtype.Infinity, Status: pgtype.Present}, result: "\"infinity\""},
208 {source: pgtype.Timestamptz{InfinityModifier: pgtype.NegativeInfinity, Status: pgtype.Present}, result: "\"-infinity\""},
209 }
210 for i, tt := range successfulTests {
211 r, err := tt.source.MarshalJSON()
212 if err != nil {
213 t.Errorf("%d: %v", i, err)
214 }
215
216 if string(r) != tt.result {
217 t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, string(r))
218 }
219 }
220 }
221
222 func TestTimestamptzUnmarshalJSON(t *testing.T) {
223 successfulTests := []struct {
224 source string
225 result pgtype.Timestamptz
226 }{
227 {source: "null", result: pgtype.Timestamptz{Status: pgtype.Null}},
228 {source: "\"2012-03-29T10:05:45-06:00\"", result: pgtype.Timestamptz{Time: time.Date(2012, 3, 29, 10, 5, 45, 0, time.FixedZone("", -6*60*60)), Status: pgtype.Present}},
229 {source: "\"2012-03-29T10:05:45.555-06:00\"", result: pgtype.Timestamptz{Time: time.Date(2012, 3, 29, 10, 5, 45, 555*1000*1000, time.FixedZone("", -6*60*60)), Status: pgtype.Present}},
230 {source: "\"infinity\"", result: pgtype.Timestamptz{InfinityModifier: pgtype.Infinity, Status: pgtype.Present}},
231 {source: "\"-infinity\"", result: pgtype.Timestamptz{InfinityModifier: pgtype.NegativeInfinity, Status: pgtype.Present}},
232 }
233 for i, tt := range successfulTests {
234 var r pgtype.Timestamptz
235 err := r.UnmarshalJSON([]byte(tt.source))
236 if err != nil {
237 t.Errorf("%d: %v", i, err)
238 }
239
240 if !r.Time.Equal(tt.result.Time) || r.Status != tt.result.Status || r.InfinityModifier != tt.result.InfinityModifier {
241 t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r)
242 }
243 }
244 }
245
View as plain text