@@ -2,12 +2,14 @@ package pgtype_test
2
2
3
3
import (
4
4
"context"
5
+ "encoding/json"
5
6
"testing"
6
7
"time"
7
8
8
9
pgx "github.com/jackc/pgx/v5"
9
10
"github.com/jackc/pgx/v5/pgtype"
10
11
"github.com/jackc/pgx/v5/pgxtest"
12
+ "github.com/stretchr/testify/assert"
11
13
"github.com/stretchr/testify/require"
12
14
)
13
15
@@ -100,13 +102,24 @@ func TestTimestampCodecDecodeTextInvalid(t *testing.T) {
100
102
}
101
103
102
104
func TestTimestampMarshalJSON (t * testing.T ) {
105
+
106
+ tsStruct := struct {
107
+ TS pgtype.Timestamp `json:"ts"`
108
+ }{}
109
+
110
+ tm := time .Date (2012 , 3 , 29 , 10 , 5 , 45 , 0 , time .UTC )
111
+ tsString := "\" " + tm .Format ("2006-01-02T15:04:05" ) + "\" " // `"2012-03-29T10:05:45"`
112
+ var pgt pgtype.Timestamp
113
+ _ = pgt .Scan (tm )
114
+
103
115
successfulTests := []struct {
104
116
source pgtype.Timestamp
105
117
result string
106
118
}{
107
119
{source : pgtype.Timestamp {}, result : "null" },
108
- {source : pgtype.Timestamp {Time : time .Date (2012 , 3 , 29 , 10 , 5 , 45 , 0 , time .UTC ), Valid : true }, result : "\" 2012-03-29T10:05:45Z\" " },
109
- {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\" " },
120
+ {source : pgtype.Timestamp {Time : tm , Valid : true }, result : tsString },
121
+ {source : pgt , result : tsString },
122
+ {source : pgtype.Timestamp {Time : tm .Add (time .Second * 555 / 1000 ), Valid : true }, result : `"2012-03-29T10:05:45.555"` },
110
123
{source : pgtype.Timestamp {InfinityModifier : pgtype .Infinity , Valid : true }, result : "\" infinity\" " },
111
124
{source : pgtype.Timestamp {InfinityModifier : pgtype .NegativeInfinity , Valid : true }, result : "\" -infinity\" " },
112
125
}
@@ -116,12 +129,32 @@ func TestTimestampMarshalJSON(t *testing.T) {
116
129
t .Errorf ("%d: %v" , i , err )
117
130
}
118
131
119
- if string ( r ) != tt .result {
132
+ if ! assert . Equal ( t , tt .result , string ( r )) {
120
133
t .Errorf ("%d: expected %v to convert to %v, but it was %v" , i , tt .source , tt .result , string (r ))
121
134
}
135
+ tsStruct .TS = tt .source
136
+ b , err := json .Marshal (tsStruct )
137
+ assert .NoErrorf (t , err , "failed to marshal %v %s" , tt .source , err )
138
+ t2 := tsStruct
139
+ t2 .TS = pgtype.Timestamp {} // Clear out the value so that we can compare after unmarshalling
140
+ err = json .Unmarshal (b , & t2 )
141
+ assert .NoErrorf (t , err , "failed to unmarshal %v with %s" , tt .source , err )
142
+ assert .True (t , tsStruct .TS .Time .Unix () == t2 .TS .Time .Unix ())
122
143
}
123
144
}
124
145
146
+ func TestTimestampUnmarshalJSONErrors (t * testing.T ) {
147
+ tsStruct := struct {
148
+ TS pgtype.Timestamp `json:"ts"`
149
+ }{}
150
+ goodJson1 := []byte (`{"ts":"2012-03-29T10:05:45"}` )
151
+ assert .NoError (t , json .Unmarshal (goodJson1 , & tsStruct ))
152
+ goodJson2 := []byte (`{"ts":"2012-03-29T10:05:45Z"}` )
153
+ assert .NoError (t , json .Unmarshal (goodJson2 , & tsStruct ))
154
+ badJson := []byte (`{"ts":"2012-03-29"}` )
155
+ assert .Error (t , json .Unmarshal (badJson , & tsStruct ))
156
+ }
157
+
125
158
func TestTimestampUnmarshalJSON (t * testing.T ) {
126
159
successfulTests := []struct {
127
160
source string
0 commit comments