From d1772f082687b34ca377224523c6e1b5b545425a Mon Sep 17 00:00:00 2001 From: Greg Holt Date: Mon, 21 Aug 2017 13:22:09 -0700 Subject: Added TestNilAndEmptyBytes --- sqlite3_test.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) (limited to 'sqlite3_test.go') diff --git a/sqlite3_test.go b/sqlite3_test.go index 7c545e1..8169f3d 100644 --- a/sqlite3_test.go +++ b/sqlite3_test.go @@ -6,6 +6,7 @@ package sqlite3 import ( + "bytes" "database/sql" "database/sql/driver" "errors" @@ -1343,6 +1344,59 @@ func TestUpdateAndTransactionHooks(t *testing.T) { } } +func TestNilAndEmptyBytes(t *testing.T) { + db, err := sql.Open("sqlite3", ":memory:") + if err != nil { + t.Fatal(err) + } + defer db.Close() + actualNil := []byte("use this to use an actual nil not a reference to nil") + emptyBytes := []byte{} + for tsti, tst := range []struct { + name string + columnType string + insertBytes []byte + expectedBytes []byte + }{ + {"actual nil blob", "blob", actualNil, nil}, + {"referenced nil blob", "blob", nil, nil}, + {"empty blob", "blob", emptyBytes, emptyBytes}, + {"actual nil text", "text", actualNil, nil}, + {"referenced nil text", "text", nil, nil}, + {"empty text", "text", emptyBytes, emptyBytes}, + } { + if _, err = db.Exec(fmt.Sprintf("create table tbl%d (txt %s)", tsti, tst.columnType)); err != nil { + t.Fatal(tst.name, err) + } + if bytes.Equal(tst.insertBytes, actualNil) { + if _, err = db.Exec(fmt.Sprintf("insert into tbl%d (txt) values (?)", tsti), nil); err != nil { + t.Fatal(tst.name, err) + } + } else { + if _, err = db.Exec(fmt.Sprintf("insert into tbl%d (txt) values (?)", tsti), &tst.insertBytes); err != nil { + t.Fatal(tst.name, err) + } + } + rows, err := db.Query(fmt.Sprintf("select txt from tbl%d", tsti)) + if err != nil { + t.Fatal(tst.name, err) + } + if !rows.Next() { + t.Fatal(tst.name, "no rows") + } + var scanBytes []byte + if err = rows.Scan(&scanBytes); err != nil { + t.Fatal(tst.name, err) + } + if err = rows.Err(); err != nil { + t.Fatal(tst.name, err) + } + if !bytes.Equal(scanBytes, tst.expectedBytes) { + t.Errorf("%s: %#v != %#v", tst.name, scanBytes, tst.expectedBytes) + } + } +} + var customFunctionOnce sync.Once func BenchmarkCustomFunctions(b *testing.B) { -- cgit v1.2.3 From b1c8062c18ee31834bdd0cd70c90af8590ce1f1a Mon Sep 17 00:00:00 2001 From: Greg Holt Date: Mon, 21 Aug 2017 13:45:34 -0700 Subject: Improved TestNilAndEmptyBytes I forgot that bytes.Equals treats nil and []byte{} as equal. --- sqlite3_test.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'sqlite3_test.go') diff --git a/sqlite3_test.go b/sqlite3_test.go index 8169f3d..09e6727 100644 --- a/sqlite3_test.go +++ b/sqlite3_test.go @@ -1391,7 +1391,9 @@ func TestNilAndEmptyBytes(t *testing.T) { if err = rows.Err(); err != nil { t.Fatal(tst.name, err) } - if !bytes.Equal(scanBytes, tst.expectedBytes) { + if tst.expectedBytes == nil && scanBytes != nil { + t.Errorf("%s: %#v != %#v", tst.name, scanBytes, tst.expectedBytes) + } else if !bytes.Equal(scanBytes, tst.expectedBytes) { t.Errorf("%s: %#v != %#v", tst.name, scanBytes, tst.expectedBytes) } } -- cgit v1.2.3