-
Notifications
You must be signed in to change notification settings - Fork 3
/
nullfloat64_test.go
165 lines (149 loc) · 3.3 KB
/
nullfloat64_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 testMarshalJSONWithFloat64Null(t *testing.T) {
v := Float64{}
v.Valid = false
bytes, err := v.MarshalJSON()
if err != nil {
t.Fatal(err)
}
t.Log(string(bytes))
var test struct {
F Float64 `json:"float64"`
}
test.F = v
jsoni, err := json.Marshal(test)
if err != nil {
t.Fatal(err)
}
t.Log(string(jsoni))
}
func testMarshalJSONWithFloat64NotNull(t *testing.T) {
v := NewFloat64(3.1416)
bytes, err := v.MarshalJSON()
if err != nil {
t.Fatal(err)
}
t.Log(string(bytes))
var test struct {
F Float64 `json:"float64"`
}
test.F = v
jsoni, err := json.Marshal(test)
if err != nil {
t.Fatal(err)
}
t.Log(string(jsoni))
}
func testUnmarshalJSONWithFloat64NotNull(t *testing.T) {
var test struct {
F Float64 `json:"float64"`
}
err := json.Unmarshal([]byte(`{"float64":3.1416}`), &test)
if err != nil {
t.Fatal(err)
}
if test.F.Valid == false {
t.Fatal(err)
}
t.Logf("%+v", test)
}
func testUnmarshalJSONWithFloat64Null(t *testing.T) {
var test struct {
F Float64 `json:"float64"`
}
err := json.Unmarshal([]byte(`{"float64": null}`), &test)
if err != nil {
t.Fatal(err)
}
if test.F.Valid == true {
t.Fatal(err)
}
t.Logf("%+v", test)
}
func testScanFloat64Null(t *testing.T) {
v := Float64{}
err := v.Scan(nil)
if err != nil {
t.Fatal(err)
}
if v.Valid == true {
t.Fatal(err)
}
t.Logf("%+v", v)
}
func testScanFloat64NotNull(t *testing.T) {
v := Float64{}
err := v.Scan(3.1416)
if err != nil {
t.Fatal(err)
}
if v.Valid == false {
t.Fatal(err)
}
t.Logf("%+v", v)
}
func testValueFloat64Null(t *testing.T) {
v := Float64{}
v.Valid = false
err, value := v.Value()
if err != nil {
t.Fatal(err)
}
if value == nil {
t.Log(value)
} else {
t.Fatal(value)
}
}
func testValueFloat64NotNull(t *testing.T) {
v := NewFloat64(3.1416)
value, err := v.Value()
if err != nil {
t.Fatal(err)
}
if v.Valid == false {
t.Fatal(err)
}
t.Logf("%d", value)
}
func testValueFloat64NotNullInStruct(t *testing.T) {
jsoniter.RegisterTypeEncoder(reflect.TypeOf(Float64{}).String(), &Float64{})
type tmp struct {
Ptr *Float64 `json:"ptr,omitempty"`
Always Float64 `json:"always"`
OkFloat64 Float64 `json:"ok_Float64,omitempty"`
NokFloat64 Float64 `json:"nok_Float64,omitempty"`
}
value := tmp{}
value.Always = NewFloat64(1.1)
value.OkFloat64 = NewFloat64(2.2)
value.NokFloat64.Valid = false
value.NokFloat64.Float64 = 0
t.Logf("%+v", value)
jsoni, err := json.Marshal(value)
if err != nil {
t.Fatal(err)
}
str := string(jsoni)
if !strings.Contains(str, "nok_Float64") {
t.Failed()
}
t.Log(str)
}
func TestNullFloat64(t *testing.T) {
t.Run("testMarshalJSONWithFloat64Null", testMarshalJSONWithFloat64Null)
t.Run("testMarshalJSONWithFloat64NotNull", testMarshalJSONWithFloat64NotNull)
t.Run("testUnmarshalJSONWithFloat64Null", testUnmarshalJSONWithFloat64Null)
t.Run("testUnmarshalJSONWithFloat64NotNull", testUnmarshalJSONWithFloat64NotNull)
t.Run("testScanFloat64Null", testScanFloat64Null)
t.Run("testScanFloat64NotNull", testScanFloat64NotNull)
t.Run("testValueFloat64Null", testValueFloat64Null)
t.Run("testValueFloat64NotNull", testValueFloat64NotNull)
t.Run("testValueFloat64NotNullInStruct", testValueFloat64NotNullInStruct)
}