-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnulluint32_test.go
165 lines (149 loc) · 3.2 KB
/
nulluint32_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 testMarshalJSONWithUint32Null(t *testing.T) {
v := Uint32{}
v.Valid = false
bytes, err := v.MarshalJSON()
if err != nil {
t.Fatal(err)
}
t.Log(string(bytes))
var test struct {
I Uint32 `json:"uint32"`
}
test.I = v
jsoni, err := json.Marshal(test)
if err != nil {
t.Fatal(err)
}
t.Log(string(jsoni))
}
func testMarshalJSONWithUint32NotNull(t *testing.T) {
v := NewUint32(0)
bytes, err := v.MarshalJSON()
if err != nil {
t.Fatal(err)
}
t.Log(string(bytes))
var test struct {
I Uint32 `json:"uint32"`
}
test.I = v
jsoni, err := json.Marshal(test)
if err != nil {
t.Fatal(err)
}
t.Log(string(jsoni))
}
func testUnmarshalJSONWithUint32NotNull(t *testing.T) {
var test struct {
I Uint32 `json:"uint32"`
}
err := json.Unmarshal([]byte(`{"uint32":0}`), &test)
if err != nil {
t.Fatal(err)
}
if test.I.Valid == false {
t.Fatal(err)
}
t.Logf("%+v", test)
}
func testUnmarshalJSONWithUint32Null(t *testing.T) {
var test struct {
I Uint32 `json:"uint32"`
}
err := json.Unmarshal([]byte(`{"uint32": null}`), &test)
if err != nil {
t.Fatal(err)
}
if test.I.Valid == true {
t.Fatal(err)
}
t.Logf("%+v", test)
}
func testScanUint32Null(t *testing.T) {
v := Uint32{}
err := v.Scan(nil)
if err != nil {
t.Fatal(err)
}
if v.Valid == true {
t.Fatal(err)
}
t.Logf("%+v", v)
}
func testScanUint32NotNull(t *testing.T) {
v := Uint32{}
err := v.Scan(0)
if err != nil {
t.Fatal(err)
}
if v.Valid == false {
t.Fatal(err)
}
t.Logf("%+v", v)
}
func testValueUint32Null(t *testing.T) {
v := Uint32{}
v.Valid = false
err, value := v.Value()
if err != nil {
t.Fatal(err)
}
if value == nil {
t.Log(value)
} else {
t.Fatal(value)
}
}
func testValueUint32NotNull(t *testing.T) {
v := NewUint32(0)
value, err := v.Value()
if err != nil {
t.Fatal(err)
}
if v.Valid == false {
t.Fatal(err)
}
t.Logf("%d", value)
}
func testValueUint32NotNullInStruct(t *testing.T) {
jsoniter.RegisterTypeEncoder(reflect.TypeOf(Uint32{}).String(), &Uint32{})
type tmp struct {
Ptr *Uint32 `json:"ptr,omitempty"`
Always Uint32 `json:"always"`
OkUint32 Uint32 `json:"ok_Uint32,omitempty"`
NokUint32 Uint32 `json:"nok_Uint32,omitempty"`
}
value := tmp{}
value.Always = NewUint32(1)
value.OkUint32 = NewUint32(2)
value.NokUint32.Valid = false
value.NokUint32.Uint32 = 0
t.Logf("%+v", value)
jsoni, err := json.Marshal(value)
if err != nil {
t.Fatal(err)
}
str := string(jsoni)
if !strings.Contains(str, "nok_Uint32") {
t.Failed()
}
t.Log(str)
}
func TestNullUint32(t *testing.T) {
t.Run("testMarshalJSONWithUint32Null", testMarshalJSONWithUint32Null)
t.Run("testMarshalJSONWithUint32NotNull", testMarshalJSONWithUint32NotNull)
t.Run("testUnmarshalJSONWithUint32Null", testUnmarshalJSONWithUint32Null)
t.Run("testUnmarshalJSONWithUint32NotNull", testUnmarshalJSONWithUint32NotNull)
t.Run("testScanUint32Null", testScanUint32Null)
t.Run("testScanUint32NotNull", testScanUint32NotNull)
t.Run("testValueUint32Null", testValueUint32Null)
t.Run("testValueUint32NotNull", testValueUint32NotNull)
t.Run("testValueUint32NotNull", testValueUint32NotNullInStruct)
}