Skip to content

Commit d1772f0

Browse files
committed
Added TestNilAndEmptyBytes
1 parent 6654e41 commit d1772f0

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

sqlite3_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package sqlite3
77

88
import (
9+
"bytes"
910
"database/sql"
1011
"database/sql/driver"
1112
"errors"
@@ -1343,6 +1344,59 @@ func TestUpdateAndTransactionHooks(t *testing.T) {
13431344
}
13441345
}
13451346

1347+
func TestNilAndEmptyBytes(t *testing.T) {
1348+
db, err := sql.Open("sqlite3", ":memory:")
1349+
if err != nil {
1350+
t.Fatal(err)
1351+
}
1352+
defer db.Close()
1353+
actualNil := []byte("use this to use an actual nil not a reference to nil")
1354+
emptyBytes := []byte{}
1355+
for tsti, tst := range []struct {
1356+
name string
1357+
columnType string
1358+
insertBytes []byte
1359+
expectedBytes []byte
1360+
}{
1361+
{"actual nil blob", "blob", actualNil, nil},
1362+
{"referenced nil blob", "blob", nil, nil},
1363+
{"empty blob", "blob", emptyBytes, emptyBytes},
1364+
{"actual nil text", "text", actualNil, nil},
1365+
{"referenced nil text", "text", nil, nil},
1366+
{"empty text", "text", emptyBytes, emptyBytes},
1367+
} {
1368+
if _, err = db.Exec(fmt.Sprintf("create table tbl%d (txt %s)", tsti, tst.columnType)); err != nil {
1369+
t.Fatal(tst.name, err)
1370+
}
1371+
if bytes.Equal(tst.insertBytes, actualNil) {
1372+
if _, err = db.Exec(fmt.Sprintf("insert into tbl%d (txt) values (?)", tsti), nil); err != nil {
1373+
t.Fatal(tst.name, err)
1374+
}
1375+
} else {
1376+
if _, err = db.Exec(fmt.Sprintf("insert into tbl%d (txt) values (?)", tsti), &tst.insertBytes); err != nil {
1377+
t.Fatal(tst.name, err)
1378+
}
1379+
}
1380+
rows, err := db.Query(fmt.Sprintf("select txt from tbl%d", tsti))
1381+
if err != nil {
1382+
t.Fatal(tst.name, err)
1383+
}
1384+
if !rows.Next() {
1385+
t.Fatal(tst.name, "no rows")
1386+
}
1387+
var scanBytes []byte
1388+
if err = rows.Scan(&scanBytes); err != nil {
1389+
t.Fatal(tst.name, err)
1390+
}
1391+
if err = rows.Err(); err != nil {
1392+
t.Fatal(tst.name, err)
1393+
}
1394+
if !bytes.Equal(scanBytes, tst.expectedBytes) {
1395+
t.Errorf("%s: %#v != %#v", tst.name, scanBytes, tst.expectedBytes)
1396+
}
1397+
}
1398+
}
1399+
13461400
var customFunctionOnce sync.Once
13471401

13481402
func BenchmarkCustomFunctions(b *testing.B) {

0 commit comments

Comments
 (0)