-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime_test.go
74 lines (70 loc) · 1.57 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
package civil
import (
"bytes"
"fmt"
"testing"
"time"
"cloud.google.com/go/civil"
"github.com/stretchr/testify/assert"
)
func TestMarshalCivilTime(t *testing.T) {
for _, tc := range []struct {
date time.Time
expected string
}{
{
date: time.Date(1991, time.June, 1, 12, 0, 0, 0, time.UTC),
expected: `"12:00:00"`,
},
{
date: time.Date(2020, time.November, 6, 12, 30, 10, 0, time.UTC),
expected: `"12:30:10"`,
},
{
date: time.Time{},
expected: `"00:00:00"`,
},
} {
t.Run(tc.expected, func(t *testing.T) {
d := civil.TimeOf(tc.date)
b := bytes.Buffer{}
MarshalCivilTime(d).MarshalGQL(&b)
assert.Equal(t, tc.expected, b.String())
})
}
}
func TestUnmarshalCivilTime(t *testing.T) {
for name, tc := range map[string]struct {
time interface{}
expected civil.Time
err error
}{
"12:00:00": {
time: "12:00:00",
expected: civil.TimeOf(time.Date(1991, time.January, 1, 12, 0, 0, 0, time.UTC)),
},
"12:30:00": {
time: "12:30:00",
expected: civil.TimeOf(time.Date(2020, time.November, 6, 12, 30, 0, 0, time.UTC)),
},
"1230": {
time: "1230",
expected: civil.Time{},
err: fmt.Errorf(
"civil.ParseTime: %w",
&time.ParseError{Layout: "15:04:05.999999999", Value: "1230", LayoutElem: ":", ValueElem: "30"},
),
},
"numeric": {
time: 1230,
expected: civil.Time{},
err: ErrorTimeMustBeString,
},
} {
t.Run(name, func(t *testing.T) {
d, err := UnmarshalCivilTime(tc.time)
assert.Equal(t, tc.expected, d)
assert.Equal(t, tc.err, err)
})
}
}