@@ -108,6 +108,7 @@ func TestTimestampMarshalJSON(t *testing.T) {
108
108
}{}
109
109
110
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"`
111
112
var pgt pgtype.Timestamp
112
113
_ = pgt .Scan (tm )
113
114
@@ -116,9 +117,9 @@ func TestTimestampMarshalJSON(t *testing.T) {
116
117
result string
117
118
}{
118
119
{source : pgtype.Timestamp {}, result : "null" },
119
- {source : pgtype.Timestamp {Time : tm , Valid : true }, result : `"2012-03-29T10:05:45"` },
120
- {source : pgt , result : `"2012-03-29T10:05:45"` },
121
- {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.555"` },
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"` },
122
123
{source : pgtype.Timestamp {InfinityModifier : pgtype .Infinity , Valid : true }, result : "\" infinity\" " },
123
124
{source : pgtype.Timestamp {InfinityModifier : pgtype .NegativeInfinity , Valid : true }, result : "\" -infinity\" " },
124
125
}
@@ -128,13 +129,14 @@ func TestTimestampMarshalJSON(t *testing.T) {
128
129
t .Errorf ("%d: %v" , i , err )
129
130
}
130
131
131
- if string ( r ) != tt .result {
132
+ if ! assert . Equal ( t , tt .result , string ( r )) {
132
133
t .Errorf ("%d: expected %v to convert to %v, but it was %v" , i , tt .source , tt .result , string (r ))
133
134
}
134
135
tsStruct .TS = tt .source
135
136
b , err := json .Marshal (tsStruct )
136
137
assert .NoErrorf (t , err , "failed to marshal %v %s" , tt .source , err )
137
138
t2 := tsStruct
139
+ t2 .TS = pgtype.Timestamp {} // Clear out the value so that we can compare after unmarshalling
138
140
err = json .Unmarshal (b , & t2 )
139
141
assert .NoErrorf (t , err , "failed to unmarshal %v with %s" , tt .source , err )
140
142
assert .True (t , tsStruct .TS .Time .Unix () == t2 .TS .Time .Unix ())
0 commit comments