Skip to content

Commit 3c640a4

Browse files
committed
Making the tests a little cleaner and clear
1 parent 5424d3c commit 3c640a4

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

pgtype/timestamp_test.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ func TestTimestampMarshalJSON(t *testing.T) {
108108
}{}
109109

110110
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"`
111112
var pgt pgtype.Timestamp
112113
_ = pgt.Scan(tm)
113114

@@ -116,9 +117,9 @@ func TestTimestampMarshalJSON(t *testing.T) {
116117
result string
117118
}{
118119
{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"`},
122123
{source: pgtype.Timestamp{InfinityModifier: pgtype.Infinity, Valid: true}, result: "\"infinity\""},
123124
{source: pgtype.Timestamp{InfinityModifier: pgtype.NegativeInfinity, Valid: true}, result: "\"-infinity\""},
124125
}
@@ -128,13 +129,14 @@ func TestTimestampMarshalJSON(t *testing.T) {
128129
t.Errorf("%d: %v", i, err)
129130
}
130131

131-
if string(r) != tt.result {
132+
if !assert.Equal(t, tt.result, string(r)) {
132133
t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, string(r))
133134
}
134135
tsStruct.TS = tt.source
135136
b, err := json.Marshal(tsStruct)
136137
assert.NoErrorf(t, err, "failed to marshal %v %s", tt.source, err)
137138
t2 := tsStruct
139+
t2.TS = pgtype.Timestamp{} // Clear out the value so that we can compare after unmarshalling
138140
err = json.Unmarshal(b, &t2)
139141
assert.NoErrorf(t, err, "failed to unmarshal %v with %s", tt.source, err)
140142
assert.True(t, tsStruct.TS.Time.Unix() == t2.TS.Time.Unix())

0 commit comments

Comments
 (0)