-
Notifications
You must be signed in to change notification settings - Fork 3
/
nulluint64_test.go
165 lines (149 loc) · 3.2 KB
/
nulluint64_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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package nulltype
import (
"reflect"
"strings"
"testing"
jsoniter "github.com/json-iterator/go"
)
func testMarshalJSONWithUint64Null(t *testing.T) {
v := Uint64{}
v.Valid = false
bytes, err := v.MarshalJSON()
if err != nil {
t.Fatal(err)
}
t.Log(string(bytes))
var test struct {
I Uint64 `json:"uint64"`
}
test.I = v
jsoni, err := json.Marshal(test)
if err != nil {
t.Fatal(err)
}
t.Log(string(jsoni))
}
func testMarshalJSONWithUint64NotNull(t *testing.T) {
v := NewUint64(0)
bytes, err := v.MarshalJSON()
if err != nil {
t.Fatal(err)
}
t.Log(string(bytes))
var test struct {
I Uint64 `json:"uint64"`
}
test.I = v
jsoni, err := json.Marshal(test)
if err != nil {
t.Fatal(err)
}
t.Log(string(jsoni))
}
func testUnmarshalJSONWithUint64NotNull(t *testing.T) {
var test struct {
I Uint64 `json:"uint64"`
}
err := json.Unmarshal([]byte(`{"uint64":0}`), &test)
if err != nil {
t.Fatal(err)
}
if test.I.Valid == false {
t.Fatal(err)
}
t.Logf("%+v", test)
}
func testUnmarshalJSONWithUint64Null(t *testing.T) {
var test struct {
I Uint64 `json:"uint64"`
}
err := json.Unmarshal([]byte(`{"uint64": null}`), &test)
if err != nil {
t.Fatal(err)
}
if test.I.Valid == true {
t.Fatal(err)
}
t.Logf("%+v", test)
}
func testScanUint64Null(t *testing.T) {
v := Uint64{}
err := v.Scan(nil)
if err != nil {
t.Fatal(err)
}
if v.Valid == true {
t.Fatal(err)
}
t.Logf("%+v", v)
}
func testScanUint64NotNull(t *testing.T) {
v := Uint64{}
err := v.Scan(0)
if err != nil {
t.Fatal(err)
}
if v.Valid == false {
t.Fatal(err)
}
t.Logf("%+v", v)
}
func testValueUint64Null(t *testing.T) {
v := Uint64{}
v.Valid = false
err, value := v.Value()
if err != nil {
t.Fatal(err)
}
if value == nil {
t.Log(value)
} else {
t.Fatal(value)
}
}
func testValueUint64NotNull(t *testing.T) {
v := NewUint64(0)
value, err := v.Value()
if err != nil {
t.Fatal(err)
}
if v.Valid == false {
t.Fatal(err)
}
t.Logf("%d", value)
}
func testValueUint64NotNullInStruct(t *testing.T) {
jsoniter.RegisterTypeEncoder(reflect.TypeOf(Uint64{}).String(), &Uint64{})
type tmp struct {
Ptr *Uint64 `json:"ptr,omitempty"`
Always Uint64 `json:"always"`
OkUint64 Uint64 `json:"ok_Uint64,omitempty"`
NokUint64 Uint64 `json:"nok_Uint64,omitempty"`
}
value := tmp{}
value.Always = NewUint64(1)
value.OkUint64 = NewUint64(2)
value.NokUint64.Valid = false
value.NokUint64.Uint64 = 0
t.Logf("%+v", value)
jsoni, err := json.Marshal(value)
if err != nil {
t.Fatal(err)
}
str := string(jsoni)
if !strings.Contains(str, "nok_Uint64") {
t.Failed()
}
t.Log(str)
}
func TestNullUint64(t *testing.T) {
t.Run("testMarshalJSONWithUint64Null", testMarshalJSONWithUint64Null)
t.Run("testMarshalJSONWithUint64NotNull", testMarshalJSONWithUint64NotNull)
t.Run("testUnmarshalJSONWithUint64Null", testUnmarshalJSONWithUint64Null)
t.Run("testUnmarshalJSONWithUint64NotNull", testUnmarshalJSONWithUint64NotNull)
t.Run("testScanUint64Null", testScanUint64Null)
t.Run("testScanUint64NotNull", testScanUint64NotNull)
t.Run("testValueUint64Null", testValueUint64Null)
t.Run("testValueUint64NotNull", testValueUint64NotNull)
t.Run("testValueUint64NotNull", testValueUint64NotNullInStruct)
}