Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-db/go/time_test.go
2497 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package db
6
7
import (
8
"encoding/json"
9
"testing"
10
"time"
11
12
"github.com/stretchr/testify/require"
13
)
14
15
func TestVarcharTime_Scan(t *testing.T) {
16
type Expectation struct {
17
Time VarcharTime
18
Error bool
19
}
20
21
for _, scenario := range []struct {
22
Name string
23
24
Input interface{}
25
Expected Expectation
26
}{
27
{
28
Name: "nil value does not error and sets invalid",
29
Input: nil,
30
Expected: Expectation{
31
Error: false,
32
},
33
},
34
{
35
Name: "empty uint8 slice does not error and sets invalid",
36
Input: []uint8{},
37
Expected: Expectation{
38
Error: false,
39
},
40
},
41
{
42
Name: "parses valid ISO 8601 from TypeScript from []uint8",
43
Input: []uint8("2019-05-10T09:54:28.185Z"),
44
Expected: Expectation{
45
Time: VarcharTime{
46
t: time.Date(2019, 05, 10, 9, 54, 28, 185000000, time.UTC),
47
valid: true,
48
},
49
Error: false,
50
},
51
},
52
{
53
Name: "invalid string errors",
54
Input: "2019-05-10T09:54:28.185Z-not-a-datetime",
55
Expected: Expectation{
56
Error: true,
57
},
58
},
59
{
60
Name: "string is parsed",
61
Input: "2019-05-10T09:54:28.185Z",
62
Expected: Expectation{
63
Time: VarcharTime{
64
t: time.Date(2019, 05, 10, 9, 54, 28, 185000000, time.UTC),
65
valid: true,
66
},
67
Error: false,
68
},
69
},
70
} {
71
t.Run(scenario.Name, func(t *testing.T) {
72
var vt VarcharTime
73
err := vt.Scan(scenario.Input)
74
75
require.Equal(t, scenario.Expected, Expectation{
76
Time: vt,
77
Error: err != nil,
78
})
79
})
80
}
81
}
82
83
func TestVarcharTime_Value_ISO8601(t *testing.T) {
84
for _, scenario := range []struct {
85
Time VarcharTime
86
Expected string
87
}{
88
{
89
Time: NewVarCharTime(time.Date(2019, 05, 10, 9, 54, 28, 185000000, time.UTC)),
90
Expected: "2019-05-10T09:54:28.185Z",
91
},
92
{
93
Time: VarcharTime{},
94
Expected: "",
95
},
96
} {
97
wireFormat, err := scenario.Time.Value()
98
require.NoError(t, err)
99
require.Equal(t, scenario.Expected, wireFormat)
100
}
101
}
102
103
func TestVarcharTime_String_ISO8601(t *testing.T) {
104
for _, scenario := range []struct {
105
Time VarcharTime
106
Expected string
107
}{
108
{
109
Time: NewVarCharTime(time.Date(2019, 05, 10, 9, 54, 28, 185000000, time.UTC)),
110
Expected: "2019-05-10T09:54:28.185Z",
111
},
112
{
113
Time: VarcharTime{},
114
Expected: "",
115
},
116
} {
117
require.Equal(t, scenario.Expected, scenario.Time.String())
118
}
119
}
120
121
func TestVarCharTime_ToJSON(t *testing.T) {
122
vt := NewVarCharTime(time.Date(2022, 7, 25, 11, 17, 23, 300, time.UTC))
123
124
serialized, err := json.Marshal(vt)
125
require.NoError(t, err)
126
require.JSONEq(t, `"2022-07-25T11:17:23.0000003Z"`, string(serialized))
127
}
128
129