-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime_test.go
103 lines (95 loc) · 2.34 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
package nullable
// Do not modify. Generated by nullable-generate.
import (
"database/sql/driver"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestTime(t *testing.T) {
testCases := []struct {
ScanValue interface{}
ExpectedError bool
ExpectedValid bool
ExpectedValue time.Time
JSONText string
}{
{
ScanValue: time.Date(2001, 11, 10, 15, 04, 05, 0, time.FixedZone("AEST", 10*3600)),
ExpectedValid: true,
ExpectedValue: time.Date(2001, 11, 10, 15, 04, 05, 0, time.FixedZone("AEST", 10*3600)),
JSONText: `"2001-11-10T15:04:05+10:00"`,
},
{
ScanValue: 53.5,
ExpectedError: true,
ExpectedValid: false,
ExpectedValue: time.Time{},
JSONText: `null`,
},
{
ScanValue: nil,
ExpectedValid: false,
ExpectedValue: time.Time{},
JSONText: "null",
},
}
assert := assert.New(t)
for i, tc := range testCases {
tcName := fmt.Sprintf("test case %d", i)
var nv Time
err := nv.Scan(tc.ScanValue)
if tc.ExpectedError {
assert.Error(err, tcName)
continue
} else {
assert.NoError(err, tcName)
assert.Equal(tc.ExpectedValid, nv.Valid, tcName)
assert.Equal(tc.ExpectedValue, nv.Time, tcName)
}
v, err := nv.Value()
assert.NoError(err)
if tc.ExpectedValid {
assert.Equal(driver.Value((tc.ExpectedValue)), v, tcName)
assert.NotNil(nv.Ptr(), tcName)
assert.Equal(nv.Time, *(nv.Ptr()), tcName)
nv2 := TimeFromPtr(nv.Ptr())
assert.Equal(nv, nv2, tcName)
} else {
assert.Nil(v, tcName)
assert.Nil(nv.Ptr(), tcName)
nv2 := TimeFromPtr(nv.Ptr())
assert.Equal(nv, nv2, tcName)
}
jsonText, err := nv.MarshalJSON()
assert.NoError(err)
assert.Equal(tc.JSONText, string(jsonText), tcName)
var nt2 Time
err = nt2.UnmarshalJSON(jsonText)
assert.NoError(err)
assert.Equal(nv.Valid, nt2.Valid, tcName)
// invalid JSON for any type
err = nt2.UnmarshalJSON([]byte("00 this is not valid xx"))
assert.Error(err)
// test Normalized comparison
{
n1 := Time{
Time: time.Now(),
}
n2 := Time{
Time: time.Time{},
}
n3 := Time{
Time: time.Now(),
Valid: true,
}
if n1.Normalized() != n2.Normalized() {
t.Errorf("expected equal, got not equal: %v != %v", n1, n2)
}
if n3.Normalized() != n3.Normalized() {
t.Errorf("expected equal, got not equal: %v != %v", n3, n3)
}
}
}
}