Skip to content

Commit 41ffe57

Browse files
committed
tests: add tests for time marshaling and unmarshaling
Add some unit tests for time marshaling and unmarshaling to ensure that it works properly. Additionally, this modifies the time struct unmarshal process so it doesn't modify the struct if unmarshaling fails.
1 parent a01d24f commit 41ffe57

File tree

2 files changed

+133
-5
lines changed

2 files changed

+133
-5
lines changed

time.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var (
1717
}
1818
)
1919

20-
// Time represents either a relavite or absolute time.
20+
// Time represents either a relative or absolute time.
2121
// If Time is its zero value then it represents a time.Time{}.
2222
// To represent the now time you must set IsRelative to true.
2323
type Time struct {
@@ -60,13 +60,13 @@ func (t *Time) UnmarshalText(data []byte) error {
6060
t.IsRelative = true
6161
return nil
6262
}
63-
t.IsRelative = false
64-
t.Relative = 0
65-
t.Absolute, err = time.Parse(time.RFC3339Nano, str)
63+
ts, err := time.Parse(time.RFC3339Nano, str)
6664
if err != nil {
6765
return err
6866
}
69-
t.Absolute = t.Absolute.UTC()
67+
t.Absolute = ts.UTC()
68+
t.IsRelative = false
69+
t.Relative = 0
7070
return nil
7171
}
7272

time_test.go

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package flux_test
2+
3+
import (
4+
"math"
5+
"testing"
6+
"time"
7+
8+
"github.com/google/go-cmp/cmp"
9+
"github.com/influxdata/flux"
10+
)
11+
12+
func TestTime_MarshalText(t *testing.T) {
13+
for _, tt := range []struct {
14+
ts flux.Time
15+
want string
16+
}{
17+
{
18+
ts: flux.Time{
19+
IsRelative: true,
20+
},
21+
want: "now",
22+
},
23+
{
24+
ts: flux.Time{
25+
Relative: -time.Minute,
26+
IsRelative: true,
27+
},
28+
want: "-1m0s",
29+
},
30+
{
31+
ts: flux.Time{
32+
Relative: time.Minute,
33+
IsRelative: true,
34+
},
35+
want: "1m0s",
36+
},
37+
{
38+
ts: flux.Time{
39+
Absolute: time.Unix(0, 0).UTC(),
40+
},
41+
want: "1970-01-01T00:00:00Z",
42+
},
43+
{
44+
ts: flux.Time{
45+
// Minimum time in influxql.
46+
Absolute: time.Unix(0, math.MinInt64+2).UTC(),
47+
},
48+
want: "1677-09-21T00:12:43.145224194Z",
49+
},
50+
{
51+
ts: flux.Time{
52+
// Maximum time in influxql.
53+
Absolute: time.Unix(0, math.MaxInt64-1).UTC(),
54+
},
55+
want: "2262-04-11T23:47:16.854775806Z",
56+
},
57+
} {
58+
t.Run(tt.want, func(t *testing.T) {
59+
data, err := tt.ts.MarshalText()
60+
if err != nil {
61+
t.Fatalf("unexpected error: %s", err)
62+
}
63+
64+
if want, got := tt.want, string(data); want != got {
65+
t.Fatalf("unexpected text -want/+got\n\t- %s\n\t+ %s", want, got)
66+
}
67+
})
68+
}
69+
}
70+
71+
func TestTime_UnmarshalText(t *testing.T) {
72+
for _, tt := range []struct {
73+
s string
74+
want flux.Time
75+
}{
76+
{
77+
s: "now",
78+
want: flux.Time{
79+
IsRelative: true,
80+
},
81+
},
82+
{
83+
s: "-1m0s",
84+
want: flux.Time{
85+
Relative: -time.Minute,
86+
IsRelative: true,
87+
},
88+
},
89+
{
90+
s: "1m0s",
91+
want: flux.Time{
92+
Relative: time.Minute,
93+
IsRelative: true,
94+
},
95+
},
96+
{
97+
s: "1970-01-01T00:00:00Z",
98+
want: flux.Time{
99+
Absolute: time.Unix(0, 0).UTC(),
100+
},
101+
},
102+
{
103+
s: "1677-09-21T00:12:43.145224194Z",
104+
want: flux.Time{
105+
// Minimum time in influxql.
106+
Absolute: time.Unix(0, math.MinInt64+2).UTC(),
107+
},
108+
},
109+
{
110+
s: "2262-04-11T23:47:16.854775806Z",
111+
want: flux.Time{
112+
// Maximum time in influxql.
113+
Absolute: time.Unix(0, math.MaxInt64-1).UTC(),
114+
},
115+
},
116+
} {
117+
t.Run(tt.s, func(t *testing.T) {
118+
var ts flux.Time
119+
if err := ts.UnmarshalText([]byte(tt.s)); err != nil {
120+
t.Fatalf("unexpected error: %s", err)
121+
}
122+
123+
if want, got := tt.want, ts; !cmp.Equal(want, got) {
124+
t.Fatalf("unexpected text -want/+got\n%s", cmp.Diff(want, got))
125+
}
126+
})
127+
}
128+
}

0 commit comments

Comments
 (0)