-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlnull_test.go
246 lines (233 loc) · 7.41 KB
/
sqlnull_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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package sqlnull_test
import (
"database/sql"
"encoding/json"
"testing"
"time"
"github.com/gkits/sqlnull"
_ "github.com/mattn/go-sqlite3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type target struct {
Generic sqlnull.Null[string] `json:"generic"`
String sqlnull.NullString `json:"string"`
Bool sqlnull.NullBool `json:"bool"`
Byte sqlnull.NullByte `json:"byte"`
Int16 sqlnull.NullInt16 `json:"int16"`
Int32 sqlnull.NullInt32 `json:"int32"`
Int64 sqlnull.NullInt64 `json:"int64"`
Float64 sqlnull.NullFloat64 `json:"float64"`
Time sqlnull.NullTime `json:"time"`
}
func Test_MarshalJSON(t *testing.T) {
cases := []struct {
name string
in target
want string
}{
{
name: "successfully marshal with values",
in: target{
Generic: sqlnull.Null[string]{V: "generic", Valid: true},
String: sqlnull.NullString{String: "string", Valid: true},
Bool: sqlnull.NullBool{Bool: true, Valid: true},
Byte: sqlnull.NullByte{Byte: 255, Valid: true},
Int16: sqlnull.NullInt16{Int16: 16, Valid: true},
Int32: sqlnull.NullInt32{Int32: 32, Valid: true},
Int64: sqlnull.NullInt64{Int64: 64, Valid: true},
Float64: sqlnull.NullFloat64{Float64: 64.6464, Valid: true},
Time: sqlnull.NullTime{Time: time.Date(2024, 10, 23, 17, 50, 0, 0, time.UTC), Valid: true},
},
want: `{
"generic": "generic",
"string": "string",
"bool": true,
"byte": 255,
"int16": 16,
"int32": 32,
"int64": 64,
"float64": 64.6464,
"time": "2024-10-23T17:50:00Z"
}`,
},
{
name: "successfully marshal with null",
in: target{
Generic: sqlnull.Null[string]{V: "generic", Valid: false},
String: sqlnull.NullString{String: "string", Valid: false},
Bool: sqlnull.NullBool{Bool: true, Valid: false},
Byte: sqlnull.NullByte{Byte: 255, Valid: false},
Int16: sqlnull.NullInt16{Int16: 16, Valid: false},
Int32: sqlnull.NullInt32{Int32: 32, Valid: false},
Int64: sqlnull.NullInt64{Int64: 64, Valid: false},
Float64: sqlnull.NullFloat64{Float64: 64.6464, Valid: false},
Time: sqlnull.NullTime{Time: time.Date(2024, 10, 23, 17, 50, 0, 0, time.UTC), Valid: false},
},
want: `{
"generic": null,
"string": null,
"bool": null,
"byte": null,
"int16": null,
"int32": null,
"int64": null,
"float64": null,
"time": null
}`,
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got, err := json.Marshal(c.in)
require.NoError(t, err)
assert.JSONEq(t, c.want, string(got))
})
}
}
func Test_UnmarshalJSON(t *testing.T) {
cases := []struct {
name string
in []byte
want target
}{
{
name: "successfully marshal with values",
in: []byte(`{
"generic": "generic",
"string": "string",
"bool": true,
"byte": 255,
"int16": 16,
"int32": 32,
"int64": 64,
"float64": 64.6464,
"time": "2024-10-23T17:50:00Z"
}`),
want: target{
Generic: sqlnull.Null[string]{V: "generic", Valid: true},
String: sqlnull.NullString{String: "string", Valid: true},
Bool: sqlnull.NullBool{Bool: true, Valid: true},
Byte: sqlnull.NullByte{Byte: 255, Valid: true},
Int16: sqlnull.NullInt16{Int16: 16, Valid: true},
Int32: sqlnull.NullInt32{Int32: 32, Valid: true},
Int64: sqlnull.NullInt64{Int64: 64, Valid: true},
Float64: sqlnull.NullFloat64{Float64: 64.6464, Valid: true},
Time: sqlnull.NullTime{Time: time.Date(2024, 10, 23, 17, 50, 0, 0, time.UTC), Valid: true},
},
},
{
name: "successfully marshal with null",
in: []byte(`{
"generic": null,
"string": null,
"bool": null,
"byte": null,
"int16": null,
"int32": null,
"int64": null,
"float64": null,
"time": null
}`),
want: target{
Generic: sqlnull.Null[string]{V: "", Valid: false},
String: sqlnull.NullString{String: "", Valid: false},
Bool: sqlnull.NullBool{Bool: false, Valid: false},
Byte: sqlnull.NullByte{Byte: 0, Valid: false},
Int16: sqlnull.NullInt16{Int16: 0, Valid: false},
Int32: sqlnull.NullInt32{Int32: 0, Valid: false},
Int64: sqlnull.NullInt64{Int64: 0, Valid: false},
Float64: sqlnull.NullFloat64{Float64: 0, Valid: false},
Time: sqlnull.NullTime{Time: time.Time{}, Valid: false},
},
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var got target
err := json.Unmarshal(c.in, &got)
require.NoError(t, err)
assert.Equal(t, c.want, got)
})
}
}
func Test_Scan(t *testing.T) {
db, err := sql.Open("sqlite3", "/tmp/test.sqlite")
require.NoError(t, err)
_, err = db.Exec(`
DROP TABLE IF EXISTS test;
CREATE TABLE test (
id INTEGER PRIMARY KEY,
generic TEXT,
string TEXT,
bool INTEGER,
byte INTEGER,
int64 INTEGER,
int32 INTEGER,
int16 INTEGER,
float64 REAL,
time TIMESTAMP
);
INSERT INTO test (id, generic, string, bool, byte, int64, int32, int16, float64, time)
VALUES (1, 'generic', 'string', TRUE, 255, 64, 32, 16, 64.6464, '2024-10-23T17:50:00Z'),
(2, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);`)
require.NoError(t, err)
query, err := db.Prepare(`SELECT * FROM test WHERE id = $1;`)
require.NoError(t, err)
cases := []struct {
name string
in int
want target
}{
{
name: "scan values",
in: 1,
want: target{
Generic: sqlnull.Null[string]{V: "generic", Valid: true},
String: sqlnull.NullString{String: "string", Valid: true},
Bool: sqlnull.NullBool{Bool: true, Valid: true},
Byte: sqlnull.NullByte{Byte: 255, Valid: true},
Int16: sqlnull.NullInt16{Int16: 16, Valid: true},
Int32: sqlnull.NullInt32{Int32: 32, Valid: true},
Int64: sqlnull.NullInt64{Int64: 64, Valid: true},
Float64: sqlnull.NullFloat64{Float64: 64.6464, Valid: true},
Time: sqlnull.NullTime{Time: time.Date(2024, 10, 23, 17, 50, 0, 0, time.UTC), Valid: true},
},
},
{
name: "scan null",
in: 2,
want: target{
Generic: sqlnull.Null[string]{V: "", Valid: false},
String: sqlnull.NullString{String: "", Valid: false},
Bool: sqlnull.NullBool{Bool: false, Valid: false},
Byte: sqlnull.NullByte{Byte: 0, Valid: false},
Int16: sqlnull.NullInt16{Int16: 0, Valid: false},
Int32: sqlnull.NullInt32{Int32: 0, Valid: false},
Int64: sqlnull.NullInt64{Int64: 0, Valid: false},
Float64: sqlnull.NullFloat64{Float64: 0, Valid: false},
Time: sqlnull.NullTime{Time: time.Time{}, Valid: false},
},
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var got target
var id int
err := query.QueryRow(c.in).Scan(
&id,
&got.Generic,
&got.String,
&got.Bool,
&got.Byte,
&got.Int64,
&got.Int32,
&got.Int16,
&got.Float64,
&got.Time,
)
require.NoError(t, err)
assert.Equal(t, c.want, got)
})
}
}