More tests.

This commit is contained in:
Nuno Cruces
2023-01-26 02:48:31 +00:00
parent 17b720a217
commit a327cf5e08
4 changed files with 314 additions and 4 deletions

View File

@@ -1,6 +1,7 @@
package sqlite3_test
import (
"errors"
"os"
"path/filepath"
"testing"
@@ -23,6 +24,23 @@ func TestOpen_file(t *testing.T) {
testOpen(t, filepath.Join(dir, "test.db"))
}
func TestOpen_dir(t *testing.T) {
_, err := sqlite3.Open(".")
if err == nil {
t.Fatal("want error")
}
var serr *sqlite3.Error
if !errors.As(err, &serr) {
t.Fatal("want sqlite3.Error")
}
if serr.Code != sqlite3.CANTOPEN {
t.Fatal("want sqlite3.CANTOPEN")
}
if got := err.Error(); got != "sqlite3: unable to open database file" {
t.Fatal("got message: ", got)
}
}
func testOpen(t *testing.T, name string) {
db, err := sqlite3.Open(name)
if err != nil {
@@ -51,17 +69,17 @@ func testOpen(t *testing.T, name string) {
idx := 0
for ; stmt.Step(); idx++ {
if ids[idx] != stmt.ColumnInt(0) {
t.Errorf("expected %d got %d", ids[idx], stmt.ColumnInt(0))
t.Errorf("want %d got %d", ids[idx], stmt.ColumnInt(0))
}
if names[idx] != stmt.ColumnText(1) {
t.Errorf("expected %q got %q", names[idx], stmt.ColumnText(1))
t.Errorf("want %q got %q", names[idx], stmt.ColumnText(1))
}
}
if err := stmt.Err(); err != nil {
t.Fatal(err)
}
if idx != 3 {
t.Errorf("expected %d rows got %d", len(ids), idx)
t.Errorf("want %d rows got %d", len(ids), idx)
}
err = stmt.Close()