-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnull_time_test.go
141 lines (135 loc) · 3.63 KB
/
null_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
129
130
131
132
133
134
135
136
137
138
139
140
141
package typ
import (
"database/sql/driver"
"encoding/json"
"reflect"
"testing"
"time"
)
var (
nullTimeReflectTypes = []reflect.Type{
reflect.TypeOf(&NullTime{}),
reflect.TypeOf(&NotNullTime{}),
}
)
func init() {
// Test Data
matrixSuite.Register(reflect.TypeOf(&NullTime{}), []dataItem{
{reflect.ValueOf(&NullTime{}), nil},
})
matrixSuite.Register(reflect.TypeOf(&NotNullTime{}), []dataItem{
{reflect.ValueOf(&NotNullTime{}), nil},
})
matrixSuite.Register(reflect.TypeOf(time.Time{}), []dataItem{
{reflect.ValueOf(time.Now()), nil},
})
// Converters
// - from &Null*{} to JSONToken
matrixSuite.SetConverters(nullTimeReflectTypes, jsonTokenReflectTypes, func(from interface{}, to reflect.Type, opts ...interface{}) (interface{}, bool) {
var (
v interface{}
null bool
present bool
)
switch tv := from.(type) {
case *NullTime:
v, null, present = tv.V(), !tv.Valid(), tv.Present()
case *NotNullTime:
v, null, _ = tv.V(), !tv.Valid(), tv.Present()
present = true
}
rv := reflect.ValueOf(v)
b, err := json.Marshal(v)
if null || !present {
b, err = []byte("null"), nil
}
if !rv.IsValid() {
return nil, false
}
jt := JSONToken{from, rv.Type(), b, err}
return jt, err == nil
})
matrixSuite.SetConverters([]reflect.Type{reflect.TypeOf(time.Time{})}, nullTimeReflectTypes, func(from interface{}, to reflect.Type, opts ...interface{}) (interface{}, bool) {
v := from.(time.Time)
if to == reflect.TypeOf(&NullTime{}) {
return &NullTime{TimeCommon{P: &v}}, true
}
if to == reflect.TypeOf(&NotNullTime{}) {
return &NotNullTime{TimeCommon{P: &v}}, true
}
return nil, false
})
matrixSuite.SetConverter(reflect.TypeOf(time.Time{}), sqlValueReflectType, func(from interface{}, to reflect.Type, opts ...interface{}) (interface{}, bool) {
sv := from.(time.Time)
return SQLValueType{sv, from}, true
})
// - from &Null*{} to SQLValueType
matrixSuite.SetConverters(nullTimeReflectTypes, sqlValueReflectTypes, func(from interface{}, to reflect.Type, opts ...interface{}) (interface{}, bool) {
var (
v interface{}
null bool
)
switch tv := from.(type) {
case *NullTime:
v, null = tv.V(), !tv.Valid()
case *NotNullTime:
v, null = tv.V(), !tv.Valid()
}
if null || v == nil {
return SQLValueType{}, true
}
rv := reflect.ValueOf(v)
cv, valid, _ := matrixSuite.Convert(rv.Interface(), to)
if !valid {
return SQLValueType{}, false
}
return cv, driver.IsValue(cv.(SQLValueType).SQLValue)
})
// For other types
matrixSuite.SetConverters(interfaceReflectTypes, nullTimeReflectTypes, func(from interface{}, to reflect.Type, opts ...interface{}) (interface{}, bool) {
return nil, false
})
}
func TestNullTime(t *testing.T) {
for _, nv := range []interface{}{
&NullTime{},
} {
testMarshalJSON(t, nv)
testUnmarshalJSON(t, nv)
testScanSQL(t, nv)
testValueSQL(t, nv)
testTyp(t, nv)
testClone(t, nv)
testSet(t, nv)
testNType(t, nv, NTime)
}
}
func TestNotNullTime(t *testing.T) {
for _, nv := range []interface{}{
&NotNullTime{},
} {
testMarshalJSON(t, nv)
testUnmarshalJSON(t, nv)
testScanSQL(t, nv)
testValueSQL(t, nv)
testTyp(t, nv)
testClone(t, nv)
testSet(t, nv)
testNType(t, nv, NNTime)
}
}
func TestNullTimeSlice(t *testing.T) {
ns := []TimeAccessor{
NTime(time.Now()),
NTime(time.Now()),
&NullTime{TimeCommon{Error: ErrDefaultValue}},
}
sl := TimeSlice(ns, false)
if len(sl) != len(ns) || cap(sl) != cap(ns) {
t.Errorf("NullTimeSlice(%v, false), slice length not equal", ns)
}
sl = TimeSlice(ns, true)
if len(sl) != len(ns)-1 || cap(sl) != cap(ns) {
t.Errorf("NullTimeSlice(%v, true), slice length not equal", ns)
}
}