-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
int_test.go
72 lines (67 loc) · 1.44 KB
/
int_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
package nullable
import (
"database/sql"
"encoding/json"
"testing"
)
func TestInt64_MarshalJSON(t *testing.T) {
tests := []struct {
name string
ni Int64
want string
wantErr bool
}{
{
name: "Valid int",
ni: Int64{NullInt64: sql.NullInt64{Int64: 123, Valid: true}},
want: `123`,
},
{
name: "Null int",
ni: Int64{NullInt64: sql.NullInt64{Valid: false}},
want: `null`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := json.Marshal(tt.ni)
if (err != nil) != tt.wantErr {
t.Errorf("MarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
return
}
if string(got) != tt.want {
t.Errorf("MarshalJSON() got = %v, want %v", string(got), tt.want)
}
})
}
}
func TestInt64_UnmarshalJSON(t *testing.T) {
tests := []struct {
name string
data string
want Int64
wantErr bool
}{
{
name: "Valid int",
data: `123`,
want: Int64{NullInt64: sql.NullInt64{Int64: 123, Valid: true}},
},
{
name: "Null int",
data: `null`,
want: Int64{NullInt64: sql.NullInt64{Int64: 0, Valid: false}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var ni Int64
if err := json.Unmarshal([]byte(tt.data), &ni); (err != nil) != tt.wantErr {
t.Errorf("UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
}
if ni != tt.want {
t.Errorf("UnmarshalJSON() got = %v, want %v", ni, tt.want)
}
})
}
}