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