-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdate_test.go
74 lines (70 loc) · 1.55 KB
/
date_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 TestMarshalCivilDate(t *testing.T) {
for _, tc := range []struct {
date time.Time
expected string
}{
{
date: time.Date(1991, time.June, 1, 0, 0, 0, 0, time.UTC),
expected: `"1991-06-01"`,
},
{
date: time.Date(2020, time.November, 6, 12, 0, 0, 0, time.UTC),
expected: `"2020-11-06"`,
},
{
date: time.Time{},
expected: `"0001-01-01"`,
},
} {
t.Run(tc.expected, func(t *testing.T) {
d := civil.DateOf(tc.date)
b := bytes.Buffer{}
MarshalCivilDate(d).MarshalGQL(&b)
assert.Equal(t, tc.expected, b.String())
})
}
}
func TestUnmarshalCivilDate(t *testing.T) {
for name, tc := range map[string]struct {
date interface{}
expected civil.Date
err error
}{
"1991-01-01": {
date: "1991-01-01",
expected: civil.DateOf(time.Date(1991, time.January, 1, 0, 0, 0, 0, time.UTC)),
},
"2020-11-06": {
date: "2020-11-06",
expected: civil.DateOf(time.Date(2020, time.November, 6, 12, 0, 0, 0, time.UTC)),
},
"2020": {
date: "2020",
expected: civil.Date{},
err: fmt.Errorf(
"civil.ParseDate: %w",
&time.ParseError{Layout: "2006-01-02", Value: "2020", LayoutElem: "-"},
),
},
"numeric": {
date: 2020,
expected: civil.Date{},
err: ErrorDateMustBeString,
},
} {
t.Run(name, func(t *testing.T) {
d, err := UnmarshalCivilDate(tc.date)
assert.Equal(t, tc.expected, d)
assert.Equal(t, tc.err, err)
})
}
}